react-native-nitro-storage 0.3.0 → 0.3.2
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/README.md +594 -247
- package/android/CMakeLists.txt +2 -0
- package/android/src/main/cpp/AndroidStorageAdapterCpp.cpp +102 -11
- package/android/src/main/cpp/AndroidStorageAdapterCpp.hpp +16 -0
- package/android/src/main/java/com/nitrostorage/AndroidStorageAdapter.kt +154 -34
- package/android/src/main/java/com/nitrostorage/NitroStoragePackage.kt +2 -2
- package/cpp/bindings/HybridStorage.cpp +176 -21
- package/cpp/bindings/HybridStorage.hpp +29 -2
- package/cpp/core/NativeStorageAdapter.hpp +16 -0
- package/ios/IOSStorageAdapterCpp.hpp +20 -0
- package/ios/IOSStorageAdapterCpp.mm +239 -32
- package/lib/commonjs/Storage.types.js +23 -1
- package/lib/commonjs/Storage.types.js.map +1 -1
- package/lib/commonjs/index.js +292 -75
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/index.web.js +473 -86
- package/lib/commonjs/index.web.js.map +1 -1
- package/lib/commonjs/internal.js +10 -0
- package/lib/commonjs/internal.js.map +1 -1
- package/lib/commonjs/storage-hooks.js +36 -0
- package/lib/commonjs/storage-hooks.js.map +1 -0
- package/lib/module/Storage.types.js +22 -0
- package/lib/module/Storage.types.js.map +1 -1
- package/lib/module/index.js +264 -75
- package/lib/module/index.js.map +1 -1
- package/lib/module/index.web.js +445 -86
- package/lib/module/index.web.js.map +1 -1
- package/lib/module/internal.js +8 -0
- package/lib/module/internal.js.map +1 -1
- package/lib/module/storage-hooks.js +30 -0
- package/lib/module/storage-hooks.js.map +1 -0
- package/lib/typescript/Storage.nitro.d.ts +12 -0
- package/lib/typescript/Storage.nitro.d.ts.map +1 -1
- package/lib/typescript/Storage.types.d.ts +20 -0
- package/lib/typescript/Storage.types.d.ts.map +1 -1
- package/lib/typescript/index.d.ts +33 -10
- package/lib/typescript/index.d.ts.map +1 -1
- package/lib/typescript/index.web.d.ts +45 -10
- package/lib/typescript/index.web.d.ts.map +1 -1
- package/lib/typescript/internal.d.ts +2 -0
- package/lib/typescript/internal.d.ts.map +1 -1
- package/lib/typescript/storage-hooks.d.ts +10 -0
- package/lib/typescript/storage-hooks.d.ts.map +1 -0
- package/nitrogen/generated/shared/c++/HybridStorageSpec.cpp +12 -0
- package/nitrogen/generated/shared/c++/HybridStorageSpec.hpp +12 -0
- package/package.json +8 -3
- package/src/Storage.nitro.ts +13 -2
- package/src/Storage.types.ts +22 -0
- package/src/index.ts +382 -123
- package/src/index.web.ts +618 -134
- package/src/internal.ts +14 -4
- package/src/migration.ts +1 -1
- package/src/storage-hooks.ts +48 -0
package/src/internal.ts
CHANGED
|
@@ -4,6 +4,7 @@ export const MIGRATION_VERSION_KEY = "__nitro_storage_migration_version__";
|
|
|
4
4
|
export const NATIVE_BATCH_MISSING_SENTINEL =
|
|
5
5
|
"__nitro_storage_batch_missing__::v1";
|
|
6
6
|
const PRIMITIVE_FAST_PATH_PREFIX = "__nitro_storage_primitive__:";
|
|
7
|
+
const NAMESPACE_SEPARATOR = ":";
|
|
7
8
|
|
|
8
9
|
export type StoredEnvelope = {
|
|
9
10
|
__nitroStorageEnvelope: true;
|
|
@@ -41,7 +42,7 @@ export type ScopedBatchItem = {
|
|
|
41
42
|
|
|
42
43
|
export function assertBatchScope(
|
|
43
44
|
items: readonly ScopedBatchItem[],
|
|
44
|
-
scope: StorageScope
|
|
45
|
+
scope: StorageScope,
|
|
45
46
|
): void {
|
|
46
47
|
const mismatchedItem = items.find((item) => item.scope !== scope);
|
|
47
48
|
if (!mismatchedItem) {
|
|
@@ -53,12 +54,12 @@ export function assertBatchScope(
|
|
|
53
54
|
StorageScope[mismatchedItem.scope] ?? String(mismatchedItem.scope);
|
|
54
55
|
|
|
55
56
|
throw new Error(
|
|
56
|
-
`Batch scope mismatch for "${mismatchedItem.key}": expected ${expectedScope}, received ${actualScope}
|
|
57
|
+
`Batch scope mismatch for "${mismatchedItem.key}": expected ${expectedScope}, received ${actualScope}.`,
|
|
57
58
|
);
|
|
58
59
|
}
|
|
59
60
|
|
|
60
61
|
export function decodeNativeBatchValue(
|
|
61
|
-
value: string | undefined
|
|
62
|
+
value: string | undefined,
|
|
62
63
|
): string | undefined {
|
|
63
64
|
if (value === NATIVE_BATCH_MISSING_SENTINEL) {
|
|
64
65
|
return undefined;
|
|
@@ -67,6 +68,15 @@ export function decodeNativeBatchValue(
|
|
|
67
68
|
return value;
|
|
68
69
|
}
|
|
69
70
|
|
|
71
|
+
export function prefixKey(namespace: string | undefined, key: string): string {
|
|
72
|
+
if (!namespace) return key;
|
|
73
|
+
return `${namespace}${NAMESPACE_SEPARATOR}${key}`;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export function isNamespaced(key: string, namespace: string): boolean {
|
|
77
|
+
return key.startsWith(`${namespace}${NAMESPACE_SEPARATOR}`);
|
|
78
|
+
}
|
|
79
|
+
|
|
70
80
|
export function serializeWithPrimitiveFastPath<T>(value: T): string {
|
|
71
81
|
if (value === null) {
|
|
72
82
|
return `${PRIMITIVE_FAST_PATH_PREFIX}l`;
|
|
@@ -91,7 +101,7 @@ export function serializeWithPrimitiveFastPath<T>(value: T): string {
|
|
|
91
101
|
const serialized = JSON.stringify(value);
|
|
92
102
|
if (serialized === undefined) {
|
|
93
103
|
throw new Error(
|
|
94
|
-
"Unable to serialize value with default serializer. Provide a custom serialize function."
|
|
104
|
+
"Unable to serialize value with default serializer. Provide a custom serialize function.",
|
|
95
105
|
);
|
|
96
106
|
}
|
|
97
107
|
return serialized;
|
package/src/migration.ts
CHANGED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { useRef, useSyncExternalStore } from "react";
|
|
2
|
+
|
|
3
|
+
type HookStorageItem<T> = {
|
|
4
|
+
get: () => T;
|
|
5
|
+
set: (value: T | ((prev: T) => T)) => void;
|
|
6
|
+
subscribe: (callback: () => void) => () => void;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
export function useStorage<T>(
|
|
10
|
+
item: HookStorageItem<T>,
|
|
11
|
+
): [T, (value: T | ((prev: T) => T)) => void] {
|
|
12
|
+
const value = useSyncExternalStore(item.subscribe, item.get, item.get);
|
|
13
|
+
return [value, item.set];
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function useStorageSelector<T, TSelected>(
|
|
17
|
+
item: HookStorageItem<T>,
|
|
18
|
+
selector: (value: T) => TSelected,
|
|
19
|
+
isEqual: (prev: TSelected, next: TSelected) => boolean = Object.is,
|
|
20
|
+
): [TSelected, (value: T | ((prev: T) => T)) => void] {
|
|
21
|
+
const selectedRef = useRef<
|
|
22
|
+
{ hasValue: false } | { hasValue: true; value: TSelected }
|
|
23
|
+
>({
|
|
24
|
+
hasValue: false,
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
const getSelectedSnapshot = () => {
|
|
28
|
+
const nextSelected = selector(item.get());
|
|
29
|
+
const current = selectedRef.current;
|
|
30
|
+
if (current.hasValue && isEqual(current.value, nextSelected)) {
|
|
31
|
+
return current.value;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
selectedRef.current = { hasValue: true, value: nextSelected };
|
|
35
|
+
return nextSelected;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
const selectedValue = useSyncExternalStore(
|
|
39
|
+
item.subscribe,
|
|
40
|
+
getSelectedSnapshot,
|
|
41
|
+
getSelectedSnapshot,
|
|
42
|
+
);
|
|
43
|
+
return [selectedValue, item.set];
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function useSetStorage<T>(item: HookStorageItem<T>) {
|
|
47
|
+
return item.set;
|
|
48
|
+
}
|