react-native-onyx 2.0.21 → 2.0.23

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.
Files changed (41) hide show
  1. package/API.md +49 -64
  2. package/dist/DevTools.js +0 -1
  3. package/dist/Onyx.d.ts +49 -258
  4. package/dist/Onyx.js +192 -1165
  5. package/dist/OnyxCache.d.ts +14 -15
  6. package/dist/OnyxUtils.d.ts +320 -0
  7. package/dist/OnyxUtils.js +1061 -0
  8. package/dist/PerformanceUtils.d.ts +3 -5
  9. package/dist/index.d.ts +3 -2
  10. package/dist/storage/InstanceSync/index.d.ts +14 -0
  11. package/dist/storage/InstanceSync/index.js +20 -0
  12. package/dist/storage/InstanceSync/index.web.d.ts +27 -0
  13. package/dist/storage/InstanceSync/index.web.js +59 -0
  14. package/dist/storage/__mocks__/index.d.ts +15 -13
  15. package/dist/storage/__mocks__/index.js +43 -81
  16. package/dist/storage/index.d.ts +6 -2
  17. package/dist/storage/index.js +170 -2
  18. package/dist/storage/platforms/index.d.ts +2 -0
  19. package/dist/storage/{NativeStorage.js → platforms/index.js} +2 -2
  20. package/dist/storage/platforms/index.native.d.ts +2 -0
  21. package/dist/storage/{index.native.js → platforms/index.native.js} +2 -2
  22. package/dist/storage/providers/{IDBKeyVal.js → IDBKeyValProvider.js} +23 -19
  23. package/dist/storage/providers/MemoryOnlyProvider.d.ts +9 -0
  24. package/dist/storage/providers/MemoryOnlyProvider.js +124 -0
  25. package/dist/storage/providers/NoopProvider.js +85 -0
  26. package/dist/storage/providers/SQLiteProvider.d.ts +3 -0
  27. package/dist/storage/providers/{SQLiteStorage.js → SQLiteProvider.js} +17 -11
  28. package/dist/storage/providers/types.d.ts +17 -14
  29. package/dist/types.d.ts +128 -55
  30. package/dist/types.js +2 -0
  31. package/dist/useOnyx.js +11 -10
  32. package/dist/utils.d.ts +2 -2
  33. package/dist/utils.js +29 -16
  34. package/dist/withOnyx.js +6 -5
  35. package/package.json +1 -1
  36. package/dist/storage/NativeStorage.d.ts +0 -2
  37. package/dist/storage/WebStorage.d.ts +0 -3
  38. package/dist/storage/WebStorage.js +0 -62
  39. package/dist/storage/index.native.d.ts +0 -2
  40. /package/dist/storage/providers/{IDBKeyVal.d.ts → IDBKeyValProvider.d.ts} +0 -0
  41. /package/dist/storage/providers/{SQLiteStorage.d.ts → NoopProvider.d.ts} +0 -0
package/dist/Onyx.d.ts CHANGED
@@ -1,156 +1,7 @@
1
- import {Component} from 'react';
2
1
  import * as Logger from './Logger';
