react-native-onyx 2.0.112 → 2.0.114

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.
@@ -246,6 +246,10 @@ declare function subscribeToKey<TKey extends OnyxKey>(connectOptions: ConnectOpt
246
246
  */
247
247
  declare function unsubscribeFromKey(subscriptionID: number): void;
248
248
  declare function updateSnapshots(data: OnyxUpdate[], mergeFn: typeof Onyx.merge): Array<() => Promise<void>>;
249
+ /**
250
+ * Clear internal variables used in this file, useful in test environments.
251
+ */
252
+ declare function clearOnyxUtilsInternals(): void;
249
253
  declare const OnyxUtils: {
250
254
  METHOD: {
251
255
  readonly SET: "set";
@@ -305,3 +309,4 @@ declare const OnyxUtils: {
305
309
  };
306
310
  export type { OnyxMethod };
307
311
  export default OnyxUtils;
312
+ export { clearOnyxUtilsInternals };
package/dist/OnyxUtils.js CHANGED
@@ -26,6 +26,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
26
26
  return (mod && mod.__esModule) ? mod : { "default": mod };
27
27
  };
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
+ exports.clearOnyxUtilsInternals = void 0;
29
30
  /* eslint-disable no-continue */
30
31
  const fast_equals_1 = require("fast-equals");
31
32
  const clone_1 = __importDefault(require("lodash/clone"));
@@ -51,20 +52,20 @@ const METHOD = {
51
52
  CLEAR: 'clear',
52
53
  };
53
54
  // Key/value store of Onyx key and arrays of values to merge
54
- const mergeQueue = {};
55
- const mergeQueuePromise = {};
55
+ let mergeQueue = {};
56
+ let mergeQueuePromise = {};
56
57
  // Holds a mapping of all the React components that want their state subscribed to a store key
57
- const callbackToStateMapping = {};
58
+ let callbackToStateMapping = {};
58
59
  // Keeps a copy of the values of the onyx collection keys as a map for faster lookups
59
60
  let onyxCollectionKeySet = new Set();
60
61
  // Holds a mapping of the connected key to the subscriptionID for faster lookups
61
- const onyxKeyToSubscriptionIDs = new Map();
62
+ let onyxKeyToSubscriptionIDs = new Map();
62
63
  // Optional user-provided key value states set when Onyx initializes or clears
63
64
  let defaultKeyStates = {};
64
65
  let batchUpdatesPromise = null;
65
66
  let batchUpdatesQueue = [];
66
67
  // Used for comparison with a new update to avoid invoking the Onyx.connect callback with the same data.
67
- const lastConnectionCallbackData = new Map();
68
+ let lastConnectionCallbackData = new Map();
68
69
  let snapshotKey = null;
69
70
  let fullyMergedSnapshotKeys;
70
71
  // Keeps track of the last subscriptionID that was used so we can keep incrementing it
@@ -1048,12 +1049,21 @@ function subscribeToKey(connectOptions) {
1048
1049
  // can send data back to the subscriber. Note that multiple keys can match as a subscriber could either be
1049
1050
  // subscribed to a "collection key" or a single key.
1050
1051
  const matchingKeys = [];
1051
- keys.forEach((key) => {
1052
- if (!isKeyMatch(mapping.key, key)) {
1053
- return;
1052
+ // Performance optimization: For single key subscriptions, avoid O(n) iteration
1053
+ if (!isCollectionKey(mapping.key)) {
1054
+ if (keys.has(mapping.key)) {
1055
+ matchingKeys.push(mapping.key);
1054
1056
  }
1055
- matchingKeys.push(key);
1056
- });
1057
+ }
1058
+ else {
1059
+ // Collection case - need to iterate through all keys to find matches (O(n))
1060
+ keys.forEach((key) => {
1061
+ if (!isKeyMatch(mapping.key, key)) {
1062
+ return;
1063
+ }
1064
+ matchingKeys.push(key);
1065
+ });
1066
+ }
1057
1067
  // If the key being connected to does not exist we initialize the value with null. For subscribers that connected
1058
1068
  // directly via connect() they will simply get a null value sent to them without any information about which key matched
1059
1069
  // since there are none matched. In withOnyx() we wait for all connected keys to return a value before rendering the child
@@ -1170,6 +1180,18 @@ function updateSnapshots(data, mergeFn) {
1170
1180
  });
1171
1181
  return promises;
1172
1182
  }
1183
+ /**
1184
+ * Clear internal variables used in this file, useful in test environments.
1185
+ */
1186
+ function clearOnyxUtilsInternals() {
1187
+ mergeQueue = {};
1188
+ mergeQueuePromise = {};
1189
+ callbackToStateMapping = {};
1190
+ onyxKeyToSubscriptionIDs = new Map();
1191
+ batchUpdatesQueue = [];
1192
+ lastConnectionCallbackData = new Map();
1193
+ }
1194
+ exports.clearOnyxUtilsInternals = clearOnyxUtilsInternals;
1173
1195
  const OnyxUtils = {
1174
1196
  METHOD,
1175
1197
  getMergeQueue,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-onyx",
3
- "version": "2.0.112",
3
+ "version": "2.0.114",
4
4
  "author": "Expensify, Inc.",
5
5
  "homepage": "https://expensify.com",
6
6
  "description": "State management for React Native",