react-native-onyx 3.0.93 → 3.0.94

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.
Files changed (2) hide show
  1. package/dist/OnyxCache.js +20 -10
  2. package/package.json +1 -1
package/dist/OnyxCache.js CHANGED
@@ -286,7 +286,7 @@ class OnyxCache {
286
286
  // Initialize frozen snapshots for collection keys
287
287
  for (const collectionKey of collectionKeys) {
288
288
  if (!this.collectionSnapshots.has(collectionKey)) {
289
- this.collectionSnapshots.set(collectionKey, Object.freeze({}));
289
+ this.collectionSnapshots.set(collectionKey, FROZEN_EMPTY_COLLECTION);
290
290
  }
291
291
  }
292
292
  }
@@ -302,6 +302,7 @@ class OnyxCache {
302
302
  const previousSnapshot = this.collectionSnapshots.get(collectionKey);
303
303
  const members = {};
304
304
  let hasMemberChanges = false;
305
+ let hasMembers = false;
305
306
  // Use the indexed forward lookup for O(collectionMembers) iteration.
306
307
  // Falls back to scanning all storageKeys if the index isn't populated yet.
307
308
  const memberKeys = OnyxKeys_1.default.getMembersOfCollection(collectionKey);
@@ -318,6 +319,7 @@ class OnyxCache {
318
319
  // and should not be included in the frozen collection snapshot.
319
320
  if (val !== undefined && val !== null) {
320
321
  members[key] = val;
322
+ hasMembers = true;
321
323
  // Check if this member's reference changed from the old snapshot
322
324
  if (!hasMemberChanges && (!previousSnapshot || previousSnapshot[key] !== val)) {
323
325
  hasMemberChanges = true;
@@ -342,6 +344,13 @@ class OnyxCache {
342
344
  if (!hasMemberChanges && previousSnapshot) {
343
345
  return;
344
346
  }
347
+ // When the collection has no members, reuse one shared empty object for every empty
348
+ // collection. That way reads can tell a collection is empty with a quick `===` check
349
+ // instead of looping over its keys every time.
350
+ if (!hasMembers) {
351
+ this.collectionSnapshots.set(collectionKey, FROZEN_EMPTY_COLLECTION);
352
+ return;
353
+ }
345
354
  Object.freeze(members);
346
355
  this.collectionSnapshots.set(collectionKey, members);
347
356
  }
@@ -356,17 +365,18 @@ class OnyxCache {
356
365
  this.dirtyCollections.delete(collectionKey);
357
366
  }
358
367
  const snapshot = this.collectionSnapshots.get(collectionKey);
359
- if (utils_1.default.isEmptyObject(snapshot)) {
360
- // We check storageKeys.size (not collection-specific keys) to distinguish
361
- // "init complete, this collection is genuinely empty" from "init not done yet."
362
- // During init, setAllKeys loads ALL keys at once — so if any key exists,
363
- // the full storage picture is loaded and an empty collection is truly empty.
364
- // Returning undefined before init prevents subscribers from seeing a false empty state.
365
- if (this.storageKeys.size > 0) {
366
- return FROZEN_EMPTY_COLLECTION;
367
- }
368
+ // We never stored anything for this collection key.
369
+ if (snapshot === undefined) {
368
370
  return undefined;
369
371
  }
372
+ // The collection is empty (it holds our shared empty object). But "empty" is ambiguous
373
+ // during startup: we can't tell an actually-empty collection apart from one whose data
374
+ // hasn't loaded yet. Once any key exists, we know setAllKeys has run and loaded everything,
375
+ // so an empty collection really is empty. Before that, return undefined so subscribers
376
+ // don't briefly see a collection as empty when it just hasn't loaded.
377
+ if (snapshot === FROZEN_EMPTY_COLLECTION) {
378
+ return this.storageKeys.size > 0 ? FROZEN_EMPTY_COLLECTION : undefined;
379
+ }
370
380
  return snapshot;
371
381
  }
372
382
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-onyx",
3
- "version": "3.0.93",
3
+ "version": "3.0.94",
4
4
  "author": "Expensify, Inc.",
5
5
  "homepage": "https://expensify.com",
6
6
  "description": "State management for React Native",