react-native-nitro-storage 0.1.1 → 0.1.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 +43 -22
- package/android/src/main/cpp/AndroidStorageAdapterCpp.cpp +10 -0
- package/android/src/main/cpp/AndroidStorageAdapterCpp.hpp +13 -0
- package/android/src/main/java/com/nitrostorage/AndroidStorageAdapter.kt +10 -0
- package/cpp/bindings/HybridStorage.cpp +50 -0
- package/cpp/bindings/HybridStorage.hpp +4 -0
- package/cpp/core/NativeStorageAdapter.hpp +3 -0
- package/ios/IOSStorageAdapterCpp.hpp +3 -0
- package/ios/IOSStorageAdapterCpp.mm +13 -0
- package/lib/commonjs/Storage.nitro.js +0 -7
- package/lib/commonjs/Storage.nitro.js.map +1 -1
- package/lib/commonjs/Storage.types.js +13 -0
- package/lib/commonjs/Storage.types.js.map +1 -0
- package/lib/commonjs/index.js +69 -8
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/index.web.js +268 -0
- package/lib/commonjs/index.web.js.map +1 -0
- package/lib/commonjs/package.json +1 -0
- package/lib/module/Storage.nitro.js +1 -6
- package/lib/module/Storage.nitro.js.map +1 -1
- package/lib/module/Storage.types.js +9 -0
- package/lib/module/Storage.types.js.map +1 -0
- package/lib/module/index.js +66 -8
- package/lib/module/index.js.map +1 -1
- package/lib/module/index.web.js +257 -0
- package/lib/module/index.web.js.map +1 -0
- package/lib/typescript/Storage.nitro.d.ts +5 -6
- package/lib/typescript/Storage.nitro.d.ts.map +1 -1
- package/lib/typescript/Storage.types.d.ts +6 -0
- package/lib/typescript/Storage.types.d.ts.map +1 -0
- package/lib/typescript/index.d.ts +12 -3
- package/lib/typescript/index.d.ts.map +1 -1
- package/lib/typescript/index.web.d.ts +50 -0
- package/lib/typescript/index.web.d.ts.map +1 -0
- package/nitrogen/generated/android/NitroStorage+autolinking.cmake +1 -1
- package/nitrogen/generated/android/NitroStorage+autolinking.gradle +1 -1
- package/nitrogen/generated/android/NitroStorageOnLoad.cpp +1 -1
- package/nitrogen/generated/android/NitroStorageOnLoad.hpp +1 -1
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/com/nitrostorage/NitroStorageOnLoad.kt +1 -1
- package/nitrogen/generated/ios/NitroStorage+autolinking.rb +2 -2
- package/nitrogen/generated/ios/NitroStorage-Swift-Cxx-Bridge.cpp +1 -1
- package/nitrogen/generated/ios/NitroStorage-Swift-Cxx-Bridge.hpp +1 -1
- package/nitrogen/generated/ios/NitroStorage-Swift-Cxx-Umbrella.hpp +1 -1
- package/nitrogen/generated/ios/NitroStorageAutolinking.mm +1 -1
- package/nitrogen/generated/ios/NitroStorageAutolinking.swift +1 -1
- package/nitrogen/generated/shared/c++/HybridStorageSpec.cpp +5 -1
- package/nitrogen/generated/shared/c++/HybridStorageSpec.hpp +6 -1
- package/package.json +6 -25
- package/src/Storage.nitro.ts +6 -7
- package/src/Storage.types.ts +5 -0
- package/src/index.ts +88 -14
- package/src/index.web.ts +341 -0
package/README.md
CHANGED
|
@@ -35,7 +35,11 @@ Familiar, elegant API with `createStorageItem` and `useStorage`. Works inside an
|
|
|
35
35
|
|
|
36
36
|
### **Production-Ready**
|
|
37
37
|
|
|
38
|
-
Thread-safe C++ core, comprehensive test coverage, and battle-tested on iOS and
|
|
38
|
+
Thread-safe C++ core, comprehensive test coverage, and battle-tested on iOS, Android, and **Web**.
|
|
39
|
+
|
|
40
|
+
### **Web Support**
|
|
41
|
+
|
|
42
|
+
Fully functional on the web via `localStorage` and `sessionStorage`. All hooks and storage atoms are fully reactive across all platforms.
|
|
39
43
|
|
|
40
44
|
---
|
|
41
45
|
|
|
@@ -307,6 +311,29 @@ const unsubscribe = counterAtom.subscribe(() => {
|
|
|
307
311
|
unsubscribe();
|
|
308
312
|
```
|
|
309
313
|
|
|
314
|
+
### Batch Operations
|
|
315
|
+
|
|
316
|
+
Read or write multiple items at once. This is highly optimized on the native side for minimum overhead.
|
|
317
|
+
|
|
318
|
+
```typescript
|
|
319
|
+
import { getBatch, setBatch, removeBatch } from "react-native-nitro-storage";
|
|
320
|
+
|
|
321
|
+
// Write multiple items
|
|
322
|
+
setBatch(
|
|
323
|
+
[
|
|
324
|
+
{ item: item1, value: "v1" },
|
|
325
|
+
{ item: item2, value: "v2" },
|
|
326
|
+
],
|
|
327
|
+
StorageScope.Disk
|
|
328
|
+
);
|
|
329
|
+
|
|
330
|
+
// Read multiple items
|
|
331
|
+
const [v1, v2] = getBatch([item1, item2], StorageScope.Disk);
|
|
332
|
+
|
|
333
|
+
// Remove multiple items
|
|
334
|
+
removeBatch([item1, item2], StorageScope.Disk);
|
|
335
|
+
```
|
|
336
|
+
|
|
310
337
|
### Functional Updates
|
|
311
338
|
|
|
312
339
|
Update state based on the previous value, just like `useState`.
|
|
@@ -331,16 +358,18 @@ function IncrementButton() {
|
|
|
331
358
|
|
|
332
359
|
### Clearing Data
|
|
333
360
|
|
|
334
|
-
Clear
|
|
361
|
+
Clear entire storage scopes at once.
|
|
335
362
|
|
|
336
363
|
```typescript
|
|
337
364
|
import { storage } from "react-native-nitro-storage";
|
|
338
365
|
|
|
339
|
-
// Clear all
|
|
366
|
+
// Clear all storage (all scopes)
|
|
340
367
|
storage.clearAll();
|
|
341
368
|
|
|
342
|
-
//
|
|
369
|
+
// Clear specific scope
|
|
343
370
|
storage.clear(StorageScope.Memory);
|
|
371
|
+
storage.clear(StorageScope.Disk);
|
|
372
|
+
storage.clear(StorageScope.Secure);
|
|
344
373
|
```
|
|
345
374
|
|
|
346
375
|
### Migration from MMKV
|
|
@@ -417,8 +446,14 @@ Returns the setter function only. Does not subscribe to updates.
|
|
|
417
446
|
|
|
418
447
|
### `storage` Object
|
|
419
448
|
|
|
420
|
-
- `clearAll()`: Clears all
|
|
421
|
-
- `clear(scope
|
|
449
|
+
- `clearAll()`: Clears all storage across all scopes.
|
|
450
|
+
- `clear(scope)`: Clears a specific storage scope.
|
|
451
|
+
|
|
452
|
+
### Batch Operations
|
|
453
|
+
|
|
454
|
+
- `getBatch(items, scope)`: Returns an array of values for the given items.
|
|
455
|
+
- `setBatch(items, scope)`: Sets multiple values at once.
|
|
456
|
+
- `removeBatch(items, scope)`: Removes multiple items at once.
|
|
422
457
|
|
|
423
458
|
---
|
|
424
459
|
|
|
@@ -486,27 +521,13 @@ Built on [Nitro Modules](https://nitro.margelo.com) for maximum performance:
|
|
|
486
521
|
| Type-Safe | ✅ | ⚠️ | ⚠️ | ✅ | ⚠️ |
|
|
487
522
|
| Unified API | ✅ | ❌ | ❌ | ❌ | ❌ |
|
|
488
523
|
| React Hooks | ✅ | ❌ | ❌ | ✅ | ❌ |
|
|
524
|
+
| Web Support | ✅ | ❌ | ✅ | ✅ | ❌ |
|
|
489
525
|
|
|
490
526
|
---
|
|
491
527
|
|
|
492
528
|
## 📄 License
|
|
493
529
|
|
|
494
|
-
MIT
|
|
495
|
-
|
|
496
|
-
---
|
|
497
|
-
|
|
498
|
-
## 🙏 Acknowledgments
|
|
499
|
-
|
|
500
|
-
Built with ❤️ using [Nitro Modules](https://nitro.margelo.com) by [Marc Rousavy](https://github.com/mrousavy)
|
|
501
|
-
|
|
502
|
-
---
|
|
503
|
-
|
|
504
|
-
## 📚 Learn More
|
|
505
|
-
|
|
506
|
-
- [Nitro Modules Documentation](https://nitro.margelo.com)
|
|
507
|
-
- [Example App](./apps/example)
|
|
508
|
-
- [API Reference](#-api-reference)
|
|
509
|
-
- [Performance Benchmarks](#-performance-benchmarks)
|
|
530
|
+
MIT
|
|
510
531
|
|
|
511
532
|
---
|
|
512
533
|
|
|
@@ -46,4 +46,14 @@ void AndroidStorageAdapterCpp::deleteSecure(const std::string& key) {
|
|
|
46
46
|
method(AndroidStorageAdapterJava::javaClassStatic(), key);
|
|
47
47
|
}
|
|
48
48
|
|
|
49
|
+
void AndroidStorageAdapterCpp::clearDisk() {
|
|
50
|
+
static auto method = AndroidStorageAdapterJava::javaClassStatic()->getStaticMethod<void()>("clearDisk");
|
|
51
|
+
method(AndroidStorageAdapterJava::javaClassStatic());
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
void AndroidStorageAdapterCpp::clearSecure() {
|
|
55
|
+
static auto method = AndroidStorageAdapterJava::javaClassStatic()->getStaticMethod<void()>("clearSecure");
|
|
56
|
+
method(AndroidStorageAdapterJava::javaClassStatic());
|
|
57
|
+
}
|
|
58
|
+
|
|
49
59
|
} // namespace NitroStorage
|
|
@@ -49,6 +49,16 @@ struct AndroidStorageAdapterJava : facebook::jni::JavaClass<AndroidStorageAdapte
|
|
|
49
49
|
static auto method = javaClassStatic()->getMethod<void(std::string)>("deleteSecure");
|
|
50
50
|
method(self(), key);
|
|
51
51
|
}
|
|
52
|
+
|
|
53
|
+
void clearDisk() {
|
|
54
|
+
static auto method = javaClassStatic()->getStaticMethod<void()>("clearDisk");
|
|
55
|
+
method(javaClassStatic());
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
void clearSecure() {
|
|
59
|
+
static auto method = javaClassStatic()->getStaticMethod<void()>("clearSecure");
|
|
60
|
+
method(javaClassStatic());
|
|
61
|
+
}
|
|
52
62
|
};
|
|
53
63
|
|
|
54
64
|
class AndroidStorageAdapterCpp : public NativeStorageAdapter {
|
|
@@ -63,6 +73,9 @@ public:
|
|
|
63
73
|
void setSecure(const std::string& key, const std::string& value) override;
|
|
64
74
|
std::optional<std::string> getSecure(const std::string& key) override;
|
|
65
75
|
void deleteSecure(const std::string& key) override;
|
|
76
|
+
|
|
77
|
+
void clearDisk() override;
|
|
78
|
+
void clearSecure() override;
|
|
66
79
|
};
|
|
67
80
|
|
|
68
81
|
} // namespace NitroStorage
|
|
@@ -90,5 +90,15 @@ class AndroidStorageAdapter private constructor(private val context: Context) {
|
|
|
90
90
|
fun deleteSecure(key: String) {
|
|
91
91
|
instance?.encryptedPreferences?.edit()?.remove(key)?.apply()
|
|
92
92
|
}
|
|
93
|
+
|
|
94
|
+
@JvmStatic
|
|
95
|
+
fun clearDisk() {
|
|
96
|
+
instance?.sharedPreferences?.edit()?.clear()?.apply()
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
@JvmStatic
|
|
100
|
+
fun clearSecure() {
|
|
101
|
+
instance?.encryptedPreferences?.edit()?.clear()?.apply()
|
|
102
|
+
}
|
|
93
103
|
}
|
|
94
104
|
}
|
|
@@ -113,6 +113,56 @@ std::function<void()> HybridStorage::addOnChange(
|
|
|
113
113
|
};
|
|
114
114
|
}
|
|
115
115
|
|
|
116
|
+
void HybridStorage::clear(double scope) {
|
|
117
|
+
Scope s = toScope(scope);
|
|
118
|
+
|
|
119
|
+
switch (s) {
|
|
120
|
+
case Scope::Memory: {
|
|
121
|
+
std::lock_guard<std::mutex> lock(memoryMutex_);
|
|
122
|
+
memoryStore_.clear();
|
|
123
|
+
break;
|
|
124
|
+
}
|
|
125
|
+
case Scope::Disk:
|
|
126
|
+
nativeAdapter_->clearDisk();
|
|
127
|
+
break;
|
|
128
|
+
case Scope::Secure:
|
|
129
|
+
nativeAdapter_->clearSecure();
|
|
130
|
+
break;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
notifyListeners(static_cast<int>(s), "", std::nullopt);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
void HybridStorage::setBatch(const std::vector<std::string>& keys, const std::vector<std::string>& values, double scope) {
|
|
137
|
+
if (keys.size() != values.size()) {
|
|
138
|
+
throw std::runtime_error("NitroStorage: Keys and values size mismatch in setBatch");
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
for (size_t i = 0; i < keys.size(); ++i) {
|
|
142
|
+
set(keys[i], values[i], scope);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
std::vector<std::string> HybridStorage::getBatch(const std::vector<std::string>& keys, double scope) {
|
|
148
|
+
std::vector<std::string> results;
|
|
149
|
+
results.reserve(keys.size());
|
|
150
|
+
|
|
151
|
+
for (const auto& key : keys) {
|
|
152
|
+
auto val = get(key, scope);
|
|
153
|
+
results.push_back(val.value_or(""));
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
return results;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
void HybridStorage::removeBatch(const std::vector<std::string>& keys, double scope) {
|
|
161
|
+
for (const auto& key : keys) {
|
|
162
|
+
remove(key, scope);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
116
166
|
void HybridStorage::notifyListeners(
|
|
117
167
|
int scope,
|
|
118
168
|
const std::string& key,
|
|
@@ -19,6 +19,10 @@ public:
|
|
|
19
19
|
void set(const std::string& key, const std::string& value, double scope) override;
|
|
20
20
|
std::optional<std::string> get(const std::string& key, double scope) override;
|
|
21
21
|
void remove(const std::string& key, double scope) override;
|
|
22
|
+
void clear(double scope) override;
|
|
23
|
+
void setBatch(const std::vector<std::string>& keys, const std::vector<std::string>& values, double scope) override;
|
|
24
|
+
std::vector<std::string> getBatch(const std::vector<std::string>& keys, double scope) override;
|
|
25
|
+
void removeBatch(const std::vector<std::string>& keys, double scope) override;
|
|
22
26
|
std::function<void()> addOnChange(
|
|
23
27
|
double scope,
|
|
24
28
|
const std::function<void(const std::string&, const std::optional<std::string>&)>& callback
|
|
@@ -16,6 +16,9 @@ public:
|
|
|
16
16
|
virtual void setSecure(const std::string& key, const std::string& value) = 0;
|
|
17
17
|
virtual std::optional<std::string> getSecure(const std::string& key) = 0;
|
|
18
18
|
virtual void deleteSecure(const std::string& key) = 0;
|
|
19
|
+
|
|
20
|
+
virtual void clearDisk() = 0;
|
|
21
|
+
virtual void clearSecure() = 0;
|
|
19
22
|
};
|
|
20
23
|
|
|
21
24
|
} // namespace NitroStorage
|
|
@@ -16,6 +16,9 @@ public:
|
|
|
16
16
|
void setSecure(const std::string& key, const std::string& value) override;
|
|
17
17
|
std::optional<std::string> getSecure(const std::string& key) override;
|
|
18
18
|
void deleteSecure(const std::string& key) override;
|
|
19
|
+
|
|
20
|
+
void clearDisk() override;
|
|
21
|
+
void clearSecure() override;
|
|
19
22
|
};
|
|
20
23
|
|
|
21
24
|
} // namespace NitroStorage
|
|
@@ -75,4 +75,17 @@ void IOSStorageAdapterCpp::deleteSecure(const std::string& key) {
|
|
|
75
75
|
SecItemDelete((__bridge CFDictionaryRef)query);
|
|
76
76
|
}
|
|
77
77
|
|
|
78
|
+
void IOSStorageAdapterCpp::clearDisk() {
|
|
79
|
+
NSString* appDomain = [[NSBundle mainBundle] bundleIdentifier];
|
|
80
|
+
[[NSUserDefaults standardUserDefaults] removePersistentDomainForName:appDomain];
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
void IOSStorageAdapterCpp::clearSecure() {
|
|
84
|
+
NSDictionary* query = @{
|
|
85
|
+
(__bridge id)kSecClass: (__bridge id)kSecClassGenericPassword,
|
|
86
|
+
(__bridge id)kSecAttrService: kKeychainService
|
|
87
|
+
};
|
|
88
|
+
SecItemDelete((__bridge CFDictionaryRef)query);
|
|
89
|
+
}
|
|
90
|
+
|
|
78
91
|
} // namespace NitroStorage
|
|
@@ -3,11 +3,4 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports.StorageScope = void 0;
|
|
7
|
-
let StorageScope = exports.StorageScope = /*#__PURE__*/function (StorageScope) {
|
|
8
|
-
StorageScope[StorageScope["Memory"] = 0] = "Memory";
|
|
9
|
-
StorageScope[StorageScope["Disk"] = 1] = "Disk";
|
|
10
|
-
StorageScope[StorageScope["Secure"] = 2] = "Secure";
|
|
11
|
-
return StorageScope;
|
|
12
|
-
}({});
|
|
13
6
|
//# sourceMappingURL=Storage.nitro.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":[
|
|
1
|
+
{"version":3,"names":[],"sourceRoot":"../../src","sources":["Storage.nitro.ts"],"mappings":"","ignoreList":[]}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.StorageScope = void 0;
|
|
7
|
+
let StorageScope = exports.StorageScope = /*#__PURE__*/function (StorageScope) {
|
|
8
|
+
StorageScope[StorageScope["Memory"] = 0] = "Memory";
|
|
9
|
+
StorageScope[StorageScope["Disk"] = 1] = "Disk";
|
|
10
|
+
StorageScope[StorageScope["Secure"] = 2] = "Secure";
|
|
11
|
+
return StorageScope;
|
|
12
|
+
}({});
|
|
13
|
+
//# sourceMappingURL=Storage.types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["StorageScope","exports"],"sourceRoot":"../../src","sources":["Storage.types.ts"],"mappings":";;;;;;IAAYA,YAAY,GAAAC,OAAA,CAAAD,YAAA,0BAAZA,YAAY;EAAZA,YAAY,CAAZA,YAAY;EAAZA,YAAY,CAAZA,YAAY;EAAZA,YAAY,CAAZA,YAAY;EAAA,OAAZA,YAAY;AAAA","ignoreList":[]}
|
package/lib/commonjs/index.js
CHANGED
|
@@ -6,16 +6,19 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
6
6
|
Object.defineProperty(exports, "StorageScope", {
|
|
7
7
|
enumerable: true,
|
|
8
8
|
get: function () {
|
|
9
|
-
return
|
|
9
|
+
return _Storage.StorageScope;
|
|
10
10
|
}
|
|
11
11
|
});
|
|
12
12
|
exports.createStorageItem = createStorageItem;
|
|
13
|
+
exports.getBatch = getBatch;
|
|
14
|
+
exports.removeBatch = removeBatch;
|
|
15
|
+
exports.setBatch = setBatch;
|
|
13
16
|
exports.storage = void 0;
|
|
14
17
|
exports.useSetStorage = useSetStorage;
|
|
15
18
|
exports.useStorage = useStorage;
|
|
16
19
|
var _react = require("react");
|
|
17
20
|
var _reactNativeNitroModules = require("react-native-nitro-modules");
|
|
18
|
-
var
|
|
21
|
+
var _Storage = require("./Storage.types");
|
|
19
22
|
let _storageModule = null;
|
|
20
23
|
function getStorageModule() {
|
|
21
24
|
if (!_storageModule) {
|
|
@@ -30,10 +33,17 @@ function notifyMemoryListeners(key, value) {
|
|
|
30
33
|
}
|
|
31
34
|
const storage = exports.storage = {
|
|
32
35
|
clear: scope => {
|
|
33
|
-
|
|
36
|
+
if (scope === _Storage.StorageScope.Memory) {
|
|
37
|
+
memoryStore.clear();
|
|
38
|
+
notifyMemoryListeners("", undefined);
|
|
39
|
+
} else {
|
|
40
|
+
getStorageModule().clear(scope);
|
|
41
|
+
}
|
|
34
42
|
},
|
|
35
43
|
clearAll: () => {
|
|
36
|
-
storage.clear(
|
|
44
|
+
storage.clear(_Storage.StorageScope.Memory);
|
|
45
|
+
storage.clear(_Storage.StorageScope.Disk);
|
|
46
|
+
storage.clear(_Storage.StorageScope.Secure);
|
|
37
47
|
}
|
|
38
48
|
};
|
|
39
49
|
function defaultSerialize(value) {
|
|
@@ -45,14 +55,16 @@ function defaultDeserialize(value) {
|
|
|
45
55
|
function createStorageItem(config) {
|
|
46
56
|
const serialize = config.serialize ?? defaultSerialize;
|
|
47
57
|
const deserialize = config.deserialize ?? defaultDeserialize;
|
|
48
|
-
const isMemory = config.scope ===
|
|
58
|
+
const isMemory = config.scope === _Storage.StorageScope.Memory;
|
|
49
59
|
const listeners = new Set();
|
|
50
60
|
let unsubscribe = null;
|
|
51
61
|
const ensureSubscription = () => {
|
|
52
62
|
if (!unsubscribe) {
|
|
53
63
|
if (isMemory) {
|
|
54
64
|
const listener = key => {
|
|
55
|
-
if (key === config.key) {
|
|
65
|
+
if (key === "" || key === config.key) {
|
|
66
|
+
lastRaw = undefined;
|
|
67
|
+
lastValue = undefined;
|
|
56
68
|
listeners.forEach(l => l());
|
|
57
69
|
}
|
|
58
70
|
};
|
|
@@ -60,7 +72,9 @@ function createStorageItem(config) {
|
|
|
60
72
|
unsubscribe = () => memoryListeners.delete(listener);
|
|
61
73
|
} else {
|
|
62
74
|
unsubscribe = getStorageModule().addOnChange(config.scope, key => {
|
|
63
|
-
if (key === config.key) {
|
|
75
|
+
if (key === "" || key === config.key) {
|
|
76
|
+
lastRaw = undefined;
|
|
77
|
+
lastValue = undefined;
|
|
64
78
|
listeners.forEach(listener => listener());
|
|
65
79
|
}
|
|
66
80
|
});
|
|
@@ -93,7 +107,7 @@ function createStorageItem(config) {
|
|
|
93
107
|
};
|
|
94
108
|
const set = valueOrFn => {
|
|
95
109
|
const currentValue = get();
|
|
96
|
-
const newValue = valueOrFn
|
|
110
|
+
const newValue = typeof valueOrFn === "function" ? valueOrFn(currentValue) : valueOrFn;
|
|
97
111
|
if (isMemory) {
|
|
98
112
|
memoryStore.set(config.key, newValue);
|
|
99
113
|
notifyMemoryListeners(config.key, newValue);
|
|
@@ -126,6 +140,13 @@ function createStorageItem(config) {
|
|
|
126
140
|
set,
|
|
127
141
|
delete: deleteItem,
|
|
128
142
|
subscribe,
|
|
143
|
+
serialize,
|
|
144
|
+
deserialize,
|
|
145
|
+
_triggerListeners: () => {
|
|
146
|
+
lastRaw = undefined;
|
|
147
|
+
lastValue = undefined;
|
|
148
|
+
listeners.forEach(l => l());
|
|
149
|
+
},
|
|
129
150
|
scope: config.scope,
|
|
130
151
|
key: config.key
|
|
131
152
|
};
|
|
@@ -137,4 +158,44 @@ function useStorage(item) {
|
|
|
137
158
|
function useSetStorage(item) {
|
|
138
159
|
return item.set;
|
|
139
160
|
}
|
|
161
|
+
function getBatch(items, scope) {
|
|
162
|
+
if (scope === _Storage.StorageScope.Memory) {
|
|
163
|
+
return items.map(item => item.get());
|
|
164
|
+
}
|
|
165
|
+
const keys = items.map(item => item.key);
|
|
166
|
+
const rawValues = getStorageModule().getBatch(keys, scope);
|
|
167
|
+
return items.map((item, idx) => {
|
|
168
|
+
const raw = rawValues[idx];
|
|
169
|
+
if (raw === undefined) {
|
|
170
|
+
return item.get();
|
|
171
|
+
}
|
|
172
|
+
return item.deserialize(raw);
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
function setBatch(items, scope) {
|
|
176
|
+
if (scope === _Storage.StorageScope.Memory) {
|
|
177
|
+
items.forEach(({
|
|
178
|
+
item,
|
|
179
|
+
value
|
|
180
|
+
}) => item.set(value));
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
const keys = items.map(i => i.item.key);
|
|
184
|
+
const values = items.map(i => i.item.serialize(i.value));
|
|
185
|
+
getStorageModule().setBatch(keys, values, scope);
|
|
186
|
+
items.forEach(({
|
|
187
|
+
item
|
|
188
|
+
}) => {
|
|
189
|
+
item._triggerListeners();
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
function removeBatch(items, scope) {
|
|
193
|
+
if (scope === _Storage.StorageScope.Memory) {
|
|
194
|
+
items.forEach(item => item.delete());
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
const keys = items.map(item => item.key);
|
|
198
|
+
getStorageModule().removeBatch(keys, scope);
|
|
199
|
+
items.forEach(item => item.delete());
|
|
200
|
+
}
|
|
140
201
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["_react","require","_reactNativeNitroModules","
|
|
1
|
+
{"version":3,"names":["_react","require","_reactNativeNitroModules","_Storage","_storageModule","getStorageModule","NitroModules","createHybridObject","memoryStore","Map","memoryListeners","Set","notifyMemoryListeners","key","value","forEach","listener","storage","exports","clear","scope","StorageScope","Memory","undefined","clearAll","Disk","Secure","defaultSerialize","JSON","stringify","defaultDeserialize","parse","createStorageItem","config","serialize","deserialize","isMemory","listeners","unsubscribe","ensureSubscription","lastRaw","lastValue","l","add","delete","addOnChange","get","raw","defaultValue","set","valueOrFn","currentValue","newValue","serialized","deleteItem","remove","subscribe","callback","size","_triggerListeners","useStorage","item","useSyncExternalStore","useSetStorage","getBatch","items","map","keys","rawValues","idx","setBatch","i","values","removeBatch"],"sourceRoot":"../../src","sources":["index.ts"],"mappings":";;;;;;;;;;;;;;;;;;AAAA,IAAAA,MAAA,GAAAC,OAAA;AACA,IAAAC,wBAAA,GAAAD,OAAA;AAEA,IAAAE,QAAA,GAAAF,OAAA;A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