react-native-onyx 2.0.19 → 2.0.21

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.
@@ -1,23 +1,41 @@
1
- declare const _default: DevTools;
2
- export default _default;
1
+ type DevtoolsOptions = {
2
+ maxAge?: number;
3
+ name?: string;
4
+ postTimelineUpdate?: () => void;
5
+ preAction?: () => void;
6
+ logTrace?: boolean;
7
+ remote?: boolean;
8
+ };
9
+ type DevtoolsSubscriber = (message: {
10
+ type: string;
11
+ payload: unknown;
12
+ state: string;
13
+ }) => void;
14
+ type DevtoolsConnection = {
15
+ send(data: Record<string, unknown>, state: Record<string, unknown>): void;
16
+ init(state: Record<string, unknown>): void;
17
+ unsubscribe(): void;
18
+ subscribe(cb: DevtoolsSubscriber): () => void;
19
+ };
3
20
  declare class DevTools {
4
- remoteDev: any;
5
- state: {};
6
- defaultState: {};
7
- connectViaExtension(options: any): any;
21
+ private remoteDev?;
22
+ private state;
23
+ private defaultState;
24
+ constructor();
25
+ connectViaExtension(options?: DevtoolsOptions): DevtoolsConnection | undefined;
8
26
  /**
9
27
  * Registers an action that updated the current state of the storage
10
28
  *
11
- * @param {string} type - name of the action
12
- * @param {any} payload - data written to the storage
13
- * @param {object} stateChanges - partial state that got updated after the changes
29
+ * @param type - name of the action
30
+ * @param payload - data written to the storage
31
+ * @param stateChanges - partial state that got updated after the changes
14
32
  */
15
- registerAction(type: string, payload?: any, stateChanges?: object): void;
16
- initState(initialState?: {}): void;
33
+ registerAction(type: string, payload: unknown, stateChanges?: Record<string, unknown>): void;
34
+ initState(initialState?: Record<string, unknown>): void;
17
35
  /**
18
36
  * This clears the internal state of the DevTools, preserving the keys included in `keysToPreserve`
19
- *
20
- * @param {string[]} keysToPreserve
21
37
  */
22
38
  clearState(keysToPreserve?: string[]): void;
23
39
  }
40
+ declare const _default: DevTools;
41
+ export default _default;
package/dist/DevTools.js CHANGED
@@ -1,11 +1,6 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
- const underscore_1 = __importDefault(require("underscore"));
7
3
  const ERROR_LABEL = 'Onyx DevTools - Error: ';
