Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 31 additions & 10 deletions lib/Onyx.js
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,10 @@ function getCachedCollection(collectionKey) {
* @private
* @param {String} collectionKey
* @param {Object} partialCollection - a partial collection of grouped member keys
* @param {boolean} [notifyRegularSubscibers=true]
* @param {boolean} [notifyWithOnyxSubscibers=true]
*/
function keysChanged(collectionKey, partialCollection) {
function keysChanged(collectionKey, partialCollection, notifyRegularSubscibers = true, notifyWithOnyxSubscibers = true) {
// We are iterating over all subscribers similar to keyChanged(). However, we are looking for subscribers who are subscribing to either a collection key or
// individual collection key member for the collection that is being updated. It is important to note that the collection parameter cane be a PARTIAL collection
// and does not represent all of the combined keys and values for a collection key. It is just the "new" data that was merged in via mergeCollection().
Expand Down Expand Up @@ -368,6 +370,10 @@ function keysChanged(collectionKey, partialCollection) {

// Regular Onyx.connect() subscriber found.
if (_.isFunction(subscriber.callback)) {
if (!notifyRegularSubscibers) {
continue;
}

// If they are subscribed to the collection key and using waitForCollectionCallback then we'll
// send the whole cached collection.
if (isSubscribedToCollectionKey) {
Expand Down Expand Up @@ -398,6 +404,10 @@ function keysChanged(collectionKey, partialCollection) {

// React component subscriber found.
if (subscriber.withOnyxInstance) {
if (!notifyWithOnyxSubscibers) {
continue;
}

// We are subscribed to a collection key so we must update the data in state with the new
// collection member key values from the partial update.
if (isSubscribedToCollectionKey) {
Expand Down Expand Up @@ -489,8 +499,10 @@ function keysChanged(collectionKey, partialCollection) {
* @param {String} key
* @param {*} data
* @param {Function} [canUpdateSubscriber] only subscribers that pass this truth test will be updated
* @param {boolean} [notifyRegularSubscibers=true]
* @param {boolean} [notifyWithOnyxSubscibers=true]
*/
function keyChanged(key, data, canUpdateSubscriber) {
function keyChanged(key, data, canUpdateSubscriber, notifyRegularSubscibers = true, notifyWithOnyxSubscibers = true) {
// Add or remove this key from the recentlyAccessedKeys lists
if (!_.isNull(data)) {
addLastAccessedKey(key);
Expand All @@ -510,6 +522,9 @@ function keyChanged(key, data, canUpdateSubscriber) {

// Subscriber is a regular call to connect() and provided a callback
if (_.isFunction(subscriber.callback)) {
if (!notifyRegularSubscibers) {
continue;
}
if (isCollectionKey(subscriber.key) && subscriber.waitForCollectionCallback) {
const cachedCollection = getCachedCollection(subscriber.key);
cachedCollection[key] = data;
Expand All @@ -523,6 +538,10 @@ function keyChanged(key, data, canUpdateSubscriber) {

// Subscriber connected via withOnyx() HOC
if (subscriber.withOnyxInstance) {
if (!notifyWithOnyxSubscibers) {
continue;
}

// Check if we are subscribing to a collection key and overwrite the collection member key value in state
if (isCollectionKey(subscriber.key)) {
// If the subscriber has a selector, then the consumer of this data must only be given the data
Expand Down Expand Up @@ -805,10 +824,10 @@ function disconnect(connectionID, keyToRemoveFromEvictionBlocklist) {

let scheduledPromise;

// [key, value, canUpdateSubscriber]
// [key, value, canUpdateSubscriber, notifyRegularSubscribers, notifyWithOnyxSubscribers]
let pendingSubscriberUpdates = [];

// [key, value]
// [key, value, notifyRegularSubscribers, notifyWithOnyxSubscribers]
let pendingCollectionSubscriberUpdates = [];

/**
Expand Down Expand Up @@ -836,10 +855,10 @@ function scheduleSubscriberUpdatesFlushIfNeeded() {
pendingCollectionSubscriberUpdates = [];
unstable_batchedUpdates(() => {
for (let i = 0; i < updatesCopy.length; ++i) {
keyChanged(updatesCopy[i][0], updatesCopy[i][1], updatesCopy[i][2]);
keyChanged(updatesCopy[i][0], updatesCopy[i][1], updatesCopy[i][2], updatesCopy[i][3], updatesCopy[i][4]);
}
for (let i = 0; i < updatesCollectionCopy.length; ++i) {
keysChanged(updatesCollectionCopy[i][0], updatesCollectionCopy[i][1]);
keysChanged(updatesCollectionCopy[i][0], updatesCollectionCopy[i][1], updatesCollectionCopy[i][2], updatesCollectionCopy[i][3]);
}
});
resolve();
Expand All @@ -860,8 +879,9 @@ function scheduleSubscriberUpdatesFlushIfNeeded() {
* @returns {Promise}
*/
function scheduleSubscriberUpdate(key, value, canUpdateSubscriber) {
pendingSubscriberUpdates.push([key, value, canUpdateSubscriber]);
return scheduleSubscriberUpdatesFlushIfNeeded();
const promise = Promise.resolve().then(() => keyChanged(key, value, canUpdateSubscriber, true, false));
pendingSubscriberUpdates.push([key, value, canUpdateSubscriber, false, true]);
return Promise.all([scheduleSubscriberUpdatesFlushIfNeeded(), promise]);
}

/**
Expand All @@ -874,8 +894,9 @@ function scheduleSubscriberUpdate(key, value, canUpdateSubscriber) {
* @returns {Promise}
*/
function scheduleNotifyCollectionSubscribers(key, value) {
pendingCollectionSubscriberUpdates.push([key, value]);
return scheduleSubscriberUpdatesFlushIfNeeded();
const promise = Promise.resolve().then(() => keysChanged(key, value, true, false));
pendingCollectionSubscriberUpdates.push([key, value, false, true]);
return Promise.all([scheduleSubscriberUpdatesFlushIfNeeded(), promise]);
}

/**
Expand Down
8 changes: 3 additions & 5 deletions tests/unit/onyxTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -800,11 +800,9 @@ describe('Onyx', () => {
{onyxMethod: Onyx.METHOD.MERGE_COLLECTION, key: ONYX_KEYS.COLLECTION.TEST_UPDATE, value: {[itemKey]: {a: 'a'}}},
])
.then(() => {
// We are connecting before we update (set) the onyx data, hence the callback will be
// invoked with the data on the second call:
expect(collectionCallback).toHaveBeenNthCalledWith(2, {[itemKey]: {a: 'a'}});
expect(testCallback).toHaveBeenNthCalledWith(2, 'taco', ONYX_KEYS.TEST_KEY);
expect(otherTestCallback).toHaveBeenNthCalledWith(2, 'pizza', ONYX_KEYS.OTHER_TEST);
expect(collectionCallback).toHaveBeenNthCalledWith(1, {[itemKey]: {a: 'a'}});
expect(testCallback).toHaveBeenNthCalledWith(1, 'taco', ONYX_KEYS.TEST_KEY);
expect(otherTestCallback).toHaveBeenNthCalledWith(1, 'pizza', ONYX_KEYS.OTHER_TEST);
Onyx.disconnect(connectionIDs);
});
});
Expand Down