3
- import {CollectionKey, CollectionKeyBase, DeepRecord, KeyValueMapping, NullishDeep, OnyxCollection, OnyxEntry, OnyxKey, Selector} from './types';
4
-
5
- /**
6
- * Represents a mapping object where each `OnyxKey` maps to either a value of its corresponding type in `KeyValueMapping` or `null`.
7
- *
8
- * It's very similar to `KeyValueMapping` but this type accepts using `null` as well.
9
- */
10
- type NullableKeyValueMapping = {
11
- [TKey in OnyxKey]: OnyxEntry<KeyValueMapping[TKey]>;
12
- };
13
-
14
- /**
15
- * Represents the base options used in `Onyx.connect()` method.
16
- */
17
- type BaseConnectOptions = {
18
- statePropertyName?: string;
19
- withOnyxInstance?: Component;
20
- initWithStoredValues?: boolean;
21
- };
22
-
23
- type TryGetCachedValueMapping<TKey extends OnyxKey> = {
24
- selector?: Selector<TKey, unknown, unknown>;
25
- withOnyxInstance?: Component;
26
- };
27
-
28
- /**
29
- * Represents the options used in `Onyx.connect()` method.
30
- * The type is built from `BaseConnectOptions` and extended to handle key/callback related options.
31
- * It includes two different forms, depending on whether we are waiting for a collection callback or not.
32
- *
33
- * If `waitForCollectionCallback` is `true`, it expects `key` to be a Onyx collection key and `callback` will be triggered with the whole collection
34
- * and will pass `value` as an `OnyxCollection`.
35
- *
36
- *
37
- * If `waitForCollectionCallback` is `false` or not specified, the `key` can be any Onyx key and `callback` will be triggered with updates of each collection item
38
- * and will pass `value` as an `OnyxEntry`.
39
- */
40
- type ConnectOptions<TKey extends OnyxKey> = BaseConnectOptions &
41
- (
42
- | {
43
- key: TKey extends CollectionKeyBase ? TKey : never;
44
- callback?: (value: OnyxCollection<KeyValueMapping[TKey]>) => void;
45
- waitForCollectionCallback: true;
46
- }
47
- | {
48
- key: TKey;
49
- callback?: (value: OnyxEntry<KeyValueMapping[TKey]>, key: TKey) => void;
50
- waitForCollectionCallback?: false;
51
- }
52
- );
53
-
54
- /**
55
- * Represents a mapping between Onyx collection keys and their respective values.
56
- *
57
- * It helps to enforce that a Onyx collection key should not be without suffix (e.g. should always be of the form `${TKey}${string}`),
58
- * and to map each Onyx collection key with suffix to a value of type `TValue`.
59
- *
60
- * Also, the `TMap` type is inferred automatically in `mergeCollection()` method and represents
61
- * the object of collection keys/values specified in the second parameter of the method.
62
- */
63
- type Collection<TKey extends CollectionKeyBase, TMap, TValue> = {
64
- [MapK in keyof TMap]: MapK extends `${TKey}${string}`
65
- ? MapK extends `${TKey}`
66
- ? never // forbids empty id
67
- : TValue
68
- : never;
69
- };
70
-
71
- /**
72
- * Represents different kinds of updates that can be passed to `Onyx.update()` method. It is a discriminated union of
73
- * different update methods (`SET`, `MERGE`, `MERGE_COLLECTION`), each with their own key and value structure.
74
- */
75
- type OnyxUpdate =
76
- | {
77
- [TKey in OnyxKey]:
78
- | {
79
- onyxMethod: typeof METHOD.SET;
80
- key: TKey;
81
- value: OnyxEntry<KeyValueMapping[TKey]>;
82
- }
83
- | {
84
- onyxMethod: typeof METHOD.MERGE;
85
- key: TKey;
86
- value: OnyxEntry<NullishDeep<KeyValueMapping[TKey]>>;
87
- };
88
- }[OnyxKey]
89
- | {
90
- [TKey in CollectionKeyBase]: {
91
- onyxMethod: typeof METHOD.MERGE_COLLECTION;
92
- key: TKey;
93
- value: Record<`${TKey}${string}`, NullishDeep<KeyValueMapping[TKey]>>;
94
- };
95
- }[CollectionKeyBase];
96
-
97
- /**
98
- * Represents the options used in `Onyx.init()` method.
99
- */
100
- type InitOptions = {
101
- keys?: DeepRecord<string, OnyxKey>;
102
- initialKeyStates?: Partial<NullableKeyValueMapping>;
103
- safeEvictionKeys?: OnyxKey[];
104
- maxCachedKeysCount?: number;
105
- shouldSyncMultipleInstances?: boolean;
106
- debugSetState?: boolean;
107
- };
108
-
109
- declare const METHOD: {
110
- readonly SET: 'set';
111
- readonly MERGE: 'merge';
112
- readonly MERGE_COLLECTION: 'mergecollection';
113
- readonly MULTI_SET: 'multiset';
114
- readonly CLEAR: 'clear';
115
- };
116
-
117
- /**
118
- * Returns current key names stored in persisted storage
119
- */
120
- declare function getAllKeys(): Promise<Array<OnyxKey>>;
121
-
122
- /**
123
- * Checks to see if the a subscriber's supplied key
124
- * is associated with a collection of keys.
125
- */
126
- declare function isCollectionKey(key: OnyxKey): key is CollectionKeyBase;
127
-
128
- declare function isCollectionMemberKey<TCollectionKey extends CollectionKeyBase>(collectionKey: TCollectionKey, key: string): key is `${TCollectionKey}${string}`;
129
-
130
- /**
131
- * Splits a collection member key into the collection key part and the ID part.
132
- * @param key - The collection member key to split.
133
- * @returns A tuple where the first element is the collection part and the second element is the ID part.
134
- */
135
- declare function splitCollectionMemberKey<TKey extends CollectionKey>(key: TKey): [TKey extends `${infer Prefix}_${string}` ? `${Prefix}_` : never, string];
136
-
137
- /**
138
- * Checks to see if this key has been flagged as
139
- * safe for removal.
140
- */
141
- declare function isSafeEvictionKey(testKey: OnyxKey): boolean;
142
-
143
- /**
144
- * Removes a key previously added to this list
145
- * which will enable it to be deleted again.
146
- */
147
- declare function removeFromEvictionBlockList(key: OnyxKey, connectionID: number): void;
148
-
149
- /**
150
- * Keys added to this list can never be deleted.
151
- */
152
- declare function addToEvictionBlockList(key: OnyxKey, connectionID: number): void;
153
-
2
+ import type { Collection, CollectionKeyBase, ConnectOptions, InitOptions, KeyValueMapping, Mapping, NullableKeyValueMapping, NullishDeep, OnyxEntry, OnyxKey, OnyxUpdate } from './types';
3
+ /** Initialize the store with actions and listening for storage events */
4
+ declare function init({ keys, initialKeyStates, safeEvictionKeys, maxCachedKeysCount, shouldSyncMultipleInstances, debugSetState, }: InitOptions): void;
154
5
  /**
155
6
  * Subscribes a react component's state directly to a store key
156
7
  *
@@ -170,10 +21,16 @@ declare function addToEvictionBlockList(key: OnyxKey, connectionID: number): voi
170
21
  * @param [mapping.initWithStoredValues] If set to false, then no data will be prefilled into the
171
22
  * component
172
23
  * @param [mapping.waitForCollectionCallback] If set to true, it will return the entire collection to the callback as a single object
24
+ * @param [mapping.selector] THIS PARAM IS ONLY USED WITH withOnyx(). If included, this will be used to subscribe to a subset of an Onyx key's data.
25
+ * The sourceData and withOnyx state are passed to the selector and should return the simplified data. Using this setting on `withOnyx` can have very positive
26
+ * performance benefits because the component will only re-render when the subset of data changes. Otherwise, any change of data on any property would normally
27
+ * cause the component to re-render (and that can be expensive from a performance standpoint).
28
+ * @param [mapping.initialValue] THIS PARAM IS ONLY USED WITH withOnyx().
29
+ * If included, this will be passed to the component so that something can be rendered while data is being fetched from the DB.
30
+ * Note that it will not cause the component to have the loading prop set to true.
173
31
  * @returns an ID to use when calling disconnect
174
32
  */
