esoftplay 0.0.117-d → 0.0.117-e
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/global.ts +13 -39
- package/package.json +1 -1
package/global.ts
CHANGED
|
@@ -35,28 +35,20 @@ class Context {
|
|
|
35
35
|
|
|
36
36
|
export const globalIdx = new Context()
|
|
37
37
|
|
|
38
|
-
|
|
39
|
-
let subscriber = {}
|
|
40
|
-
let debouceTime
|
|
41
|
-
let persistKeys: any = {}
|
|
42
|
-
|
|
43
38
|
export default function useGlobalState<T>(initValue: T, o?: useGlobalOption): useGlobalReturn<T> {
|
|
44
39
|
const _idx = globalIdx.idx
|
|
45
|
-
if (!
|
|
46
|
-
|
|
40
|
+
if (!_global.useGlobalSubscriber[_idx])
|
|
41
|
+
_global.useGlobalSubscriber[_idx] = [];
|
|
47
42
|
let value: T = initValue;
|
|
48
|
-
|
|
43
|
+
|
|
49
44
|
// rehidryte instant
|
|
50
45
|
if (o?.persistKey) {
|
|
51
|
-
|
|
52
|
-
if (
|
|
46
|
+
AsyncStorage.getItem(o.persistKey).then((p) => {
|
|
47
|
+
if (p) {
|
|
53
48
|
if (p.startsWith("{") || p.startsWith("["))
|
|
54
|
-
try {
|
|
55
|
-
|
|
56
|
-
} catch (error) { }
|
|
57
|
-
else {
|
|
58
|
-
set(p);
|
|
59
|
-
}
|
|
49
|
+
try { set(JSON.parse(p)) } catch (error) { }
|
|
50
|
+
else
|
|
51
|
+
try { set(p) } catch (error) { }
|
|
60
52
|
}
|
|
61
53
|
})
|
|
62
54
|
}
|
|
@@ -77,7 +69,7 @@ export default function useGlobalState<T>(initValue: T, o?: useGlobalOption): us
|
|
|
77
69
|
const isChange = !isEqual(value, ns)
|
|
78
70
|
if (isChange) {
|
|
79
71
|
value = ns
|
|
80
|
-
fastLoop(
|
|
72
|
+
fastLoop(_global.useGlobalSubscriber?.[_idx], (c) => { c?.(ns) })
|
|
81
73
|
if (o?.persistKey) {
|
|
82
74
|
AsyncStorage.setItem(o.persistKey, JSON.stringify(ns))
|
|
83
75
|
}
|
|
@@ -111,9 +103,9 @@ export default function useGlobalState<T>(initValue: T, o?: useGlobalOption): us
|
|
|
111
103
|
|
|
112
104
|
function subscribe(func: any) {
|
|
113
105
|
R.useLayoutEffect(() => {
|
|
114
|
-
|
|
106
|
+
_global.useGlobalSubscriber[_idx].push(func);
|
|
115
107
|
return () => {
|
|
116
|
-
|
|
108
|
+
_global.useGlobalSubscriber[_idx] = fastFilter(_global.useGlobalSubscriber?.[_idx], (f) => f !== func)
|
|
117
109
|
};
|
|
118
110
|
}, [func]);
|
|
119
111
|
}
|
|
@@ -145,26 +137,8 @@ export default function useGlobalState<T>(initValue: T, o?: useGlobalOption): us
|
|
|
145
137
|
const children = props.render(state)
|
|
146
138
|
return children ? R.cloneElement(children) : null
|
|
147
139
|
}
|
|
148
|
-
|
|
140
|
+
|
|
149
141
|
globalIdx.increment()
|
|
142
|
+
_global.useGlobalStateReady = 1
|
|
150
143
|
return { useState, get, set, useSelector, reset: del, connect: _connect };
|
|
151
144
|
}
|
|
152
|
-
|
|
153
|
-
function debounce(func: () => any, delay: number): void {
|
|
154
|
-
clearTimeout(debouceTime)
|
|
155
|
-
debouceTime = setTimeout(() => func(), delay)
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
function rehidryte(key: string, func: (e: string) => void) {
|
|
159
|
-
persistKeys[key] = func
|
|
160
|
-
debounce(() => {
|
|
161
|
-
AsyncStorage.multiGet(Object.keys(persistKeys), (e, v) => {
|
|
162
|
-
if (v && !e) {
|
|
163
|
-
v.forEach((iv, i) => { persistKeys[iv[0]]?.(iv[1]) })
|
|
164
|
-
_global.useGlobalStateReady = 1
|
|
165
|
-
} else {
|
|
166
|
-
_global.useGlobalStateReady = 1
|
|
167
|
-
}
|
|
168
|
-
})
|
|
169
|
-
}, 100)
|
|
170
|
-
}
|