@buoy-gg/storage 1.7.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 +607 -0
- package/lib/commonjs/index.js +34 -0
- package/lib/commonjs/package.json +1 -0
- package/lib/commonjs/preset.js +94 -0
- package/lib/commonjs/storage/components/DiffViewer/DiffOptionsPanel.js +356 -0
- package/lib/commonjs/storage/components/DiffViewer/TreeDiffViewer.js +29 -0
- package/lib/commonjs/storage/components/DiffViewer/components/DiffSummary.js +121 -0
- package/lib/commonjs/storage/components/DiffViewer/modes/ThemedSplitView.js +419 -0
- package/lib/commonjs/storage/components/DiffViewer/themes/diffThemes.js +122 -0
- package/lib/commonjs/storage/components/GameUIStorageBrowser.js +924 -0
- package/lib/commonjs/storage/components/GameUIStorageStats.js +746 -0
- package/lib/commonjs/storage/components/MMKVInstanceInfoPanel.js +257 -0
- package/lib/commonjs/storage/components/MMKVInstanceSelector.js +418 -0
- package/lib/commonjs/storage/components/SelectionActionBar.js +224 -0
- package/lib/commonjs/storage/components/StorageActionButtons.js +239 -0
- package/lib/commonjs/storage/components/StorageActions.js +192 -0
- package/lib/commonjs/storage/components/StorageBrowserMode.js +31 -0
- package/lib/commonjs/storage/components/StorageEventDetailContent.js +1025 -0
- package/lib/commonjs/storage/components/StorageEventFilterView.js +141 -0
- package/lib/commonjs/storage/components/StorageEventListener.js +357 -0
- package/lib/commonjs/storage/components/StorageEventsSection.js +24 -0
- package/lib/commonjs/storage/components/StorageFilterCards.js +345 -0
- package/lib/commonjs/storage/components/StorageFilterViewV2.js +42 -0
- package/lib/commonjs/storage/components/StorageKeyCard.js +516 -0
- package/lib/commonjs/storage/components/StorageKeyRow.js +356 -0
- package/lib/commonjs/storage/components/StorageKeySection.js +105 -0
- package/lib/commonjs/storage/components/StorageKeyStats.js +344 -0
- package/lib/commonjs/storage/components/StorageModalWithTabs.js +871 -0
- package/lib/commonjs/storage/components/StorageSection.js +43 -0
- package/lib/commonjs/storage/hooks/useAsyncStorageKeys.js +126 -0
- package/lib/commonjs/storage/hooks/useMMKVInstances.js +221 -0
- package/lib/commonjs/storage/hooks/useMMKVKeys.js +362 -0
- package/lib/commonjs/storage/hooks/useTickEverySecond.js +21 -0
- package/lib/commonjs/storage/index.js +148 -0
- package/lib/commonjs/storage/types.js +5 -0
- package/lib/commonjs/storage/utils/AsyncStorageListener.js +510 -0
- package/lib/commonjs/storage/utils/MMKVInstanceRegistry.js +202 -0
- package/lib/commonjs/storage/utils/MMKVListener.js +380 -0
- package/lib/commonjs/storage/utils/clearAllStorage.js +47 -0
- package/lib/commonjs/storage/utils/index.js +180 -0
- package/lib/commonjs/storage/utils/lineDiff.js +363 -0
- package/lib/commonjs/storage/utils/mmkvAvailability.js +62 -0
- package/lib/commonjs/storage/utils/mmkvTypeDetection.js +139 -0
- package/lib/commonjs/storage/utils/objectDiff.js +157 -0
- package/lib/commonjs/storage/utils/safeAsyncStorage.js +140 -0
- package/lib/commonjs/storage/utils/storageActionHelpers.js +46 -0
- package/lib/commonjs/storage/utils/storageQueryUtils.js +35 -0
- package/lib/commonjs/storage/utils/valueType.js +18 -0
- package/lib/module/index.js +7 -0
- package/lib/module/preset.js +89 -0
- package/lib/module/storage/components/DiffViewer/DiffOptionsPanel.js +352 -0
- package/lib/module/storage/components/DiffViewer/TreeDiffViewer.js +25 -0
- package/lib/module/storage/components/DiffViewer/components/DiffSummary.js +117 -0
- package/lib/module/storage/components/DiffViewer/modes/ThemedSplitView.js +415 -0
- package/lib/module/storage/components/DiffViewer/themes/diffThemes.js +118 -0
- package/lib/module/storage/components/GameUIStorageBrowser.js +922 -0
- package/lib/module/storage/components/GameUIStorageStats.js +742 -0
- package/lib/module/storage/components/MMKVInstanceInfoPanel.js +253 -0
- package/lib/module/storage/components/MMKVInstanceSelector.js +414 -0
- package/lib/module/storage/components/SelectionActionBar.js +221 -0
- package/lib/module/storage/components/StorageActionButtons.js +236 -0
- package/lib/module/storage/components/StorageActions.js +189 -0
- package/lib/module/storage/components/StorageBrowserMode.js +27 -0
- package/lib/module/storage/components/StorageEventDetailContent.js +1020 -0
- package/lib/module/storage/components/StorageEventFilterView.js +137 -0
- package/lib/module/storage/components/StorageEventListener.js +354 -0
- package/lib/module/storage/components/StorageEventsSection.js +20 -0
- package/lib/module/storage/components/StorageFilterCards.js +341 -0
- package/lib/module/storage/components/StorageFilterViewV2.js +38 -0
- package/lib/module/storage/components/StorageKeyCard.js +513 -0
- package/lib/module/storage/components/StorageKeyRow.js +353 -0
- package/lib/module/storage/components/StorageKeySection.js +101 -0
- package/lib/module/storage/components/StorageKeyStats.js +340 -0
- package/lib/module/storage/components/StorageModalWithTabs.js +867 -0
- package/lib/module/storage/components/StorageSection.js +40 -0
- package/lib/module/storage/hooks/useAsyncStorageKeys.js +121 -0
- package/lib/module/storage/hooks/useMMKVInstances.js +216 -0
- package/lib/module/storage/hooks/useMMKVKeys.js +359 -0
- package/lib/module/storage/hooks/useTickEverySecond.js +18 -0
- package/lib/module/storage/index.js +25 -0
- package/lib/module/storage/types.js +3 -0
- package/lib/module/storage/utils/AsyncStorageListener.js +500 -0
- package/lib/module/storage/utils/MMKVInstanceRegistry.js +196 -0
- package/lib/module/storage/utils/MMKVListener.js +367 -0
- package/lib/module/storage/utils/clearAllStorage.js +42 -0
- package/lib/module/storage/utils/index.js +22 -0
- package/lib/module/storage/utils/lineDiff.js +359 -0
- package/lib/module/storage/utils/mmkvAvailability.js +56 -0
- package/lib/module/storage/utils/mmkvTypeDetection.js +133 -0
- package/lib/module/storage/utils/objectDiff.js +153 -0
- package/lib/module/storage/utils/safeAsyncStorage.js +134 -0
- package/lib/module/storage/utils/storageActionHelpers.js +42 -0
- package/lib/module/storage/utils/storageQueryUtils.js +30 -0
- package/lib/module/storage/utils/valueType.js +14 -0
- package/lib/typescript/index.d.ts +3 -0
- package/lib/typescript/index.d.ts.map +1 -0
- package/lib/typescript/preset.d.ts +90 -0
- package/lib/typescript/preset.d.ts.map +1 -0
- package/lib/typescript/storage/components/DiffViewer/DiffOptionsPanel.d.ts +18 -0
- package/lib/typescript/storage/components/DiffViewer/DiffOptionsPanel.d.ts.map +1 -0
- package/lib/typescript/storage/components/DiffViewer/TreeDiffViewer.d.ts +7 -0
- package/lib/typescript/storage/components/DiffViewer/TreeDiffViewer.d.ts.map +1 -0
- package/lib/typescript/storage/components/DiffViewer/components/DiffSummary.d.ts +12 -0
- package/lib/typescript/storage/components/DiffViewer/components/DiffSummary.d.ts.map +1 -0
- package/lib/typescript/storage/components/DiffViewer/modes/ThemedSplitView.d.ts +13 -0
- package/lib/typescript/storage/components/DiffViewer/modes/ThemedSplitView.d.ts.map +1 -0
- package/lib/typescript/storage/components/DiffViewer/themes/diffThemes.d.ts +64 -0
- package/lib/typescript/storage/components/DiffViewer/themes/diffThemes.d.ts.map +1 -0
- package/lib/typescript/storage/components/GameUIStorageBrowser.d.ts +16 -0
- package/lib/typescript/storage/components/GameUIStorageBrowser.d.ts.map +1 -0
- package/lib/typescript/storage/components/GameUIStorageStats.d.ts +7 -0
- package/lib/typescript/storage/components/GameUIStorageStats.d.ts.map +1 -0
- package/lib/typescript/storage/components/MMKVInstanceInfoPanel.d.ts +42 -0
- package/lib/typescript/storage/components/MMKVInstanceInfoPanel.d.ts.map +1 -0
- package/lib/typescript/storage/components/MMKVInstanceSelector.d.ts +35 -0
- package/lib/typescript/storage/components/MMKVInstanceSelector.d.ts.map +1 -0
- package/lib/typescript/storage/components/SelectionActionBar.d.ts +21 -0
- package/lib/typescript/storage/components/SelectionActionBar.d.ts.map +1 -0
- package/lib/typescript/storage/components/StorageActionButtons.d.ts +21 -0
- package/lib/typescript/storage/components/StorageActionButtons.d.ts.map +1 -0
- package/lib/typescript/storage/components/StorageActions.d.ts +10 -0
- package/lib/typescript/storage/components/StorageActions.d.ts.map +1 -0
- package/lib/typescript/storage/components/StorageBrowserMode.d.ts +18 -0
- package/lib/typescript/storage/components/StorageBrowserMode.d.ts.map +1 -0
- package/lib/typescript/storage/components/StorageEventDetailContent.d.ts +40 -0
- package/lib/typescript/storage/components/StorageEventDetailContent.d.ts.map +1 -0
- package/lib/typescript/storage/components/StorageEventFilterView.d.ts +11 -0
- package/lib/typescript/storage/components/StorageEventFilterView.d.ts.map +1 -0
- package/lib/typescript/storage/components/StorageEventListener.d.ts +6 -0
- package/lib/typescript/storage/components/StorageEventListener.d.ts.map +1 -0
- package/lib/typescript/storage/components/StorageEventsSection.d.ts +7 -0
- package/lib/typescript/storage/components/StorageEventsSection.d.ts.map +1 -0
- package/lib/typescript/storage/components/StorageFilterCards.d.ts +36 -0
- package/lib/typescript/storage/components/StorageFilterCards.d.ts.map +1 -0
- package/lib/typescript/storage/components/StorageFilterViewV2.d.ts +9 -0
- package/lib/typescript/storage/components/StorageFilterViewV2.d.ts.map +1 -0
- package/lib/typescript/storage/components/StorageKeyCard.d.ts +17 -0
- package/lib/typescript/storage/components/StorageKeyCard.d.ts.map +1 -0
- package/lib/typescript/storage/components/StorageKeyRow.d.ts +15 -0
- package/lib/typescript/storage/components/StorageKeyRow.d.ts.map +1 -0
- package/lib/typescript/storage/components/StorageKeySection.d.ts +25 -0
- package/lib/typescript/storage/components/StorageKeySection.d.ts.map +1 -0
- package/lib/typescript/storage/components/StorageKeyStats.d.ts +15 -0
- package/lib/typescript/storage/components/StorageKeyStats.d.ts.map +1 -0
- package/lib/typescript/storage/components/StorageModalWithTabs.d.ts +13 -0
- package/lib/typescript/storage/components/StorageModalWithTabs.d.ts.map +1 -0
- package/lib/typescript/storage/components/StorageSection.d.ts +10 -0
- package/lib/typescript/storage/components/StorageSection.d.ts.map +1 -0
- package/lib/typescript/storage/hooks/useAsyncStorageKeys.d.ts +10 -0
- package/lib/typescript/storage/hooks/useAsyncStorageKeys.d.ts.map +1 -0
- package/lib/typescript/storage/hooks/useMMKVInstances.d.ts +114 -0
- package/lib/typescript/storage/hooks/useMMKVInstances.d.ts.map +1 -0
- package/lib/typescript/storage/hooks/useMMKVKeys.d.ts +94 -0
- package/lib/typescript/storage/hooks/useMMKVKeys.d.ts.map +1 -0
- package/lib/typescript/storage/hooks/useTickEverySecond.d.ts +6 -0
- package/lib/typescript/storage/hooks/useTickEverySecond.d.ts.map +1 -0
- package/lib/typescript/storage/index.d.ts +15 -0
- package/lib/typescript/storage/index.d.ts.map +1 -0
- package/lib/typescript/storage/types.d.ts +41 -0
- package/lib/typescript/storage/types.d.ts.map +1 -0
- package/lib/typescript/storage/utils/AsyncStorageListener.d.ts +195 -0
- package/lib/typescript/storage/utils/AsyncStorageListener.d.ts.map +1 -0
- package/lib/typescript/storage/utils/MMKVInstanceRegistry.d.ts +224 -0
- package/lib/typescript/storage/utils/MMKVInstanceRegistry.d.ts.map +1 -0
- package/lib/typescript/storage/utils/MMKVListener.d.ts +218 -0
- package/lib/typescript/storage/utils/MMKVListener.d.ts.map +1 -0
- package/lib/typescript/storage/utils/clearAllStorage.d.ts +11 -0
- package/lib/typescript/storage/utils/clearAllStorage.d.ts.map +1 -0
- package/lib/typescript/storage/utils/index.d.ts +8 -0
- package/lib/typescript/storage/utils/index.d.ts.map +1 -0
- package/lib/typescript/storage/utils/lineDiff.d.ts +34 -0
- package/lib/typescript/storage/utils/lineDiff.d.ts.map +1 -0
- package/lib/typescript/storage/utils/mmkvAvailability.d.ts +23 -0
- package/lib/typescript/storage/utils/mmkvAvailability.d.ts.map +1 -0
- package/lib/typescript/storage/utils/mmkvTypeDetection.d.ts +71 -0
- package/lib/typescript/storage/utils/mmkvTypeDetection.d.ts.map +1 -0
- package/lib/typescript/storage/utils/objectDiff.d.ts +35 -0
- package/lib/typescript/storage/utils/objectDiff.d.ts.map +1 -0
- package/lib/typescript/storage/utils/safeAsyncStorage.d.ts +56 -0
- package/lib/typescript/storage/utils/safeAsyncStorage.d.ts.map +1 -0
- package/lib/typescript/storage/utils/storageActionHelpers.d.ts +5 -0
- package/lib/typescript/storage/utils/storageActionHelpers.d.ts.map +1 -0
- package/lib/typescript/storage/utils/storageQueryUtils.d.ts +6 -0
- package/lib/typescript/storage/utils/storageQueryUtils.d.ts.map +1 -0
- package/lib/typescript/storage/utils/valueType.d.ts +3 -0
- package/lib/typescript/storage/utils/valueType.d.ts.map +1 -0
- package/package.json +68 -0
|
@@ -0,0 +1,367 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* MMKVListener - Hybrid Monitoring Strategy
|
|
5
|
+
*
|
|
6
|
+
* This module provides comprehensive monitoring for MMKV storage instances using
|
|
7
|
+
* a hybrid approach that combines:
|
|
8
|
+
*
|
|
9
|
+
* 1. MMKV's built-in addOnValueChangedListener() - for write operations
|
|
10
|
+
* 2. Method wrapping - for read operations and type detection
|
|
11
|
+
*
|
|
12
|
+
* Why Hybrid Strategy?
|
|
13
|
+
* - MMKV listeners only fire on writes (set/delete), not reads
|
|
14
|
+
* - MMKV listeners don't provide value type information
|
|
15
|
+
* - Method wrapping captures ALL operations with full context
|
|
16
|
+
* - Combining both gives complete observability
|
|
17
|
+
*
|
|
18
|
+
* Key Differences from AsyncStorage:
|
|
19
|
+
* - Synchronous API (no async/await needed)
|
|
20
|
+
* - Multi-instance support (track multiple MMKV instances)
|
|
21
|
+
* - Native types (string, number, boolean, buffer)
|
|
22
|
+
* - Built-in listener support (MMKV.addOnValueChangedListener)
|
|
23
|
+
*
|
|
24
|
+
* @see AsyncStorageListener.ts for AsyncStorage equivalent
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
// Use 'any' type for MMKV to avoid hard dependency on react-native-mmkv
|
|
28
|
+
|
|
29
|
+
import { detectMMKVType } from "./mmkvTypeDetection";
|
|
30
|
+
|
|
31
|
+
// MMKV method signatures
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Event types for MMKV operations
|
|
35
|
+
*/
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Instance tracking for MMKV monitoring
|
|
39
|
+
*/
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Singleton class for monitoring MMKV operations across multiple instances
|
|
43
|
+
*
|
|
44
|
+
* Uses a hybrid monitoring strategy:
|
|
45
|
+
* 1. MMKV's built-in listener API for write notifications
|
|
46
|
+
* 2. Method wrapping for read operations and type information
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```typescript
|
|
50
|
+
* import { MMKV } from 'react-native-mmkv';
|
|
51
|
+
* import { mmkvListener } from './MMKVListener';
|
|
52
|
+
*
|
|
53
|
+
* const storage = new MMKV();
|
|
54
|
+
*
|
|
55
|
+
* // Start monitoring this instance
|
|
56
|
+
* mmkvListener.addInstance(storage, 'mmkv.default');
|
|
57
|
+
*
|
|
58
|
+
* // Add listener for all MMKV events
|
|
59
|
+
* const unsubscribe = mmkvListener.addListener((event) => {
|
|
60
|
+
* console.log(`[${event.instanceId}] ${event.action}:`, event.data);
|
|
61
|
+
* });
|
|
62
|
+
*
|
|
63
|
+
* // Cleanup
|
|
64
|
+
* unsubscribe();
|
|
65
|
+
* mmkvListener.removeInstance('mmkv.default');
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
68
|
+
class MMKVListener {
|
|
69
|
+
listeners = [];
|
|
70
|
+
instances = new Map();
|
|
71
|
+
|
|
72
|
+
// Keys to ignore to prevent dev tools from triggering self-events
|
|
73
|
+
ignoredKeys = new Set(["@react_buoy_storage_event_filters", "@react_buoy_storage_key_filters", "@react_buoy_storage_is_monitoring", "REACT_QUERY_OFFLINE_CACHE"]);
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Determines if a storage key should be ignored
|
|
77
|
+
*/
|
|
78
|
+
shouldIgnoreKey(key) {
|
|
79
|
+
if (this.ignoredKeys.has(key)) return true;
|
|
80
|
+
for (const ignoredKey of this.ignoredKeys) {
|
|
81
|
+
if (key.startsWith(ignoredKey)) return true;
|
|
82
|
+
}
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Emit an MMKV event to all registered listeners
|
|
88
|
+
*/
|
|
89
|
+
emit(event) {
|
|
90
|
+
if (this.listeners.length === 0) return;
|
|
91
|
+
this.listeners.forEach(listener => {
|
|
92
|
+
try {
|
|
93
|
+
listener(event);
|
|
94
|
+
} catch (error) {
|
|
95
|
+
// Error in MMKV event listener - continuing with others
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Add an MMKV instance for monitoring
|
|
102
|
+
*
|
|
103
|
+
* This method sets up both native MMKV listeners and method wrapping
|
|
104
|
+
* to provide complete observability.
|
|
105
|
+
*
|
|
106
|
+
* @param instance - MMKV instance to monitor
|
|
107
|
+
* @param instanceId - Unique identifier for this instance
|
|
108
|
+
*
|
|
109
|
+
* @example
|
|
110
|
+
* ```typescript
|
|
111
|
+
* const storage = new MMKV();
|
|
112
|
+
* mmkvListener.addInstance(storage, 'mmkv.default');
|
|
113
|
+
*
|
|
114
|
+
* const secureStorage = new MMKV({ id: 'secure', encryptionKey: key });
|
|
115
|
+
* mmkvListener.addInstance(secureStorage, 'secure');
|
|
116
|
+
* ```
|
|
117
|
+
*/
|
|
118
|
+
addInstance(instance, instanceId) {
|
|
119
|
+
// Check if already monitoring this instance
|
|
120
|
+
if (this.instances.has(instanceId)) {
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// v4 COMPATIBILITY: Use ONLY native listener (no method wrapping)
|
|
125
|
+
// react-native-mmkv v4 instances have frozen/read-only methods that cannot be wrapped
|
|
126
|
+
// The native listener (addOnValueChangedListener) is sufficient for monitoring writes
|
|
127
|
+
|
|
128
|
+
let nativeListenerUnsubscribe;
|
|
129
|
+
if (typeof instance.addOnValueChangedListener === "function") {
|
|
130
|
+
try {
|
|
131
|
+
nativeListenerUnsubscribe = instance.addOnValueChangedListener(key => {
|
|
132
|
+
// Ignore dev tools keys
|
|
133
|
+
if (this.shouldIgnoreKey(key)) return;
|
|
134
|
+
|
|
135
|
+
// Detect the value type
|
|
136
|
+
const {
|
|
137
|
+
value,
|
|
138
|
+
type
|
|
139
|
+
} = detectMMKVType(instance, key);
|
|
140
|
+
|
|
141
|
+
// Determine action: if value is undefined/unknown, it was deleted
|
|
142
|
+
const isDelete = type === "unknown" || value === undefined;
|
|
143
|
+
if (isDelete) {
|
|
144
|
+
// Key was deleted
|
|
145
|
+
const event = {
|
|
146
|
+
action: "delete",
|
|
147
|
+
timestamp: new Date(),
|
|
148
|
+
instanceId,
|
|
149
|
+
data: {
|
|
150
|
+
key,
|
|
151
|
+
success: true
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
this.emit(event);
|
|
155
|
+
} else {
|
|
156
|
+
// Key was set/updated
|
|
157
|
+
const action = type === "string" ? "set.string" : type === "number" ? "set.number" : type === "boolean" ? "set.boolean" : type === "buffer" ? "set.buffer" : "set.string"; // fallback
|
|
158
|
+
|
|
159
|
+
const event = {
|
|
160
|
+
action,
|
|
161
|
+
timestamp: new Date(),
|
|
162
|
+
instanceId,
|
|
163
|
+
data: {
|
|
164
|
+
key,
|
|
165
|
+
value,
|
|
166
|
+
valueType: type,
|
|
167
|
+
success: true
|
|
168
|
+
}
|
|
169
|
+
};
|
|
170
|
+
this.emit(event);
|
|
171
|
+
}
|
|
172
|
+
});
|
|
173
|
+
} catch (error) {
|
|
174
|
+
// Could not add native listener
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// Store instance tracking (no originalMethods needed for v4)
|
|
179
|
+
this.instances.set(instanceId, {
|
|
180
|
+
instance,
|
|
181
|
+
instanceId,
|
|
182
|
+
originalMethods: {},
|
|
183
|
+
// Not used in v4
|
|
184
|
+
nativeListenerUnsubscribe
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Remove an MMKV instance from monitoring
|
|
190
|
+
*
|
|
191
|
+
* Restores original methods and removes native listener.
|
|
192
|
+
*
|
|
193
|
+
* @param instanceId - Identifier of instance to stop monitoring
|
|
194
|
+
*/
|
|
195
|
+
removeInstance(instanceId) {
|
|
196
|
+
const tracked = this.instances.get(instanceId);
|
|
197
|
+
if (!tracked) {
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
const {
|
|
201
|
+
nativeListenerUnsubscribe
|
|
202
|
+
} = tracked;
|
|
203
|
+
|
|
204
|
+
// Remove native listener
|
|
205
|
+
if (nativeListenerUnsubscribe) {
|
|
206
|
+
nativeListenerUnsubscribe();
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// Remove from tracking
|
|
210
|
+
this.instances.delete(instanceId);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* Remove all monitored instances
|
|
215
|
+
*/
|
|
216
|
+
removeAllInstances() {
|
|
217
|
+
const instanceIds = Array.from(this.instances.keys());
|
|
218
|
+
instanceIds.forEach(id => this.removeInstance(id));
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Add a listener for MMKV events across all monitored instances
|
|
223
|
+
*
|
|
224
|
+
* @param listener - Callback function to handle MMKV events
|
|
225
|
+
* @returns Unsubscribe function to remove the listener
|
|
226
|
+
*
|
|
227
|
+
* @example
|
|
228
|
+
* ```typescript
|
|
229
|
+
* const unsubscribe = mmkvListener.addListener((event) => {
|
|
230
|
+
* console.log(`[${event.instanceId}] ${event.action}:`, event.data);
|
|
231
|
+
* });
|
|
232
|
+
*
|
|
233
|
+
* // Later, remove the listener
|
|
234
|
+
* unsubscribe();
|
|
235
|
+
* ```
|
|
236
|
+
*/
|
|
237
|
+
addListener(listener) {
|
|
238
|
+
this.listeners.push(listener);
|
|
239
|
+
|
|
240
|
+
// Return unsubscribe function
|
|
241
|
+
return () => {
|
|
242
|
+
const index = this.listeners.indexOf(listener);
|
|
243
|
+
if (index > -1) {
|
|
244
|
+
this.listeners.splice(index, 1);
|
|
245
|
+
}
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* Remove all registered event listeners
|
|
251
|
+
*/
|
|
252
|
+
removeAllListeners() {
|
|
253
|
+
this.listeners = [];
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* Check if an instance is currently being monitored
|
|
258
|
+
*/
|
|
259
|
+
hasInstance(instanceId) {
|
|
260
|
+
return this.instances.has(instanceId);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* Get list of all monitored instance IDs
|
|
265
|
+
*/
|
|
266
|
+
getMonitoredInstances() {
|
|
267
|
+
return Array.from(this.instances.keys());
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* Get the number of currently monitored instances
|
|
272
|
+
*/
|
|
273
|
+
get instanceCount() {
|
|
274
|
+
return this.instances.size;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* Get the number of currently registered event listeners
|
|
279
|
+
*/
|
|
280
|
+
get listenerCount() {
|
|
281
|
+
return this.listeners.length;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* Check if any instances are being monitored
|
|
286
|
+
*/
|
|
287
|
+
get isActive() {
|
|
288
|
+
return this.instances.size > 0;
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/**
|
|
293
|
+
* Singleton instance of MMKVListener
|
|
294
|
+
*
|
|
295
|
+
* This ensures only one listener manager exists across the entire application.
|
|
296
|
+
*/
|
|
297
|
+
export const mmkvListener = new MMKVListener();
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* Add an MMKV instance for monitoring
|
|
301
|
+
*
|
|
302
|
+
* @param instance - MMKV instance to monitor
|
|
303
|
+
* @param instanceId - Unique identifier for this instance
|
|
304
|
+
*
|
|
305
|
+
* @example
|
|
306
|
+
* ```typescript
|
|
307
|
+
* import { MMKV } from 'react-native-mmkv';
|
|
308
|
+
* import { addMMKVInstance } from '@buoy-gg/storage';
|
|
309
|
+
*
|
|
310
|
+
* const storage = new MMKV();
|
|
311
|
+
* addMMKVInstance(storage, 'mmkv.default');
|
|
312
|
+
* ```
|
|
313
|
+
*/
|
|
314
|
+
export const addMMKVInstance = (instance, instanceId) => mmkvListener.addInstance(instance, instanceId);
|
|
315
|
+
|
|
316
|
+
/**
|
|
317
|
+
* Remove an MMKV instance from monitoring
|
|
318
|
+
*/
|
|
319
|
+
export const removeMMKVInstance = instanceId => mmkvListener.removeInstance(instanceId);
|
|
320
|
+
|
|
321
|
+
/**
|
|
322
|
+
* Remove all monitored MMKV instances
|
|
323
|
+
*/
|
|
324
|
+
export const removeAllMMKVInstances = () => mmkvListener.removeAllInstances();
|
|
325
|
+
|
|
326
|
+
/**
|
|
327
|
+
* Add an event listener for MMKV operations
|
|
328
|
+
*
|
|
329
|
+
* @param listener - Callback function to handle events
|
|
330
|
+
* @returns Unsubscribe function to remove the listener
|
|
331
|
+
*/
|
|
332
|
+
export const addMMKVListener = listener => mmkvListener.addListener(listener);
|
|
333
|
+
|
|
334
|
+
/**
|
|
335
|
+
* Remove all registered event listeners
|
|
336
|
+
*/
|
|
337
|
+
export const removeAllMMKVListeners = () => mmkvListener.removeAllListeners();
|
|
338
|
+
|
|
339
|
+
/**
|
|
340
|
+
* Check if an MMKV instance is currently being monitored
|
|
341
|
+
*/
|
|
342
|
+
export const isMMKVInstanceMonitored = instanceId => mmkvListener.hasInstance(instanceId);
|
|
343
|
+
|
|
344
|
+
/**
|
|
345
|
+
* Get list of all monitored MMKV instance IDs
|
|
346
|
+
*/
|
|
347
|
+
export const getMonitoredMMKVInstances = () => mmkvListener.getMonitoredInstances();
|
|
348
|
+
|
|
349
|
+
/**
|
|
350
|
+
* Get the current number of monitored MMKV instances
|
|
351
|
+
*/
|
|
352
|
+
export const getMMKVInstanceCount = () => mmkvListener.instanceCount;
|
|
353
|
+
|
|
354
|
+
/**
|
|
355
|
+
* Get the current number of registered event listeners
|
|
356
|
+
*/
|
|
357
|
+
export const getMMKVListenerCount = () => mmkvListener.listenerCount;
|
|
358
|
+
|
|
359
|
+
/**
|
|
360
|
+
* Check if any MMKV instances are being monitored
|
|
361
|
+
*/
|
|
362
|
+
export const isMMKVListening = () => mmkvListener.isActive;
|
|
363
|
+
|
|
364
|
+
/**
|
|
365
|
+
* Export the singleton instance for advanced usage
|
|
366
|
+
*/
|
|
367
|
+
export default mmkvListener;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
import AsyncStorage from "@react-native-async-storage/async-storage";
|
|
4
|
+
import { isDevToolsStorageKey } from "@buoy-gg/shared-ui";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Clear all storage data except dev tools keys
|
|
8
|
+
* This preserves dev tool settings while clearing app data
|
|
9
|
+
*/
|
|
10
|
+
export async function clearAllAppStorage() {
|
|
11
|
+
// Get all keys
|
|
12
|
+
const allKeys = await AsyncStorage.getAllKeys();
|
|
13
|
+
if (!allKeys || allKeys.length === 0) {
|
|
14
|
+
// No keys to clear
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// Filter out dev tool keys - we don't want to clear those
|
|
19
|
+
const keysToRemove = allKeys.filter(key => !isDevToolsStorageKey(key));
|
|
20
|
+
if (keysToRemove.length === 0) {
|
|
21
|
+
// No app keys to clear (only dev tool keys found)
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// Clearing ${keysToRemove.length} app storage keys
|
|
26
|
+
|
|
27
|
+
// Remove all non-dev-tool keys
|
|
28
|
+
await AsyncStorage.multiRemove(keysToRemove);
|
|
29
|
+
|
|
30
|
+
// Successfully cleared app storage
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Clear absolutely all storage data including dev tools
|
|
35
|
+
* Use with caution - this will reset all dev tool settings
|
|
36
|
+
*/
|
|
37
|
+
export async function clearAllStorageIncludingDevTools() {
|
|
38
|
+
// Clear everything
|
|
39
|
+
await AsyncStorage.clear();
|
|
40
|
+
|
|
41
|
+
// Successfully cleared all storage including dev tools
|
|
42
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
// Storage utilities
|
|
4
|
+
export { clearAllAppStorage } from "./clearAllStorage";
|
|
5
|
+
|
|
6
|
+
// MMKV Availability
|
|
7
|
+
export { isMMKVAvailable, getMMKVClass, getMMKVUnavailableMessage } from "./mmkvAvailability";
|
|
8
|
+
|
|
9
|
+
// AsyncStorage Event Listener
|
|
10
|
+
export { startListening, stopListening, addListener, removeAllListeners, isListening, getListenerCount } from "./AsyncStorageListener";
|
|
11
|
+
|
|
12
|
+
// Re-export default listener instance
|
|
13
|
+
export { default as asyncStorageListener } from "./AsyncStorageListener";
|
|
14
|
+
|
|
15
|
+
// MMKV Instance Registry
|
|
16
|
+
export { registerMMKVInstance, unregisterMMKVInstance, mmkvInstanceRegistry } from "./MMKVInstanceRegistry";
|
|
17
|
+
|
|
18
|
+
// MMKV Listener
|
|
19
|
+
export { addMMKVInstance, removeMMKVInstance, removeAllMMKVInstances, addMMKVListener, removeAllMMKVListeners, isMMKVInstanceMonitored, getMonitoredMMKVInstances, getMMKVInstanceCount, getMMKVListenerCount, isMMKVListening, mmkvListener } from "./MMKVListener";
|
|
20
|
+
|
|
21
|
+
// MMKV Type Detection
|
|
22
|
+
export { detectMMKVType, formatMMKVValue, isTypeMatch } from "./mmkvTypeDetection";
|