175
33
  declare function connect<TKey extends OnyxKey>(mapping: ConnectOptions<TKey>): number;
176
-
177
34
  /**
178
35
  * Remove the listener for a react component
179
36
  * @example
@@ -182,15 +39,13 @@ declare function connect<TKey extends OnyxKey>(mapping: ConnectOptions<TKey>): n
182
39
  * @param connectionID unique id returned by call to Onyx.connect()
183
40
  */
184
41
  declare function disconnect(connectionID: number, keyToRemoveFromEvictionBlocklist?: OnyxKey): void;
185
-
186
42
  /**
187
43
  * Write a value to our store with the given key
188
44
  *
189
45
  * @param key ONYXKEY to set
190
46
  * @param value value to store
191
47
  */
192
- declare function set<TKey extends OnyxKey>(key: TKey, value: OnyxEntry<KeyValueMapping[TKey]>): Promise<void>;
193
-
48
+ declare function set<TKey extends OnyxKey>(key: TKey, value: OnyxEntry<KeyValueMapping[TKey]>): Promise<void[]>;
194
49
  /**
195
50
  * Sets multiple keys and values
196
51
  *
@@ -198,15 +53,12 @@ declare function set<TKey extends OnyxKey>(key: TKey, value: OnyxEntry<KeyValueM
198
53
  *
199
54
  * @param data object keyed by ONYXKEYS and the values to set
200
55
  */
