fix prasi global
This commit is contained in:
parent
28cb3fdc4a
commit
34a356ccbd
|
|
@ -18,43 +18,17 @@ export const GlobalContext = createContext({
|
||||||
export const uState = useState;
|
export const uState = useState;
|
||||||
export const useGlobal = <T extends object>(
|
export const useGlobal = <T extends object>(
|
||||||
defaultValue: T,
|
defaultValue: T,
|
||||||
effectOrID?:
|
id: string
|
||||||
| (() => Promise<void | (() => void)> | void | (() => void))
|
|
||||||
| string,
|
|
||||||
id?: string
|
|
||||||
): T & { render: (reset?: boolean) => void } => {
|
): T & { render: (reset?: boolean) => void } => {
|
||||||
const w = window as unknown as {
|
|
||||||
globalValueID: WeakMap<any, string>;
|
|
||||||
};
|
|
||||||
|
|
||||||
if (!w.globalValueID) w.globalValueID = new WeakMap();
|
|
||||||
|
|
||||||
let _id = (typeof effectOrID === "string" ? effectOrID : id) as string;
|
|
||||||
if (!_id) {
|
|
||||||
if (!w.globalValueID.has(defaultValue)) {
|
|
||||||
w.globalValueID.set(defaultValue, createId());
|
|
||||||
}
|
|
||||||
_id = w.globalValueID.get(defaultValue) || "";
|
|
||||||
}
|
|
||||||
if (!_id) {
|
|
||||||
_id = "GLOBAL_DEFAULT";
|
|
||||||
}
|
|
||||||
const ctx = useContext(GlobalContext);
|
const ctx = useContext(GlobalContext);
|
||||||
const { global, render } = ctx;
|
const { global, render } = ctx;
|
||||||
|
|
||||||
if (!global[_id]) {
|
if (!global[id]) {
|
||||||
global[_id] = defaultValue;
|
global[id] = defaultValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
let res: any = null;
|
let res: any = null;
|
||||||
if (typeof effectOrID === "function") {
|
|
||||||
try {
|
|
||||||
res = effectOrID();
|
|
||||||
} catch (e) {
|
|
||||||
console.log(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return () => {
|
return () => {
|
||||||
if (typeof res === "function") res();
|
if (typeof res === "function") res();
|
||||||
else if (res instanceof Promise) {
|
else if (res instanceof Promise) {
|
||||||
|
|
@ -65,17 +39,15 @@ export const useGlobal = <T extends object>(
|
||||||
};
|
};
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const res = global[_id];
|
const res = global[id];
|
||||||
|
|
||||||
if (res) {
|
if (res) {
|
||||||
res.render = (reset?: boolean) => {
|
res.render = (reset?: boolean) => {
|
||||||
if (reset) {
|
if (reset) {
|
||||||
global[_id] = undefined;
|
global[id] = undefined;
|
||||||
}
|
}
|
||||||
startTransition(render);
|
startTransition(render);
|
||||||
};
|
};
|
||||||
} else {
|
|
||||||
console.log(defaultValue, _id);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return res as any;
|
return res as any;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue