esoftplay 0.0.129-a → 0.0.129-c

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 (3) hide show
  1. package/bin/router.js +1 -0
  2. package/global.ts +39 -22
  3. package/package.json +1 -1
package/bin/router.js CHANGED
@@ -447,6 +447,7 @@ declare module "esoftplay" {
447
447
  persistKey?: string,
448
448
  inFile?: boolean,
449
449
  listener?: (data: any) => void,
450
+ loadOnInit?: boolean,
450
451
  isUserData?: boolean
451
452
  }
452
453
 
package/global.ts CHANGED
@@ -21,6 +21,7 @@ export interface useGlobalOption {
21
21
  listener?: (data: any) => void,
22
22
  jsonBeautify?: boolean,
23
23
  isUserData?: boolean,
24
+ loadOnInit?: boolean,
24
25
  onFinish?: () => void
25
26
  }
26
27
 
@@ -46,31 +47,42 @@ export default function useGlobalState<T>(initValue: T, o?: useGlobalOption): us
46
47
  if (!_global.useGlobalSubscriber[_idx])
47
48
  _global.useGlobalSubscriber[_idx] = [];
48
49
  let value: T = initValue;
50
+ let loaded = -1
49
51
 
50
52
  if (o?.persistKey) {
51
- let persistKey = o?.persistKey
52
- STORAGE.getItem(persistKey).then((p: any) => {
53
- if (p) {
54
- if (persistKey != '__globalReady')
55
- if (p != undefined && typeof p == 'string' && (p.startsWith("{") || p.startsWith("[")))
56
- try { set(JSON.parse(p)) } catch (error) { }
57
- else {
58
- if (p == "true" || p == "false") {
59
- try { /* @ts-ignore */ set(eval(p)) } catch (error) { }
60
- } else if (isNaN(p)) {
61
- try { /* @ts-ignore */ set(p) } catch (error) { }
62
- } else {
63
- try { /* @ts-ignore */ set(eval(p)) } catch (error) { }
53
+ loaded = 0
54
+ if (o?.loadOnInit)
55
+ loadFromDisk()
56
+ }
57
+
58
+
59
+ function loadFromDisk() {
60
+ if (loaded == 0) {
61
+ loaded = 1
62
+ let persistKey = o?.persistKey
63
+ STORAGE.getItem(persistKey).then((p: any) => {
64
+ if (p) {
65
+ if (persistKey != '__globalReady')
66
+ if (p != undefined && typeof p == 'string' && (p.startsWith("{") || p.startsWith("[")))
67
+ try { set(JSON.parse(p)) } catch (error) { }
68
+ else {
69
+ if (p == "true" || p == "false") {
70
+ try { /* @ts-ignore */ set(eval(p)) } catch (error) { }
71
+ } else if (isNaN(p)) {
72
+ try { /* @ts-ignore */ set(p) } catch (error) { }
73
+ } else {
74
+ try { /* @ts-ignore */ set(eval(p)) } catch (error) { }
75
+ }
64
76
  }
65
- }
66
- }
67
- if (o?.onFinish) {
68
- clearTimeout(timeoutFinish)
69
- timeoutFinish = setTimeout(() => {
70
- o.onFinish?.()
71
- }, 50);
72
- }
73
- })
77
+ }
78
+ if (o?.onFinish) {
79
+ clearTimeout(timeoutFinish)
80
+ timeoutFinish = setTimeout(() => {
81
+ o.onFinish?.()
82
+ }, 50);
83
+ }
84
+ })
85
+ }
74
86
  }
75
87
 
76
88
 
@@ -116,6 +128,8 @@ export default function useGlobalState<T>(initValue: T, o?: useGlobalOption): us
116
128
  }
117
129
 
118
130
  function useSelector(se: (state: T) => any): void {
131
+ loadFromDisk()
132
+
119
133
  let [l, s] = R.useState<any>(se(value));
120
134
 
121
135
  let sl = R.useCallback(
@@ -141,6 +155,7 @@ export default function useGlobalState<T>(initValue: T, o?: useGlobalOption): us
141
155
  }
142
156
 
143
157
  function get(param?: string, ...params: string[]): any {
158
+ loadFromDisk()
144
159
  let out: any = value;
145
160
  if (param) {
146
161
  const _params = [param, ...params]
@@ -155,6 +170,8 @@ export default function useGlobalState<T>(initValue: T, o?: useGlobalOption): us
155
170
 
156
171
 
157
172
  function useState(): [T, (newState: T) => void, () => T] {
173
+ loadFromDisk()
174
+
158
175
  let [l, s] = R.useState<T>(value);
159
176
 
160
177
  let sl = R.useCallback((ns: T) => { s(ns) }, []);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "esoftplay",
3
- "version": "0.0.129-a",
3
+ "version": "0.0.129-c",
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",