@onekeyfe/react-native-async-storage 3.0.77 → 3.0.78
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/lib/module/NativeAsyncStorage.web.js +2 -1
- package/lib/module/index.js +28 -5
- package/lib/module/runtimeConfig.js +10 -4
- package/lib/typescript/src/NativeAsyncStorage.web.d.ts +4 -1
- package/lib/typescript/src/runtimeConfig.d.ts +2 -0
- package/package.json +1 -1
- package/src/NativeAsyncStorage.web.ts +6 -1
- package/src/index.tsx +41 -5
- package/src/runtimeConfig.ts +16 -6
|
@@ -135,7 +135,8 @@ const AsyncStorage = {
|
|
|
135
135
|
multiMerge: (keyValuePairs, callback) => {
|
|
136
136
|
const promises = keyValuePairs.map(item => AsyncStorage.mergeItem(item[0], item[1]));
|
|
137
137
|
return createPromiseAll(promises, callback);
|
|
138
|
-
}
|
|
138
|
+
},
|
|
139
|
+
reloadManifest: () => Promise.resolve()
|
|
139
140
|
};
|
|
140
141
|
export default AsyncStorage;
|
|
141
142
|
//# sourceMappingURL=NativeAsyncStorage.web.js.map
|
package/lib/module/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
import NativeModule from './NativeAsyncStorage';
|
|
4
|
-
import { forwardAsyncStorageWriteIfNeeded } from "./runtimeConfig.js";
|
|
4
|
+
import { forwardAsyncStorageWrite, forwardAsyncStorageWriteIfNeeded, shouldForwardAsyncStorageWrite } from "./runtimeConfig.js";
|
|
5
5
|
async function forwardWriteAndReloadManifestIfNeeded(method, args) {
|
|
6
6
|
if (!(await forwardAsyncStorageWriteIfNeeded(method, args))) {
|
|
7
7
|
return false;
|
|
@@ -10,13 +10,36 @@ async function forwardWriteAndReloadManifestIfNeeded(method, args) {
|
|
|
10
10
|
return true;
|
|
11
11
|
}
|
|
12
12
|
async function runLocalWriteWithFreshManifest(write) {
|
|
13
|
+
// This narrows the local-write stale manifest window, but it does not make
|
|
14
|
+
// uninjected writes cross-runtime serialized. Writes that already completed
|
|
15
|
+
// before forwarder installation keep the legacy local-write semantics.
|
|
13
16
|
await NativeModule.reloadManifest();
|
|
14
17
|
await write();
|
|
15
18
|
}
|
|
19
|
+
async function runReadWithFreshManifest(read) {
|
|
20
|
+
// iOS keeps one manifest cache per runtime/module instance while the files on
|
|
21
|
+
// disk are shared. Refresh before reads so main can observe bg-origin writes
|
|
22
|
+
// without routing reads through the bg runtime. Android/web implementations
|
|
23
|
+
// expose reloadManifest as a no-op.
|
|
24
|
+
await NativeModule.reloadManifest();
|
|
25
|
+
return read();
|
|
26
|
+
}
|
|
27
|
+
async function forwardClearAsMultiRemoveIfNeeded() {
|
|
28
|
+
if (!shouldForwardAsyncStorageWrite()) {
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
await NativeModule.reloadManifest();
|
|
32
|
+
const keys = [...(await NativeModule.getAllKeys())];
|
|
33
|
+
// Use the fresh main-runtime key list; bg clear may enumerate from a stale
|
|
34
|
+
// manifest.
|
|
35
|
+
await forwardAsyncStorageWrite('multiRemove', [keys]);
|
|
36
|
+
await NativeModule.reloadManifest();
|
|
37
|
+
return true;
|
|
38
|
+
}
|
|
16
39
|
function createAsyncStorage() {
|
|
17
40
|
const getItem = async (key, callback) => {
|
|
18
41
|
try {
|
|
19
|
-
const result = await NativeModule.multiGet([key]);
|
|
42
|
+
const result = await runReadWithFreshManifest(() => NativeModule.multiGet([key]));
|
|
20
43
|
const value = result?.[0]?.[1] ?? null;
|
|
21
44
|
callback?.(null, value);
|
|
22
45
|
return value;
|
|
@@ -64,7 +87,7 @@ function createAsyncStorage() {
|
|
|
64
87
|
};
|
|
65
88
|
const clear = async callback => {
|
|
66
89
|
try {
|
|
67
|
-
if (!(await
|
|
90
|
+
if (!(await forwardClearAsMultiRemoveIfNeeded())) {
|
|
68
91
|
await NativeModule.clear();
|
|
69
92
|
}
|
|
70
93
|
callback?.(null);
|
|
@@ -76,7 +99,7 @@ function createAsyncStorage() {
|
|
|
76
99
|
};
|
|
77
100
|
const getAllKeys = async callback => {
|
|
78
101
|
try {
|
|
79
|
-
const keys = await NativeModule.getAllKeys();
|
|
102
|
+
const keys = await runReadWithFreshManifest(() => NativeModule.getAllKeys());
|
|
80
103
|
callback?.(null, keys);
|
|
81
104
|
return keys;
|
|
82
105
|
} catch (e) {
|
|
@@ -90,7 +113,7 @@ function createAsyncStorage() {
|
|
|
90
113
|
};
|
|
91
114
|
const multiGet = async (keys, callback) => {
|
|
92
115
|
try {
|
|
93
|
-
const result = await NativeModule.multiGet([...keys]);
|
|
116
|
+
const result = await runReadWithFreshManifest(() => NativeModule.multiGet([...keys]));
|
|
94
117
|
callback?.(null, result);
|
|
95
118
|
return result;
|
|
96
119
|
} catch (e) {
|
|
@@ -20,15 +20,21 @@ function getWriteForwarder() {
|
|
|
20
20
|
function shouldForwardWriteToBackground() {
|
|
21
21
|
return getShouldForwardWriteGetter()?.() ?? false;
|
|
22
22
|
}
|
|
23
|
-
export
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
23
|
+
export function shouldForwardAsyncStorageWrite() {
|
|
24
|
+
return shouldForwardWriteToBackground();
|
|
25
|
+
}
|
|
26
|
+
export async function forwardAsyncStorageWrite(method, args) {
|
|
27
27
|
const forwarder = getWriteForwarder();
|
|
28
28
|
if (!forwarder) {
|
|
29
29
|
throw new Error('AsyncStorage write forwarder is not configured.');
|
|
30
30
|
}
|
|
31
31
|
await forwarder(method, args);
|
|
32
|
+
}
|
|
33
|
+
export async function forwardAsyncStorageWriteIfNeeded(method, args) {
|
|
34
|
+
if (!shouldForwardAsyncStorageWrite()) {
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
await forwardAsyncStorageWrite(method, args);
|
|
32
38
|
return true;
|
|
33
39
|
}
|
|
34
40
|
//# sourceMappingURL=runtimeConfig.js.map
|
|
@@ -6,6 +6,9 @@
|
|
|
6
6
|
* LICENSE file in the root directory of this source tree.
|
|
7
7
|
*/
|
|
8
8
|
import type { AsyncStorageStatic } from './types';
|
|
9
|
-
|
|
9
|
+
type AsyncStorageWebStatic = AsyncStorageStatic & {
|
|
10
|
+
reloadManifest: () => Promise<void>;
|
|
11
|
+
};
|
|
12
|
+
declare const AsyncStorage: AsyncStorageWebStatic;
|
|
10
13
|
export default AsyncStorage;
|
|
11
14
|
//# sourceMappingURL=NativeAsyncStorage.web.d.ts.map
|
|
@@ -10,5 +10,7 @@ export type AsyncStorageShouldForwardWriteGetter = () => boolean;
|
|
|
10
10
|
export type AsyncStorageWriteForwarder = <T extends AsyncStorageWriteMethod>(method: T, args: AsyncStorageWriteArgs<T>) => Promise<void>;
|
|
11
11
|
export declare function setAsyncStorageShouldForwardWriteGetter(getter: AsyncStorageShouldForwardWriteGetter): void;
|
|
12
12
|
export declare function setAsyncStorageWriteForwarder(forwarder: AsyncStorageWriteForwarder): void;
|
|
13
|
+
export declare function shouldForwardAsyncStorageWrite(): boolean;
|
|
14
|
+
export declare function forwardAsyncStorageWrite<T extends AsyncStorageWriteMethod>(method: T, args: AsyncStorageWriteArgs<T>): Promise<void>;
|
|
13
15
|
export declare function forwardAsyncStorageWriteIfNeeded<T extends AsyncStorageWriteMethod>(method: T, args: AsyncStorageWriteArgs<T>): Promise<boolean>;
|
|
14
16
|
//# sourceMappingURL=runtimeConfig.d.ts.map
|
package/package.json
CHANGED
|
@@ -17,6 +17,9 @@ import type {
|
|
|
17
17
|
type OnMultiResult = Function;
|
|
18
18
|
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
19
19
|
type OnResult = Function;
|
|
20
|
+
type AsyncStorageWebStatic = AsyncStorageStatic & {
|
|
21
|
+
reloadManifest: () => Promise<void>;
|
|
22
|
+
};
|
|
20
23
|
|
|
21
24
|
const merge = mergeOptions.bind({
|
|
22
25
|
concatArrays: true,
|
|
@@ -73,7 +76,7 @@ function createPromiseAll<
|
|
|
73
76
|
);
|
|
74
77
|
}
|
|
75
78
|
|
|
76
|
-
const AsyncStorage:
|
|
79
|
+
const AsyncStorage: AsyncStorageWebStatic = {
|
|
77
80
|
/**
|
|
78
81
|
* Fetches `key` value.
|
|
79
82
|
*/
|
|
@@ -176,6 +179,8 @@ const AsyncStorage: AsyncStorageStatic = {
|
|
|
176
179
|
);
|
|
177
180
|
return createPromiseAll(promises, callback);
|
|
178
181
|
},
|
|
182
|
+
|
|
183
|
+
reloadManifest: () => Promise.resolve(),
|
|
179
184
|
};
|
|
180
185
|
|
|
181
186
|
export default AsyncStorage;
|
package/src/index.tsx
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
import NativeModule from './NativeAsyncStorage';
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
forwardAsyncStorageWrite,
|
|
4
|
+
forwardAsyncStorageWriteIfNeeded,
|
|
5
|
+
shouldForwardAsyncStorageWrite,
|
|
6
|
+
} from './runtimeConfig';
|
|
3
7
|
import type {
|
|
4
8
|
AsyncStorageWriteArgs,
|
|
5
9
|
AsyncStorageWriteMethod,
|
|
@@ -18,14 +22,42 @@ async function forwardWriteAndReloadManifestIfNeeded<
|
|
|
18
22
|
}
|
|
19
23
|
|
|
20
24
|
async function runLocalWriteWithFreshManifest(write: () => Promise<void>) {
|
|
25
|
+
// This narrows the local-write stale manifest window, but it does not make
|
|
26
|
+
// uninjected writes cross-runtime serialized. Writes that already completed
|
|
27
|
+
// before forwarder installation keep the legacy local-write semantics.
|
|
21
28
|
await NativeModule.reloadManifest();
|
|
22
29
|
await write();
|
|
23
30
|
}
|
|
24
31
|
|
|
32
|
+
async function runReadWithFreshManifest<T>(read: () => Promise<T>): Promise<T> {
|
|
33
|
+
// iOS keeps one manifest cache per runtime/module instance while the files on
|
|
34
|
+
// disk are shared. Refresh before reads so main can observe bg-origin writes
|
|
35
|
+
// without routing reads through the bg runtime. Android/web implementations
|
|
36
|
+
// expose reloadManifest as a no-op.
|
|
37
|
+
await NativeModule.reloadManifest();
|
|
38
|
+
return read();
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
async function forwardClearAsMultiRemoveIfNeeded() {
|
|
42
|
+
if (!shouldForwardAsyncStorageWrite()) {
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
await NativeModule.reloadManifest();
|
|
47
|
+
const keys = [...(await NativeModule.getAllKeys())];
|
|
48
|
+
// Use the fresh main-runtime key list; bg clear may enumerate from a stale
|
|
49
|
+
// manifest.
|
|
50
|
+
await forwardAsyncStorageWrite('multiRemove', [keys]);
|
|
51
|
+
await NativeModule.reloadManifest();
|
|
52
|
+
return true;
|
|
53
|
+
}
|
|
54
|
+
|
|
25
55
|
function createAsyncStorage(): AsyncStorageStatic {
|
|
26
56
|
const getItem: AsyncStorageStatic['getItem'] = async (key, callback) => {
|
|
27
57
|
try {
|
|
28
|
-
const result = await
|
|
58
|
+
const result = await runReadWithFreshManifest(() =>
|
|
59
|
+
NativeModule.multiGet([key])
|
|
60
|
+
);
|
|
29
61
|
const value = result?.[0]?.[1] ?? null;
|
|
30
62
|
callback?.(null, value);
|
|
31
63
|
return value;
|
|
@@ -104,7 +136,7 @@ function createAsyncStorage(): AsyncStorageStatic {
|
|
|
104
136
|
|
|
105
137
|
const clear: AsyncStorageStatic['clear'] = async (callback) => {
|
|
106
138
|
try {
|
|
107
|
-
if (!(await
|
|
139
|
+
if (!(await forwardClearAsMultiRemoveIfNeeded())) {
|
|
108
140
|
await NativeModule.clear();
|
|
109
141
|
}
|
|
110
142
|
callback?.(null);
|
|
@@ -117,7 +149,9 @@ function createAsyncStorage(): AsyncStorageStatic {
|
|
|
117
149
|
|
|
118
150
|
const getAllKeys: AsyncStorageStatic['getAllKeys'] = async (callback) => {
|
|
119
151
|
try {
|
|
120
|
-
const keys = await
|
|
152
|
+
const keys = await runReadWithFreshManifest(() =>
|
|
153
|
+
NativeModule.getAllKeys()
|
|
154
|
+
);
|
|
121
155
|
callback?.(null, keys);
|
|
122
156
|
return keys;
|
|
123
157
|
} catch (e) {
|
|
@@ -133,7 +167,9 @@ function createAsyncStorage(): AsyncStorageStatic {
|
|
|
133
167
|
|
|
134
168
|
const multiGet: AsyncStorageStatic['multiGet'] = async (keys, callback) => {
|
|
135
169
|
try {
|
|
136
|
-
const result = await
|
|
170
|
+
const result = await runReadWithFreshManifest(() =>
|
|
171
|
+
NativeModule.multiGet([...keys])
|
|
172
|
+
);
|
|
137
173
|
callback?.(null, result);
|
|
138
174
|
return result;
|
|
139
175
|
} catch (e) {
|
package/src/runtimeConfig.ts
CHANGED
|
@@ -59,18 +59,28 @@ function shouldForwardWriteToBackground() {
|
|
|
59
59
|
return getShouldForwardWriteGetter()?.() ?? false;
|
|
60
60
|
}
|
|
61
61
|
|
|
62
|
-
export
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
if (!shouldForwardWriteToBackground()) {
|
|
66
|
-
return false;
|
|
67
|
-
}
|
|
62
|
+
export function shouldForwardAsyncStorageWrite() {
|
|
63
|
+
return shouldForwardWriteToBackground();
|
|
64
|
+
}
|
|
68
65
|
|
|
66
|
+
export async function forwardAsyncStorageWrite<
|
|
67
|
+
T extends AsyncStorageWriteMethod
|
|
68
|
+
>(method: T, args: AsyncStorageWriteArgs<T>): Promise<void> {
|
|
69
69
|
const forwarder = getWriteForwarder();
|
|
70
70
|
if (!forwarder) {
|
|
71
71
|
throw new Error('AsyncStorage write forwarder is not configured.');
|
|
72
72
|
}
|
|
73
73
|
|
|
74
74
|
await forwarder(method, args);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export async function forwardAsyncStorageWriteIfNeeded<
|
|
78
|
+
T extends AsyncStorageWriteMethod
|
|
79
|
+
>(method: T, args: AsyncStorageWriteArgs<T>): Promise<boolean> {
|
|
80
|
+
if (!shouldForwardAsyncStorageWrite()) {
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
await forwardAsyncStorageWrite(method, args);
|
|
75
85
|
return true;
|
|
76
86
|
}
|