201
- declare function multiSet(data: Partial<NullableKeyValueMapping>): Promise<void>;
202
-
56
+ declare function multiSet(data: Partial<NullableKeyValueMapping>): Promise<void[]>;
203
57
  /**
204
58
  * Merge a new value into an existing value at a key.
205
59
  *
206
- * The types of values that can be merged are `Object` and `Array`. To set another type of value use `Onyx.set()`. Merge
207
- * behavior uses lodash/merge under the hood for `Object` and simple concatenation for `Array`. However, it's important
208
- * to note that if you have an array value property on an `Object` that the default behavior of lodash/merge is not to
209
- * concatenate. See here: https://github.com/lodash/lodash/issues/2872
60
+ * The types of values that can be merged are `Object` and `Array`. To set another type of value use `Onyx.set()`.
61
+ * Values of type `Object` get merged with the old value, whilst for `Array`'s we simply replace the current value with the new one.
210
62
  *
211
63
  * Calls to `Onyx.merge()` are batched so that any calls performed in a single tick will stack in a queue and get
212
64
  * applied in the order they were called. Note: `Onyx.set()` calls do not work this way so use caution when mixing
@@ -217,12 +69,22 @@ declare function multiSet(data: Partial<NullableKeyValueMapping>): Promise<void>
217
69
  * Onyx.merge(ONYXKEYS.EMPLOYEE_LIST, ['Jack']); // -> ['Joe', 'Jack']
218
70
  * Onyx.merge(ONYXKEYS.POLICY, {id: 1}); // -> {id: 1}
219
71
  * Onyx.merge(ONYXKEYS.POLICY, {name: 'My Workspace'}); // -> {id: 1, name: 'My Workspace'}
72
+ */
73
+ declare function merge<TKey extends OnyxKey>(key: TKey, changes: OnyxEntry<NullishDeep<KeyValueMapping[TKey]>>): Promise<void | void[]>;
74
+ /**
75
+ * Merges a collection based on their keys
76
+ *
77
+ * @example
78
+ *
79
+ * Onyx.mergeCollection(ONYXKEYS.COLLECTION.REPORT, {
80
+ * [`${ONYXKEYS.COLLECTION.REPORT}1`]: report1,
81
+ * [`${ONYXKEYS.COLLECTION.REPORT}2`]: report2,
82
+ * });
220
83
  *
221
- * @param key ONYXKEYS key
222
- * @param value Object or Array value to merge
84
+ * @param collectionKey e.g. `ONYXKEYS.COLLECTION.REPORT`
85
+ * @param collection Object collection keyed by individual collection member keys and values
223
86
  */
224
- declare function merge<TKey extends OnyxKey>(key: TKey, value: OnyxEntry<NullishDeep<KeyValueMapping[TKey]>>): Promise<void>;
225
-
87
+ declare function mergeCollection<TKey extends CollectionKeyBase, TMap>(collectionKey: TKey, collection: Collection<TKey, TMap, NullishDeep<KeyValueMapping[TKey]>>): Promise<void>;
226
88
  /**
227
89
  * Clear out all the data in the store
228
90
  *
@@ -244,103 +106,32 @@ declare function merge<TKey extends OnyxKey>(key: TKey, value: OnyxEntry<Nullish
244
106
  *
245
107
  * @param keysToPreserve is a list of ONYXKEYS that should not be cleared with the rest of the data
246
108
  */
247
- declare function clear(keysToPreserve?: OnyxKey[]): Promise<void>;
248
-
249
- /**
250
- * Merges a collection based on their keys
251
- *
252
- * Note that both `TKey` and `TMap` types are inferred automatically, `TKey` being the
253
- * collection key specified in the first parameter and `TMap` being the object of
254
- * collection keys/values specified in the second parameter.
255
- *
256
- * @example
257
- *
258
- * Onyx.mergeCollection(ONYXKEYS.COLLECTION.REPORT, {
259
- * [`${ONYXKEYS.COLLECTION.REPORT}1`]: report1,
260
- * [`${ONYXKEYS.COLLECTION.REPORT}2`]: report2,
261
- * });
262
- *
263
- * @param collectionKey e.g. `ONYXKEYS.COLLECTION.REPORT`
264
- * @param collection Object collection keyed by individual collection member keys and values
265
- */
266
- declare function mergeCollection<TKey extends CollectionKeyBase, TMap>(collectionKey: TKey, collection: Collection<TKey, TMap, NullishDeep<KeyValueMapping[TKey]>>): Promise<void>;
267
-
109
+ declare function clear(keysToPreserve?: OnyxKey[]): Promise<void[]>;
268
110
  /**
269
111
  * Insert API responses and lifecycle data into Onyx
270
112
  *
271
- * @param data An array of update objects
113
+ * @param data An array of objects with shape {onyxMethod: oneOf('set', 'merge', 'mergeCollection', 'multiSet', 'clear'), key: string, value: *}
272
114
  * @returns resolves when all operations are complete
273
115
  */
