react-native-onyx 2.0.36 → 2.0.37
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/dist/Onyx.d.ts +3 -3
- package/dist/types.d.ts +17 -9
- package/dist/useOnyx.d.ts +15 -5
- package/dist/useOnyx.js +4 -4
- package/dist/withOnyx.d.ts +2 -0
- package/package.json +2 -2
package/dist/Onyx.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as Logger from './Logger';
|
|
2
|
-
import type { Collection, CollectionKeyBase, ConnectOptions, InitOptions, KeyValueMapping, Mapping, NullableKeyValueMapping, NullishDeep, OnyxEntry, OnyxKey, OnyxUpdate } from './types';
|
|
2
|
+
import type { Collection, CollectionKeyBase, ConnectOptions, InitOptions, KeyValueMapping, Mapping, NonUndefined, NullableKeyValueMapping, NullishDeep, OnyxEntry, OnyxKey, OnyxUpdate } from './types';
|
|
3
3
|
/** Initialize the store with actions and listening for storage events */
|
|
4
4
|
declare function init({ keys, initialKeyStates, safeEvictionKeys, maxCachedKeysCount, shouldSyncMultipleInstances, debugSetState, }: InitOptions): void;
|
|
5
5
|
/**
|
|
@@ -45,7 +45,7 @@ declare function disconnect(connectionID: number, keyToRemoveFromEvictionBlockli
|
|
|
45
45
|
* @param key ONYXKEY to set
|
|
46
46
|
* @param value value to store
|
|
47
47
|
*/
|
|
48
|
-
declare function set<TKey extends OnyxKey>(key: TKey, value: OnyxEntry<KeyValueMapping[TKey]
|
|
48
|
+
declare function set<TKey extends OnyxKey>(key: TKey, value: NonUndefined<OnyxEntry<KeyValueMapping[TKey]>>): Promise<void>;
|
|
49
49
|
/**
|
|
50
50
|
* Sets multiple keys and values
|
|
51
51
|
*
|
|
@@ -70,7 +70,7 @@ declare function multiSet(data: Partial<NullableKeyValueMapping>): Promise<void>
|
|
|
70
70
|
* Onyx.merge(ONYXKEYS.POLICY, {id: 1}); // -> {id: 1}
|
|
71
71
|
* Onyx.merge(ONYXKEYS.POLICY, {name: 'My Workspace'}); // -> {id: 1, name: 'My Workspace'}
|
|
72
72
|
*/
|
|
73
|
-
declare function merge<TKey extends OnyxKey>(key: TKey, changes: OnyxEntry<NullishDeep<KeyValueMapping[TKey]
|
|
73
|
+
declare function merge<TKey extends OnyxKey>(key: TKey, changes: NonUndefined<OnyxEntry<NullishDeep<KeyValueMapping[TKey]>>>): Promise<void>;
|
|
74
74
|
/**
|
|
75
75
|
* Merges a collection based on their keys
|
|
76
76
|
*
|
package/dist/types.d.ts
CHANGED
|
@@ -2,6 +2,14 @@ import type { Component } from 'react';
|
|
|
2
2
|
import type { Merge } from 'type-fest';
|
|
3
3
|
import type { BuiltIns } from 'type-fest/source/internal';
|
|
4
4
|
import type OnyxUtils from './OnyxUtils';
|
|
5
|
+
/**
|
|
6
|
+
* Utility type that excludes `null` from the type `TValue`.
|
|
7
|
+
*/
|
|
8
|
+
type NonNull<TValue> = TValue extends null ? never : TValue;
|
|
9
|
+
/**
|
|
10
|
+
* Utility type that excludes `undefined` from the type `TValue`.
|
|
11
|
+
*/
|
|
12
|
+
type NonUndefined<TValue> = TValue extends undefined ? never : TValue;
|
|
5
13
|
/**
|
|
6
14
|
* Represents a deeply nested record. It maps keys to values,
|
|
7
15
|
* and those values can either be of type `TValue` or further nested `DeepRecord` instances.
|
|
@@ -126,7 +134,7 @@ type NullableKeyValueMapping = {
|
|
|
126
134
|
*/
|
|
127
135
|
type Selector<TKey extends OnyxKey, TOnyxProps, TReturnType> = (value: OnyxEntry<KeyValueMapping[TKey]>, state: WithOnyxInstanceState<TOnyxProps>) => TReturnType;
|
|
128
136
|
/**
|
|
129
|
-
* Represents a single Onyx entry, that can be either `TOnyxValue` or `null` if it doesn't exist.
|
|
137
|
+
* Represents a single Onyx entry, that can be either `TOnyxValue` or `null` / `undefined` if it doesn't exist.
|
|
130
138
|
*
|
|
131
139
|
* It can be used to specify data retrieved from Onyx e.g. `withOnyx` HOC mappings.
|
|
132
140
|
*
|
|
@@ -153,9 +161,9 @@ type Selector<TKey extends OnyxKey, TOnyxProps, TReturnType> = (value: OnyxEntry
|
|
|
153
161
|
* })(Component);
|
|
154
162
|
* ```
|
|
155
163
|
*/
|
|
156
|
-
type OnyxEntry<TOnyxValue> = TOnyxValue | null;
|
|
164
|
+
type OnyxEntry<TOnyxValue> = TOnyxValue | null | undefined;
|
|
157
165
|
/**
|
|
158
|
-
* Represents an Onyx collection of entries, that can be either a record of `TOnyxValue`s or `null` if it is empty or doesn't exist.
|
|
166
|
+
* Represents an Onyx collection of entries, that can be either a record of `TOnyxValue`s or `null` / `undefined` if it is empty or doesn't exist.
|
|
159
167
|
*
|
|
160
168
|
* It can be used to specify collection data retrieved from Onyx e.g. `withOnyx` HOC mappings.
|
|
161
169
|
*
|
|
@@ -182,7 +190,7 @@ type OnyxEntry<TOnyxValue> = TOnyxValue | null;
|
|
|
182
190
|
* })(Component);
|
|
183
191
|
* ```
|
|
184
192
|
*/
|
|
185
|
-
type OnyxCollection<TOnyxValue> = OnyxEntry<Record<string, TOnyxValue | null>>;
|
|
193
|
+
type OnyxCollection<TOnyxValue> = OnyxEntry<Record<string, TOnyxValue | null | undefined>>;
|
|
186
194
|
/** Utility type to extract `TOnyxValue` from `OnyxCollection<TOnyxValue>` */
|
|
187
195
|
type ExtractOnyxCollectionValue<TOnyxCollection> = TOnyxCollection extends NonNullable<OnyxCollection<infer U>> ? U : never;
|
|
188
196
|
type NonTransformableTypes = BuiltIns | ((...args: any[]) => unknown) | Map<unknown, unknown> | Set<unknown> | ReadonlyMap<unknown, unknown> | ReadonlySet<unknown> | unknown[] | readonly unknown[];
|
|
@@ -247,8 +255,8 @@ type WithOnyxConnectOptions<TKey extends OnyxKey> = {
|
|
|
247
255
|
selector?: Selector<TKey, unknown, unknown>;
|
|
248
256
|
canEvict?: boolean;
|
|
249
257
|
};
|
|
250
|
-
type DefaultConnectCallback<TKey extends OnyxKey> = (value: OnyxEntry<KeyValueMapping[TKey]
|
|
251
|
-
type CollectionConnectCallback<TKey extends OnyxKey> = (value: OnyxCollection<KeyValueMapping[TKey]
|
|
258
|
+
type DefaultConnectCallback<TKey extends OnyxKey> = (value: NonUndefined<OnyxEntry<KeyValueMapping[TKey]>>, key: TKey) => void;
|
|
259
|
+
type CollectionConnectCallback<TKey extends OnyxKey> = (value: NonUndefined<OnyxCollection<KeyValueMapping[TKey]>>) => void;
|
|
252
260
|
/** Represents the callback function used in `Onyx.connect()` method with a regular key. */
|
|
253
261
|
type DefaultConnectOptions<TKey extends OnyxKey> = {
|
|
254
262
|
key: TKey;
|
|
@@ -286,11 +294,11 @@ type OnyxUpdate = {
|
|
|
286
294
|
[TKey in OnyxKey]: {
|
|
287
295
|
onyxMethod: typeof OnyxUtils.METHOD.SET;
|
|
288
296
|
key: TKey;
|
|
289
|
-
value: OnyxEntry<KeyValueMapping[TKey]
|
|
297
|
+
value: NonUndefined<OnyxEntry<KeyValueMapping[TKey]>>;
|
|
290
298
|
} | {
|
|
291
299
|
onyxMethod: typeof OnyxUtils.METHOD.MERGE;
|
|
292
300
|
key: TKey;
|
|
293
|
-
value: OnyxEntry<NullishDeep<KeyValueMapping[TKey]
|
|
301
|
+
value: NonUndefined<OnyxEntry<NullishDeep<KeyValueMapping[TKey]>>>;
|
|
294
302
|
} | {
|
|
295
303
|
onyxMethod: typeof OnyxUtils.METHOD.MULTI_SET;
|
|
296
304
|
key: TKey;
|
|
@@ -334,4 +342,4 @@ type InitOptions = {
|
|
|
334
342
|
/** Enables debugging setState() calls to connected components */
|
|
335
343
|
debugSetState?: boolean;
|
|
336
344
|
};
|
|
337
|
-
export type {
|
|
345
|
+
export type { BaseConnectOptions, Collection, CollectionConnectCallback, CollectionConnectOptions, CollectionKey, CollectionKeyBase, ConnectOptions, CustomTypeOptions, DeepRecord, DefaultConnectCallback, DefaultConnectOptions, ExtractOnyxCollectionValue, InitOptions, Key, KeyValueMapping, Mapping, NonNull, NonUndefined, NullableKeyValueMapping, NullishDeep, OnyxCollection, OnyxEntry, OnyxKey, OnyxUpdate, OnyxValue, Selector, WithOnyxConnectOptions, WithOnyxInstance, WithOnyxInstanceState, };
|
package/dist/useOnyx.d.ts
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
import type { IsEqual } from 'type-fest';
|
|
2
|
-
import type { CollectionKeyBase, OnyxCollection,
|
|
3
|
-
|
|
2
|
+
import type { CollectionKeyBase, KeyValueMapping, NonNull, OnyxCollection, OnyxEntry, OnyxKey, Selector } from './types';
|
|
3
|
+
/**
|
|
4
|
+
* Represents a Onyx value that can be either a single entry or a collection of entries, depending on the `TKey` provided.
|
|
5
|
+
* It's a variation of `OnyxValue` type that is read-only and excludes the `null` type.
|
|
6
|
+
*/
|
|
7
|
+
type UseOnyxValue<TKey extends OnyxKey> = string extends TKey ? unknown : TKey extends CollectionKeyBase ? Readonly<NonNull<OnyxCollection<KeyValueMapping[TKey]>>> : Readonly<NonNull<OnyxEntry<KeyValueMapping[TKey]>>>;
|
|
8
|
+
type BaseUseOnyxOptions = {
|
|
4
9
|
/**
|
|
5
10
|
* Determines if this key in this subscription is safe to be evicted.
|
|
6
11
|
*/
|
|
@@ -13,10 +18,14 @@ type UseOnyxOptions<TKey extends OnyxKey, TReturnValue> = {
|
|
|
13
18
|
* If set to true, data will be retrieved from cache during the first render even if there is a pending merge for the key.
|
|
14
19
|
*/
|
|
15
20
|
allowStaleData?: boolean;
|
|
21
|
+
};
|
|
22
|
+
type UseOnyxInitialValueOption<TInitialValue> = {
|
|
16
23
|
/**
|
|
17
24
|
* This value will be returned by the hook on the first render while the data is being read from Onyx.
|
|
18
25
|
*/
|
|
19
|
-
initialValue?:
|
|
26
|
+
initialValue?: TInitialValue;
|
|
27
|
+
};
|
|
28
|
+
type UseOnyxSelectorOption<TKey extends OnyxKey, TReturnValue> = {
|
|
20
29
|
/**
|
|
21
30
|
* This will be used to subscribe to a subset of an Onyx key's data.
|
|
22
31
|
* Using this setting on `useOnyx` can have very positive performance benefits because the component will only re-render
|
|
@@ -26,11 +35,12 @@ type UseOnyxOptions<TKey extends OnyxKey, TReturnValue> = {
|
|
|
26
35
|
selector?: Selector<TKey, unknown, TReturnValue>;
|
|
27
36
|
};
|
|
28
37
|
type FetchStatus = 'loading' | 'loaded';
|
|
29
|
-
type CachedValue<TKey extends OnyxKey, TValue> = IsEqual<TValue,
|
|
38
|
+
type CachedValue<TKey extends OnyxKey, TValue> = IsEqual<TValue, UseOnyxValue<TKey>> extends true ? TValue : TKey extends CollectionKeyBase ? Readonly<NonNullable<OnyxCollection<TValue>>> : Readonly<TValue>;
|
|
30
39
|
type ResultMetadata = {
|
|
31
40
|
status: FetchStatus;
|
|
32
41
|
};
|
|
33
42
|
type UseOnyxResult<TKey extends OnyxKey, TValue> = [CachedValue<TKey, TValue>, ResultMetadata];
|
|
34
|
-
declare function useOnyx<TKey extends OnyxKey, TReturnValue =
|
|
43
|
+
declare function useOnyx<TKey extends OnyxKey, TReturnValue = UseOnyxValue<TKey>>(key: TKey, options?: BaseUseOnyxOptions & UseOnyxInitialValueOption<TReturnValue> & Required<UseOnyxSelectorOption<TKey, TReturnValue>>): UseOnyxResult<TKey, TReturnValue>;
|
|
44
|
+
declare function useOnyx<TKey extends OnyxKey, TReturnValue = UseOnyxValue<TKey>>(key: TKey, options?: BaseUseOnyxOptions & UseOnyxInitialValueOption<NoInfer<TReturnValue>>): UseOnyxResult<TKey, TReturnValue>;
|
|
35
45
|
export default useOnyx;
|
|
36
46
|
export type { UseOnyxResult, ResultMetadata, FetchStatus };
|
package/dist/useOnyx.js
CHANGED
|
@@ -21,10 +21,10 @@ function useOnyx(key, options) {
|
|
|
21
21
|
// We initialize it to `undefined` to simulate that we don't have any value from cache yet.
|
|
22
22
|
const cachedValueRef = (0, react_1.useRef)(undefined);
|
|
23
23
|
// Stores the previously result returned by the hook, containing the data from cache and the fetch status.
|
|
24
|
-
// We initialize it to `
|
|
24
|
+
// We initialize it to `undefined` and `loading` fetch status to simulate the initial result when the hook is loading from the cache.
|
|
25
25
|
// However, if `initWithStoredValues` is `true` we set the fetch status to `loaded` since we want to signal that data is ready.
|
|
26
26
|
const resultRef = (0, react_1.useRef)([
|
|
27
|
-
|
|
27
|
+
undefined,
|
|
28
28
|
{
|
|
29
29
|
status: (options === null || options === void 0 ? void 0 : options.initWithStoredValues) === false ? 'loaded' : 'loading',
|
|
30
30
|
},
|
|
@@ -76,8 +76,8 @@ function useOnyx(key, options) {
|
|
|
76
76
|
// and the result to be returned by the hook.
|
|
77
77
|
if (!(0, fast_equals_1.deepEqual)(cachedValueRef.current, newValue)) {
|
|
78
78
|
cachedValueRef.current = newValue;
|
|
79
|
-
// If the new value is `
|
|
80
|
-
resultRef.current = [((_a = cachedValueRef.current) !== null && _a !== void 0 ? _a :
|
|
79
|
+
// If the new value is `null` we default it to `undefined` to ensure the consumer get a consistent result from the hook.
|
|
80
|
+
resultRef.current = [((_a = cachedValueRef.current) !== null && _a !== void 0 ? _a : undefined), { status: newFetchStatus !== null && newFetchStatus !== void 0 ? newFetchStatus : 'loaded' }];
|
|
81
81
|
}
|
|
82
82
|
return resultRef.current;
|
|
83
83
|
}, [key, selectorRef, options === null || options === void 0 ? void 0 : options.allowStaleData, options === null || options === void 0 ? void 0 : options.initialValue]);
|
package/dist/withOnyx.d.ts
CHANGED
|
@@ -125,6 +125,8 @@ type OnyxPropCollectionMapping<TComponentProps, TOnyxProps, TOnyxProp extends ke
|
|
|
125
125
|
}[CollectionKeyBase];
|
|
126
126
|
|
|
127
127
|
/**
|
|
128
|
+
* @deprecated Use `useOnyx` instead of `withOnyx` whenever possible.
|
|
129
|
+
*
|
|
128
130
|
* This is a higher order component that provides the ability to map a state property directly to
|
|
129
131
|
* something in Onyx (a key/value store). That way, as soon as data in Onyx changes, the state will be set and the view
|
|
130
132
|
* will automatically change to reflect the new data.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-native-onyx",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.37",
|
|
4
4
|
"author": "Expensify, Inc.",
|
|
5
5
|
"homepage": "https://expensify.com",
|
|
6
6
|
"description": "State management for React Native",
|
|
@@ -85,7 +85,7 @@
|
|
|
85
85
|
"reassure": "^0.11.0",
|
|
86
86
|
"ts-node": "^10.9.2",
|
|
87
87
|
"type-fest": "^3.12.0",
|
|
88
|
-
"typescript": "^5.
|
|
88
|
+
"typescript": "^5.4.5"
|
|
89
89
|
},
|
|
90
90
|
"peerDependencies": {
|
|
91
91
|
"idb-keyval": "^6.2.1",
|