|
| 1 | +import * as Logger from './Logger'; |
| 2 | +import type { CollectionKeyBase, ConnectOptions, InitOptions, Mapping, OnyxKey, OnyxMergeCollectionInput, OnyxMergeInput, OnyxMultiSetInput, OnyxSetInput, 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; |
| 5 | +/** |
| 6 | + * Subscribes a react component's state directly to a store key |
| 7 | + * |
| 8 | + * @example |
| 9 | + * const connectionID = Onyx.connect({ |
| 10 | + * key: ONYXKEYS.SESSION, |
| 11 | + * callback: onSessionChange, |
| 12 | + * }); |
| 13 | + * |
| 14 | + * @param mapping the mapping information to connect Onyx to the components state |
| 15 | + * @param mapping.key ONYXKEY to subscribe to |
| 16 | + * @param [mapping.statePropertyName] the name of the property in the state to connect the data to |
| 17 | + * @param [mapping.withOnyxInstance] whose setState() method will be called with any changed data |
| 18 | + * This is used by React components to connect to Onyx |
| 19 | + * @param [mapping.callback] a method that will be called with changed data |
| 20 | + * This is used by any non-React code to connect to Onyx |
| 21 | + * @param [mapping.initWithStoredValues] If set to false, then no data will be prefilled into the |
| 22 | + * component |
| 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. |
| 31 | + * @returns an ID to use when calling disconnect |
| 32 | + */ |
| 33 | +declare function connect<TKey extends OnyxKey>(connectOptions: ConnectOptions<TKey>): number; |
| 34 | +/** |
| 35 | + * Remove the listener for a react component |
| 36 | + * @example |
| 37 | + * Onyx.disconnect(connectionID); |
| 38 | + * |
| 39 | + * @param connectionID unique id returned by call to Onyx.connect() |
| 40 | + */ |
| 41 | +declare function disconnect(connectionID: number, keyToRemoveFromEvictionBlocklist?: OnyxKey): void; |
| 42 | +/** |
| 43 | + * Write a value to our store with the given key |
| 44 | + * |
| 45 | + * @param key ONYXKEY to set |
| 46 | + * @param value value to store |
| 47 | + */ |
| 48 | +declare function set<TKey extends OnyxKey>(key: TKey, value: OnyxSetInput<TKey>): Promise<void>; |
| 49 | +/** |
| 50 | + * Sets multiple keys and values |
| 51 | + * |
| 52 | + * @example Onyx.multiSet({'key1': 'a', 'key2': 'b'}); |
| 53 | + * |
| 54 | + * @param data object keyed by ONYXKEYS and the values to set |
| 55 | + */ |
| 56 | +declare function multiSet(data: OnyxMultiSetInput): Promise<void>; |
| 57 | +/** |
| 58 | + * Merge a new value into an existing value at a key. |
| 59 | + * |
| 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. |
| 62 | + * |
| 63 | + * Calls to `Onyx.merge()` are batched so that any calls performed in a single tick will stack in a queue and get |
| 64 | + * applied in the order they were called. Note: `Onyx.set()` calls do not work this way so use caution when mixing |
| 65 | + * `Onyx.merge()` and `Onyx.set()`. |
| 66 | + * |
| 67 | + * @example |
| 68 | + * Onyx.merge(ONYXKEYS.EMPLOYEE_LIST, ['Joe']); // -> ['Joe'] |
| 69 | + * Onyx.merge(ONYXKEYS.EMPLOYEE_LIST, ['Jack']); // -> ['Joe', 'Jack'] |
| 70 | + * Onyx.merge(ONYXKEYS.POLICY, {id: 1}); // -> {id: 1} |
| 71 | + * Onyx.merge(ONYXKEYS.POLICY, {name: 'My Workspace'}); // -> {id: 1, name: 'My Workspace'} |
| 72 | + */ |
| 73 | +declare function merge<TKey extends OnyxKey>(key: TKey, changes: OnyxMergeInput<TKey>): Promise<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 | + * }); |
| 83 | + * |
| 84 | + * @param collectionKey e.g. `ONYXKEYS.COLLECTION.REPORT` |
| 85 | + * @param collection Object collection keyed by individual collection member keys and values |
| 86 | + */ |
| 87 | +declare function mergeCollection<TKey extends CollectionKeyBase, TMap>(collectionKey: TKey, collection: OnyxMergeCollectionInput<TKey, TMap>): Promise<void>; |
| 88 | +/** |
| 89 | + * Clear out all the data in the store |
| 90 | + * |
| 91 | + * Note that calling Onyx.clear() and then Onyx.set() on a key with a default |
| 92 | + * key state may store an unexpected value in Storage. |
| 93 | + * |
| 94 | + * E.g. |
| 95 | + * Onyx.clear(); |
| 96 | + * Onyx.set(ONYXKEYS.DEFAULT_KEY, 'default'); |
| 97 | + * Storage.getItem(ONYXKEYS.DEFAULT_KEY) |
| 98 | + * .then((storedValue) => console.log(storedValue)); |
| 99 | + * null is logged instead of the expected 'default' |
| 100 | + * |
| 101 | + * Onyx.set() might call Storage.setItem() before Onyx.clear() calls |
| 102 | + * Storage.setItem(). Use Onyx.merge() instead if possible. Onyx.merge() calls |
| 103 | + * Onyx.get(key) before calling Storage.setItem() via Onyx.set(). |
| 104 | + * Storage.setItem() from Onyx.clear() will have already finished and the merged |
| 105 | + * value will be saved to storage after the default value. |
| 106 | + * |
| 107 | + * @param keysToPreserve is a list of ONYXKEYS that should not be cleared with the rest of the data |
| 108 | + */ |
| 109 | +declare function clear(keysToPreserve?: OnyxKey[]): Promise<void>; |
| 110 | +/** |
| 111 | + * Insert API responses and lifecycle data into Onyx |
| 112 | + * |
| 113 | + * @param data An array of objects with update expressions |
| 114 | + * @returns resolves when all operations are complete |
| 115 | + */ |
| 116 | +declare function update(data: OnyxUpdate[]): Promise<void>; |
| 117 | +declare const Onyx: { |
| 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; |
| 135 | +}; |
| 136 | +export default Onyx; |
| 137 | +export type { OnyxUpdate, Mapping, ConnectOptions }; |
0 commit comments