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.
Files changed (2) hide show
  1. package/global.ts +13 -39
  2. 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 (!subscriber[_idx])
46
- subscriber[_idx] = [];
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
- rehidryte(o.persistKey, (p) => {
52
- if (typeof p == 'string') {
46
+ AsyncStorage.getItem(o.persistKey).then((p) => {
47
+ if (p) {
53
48
  if (p.startsWith("{") || p.startsWith("["))
54
- try {
55
- set(JSON.parse(p))
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(subscriber?.[_idx], (c) => { c?.(ns) })
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
- subscriber[_idx].push(func);
106
+ _global.useGlobalSubscriber[_idx].push(func);
115
107
  return () => {
116
- subscriber[_idx] = fastFilter(subscriber?.[_idx], (f) => f !== func)
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
- }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "esoftplay",
3
- "version": "0.0.117-d",
3
+ "version": "0.0.117-e",
4
4
  "description": "embedding data from esoftplay framework (web based) into mobile app",
5
5
  "main": "cache/index.js",
6
6
  "types": "../../index.d.ts",