8
- /* eslint-disable no-underscore-dangle */
9
4
  class DevTools {
10
5
  constructor() {
11
6
  this.remoteDev = this.connectViaExtension();
@@ -14,10 +9,14 @@ class DevTools {
14
9
  }
15
10
  connectViaExtension(options) {
16
11
  try {
17
- if ((options && options.remote) || typeof window === 'undefined' || !window.__REDUX_DEVTOOLS_EXTENSION__) {
12
+ // We don't want to augment the window type in a library code, so we use type assertion instead
13
+ // eslint-disable-next-line no-underscore-dangle, @typescript-eslint/no-explicit-any
14
+ const reduxDevtools = window.__REDUX_DEVTOOLS_EXTENSION__;
15
+ if ((options && options.remote) || typeof window === 'undefined' || !reduxDevtools) {
18
16
  return;
19
17
  }
20
- return window.__REDUX_DEVTOOLS_EXTENSION__.connect(options);
18
+ // eslint-disable-next-line no-underscore-dangle, @typescript-eslint/no-explicit-any
19
+ return reduxDevtools.connect(options);
21
20
  }
22
21
  catch (e) {
23
22
  console.error(ERROR_LABEL, e);
@@ -26,11 +25,11 @@ class DevTools {
26
25
  /**
27
26
  * Registers an action that updated the current state of the storage
28
27
  *
29
- * @param {string} type - name of the action
30
- * @param {any} payload - data written to the storage
31
- * @param {object} stateChanges - partial state that got updated after the changes
28
+ * @param type - name of the action
29
+ * @param payload - data written to the storage
30
+ * @param stateChanges - partial state that got updated after the changes
32
31
  */
33
- registerAction(type, payload = undefined, stateChanges = {}) {
32
+ registerAction(type, payload, stateChanges = {}) {
34
33
  try {
35
34
  if (!this.remoteDev) {
36
35
  return;
@@ -58,11 +57,13 @@ class DevTools {
58
57
  }
59
58
  /**
60
59
  * This clears the internal state of the DevTools, preserving the keys included in `keysToPreserve`
61
- *
62
- * @param {string[]} keysToPreserve
63
60
  */
64
61
  clearState(keysToPreserve = []) {
65
- const newState = underscore_1.default.mapObject(this.state, (value, key) => (keysToPreserve.includes(key) ? value : this.defaultState[key]));
62
+ const newState = Object.entries(this.state).reduce((obj, [key, value]) => {
63
+ // eslint-disable-next-line no-param-reassign
64
+ obj[key] = keysToPreserve.includes(key) ? value : this.defaultState[key];
65
+ return obj;
66
+ }, {});
66
67
  this.registerAction('CLEAR', undefined, newState);
67
68
  }
68
69
  }
package/dist/Onyx.js CHANGED
@@ -165,12 +165,12 @@ function get(key) {
165
165
  /**
166
166
  * Returns current key names stored in persisted storage
167
167
  * @private
168
- * @returns {Promise<string[]>}
168
+ * @returns {Promise<Set<Key>>}
169
169
  */
170
170
  function getAllKeys() {
171
171
  // When we've already read stored keys, resolve right away
172
172
  const storedKeys = OnyxCache_1.default.getAllKeys();
173
- if (storedKeys.length > 0) {
173
+ if (storedKeys.size > 0) {
174
174
  return Promise.resolve(storedKeys);
175
175
  }
176
176
  const taskName = 'getAllKeys';
@@ -181,7 +181,8 @@ function getAllKeys() {
181
181
  // Otherwise retrieve the keys from storage and capture a promise to aid concurrent usages
182
182
  const promise = storage_1.default.getAllKeys().then((keys) => {
183
183
  OnyxCache_1.default.setAllKeys(keys);
184
- return keys;
184
+ // return the updated set of keys
185
+ return OnyxCache_1.default.getAllKeys();
185
186
  });
186
187
  return OnyxCache_1.default.captureTask(taskName, promise);
187
188
  }
@@ -252,10 +253,16 @@ function tryGetCachedValue(key, mapping = {}) {
252
253
  const allCacheKeys = OnyxCache_1.default.getAllKeys();
253
254
  // It is possible we haven't loaded all keys yet so we do not know if the
254
255
  // collection actually exists.
255
- if (allCacheKeys.length === 0) {
256
+ if (allCacheKeys.size === 0) {
256
257
  return;
257
258
  }
258
- const matchingKeys = underscore_1.default.filter(allCacheKeys, (k) => k.startsWith(key));
259
+ const matchingKeys = [];
260
+ allCacheKeys.forEach((k) => {
261
+ if (!k.startsWith(key)) {
262
+ return;
263
+ }
264
+ matchingKeys.push(k);
265
+ });
259
266
  const values = underscore_1.default.reduce(matchingKeys, (finalObject, matchedKey) => {
260
267
  const cachedValue = OnyxCache_1.default.getValue(matchedKey);
261
268
  if (cachedValue) {
@@ -342,7 +349,7 @@ function addToEvictionBlockList(key, connectionID) {
342
349
  function addAllSafeEvictionKeysToRecentlyAccessedList() {
343
350
  return getAllKeys().then((keys) => {
344
351
  underscore_1.default.each(evictionAllowList, (safeEvictionKey) => {
345
- underscore_1.default.each(keys, (key) => {
352
+ keys.forEach((key) => {
346
353
  if (!isKeyMatch(safeEvictionKey, key)) {
347
354
  return;
348
355
  }
@@ -357,7 +364,13 @@ function addAllSafeEvictionKeysToRecentlyAccessedList() {
357
364
  * @returns {Object}
358
365
  */
359
366
  function getCachedCollection(collectionKey) {
360
- const collectionMemberKeys = underscore_1.default.filter(OnyxCache_1.default.getAllKeys(), (storedKey) => isCollectionMemberKey(collectionKey, storedKey));
367
+ const collectionMemberKeys = [];
368
+ OnyxCache_1.default.getAllKeys().forEach((storedKey) => {
369
+ if (!isCollectionMemberKey(collectionKey, storedKey)) {
370
+ return;
371
+ }
372
+ collectionMemberKeys.push(storedKey);
373
+ });
361
374
  return underscore_1.default.reduce(collectionMemberKeys, (prev, curr) => {
362
375
  const cachedValue = OnyxCache_1.default.getValue(curr);
363
376
  if (!cachedValue) {
@@ -821,7 +834,13 @@ function connect(mapping) {
821
834
  // We search all the keys in storage to see if any are a "match" for the subscriber we are connecting so that we
822
835
  // can send data back to the subscriber. Note that multiple keys can match as a subscriber could either be
823
836
  // subscribed to a "collection key" or a single key.
824
- const matchingKeys = underscore_1.default.filter(keys, (key) => isKeyMatch(mapping.key, key));
837
+ const matchingKeys = [];
838
+ keys.forEach((key) => {
839
+ if (!isKeyMatch(mapping.key, key)) {
840
+ return;
841
+ }
842
+ matchingKeys.push(key);
843
+ });
825
844
  // If the key being connected to does not exist we initialize the value with null. For subscribers that connected
826
845
  // directly via connect() they will simply get a null value sent to them without any information about which key matched
827
846
  // since there are none matched. In withOnyx() we wait for all connected keys to return a value before rendering the child
@@ -1241,7 +1260,7 @@ function clear(keysToPreserve = []) {
1241
1260
  // status, or activeClients need to remain in Onyx even when signed out)
1242
1261
  // 2. Any keys with a default state (because they need to remain in Onyx as their default, and setting them
1243
1262
  // to null would cause unknown behavior)
1244
- underscore_1.default.each(keys, (key) => {
1263
+ keys.forEach((key) => {
1245
1264
  const isKeyToPreserve = underscore_1.default.contains(keysToPreserve, key);
1246
1265
  const isDefaultKey = underscore_1.default.has(defaultKeyStates, key);
1247
1266
  // If the key is being removed or reset to default:
@@ -1336,7 +1355,7 @@ function mergeCollection(collectionKey, collection) {
1336
1355
  return true;
1337
1356
  })
1338
1357
  .keys()
1339
- .partition((key) => persistedKeys.includes(key))
1358
+ .partition((key) => persistedKeys.has(key))
1340
1359
  .value();
1341
1360
  const existingKeyCollection = underscore_1.default.pick(collection, existingKeys);
1342
1361
  const newCollection = underscore_1.default.pick(collection, newKeys);
@@ -20,7 +20,7 @@ declare class OnyxCache {
20
20
  private maxRecentKeysSize;
21
21
  constructor();
22
22
  /** Get all the storage keys */
23
- getAllKeys(): Key[];
23
+ getAllKeys(): Set<Key>;
24
24
  /**
25
25
  * Get a cached value from storage
26
26
  * @param [shouldReindexCache] – This is an LRU cache, and by default accessing a value will make it become last in line to be evicted. This flag can be used to skip that and just access the value directly without side-effects.
package/dist/OnyxCache.js CHANGED
@@ -23,7 +23,7 @@ class OnyxCache {
23
23
  }
24
24
  /** Get all the storage keys */
25
25
  getAllKeys() {
26
- return Array.from(this.storageKeys);
26
+ return this.storageKeys;
27
27
  }
28
28
  /**
29
29
  * Get a cached value from storage
package/dist/index.d.ts CHANGED
@@ -1,23 +1,8 @@
1
- import Onyx, {OnyxUpdate, ConnectOptions} from './Onyx';
2
- import {CustomTypeOptions, OnyxCollection, OnyxEntry, NullishDeep, KeyValueMapping, OnyxKey, Selector, WithOnyxInstanceState, OnyxValue} from './types';
1
+ import Onyx from './Onyx';
2
+ import type { OnyxUpdate, ConnectOptions } from './Onyx';
3
+ import type { CustomTypeOptions, OnyxCollection, OnyxEntry, NullishDeep, KeyValueMapping, OnyxKey, Selector, WithOnyxInstanceState } from './types';
4
+ import useOnyx from './useOnyx';
3
5
  import withOnyx from './withOnyx';
4
- import useOnyx, {UseOnyxResult, FetchStatus} from './useOnyx';
5
-
6
6
  export default Onyx;
7
- export {
8
- CustomTypeOptions,
9
- OnyxCollection,
10
- OnyxEntry,
11
- OnyxUpdate,
12
- withOnyx,
13
- ConnectOptions,
14
- NullishDeep,
15
- KeyValueMapping,
16
- OnyxKey,
17
- Selector,
18
- WithOnyxInstanceState,
19
- useOnyx,
20
- UseOnyxResult,
21
- OnyxValue,
22
- FetchStatus,
23
- };
7
+ export { withOnyx, useOnyx };
8
+ export type { CustomTypeOptions, OnyxCollection, OnyxEntry, OnyxUpdate, ConnectOptions, NullishDeep, KeyValueMapping, OnyxKey, Selector, WithOnyxInstanceState };
package/dist/index.js CHANGED
@@ -5,8 +5,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.useOnyx = exports.withOnyx = void 0;
7
7
  const Onyx_1 = __importDefault(require("./Onyx"));
8
- const withOnyx_1 = __importDefault(require("./withOnyx"));
9
- exports.withOnyx = withOnyx_1.default;
10
8
  const useOnyx_1 = __importDefault(require("./useOnyx"));
11
9
  exports.useOnyx = useOnyx_1.default;
10
+ const withOnyx_1 = __importDefault(require("./withOnyx"));
11
+ exports.withOnyx = withOnyx_1.default;
12
12
  exports.default = Onyx_1.default;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-onyx",
3
- "version": "2.0.19",
3
+ "version": "2.0.21",
4
4
  "author": "Expensify, Inc.",
5
5
  "homepage": "https://expensify.com",
6
6
  "description": "State management for React Native",
package/dist/MDTable.d.ts DELETED
@@ -1,36 +0,0 @@
1
- export default MDTable;
2
- declare class MDTable {
3
- /**
4
- * Create a CSV string from the table data
5
- * @returns {string}
6
- */
7
- toCSV(): string;
8
- /**
9
- * Create a JSON string from the table data
10
- * @returns {string}
11
- */
12
- toJSON(): string;
13
- /**
14
- * Create a MD string from the table data
15
- * @returns {string}
16
- */
17
- toString(): string;
18
- }
19
- declare namespace MDTable {
20
- /**
21
- * Table Factory helper
22
- * @param {Object} options
23
- * @param {string} [options.title] - optional title center above the table
24
- * @param {string[]} options.heading - table column names
25
- * @param {number[]} [options.leftAlignedCols=[]] - indexes of columns that should be left aligned
26
- * Pass the columns that are non numeric here - the rest will be aligned to the right
27
- * @param {Array} [options.rows] The table can be initialized with row. Rows can also be added by `addRow`
28
- * @returns {MDTable}
29
- */
30
- function factory({ title, heading, leftAlignedCols, rows }: {
31
- title?: string | undefined;
32
- heading: string[];
33
- leftAlignedCols?: number[] | undefined;
34
- rows?: any[] | undefined;
35
- }): MDTable;
36
- }
package/dist/MDTable.js DELETED
@@ -1,61 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- const ascii_table_1 = __importDefault(require("ascii-table"));
7
- class MDTable extends ascii_table_1.default {
8
- /**
9
- * Create a CSV string from the table data
10
- * @returns {string}
11
- */
12
- toCSV() {
13
- return [this.getTitle(), this.getHeading(), ...this.getRows()].join('\n');
14
- }
15
- /**
16
- * Create a JSON string from the table data
17
- * @returns {string}
18
- */
19
- toJSON() {
20
- return JSON.stringify(super.toJSON());
21
- }
22
- /**
23
- * Create a MD string from the table data
24
- * @returns {string}
25
- */
26
- toString() {
27
- // Ignore modifying the first |---| for titled tables
28
- let idx = this.getTitle() ? -2 : -1;
29
- const ascii = super.toString().replace(/-\|/g, () => {
30
- /* we replace "----|" with "---:|" to align the data to the right in MD */
31
- idx++;
32
- if (idx < 0 || this.leftAlignedCols.includes(idx)) {
33
- return '-|';
34
- }
35
- return ':|';
36
- });
37
- // strip the top and the bottom row (----) to make an MD table
38
- const md = ascii.split('\n').slice(1, -1).join('\n');
39
- return md;
40
- }
41
- }
42
- /**
43
- * Table Factory helper
44
- * @param {Object} options
45
- * @param {string} [options.title] - optional title center above the table
46
- * @param {string[]} options.heading - table column names
47
- * @param {number[]} [options.leftAlignedCols=[]] - indexes of columns that should be left aligned
48
- * Pass the columns that are non numeric here - the rest will be aligned to the right
49
- * @param {Array} [options.rows] The table can be initialized with row. Rows can also be added by `addRow`
50
- * @returns {MDTable}
51
- */
52
- MDTable.factory = ({ title, heading, leftAlignedCols = [], rows = [] }) => {
53
- const table = new MDTable({ title, heading, rows });
54
- table.leftAlignedCols = leftAlignedCols;
55
- /* By default we want everything aligned to the right as most values are numbers
56
- * we just override the columns that are not right aligned */
57
- heading.forEach((name, idx) => table.setAlign(idx, ascii_table_1.default.RIGHT));
58
- leftAlignedCols.forEach((idx) => table.setAlign(idx, ascii_table_1.default.LEFT));
59
- return table;
60
- };
61
- exports.default = MDTable;