esoftplay 0.0.118-d → 0.0.118-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/_cache.ts +11 -9
- package/bin/router.js +1 -0
- package/package.json +1 -1
package/_cache.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import AsyncStorage from '@react-native-async-storage/async-storage';
|
|
2
|
+
import Storage from 'esoftplay/storage';
|
|
2
3
|
import * as R from 'react';
|
|
3
4
|
import { fastFilter, fastLoop } from './fast';
|
|
4
5
|
const isEqual = require('react-fast-compare');
|
|
@@ -11,21 +12,22 @@ export interface UseCache_return<T> {
|
|
|
11
12
|
|
|
12
13
|
export interface UseCache_options {
|
|
13
14
|
persistKey?: string,
|
|
15
|
+
inFile?: boolean,
|
|
14
16
|
listener?: (data: any) => void
|
|
15
17
|
}
|
|
16
18
|
export default (() => {
|
|
17
19
|
let useCacheIdx = 0
|
|
18
|
-
let useCacheSubscriber = []
|
|
19
|
-
let value = []
|
|
20
|
+
let useCacheSubscriber: any[] = []
|
|
20
21
|
return <T>(initValue: T, o?: UseCache_options): UseCache_return<T> => {
|
|
22
|
+
const STORAGE = o?.inFile ? new Storage() : AsyncStorage
|
|
21
23
|
const _idx = useCacheIdx
|
|
22
24
|
if (!useCacheSubscriber[_idx]) {
|
|
23
25
|
useCacheSubscriber[_idx] = [];
|
|
24
26
|
}
|
|
25
|
-
value
|
|
27
|
+
let value = initValue
|
|
26
28
|
// rehidryte instant
|
|
27
29
|
if (o?.persistKey) {
|
|
28
|
-
|
|
30
|
+
STORAGE.getItem(o.persistKey).then((p) => {
|
|
29
31
|
if (p)
|
|
30
32
|
set(JSON.parse(p))
|
|
31
33
|
})
|
|
@@ -33,14 +35,14 @@ export default (() => {
|
|
|
33
35
|
|
|
34
36
|
function set(ns: T | ((x: T) => T)) {
|
|
35
37
|
let isChange = false
|
|
36
|
-
if (!isEqual(value
|
|
38
|
+
if (!isEqual(value, ns)) {
|
|
37
39
|
isChange = true
|
|
38
40
|
}
|
|
39
41
|
if (isChange) {
|
|
40
|
-
value
|
|
41
|
-
fastLoop(useCacheSubscriber[_idx], (c: any) => c?.(value
|
|
42
|
+
value = ns instanceof Function ? ns(value) : ns
|
|
43
|
+
fastLoop(useCacheSubscriber[_idx], (c: any) => c?.(value))
|
|
42
44
|
if (o?.persistKey) {
|
|
43
|
-
|
|
45
|
+
STORAGE.setItem(o.persistKey, JSON.stringify(value))
|
|
44
46
|
}
|
|
45
47
|
if (o?.listener) {
|
|
46
48
|
o?.listener?.(ns instanceof Function ? ns(value[_idx]) : ns)
|
|
@@ -50,7 +52,7 @@ export default (() => {
|
|
|
50
52
|
|
|
51
53
|
function del() {
|
|
52
54
|
if (o?.persistKey) {
|
|
53
|
-
|
|
55
|
+
STORAGE.removeItem(o.persistKey)
|
|
54
56
|
}
|
|
55
57
|
set(initValue)
|
|
56
58
|
}
|
package/bin/router.js
CHANGED