mirror of
https://github.com/eigent-ai/eigent.git
synced 2026-04-29 20:20:16 +00:00
Initial commit of eigent-main
This commit is contained in:
commit
723df5a03e
1144 changed files with 103478 additions and 0 deletions
61
package/@stackframe/stack-shared/dist/esm/hooks/use-strict-memo.js
vendored
Normal file
61
package/@stackframe/stack-shared/dist/esm/hooks/use-strict-memo.js
vendored
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
// src/hooks/use-strict-memo.tsx
|
||||
import { useId, useInsertionEffect } from "react";
|
||||
import { Result } from "../utils/results";
|
||||
var cached = /* @__PURE__ */ new Map();
|
||||
function unwrapFromInner(dependencies, inner) {
|
||||
if (dependencies.length === 0 !== "isNotNestedMap" in inner) {
|
||||
return Result.error(void 0);
|
||||
}
|
||||
if ("isNotNestedMap" in inner) {
|
||||
if (dependencies.length === 0) {
|
||||
return Result.ok(inner.value);
|
||||
} else {
|
||||
return Result.error(void 0);
|
||||
}
|
||||
} else {
|
||||
if (dependencies.length === 0) {
|
||||
return Result.error(void 0);
|
||||
} else {
|
||||
const [key, ...rest] = dependencies;
|
||||
const newInner = inner.get(key);
|
||||
if (!newInner) {
|
||||
return Result.error(void 0);
|
||||
}
|
||||
return unwrapFromInner(rest, newInner);
|
||||
}
|
||||
}
|
||||
}
|
||||
function wrapToInner(dependencies, value) {
|
||||
if (dependencies.length === 0) {
|
||||
return { isNotNestedMap: true, value };
|
||||
}
|
||||
const [key, ...rest] = dependencies;
|
||||
const inner = wrapToInner(rest, value);
|
||||
const isObject = typeof key === "object" && key !== null;
|
||||
const isUnregisteredSymbol = typeof key === "symbol" && Symbol.keyFor(key) === void 0;
|
||||
const isWeak = isObject || isUnregisteredSymbol;
|
||||
const mapType = isWeak ? WeakMap : Map;
|
||||
return new mapType([[key, inner]]);
|
||||
}
|
||||
function useStrictMemo(callback, dependencies) {
|
||||
const id = useId();
|
||||
useInsertionEffect(() => {
|
||||
return () => {
|
||||
cached.delete(id);
|
||||
};
|
||||
}, [id]);
|
||||
const c = cached.get(id);
|
||||
if (c) {
|
||||
const unwrapped = unwrapFromInner(dependencies, c);
|
||||
if (unwrapped.status === "ok") {
|
||||
return unwrapped.data;
|
||||
}
|
||||
}
|
||||
const value = callback();
|
||||
cached.set(id, wrapToInner(dependencies, value));
|
||||
return value;
|
||||
}
|
||||
export {
|
||||
useStrictMemo
|
||||
};
|
||||
//# sourceMappingURL=use-strict-memo.js.map
|
||||
Loading…
Add table
Add a link
Reference in a new issue