274
- declare function update(data: OnyxUpdate[]): Promise<void>;
275
-
276
- /**
277
- * Initialize the store with actions and listening for storage events
278
- *
279
- * @param [options={}] config object
280
- * @param [options.keys={}] `ONYXKEYS` constants object
281
- * @param [options.initialKeyStates={}] initial data to set when `init()` and `clear()` is called
282
- * @param [options.safeEvictionKeys=[]] This is an array of keys
283
- * (individual or collection patterns) that when provided to Onyx are flagged
284
- * as "safe" for removal. Any components subscribing to these keys must also
285
- * implement a canEvict option. See the README for more info.
286
- * @param [options.maxCachedKeysCount=55] Sets how many recent keys should we try to keep in cache
287
- * Setting this to 0 would practically mean no cache
288
- * We try to free cache when we connect to a safe eviction key
289
- * @param [options.shouldSyncMultipleInstances] Auto synchronize storage events between multiple instances
290
- * of Onyx running in different tabs/windows. Defaults to true for platforms that support local storage (web/desktop)
291
- * @param [options.debugSetState] Enables debugging setState() calls to connected components.
292
- * @example
293
- * Onyx.init({
294
- * keys: ONYXKEYS,
295
- * initialKeyStates: {
296
- * [ONYXKEYS.SESSION]: {loading: false},
297
- * },
298
- * });
299
- */
300
- declare function init(config?: InitOptions): void;
301
-
302
- /**
303
- * @private
304
- */
305
- declare function hasPendingMergeForKey(key: OnyxKey): boolean;
306
-
307
- /**
308
- * When set these keys will not be persisted to storage
309
- */
310
- declare function setMemoryOnlyKeys(keyList: OnyxKey[]): void;
311
-
312
- /**
313
- * Tries to get a value from the cache. If the value is not present in cache it will return the default value or undefined.
314
- * If the requested key is a collection, it will return an object with all the collection members.
315
- */
316
- declare function tryGetCachedValue<TKey extends OnyxKey>(
317
- key: TKey,
318
- mapping?: TryGetCachedValueMapping,
319
- ): TKey extends CollectionKeyBase ? OnyxCollection<KeyValueMapping[TKey]> | undefined : OnyxEntry<KeyValueMapping[TKey]> | undefined;
320
-
116
+ declare function update(data: OnyxUpdate[]): Promise<Array<void | void[]>>;
321
117
  declare const Onyx: {
322
- connect: typeof connect;
323
- disconnect: typeof disconnect;
324
- set: typeof set;
325
- multiSet: typeof multiSet;
326
- merge: typeof merge;
327
- mergeCollection: typeof mergeCollection;
328
- hasPendingMergeForKey: typeof hasPendingMergeForKey;
329
- update: typeof update;
330
- clear: typeof clear;
331
- getAllKeys: typeof getAllKeys;
332
- init: typeof init;
333
- registerLogger: typeof Logger.registerLogger;
334
- addToEvictionBlockList: typeof addToEvictionBlockList;
335
- removeFromEvictionBlockList: typeof removeFromEvictionBlockList;
336
- isSafeEvictionKey: typeof isSafeEvictionKey;
337
- METHOD: typeof METHOD;
338
- setMemoryOnlyKeys: typeof setMemoryOnlyKeys;
339
- tryGetCachedValue: typeof tryGetCachedValue;
340
- isCollectionKey: typeof isCollectionKey;
341
- isCollectionMemberKey: typeof isCollectionMemberKey;
342
- splitCollectionMemberKey: typeof splitCollectionMemberKey;
118
+ readonly METHOD: {
119
+ readonly SET: "set";
120
+ readonly MERGE: "merge";
121
+ readonly MERGE_COLLECTION: "mergecollection";
122
+ readonly MULTI_SET: "multiset";
123
+ readonly CLEAR: "clear";
124
+ };
125
+ readonly connect: typeof connect;
126
+ readonly disconnect: typeof disconnect;
127
+ readonly set: typeof set;
128
+ readonly multiSet: typeof multiSet;
129
+ readonly merge: typeof merge;
130
+ readonly mergeCollection: typeof mergeCollection;
131
+ readonly update: typeof update;
132
+ readonly clear: typeof clear;
133
+ readonly init: typeof init;
134
+ readonly registerLogger: typeof Logger.registerLogger;
343
135
  };
344
-
345
136
  export default Onyx;
346
- export {ConnectOptions, OnyxUpdate};
137
+ export type { OnyxUpdate, Mapping, ConnectOptions };