esoftplay 0.0.136-i → 0.0.136-k
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 +34 -48
- package/mmkv.ts +2 -2
- package/package.json +1 -1
package/global.ts
CHANGED
|
@@ -17,31 +17,19 @@ export interface useGlobalAutoSync {
|
|
|
17
17
|
post: (item: any) => Object,
|
|
18
18
|
isSyncing?: (isSync: boolean) => void
|
|
19
19
|
}
|
|
20
|
-
interface useGlobalOptionA {
|
|
21
|
-
persistKey?: string;
|
|
22
|
-
inFastStorage?: boolean;
|
|
23
|
-
inFile?: never; // Ensures inFile is not present in useGlobalOptionA
|
|
24
|
-
listener?: (data: any) => void;
|
|
25
|
-
useAutoSync?: useGlobalAutoSync;
|
|
26
|
-
jsonBeautify?: boolean;
|
|
27
|
-
isUserData?: boolean;
|
|
28
|
-
loadOnInit?: boolean;
|
|
29
|
-
onFinish?: () => void;
|
|
30
|
-
}
|
|
31
20
|
|
|
32
|
-
interface
|
|
33
|
-
persistKey?: string
|
|
34
|
-
inFile?: boolean
|
|
35
|
-
inFastStorage?:
|
|
36
|
-
listener?: (data: any) => void
|
|
37
|
-
useAutoSync?: useGlobalAutoSync
|
|
38
|
-
jsonBeautify?: boolean
|
|
39
|
-
isUserData?: boolean
|
|
40
|
-
loadOnInit?: boolean
|
|
41
|
-
onFinish?: () => void
|
|
21
|
+
export interface useGlobalOption {
|
|
22
|
+
persistKey?: string,
|
|
23
|
+
inFile?: boolean,
|
|
24
|
+
inFastStorage?: boolean,
|
|
25
|
+
listener?: (data: any) => void,
|
|
26
|
+
useAutoSync?: useGlobalAutoSync,
|
|
27
|
+
jsonBeautify?: boolean,
|
|
28
|
+
isUserData?: boolean,
|
|
29
|
+
loadOnInit?: boolean,
|
|
30
|
+
onFinish?: () => void
|
|
42
31
|
}
|
|
43
32
|
|
|
44
|
-
export type useGlobalOption = useGlobalOptionA | useGlobalOptionB;
|
|
45
33
|
export interface useGlobalConnect<T> {
|
|
46
34
|
render: (props: T) => any,
|
|
47
35
|
}
|
|
@@ -57,10 +45,7 @@ export default function useGlobalState<T>(initValue: T, o?: useGlobalOption): us
|
|
|
57
45
|
let sync: any = undefined
|
|
58
46
|
|
|
59
47
|
if (o?.persistKey) {
|
|
60
|
-
|
|
61
|
-
STORAGE = require('esoftplay/mmkv').default
|
|
62
|
-
} else
|
|
63
|
-
STORAGE = o?.inFile ? (require('esoftplay/storage').default) : (require('@react-native-async-storage/async-storage').default)
|
|
48
|
+
STORAGE = o?.inFastStorage ? require('esoftplay/mmkv').default : (o?.inFile ? (require('esoftplay/storage').default) : (require('@react-native-async-storage/async-storage').default))
|
|
64
49
|
loaded = 0
|
|
65
50
|
if (o?.loadOnInit)
|
|
66
51
|
loadFromDisk()
|
|
@@ -98,32 +83,33 @@ export default function useGlobalState<T>(initValue: T, o?: useGlobalOption): us
|
|
|
98
83
|
})[0]
|
|
99
84
|
}
|
|
100
85
|
|
|
101
|
-
|
|
86
|
+
function loadFromDisk() {
|
|
102
87
|
if (loaded == 0) {
|
|
103
88
|
loaded = 1
|
|
104
89
|
let persistKey = o?.persistKey
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
90
|
+
STORAGE.getItem(String(persistKey)).then((p: any) => {
|
|
91
|
+
if (p) {
|
|
92
|
+
if (persistKey != '__globalReady')
|
|
93
|
+
if (p != undefined && typeof p == 'string' && (p.startsWith("{") || p.startsWith("[")))
|
|
94
|
+
try { set(JSON.parse(p)) } catch (error) { }
|
|
95
|
+
else {
|
|
96
|
+
if (p == "true" || p == "false") {
|
|
97
|
+
try { /* @ts-ignore */ set(eval(p)) } catch (error) { }
|
|
98
|
+
} else if (isNaN(p)) {
|
|
99
|
+
try { /* @ts-ignore */ set(p) } catch (error) { }
|
|
100
|
+
} else {
|
|
101
|
+
try { /* @ts-ignore */ set(eval(p)) } catch (error) { }
|
|
102
|
+
}
|
|
117
103
|
}
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
if (o?.onFinish) {
|
|
121
|
-
clearTimeout(timeoutFinish)
|
|
122
|
-
timeoutFinish = setTimeout(() => {
|
|
123
|
-
o.onFinish?.()
|
|
104
|
+
}
|
|
105
|
+
if (o?.onFinish) {
|
|
124
106
|
clearTimeout(timeoutFinish)
|
|
125
|
-
|
|
126
|
-
|
|
107
|
+
timeoutFinish = setTimeout(() => {
|
|
108
|
+
o.onFinish?.()
|
|
109
|
+
clearTimeout(timeoutFinish)
|
|
110
|
+
}, 50);
|
|
111
|
+
}
|
|
112
|
+
})
|
|
127
113
|
}
|
|
128
114
|
}
|
|
129
115
|
|
|
@@ -181,7 +167,7 @@ export default function useGlobalState<T>(initValue: T, o?: useGlobalOption): us
|
|
|
181
167
|
set(initValue)
|
|
182
168
|
}
|
|
183
169
|
|
|
184
|
-
|
|
170
|
+
function useSelector(se: (state: T) => any): void {
|
|
185
171
|
loadFromDisk()
|
|
186
172
|
|
|
187
173
|
let [l, s] = R.useState<any>(se(value));
|
package/mmkv.ts
CHANGED
|
@@ -3,8 +3,8 @@ import { MMKV } from 'react-native-mmkv';
|
|
|
3
3
|
const storage = new MMKV()
|
|
4
4
|
|
|
5
5
|
const FastStorage = {
|
|
6
|
-
getItem(key: string): string | undefined {
|
|
7
|
-
return storage.getString(key)
|
|
6
|
+
getItem(key: string): Promise<string | undefined | null> {
|
|
7
|
+
return new Promise((r) => r(storage.getString(key)))
|
|
8
8
|
},
|
|
9
9
|
setItem(key: string, value: string) {
|
|
10
10
|
storage.set(key, value)
|