react-native-onyx 1.0.125 → 1.0.127
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 +38 -0
- package/dist/web.development.js +228 -108
- package/dist/web.development.js.map +1 -1
- package/dist/web.min.js +1 -1
- package/dist/web.min.js.map +1 -1
- package/lib/DevTools.js +71 -0
- package/lib/Onyx.d.ts +1 -0
- package/lib/Onyx.js +32 -4
- package/lib/types.d.ts +10 -5
- package/lib/utils.d.ts +8 -1
- package/lib/utils.js +5 -1
- package/lib/withOnyx.d.ts +4 -4
- package/package.json +1 -1
package/lib/DevTools.js
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import _ from 'underscore';
|
|
2
|
+
|
|
3
|
+
const ERROR_LABEL = 'Onyx DevTools - Error: ';
|
|
4
|
+
|
|
5
|
+
/* eslint-disable no-underscore-dangle */
|
|
6
|
+
class DevTools {
|
|
7
|
+
constructor() {
|
|
8
|
+
this.remoteDev = this.connectViaExtension();
|
|
9
|
+
this.state = {};
|
|
10
|
+
this.defaultState = {};
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
connectViaExtension(options) {
|
|
14
|
+
try {
|
|
15
|
+
if ((options && options.remote) || typeof window === 'undefined' || !window.__REDUX_DEVTOOLS_EXTENSION__) {
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
return window.__REDUX_DEVTOOLS_EXTENSION__.connect(options);
|
|
19
|
+
} catch (e) {
|
|
20
|
+
console.error(ERROR_LABEL, e);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Registers an action that updated the current state of the storage
|
|
26
|
+
*
|
|
27
|
+
* @param {string} type - name of the action
|
|
28
|
+
* @param {any} payload - data written to the storage
|
|
29
|
+
* @param {object} stateChanges - partial state that got updated after the changes
|
|
30
|
+
*/
|
|
31
|
+
registerAction(type, payload = undefined, stateChanges = {}) {
|
|
32
|
+
try {
|
|
33
|
+
if (!this.remoteDev) {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
const newState = {
|
|
37
|
+
...this.state,
|
|
38
|
+
...stateChanges,
|
|
39
|
+
};
|
|
40
|
+
this.remoteDev.send({type, payload}, newState);
|
|
41
|
+
this.state = newState;
|
|
42
|
+
} catch (e) {
|
|
43
|
+
console.error(ERROR_LABEL, e);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
initState(initialState = {}) {
|
|
48
|
+
try {
|
|
49
|
+
if (!this.remoteDev) {
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
this.remoteDev.init(initialState);
|
|
53
|
+
this.state = initialState;
|
|
54
|
+
this.defaultState = initialState;
|
|
55
|
+
} catch (e) {
|
|
56
|
+
console.error(ERROR_LABEL, e);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* This clears the internal state of the DevTools, preserving the keys included in `keysToPreserve`
|
|
62
|
+
*
|
|
63
|
+
* @param {string[]} keysToPreserve
|
|
64
|
+
*/
|
|
65
|
+
clearState(keysToPreserve = []) {
|
|
66
|
+
const newState = _.mapObject(this.state, (value, key) => (keysToPreserve.includes(key) ? value : this.defaultState[key]));
|
|
67
|
+
this.registerAction('CLEAR', undefined, newState);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export default new DevTools();
|
package/lib/Onyx.d.ts
CHANGED
package/lib/Onyx.js
CHANGED
|
@@ -11,6 +11,7 @@ import * as Broadcast from './broadcast';
|
|
|
11
11
|
import * as ActiveClientManager from './ActiveClientManager';
|
|
12
12
|
import utils from './utils';
|
|
13
13
|
import unstable_batchedUpdates from './batch';
|
|
14
|
+
import DevTools from './DevTools';
|
|
14
15
|
|
|
15
16
|
// Method constants
|
|
16
17
|
const METHOD = {
|
|
@@ -62,6 +63,18 @@ let onClearCallback = null;
|
|
|
62
63
|
let batchUpdatesPromise = null;
|
|
63
64
|
let batchUpdatesQueue = [];
|
|
64
65
|
|
|
66
|
+
/**
|
|
67
|
+
* Sends an action to DevTools extension
|
|
68
|
+
*
|
|
69
|
+
* @param {string} method - Onyx method from METHOD
|
|
70
|
+
* @param {string} key - Onyx key that was changed
|
|
71
|
+
* @param {any} value - contains the change that was made by the method
|
|
72
|
+
* @param {any} mergedValue - (optional) value that was written in the storage after a merge method was executed.
|
|
73
|
+
*/
|
|
74
|
+
function sendActionToDevTools(method, key, value, mergedValue = undefined) {
|
|
75
|
+
DevTools.registerAction(utils.formatActionName(method, key), value, key ? {[key]: mergedValue || value} : value);
|
|
76
|
+
}
|
|
77
|
+
|
|
65
78
|
/**
|
|
66
79
|
* We are batching together onyx updates. This helps with use cases where we schedule onyx updates after each other.
|
|
67
80
|
* This happens for example in the Onyx.update function, where we process API responses that might contain a lot of
|
|
@@ -1105,7 +1118,10 @@ function set(key, value) {
|
|
|
1105
1118
|
|
|
1106
1119
|
return Storage.setItem(key, valueAfterRemoving)
|
|
1107
1120
|
.catch((error) => evictStorageAndRetry(error, set, key, valueAfterRemoving))
|
|
1108
|
-
.then(() =>
|
|
1121
|
+
.then(() => {
|
|
1122
|
+
sendActionToDevTools(METHOD.SET, key, valueAfterRemoving);
|
|
1123
|
+
return updatePromise;
|
|
1124
|
+
});
|
|
1109
1125
|
}
|
|
1110
1126
|
|
|
1111
1127
|
/**
|
|
@@ -1158,7 +1174,10 @@ function multiSet(data) {
|
|
|
1158
1174
|
|
|
1159
1175
|
return Storage.multiSet(keyValuePairs)
|
|
1160
1176
|
.catch((error) => evictStorageAndRetry(error, multiSet, data))
|
|
1161
|
-
.then(() =>
|
|
1177
|
+
.then(() => {
|
|
1178
|
+
sendActionToDevTools(METHOD.MULTI_SET, undefined, data);
|
|
1179
|
+
return Promise.all(updatePromises);
|
|
1180
|
+
});
|
|
1162
1181
|
}
|
|
1163
1182
|
|
|
1164
1183
|
/**
|
|
@@ -1274,7 +1293,10 @@ function merge(key, changes) {
|
|
|
1274
1293
|
return updatePromise;
|
|
1275
1294
|
}
|
|
1276
1295
|
|
|
1277
|
-
return Storage.mergeItem(key, batchedChanges, modifiedData).then(() =>
|
|
1296
|
+
return Storage.mergeItem(key, batchedChanges, modifiedData).then(() => {
|
|
1297
|
+
sendActionToDevTools(METHOD.MERGE, key, changes, modifiedData);
|
|
1298
|
+
return updatePromise;
|
|
1299
|
+
});
|
|
1278
1300
|
} catch (error) {
|
|
1279
1301
|
Logger.logAlert(`An error occurred while applying merge for key: ${key}, Error: ${error}`);
|
|
1280
1302
|
return Promise.resolve();
|
|
@@ -1395,6 +1417,7 @@ function clear(keysToPreserve = []) {
|
|
|
1395
1417
|
.then(() => {
|
|
1396
1418
|
isClearing = false;
|
|
1397
1419
|
Broadcast.sendMessage({type: METHOD.CLEAR, keysToPreserve});
|
|
1420
|
+
DevTools.clearState(keysToPreserve);
|
|
1398
1421
|
return Promise.all(updatePromises);
|
|
1399
1422
|
});
|
|
1400
1423
|
});
|
|
@@ -1480,7 +1503,10 @@ function mergeCollection(collectionKey, collection) {
|
|
|
1480
1503
|
|
|
1481
1504
|
return Promise.all(promises)
|
|
1482
1505
|
.catch((error) => evictStorageAndRetry(error, mergeCollection, collection))
|
|
1483
|
-
.then(() =>
|
|
1506
|
+
.then(() => {
|
|
1507
|
+
sendActionToDevTools(METHOD.MERGE_COLLECTION, undefined, collection);
|
|
1508
|
+
return promiseUpdate;
|
|
1509
|
+
});
|
|
1484
1510
|
});
|
|
1485
1511
|
}
|
|
1486
1512
|
|
|
@@ -1659,6 +1685,8 @@ function init({
|
|
|
1659
1685
|
// Set our default key states to use when initializing and clearing Onyx data
|
|
1660
1686
|
defaultKeyStates = initialKeyStates;
|
|
1661
1687
|
|
|
1688
|
+
DevTools.initState(initialKeyStates);
|
|
1689
|
+
|
|
1662
1690
|
// Let Onyx know about which keys are safe to evict
|
|
1663
1691
|
evictionAllowList = safeEvictionKeys;
|
|
1664
1692
|
|
package/lib/types.d.ts
CHANGED
|
@@ -115,13 +115,13 @@ type KeyValueMapping = {
|
|
|
115
115
|
/**
|
|
116
116
|
* Represents a selector function type which operates based on the provided `TKey` and `ReturnType`.
|
|
117
117
|
*
|
|
118
|
-
* A `Selector` is a function that accepts a value and returns a processed value.
|
|
118
|
+
* A `Selector` is a function that accepts a value, the withOnyx's internal state and returns a processed value.
|
|
119
119
|
* This type accepts two type parameters: `TKey` and `TReturnType`.
|
|
120
120
|
*
|
|
121
121
|
* The type `TKey` extends `OnyxKey` and it is the key used to access a value in `KeyValueMapping`.
|
|
122
122
|
* `TReturnType` is the type of the returned value from the selector function.
|
|
123
123
|
*/
|
|
124
|
-
type Selector<TKey extends OnyxKey, TReturnType> = (value: OnyxEntry<KeyValueMapping[TKey]>) => TReturnType;
|
|
124
|
+
type Selector<TKey extends OnyxKey, TOnyxProps, TReturnType> = (value: OnyxEntry<KeyValueMapping[TKey]>, state: WithOnyxInstanceState<TOnyxProps>) => TReturnType;
|
|
125
125
|
|
|
126
126
|
/**
|
|
127
127
|
* Represents a single Onyx entry, that can be either `TOnyxValue` or `null` if it doesn't exist.
|
|
@@ -215,10 +215,15 @@ type NonTransformableTypes =
|
|
|
215
215
|
type NullishDeep<T> = T extends NonTransformableTypes ? T : T extends object ? NullishObjectDeep<T> : unknown;
|
|
216
216
|
|
|
217
217
|
/**
|
|
218
|
-
Same as `NullishDeep`, but accepts only `object`s as inputs. Internal helper for `NullishDeep`.
|
|
219
|
-
*/
|
|
218
|
+
* Same as `NullishDeep`, but accepts only `object`s as inputs. Internal helper for `NullishDeep`.
|
|
219
|
+
*/
|
|
220
220
|
type NullishObjectDeep<ObjectType extends object> = {
|
|
221
221
|
[KeyType in keyof ObjectType]?: NullishDeep<ObjectType[KeyType]> | null;
|
|
222
222
|
};
|
|
223
223
|
|
|
224
|
-
|
|
224
|
+
/**
|
|
225
|
+
* Represents withOnyx's internal state, containing the Onyx props and a `loading` flag.
|
|
226
|
+
*/
|
|
227
|
+
type WithOnyxInstanceState<TOnyxProps> = (TOnyxProps & {loading: boolean}) | undefined;
|
|
228
|
+
|
|
229
|
+
export {CollectionKey, CollectionKeyBase, CustomTypeOptions, DeepRecord, Key, KeyValueMapping, OnyxCollection, OnyxEntry, OnyxKey, Selector, NullishDeep, WithOnyxInstanceState};
|
package/lib/utils.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { OnyxKey } from './types';
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* Merges two objects and removes null values if "shouldRemoveNullObjectValues" is set to true
|
|
3
5
|
*
|
|
@@ -7,4 +9,9 @@
|
|
|
7
9
|
*/
|
|
8
10
|
declare function fastMerge<T>(target: T, source: T, shouldRemoveNullObjectValues: boolean = true): T;
|
|
9
11
|
|
|
10
|
-
|
|
12
|
+
/**
|
|
13
|
+
* Returns a formatted action name to be send to DevTools, given the method and optionally the key that was changed
|
|
14
|
+
*/
|
|
15
|
+
declare function formatActionName(method: string, key?: OnyxKey): string;
|
|
16
|
+
|
|
17
|
+
export default { fastMerge, formatActionName };
|
package/lib/utils.js
CHANGED
|
@@ -105,4 +105,8 @@ function removeNestedNullValues(value) {
|
|
|
105
105
|
return value;
|
|
106
106
|
}
|
|
107
107
|
|
|
108
|
-
|
|
108
|
+
function formatActionName(method, key) {
|
|
109
|
+
return key ? `${method.toUpperCase()}/${key}` : method.toUpperCase();
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export default {areObjectsEmpty, fastMerge, formatActionName, removeNestedNullValues};
|
package/lib/withOnyx.d.ts
CHANGED
|
@@ -42,7 +42,7 @@ type EntryBaseMapping<TOnyxKey extends OnyxKey> = {
|
|
|
42
42
|
*/
|
|
43
43
|
type BaseMappingKey<TComponentProps, TOnyxProps, TOnyxProp extends keyof TOnyxProps, TOnyxKey extends OnyxKey, TOnyxValue> = IsEqual<TOnyxValue, TOnyxProps[TOnyxProp]> extends true
|
|
44
44
|
? {
|
|
45
|
-
key: TOnyxKey | ((props: Omit<TComponentProps, keyof TOnyxProps>) => TOnyxKey);
|
|
45
|
+
key: TOnyxKey | ((props: Omit<TComponentProps, keyof TOnyxProps> & Partial<TOnyxProps>) => TOnyxKey);
|
|
46
46
|
}
|
|
47
47
|
: never;
|
|
48
48
|
|
|
@@ -63,7 +63,7 @@ type BaseMappingKey<TComponentProps, TOnyxProps, TOnyxProp extends keyof TOnyxPr
|
|
|
63
63
|
*/
|
|
64
64
|
type BaseMappingStringKeyAndSelector<TComponentProps, TOnyxProps, TOnyxProp extends keyof TOnyxProps, TOnyxKey extends OnyxKey> = {
|
|
65
65
|
key: TOnyxKey;
|
|
66
|
-
selector: Selector<TOnyxKey, TOnyxProps[TOnyxProp]>;
|
|
66
|
+
selector: Selector<TOnyxKey, TOnyxProps, TOnyxProps[TOnyxProp]>;
|
|
67
67
|
};
|
|
68
68
|
|
|
69
69
|
/**
|
|
@@ -82,8 +82,8 @@ type BaseMappingStringKeyAndSelector<TComponentProps, TOnyxProps, TOnyxProp exte
|
|
|
82
82
|
* ```
|
|
83
83
|
*/
|
|
84
84
|
type BaseMappingFunctionKeyAndSelector<TComponentProps, TOnyxProps, TOnyxProp extends keyof TOnyxProps, TOnyxKey extends OnyxKey> = {
|
|
85
|
-
key: (props: Omit<TComponentProps, keyof TOnyxProps>) => TOnyxKey;
|
|
86
|
-
selector: Selector<TOnyxKey, TOnyxProps[TOnyxProp]>;
|
|
85
|
+
key: (props: Omit<TComponentProps, keyof TOnyxProps> & Partial<TOnyxProps>) => TOnyxKey;
|
|
86
|
+
selector: Selector<TOnyxKey, TOnyxProps, TOnyxProps[TOnyxProp]>;
|
|
87
87
|
};
|
|
88
88
|
|
|
89
89
|
/**
|