Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Fix: Regressions due to Onyx bump in E/App #455

Closed
Closed
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
67 changes: 43 additions & 24 deletions lib/Onyx.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ const METHOD = {
MERGE_COLLECTION: 'mergecollection',
MULTI_SET: 'multiset',
CLEAR: 'clear',
SCHEDULE_SUBSCRIBER_UPDATE: 'scheduleSubscriberUpdate',
SCHEDULE_NOTIFY_COLLECTION_SUBSCRIBERS: 'scheduleNotifyCollectionSubscribers',
};

const ON_CLEAR = 'on_clear';
Expand Down Expand Up @@ -947,6 +949,10 @@ function disconnect(connectionID, keyToRemoveFromEvictionBlocklist) {
* @returns {Promise}
*/
function scheduleSubscriberUpdate(key, value, canUpdateSubscriber) {
if (ActiveClientManager.isClientTheLeader()) {
Broadcast.sendMessage({type: METHOD.SCHEDULE_SUBSCRIBER_UPDATE, key, value});
}

const promise = Promise.resolve().then(() => keyChanged(key, value, canUpdateSubscriber, true, false));
batchUpdates(() => keyChanged(key, value, canUpdateSubscriber, false, true));
return Promise.all([maybeFlushBatchUpdates(), promise]);
Expand All @@ -962,6 +968,9 @@ function scheduleSubscriberUpdate(key, value, canUpdateSubscriber) {
* @returns {Promise}
*/
function scheduleNotifyCollectionSubscribers(key, value) {
if (ActiveClientManager.isClientTheLeader()) {
Broadcast.sendMessage({type: METHOD.SCHEDULE_NOTIFY_COLLECTION_SUBSCRIBERS, key, value});
}
const promise = Promise.resolve().then(() => keysChanged(key, value, true, false));
batchUpdates(() => keysChanged(key, value, false, true));
return Promise.all([maybeFlushBatchUpdates(), promise]);
Expand Down Expand Up @@ -1416,7 +1425,6 @@ function clear(keysToPreserve = []) {
.then(() => Storage.multiSet(defaultKeyValuePairs))
.then(() => {
isClearing = false;
Broadcast.sendMessage({type: METHOD.CLEAR, keysToPreserve});
DevTools.clearState(keysToPreserve);
return Promise.all(updatePromises);
});
Expand Down Expand Up @@ -1585,30 +1593,41 @@ function onClear(callback) {
*/
function subscribeToEvents() {
Broadcast.subscribe(({data}) => {
if (!ActiveClientManager.isClientTheLeader()) {
return;
}
switch (data.type) {
case METHOD.CLEAR:
clear(data.keysToPreserve);
break;
case METHOD.SET:
set(data.key, data.value);
break;
case METHOD.MULTI_SET:
multiSet(data.key, data.value);
break;
case METHOD.MERGE:
merge(data.key, data.changes);
break;
case ON_CLEAR:
if (!onClearCallback) {
if (ActiveClientManager.isClientTheLeader()) {
switch (data.type) {
case METHOD.CLEAR:
clear(data.keysToPreserve);
break;
}
onClearCallback();
break;
default:
break;
case METHOD.SET:
set(data.key, data.value);
break;
case METHOD.MULTI_SET:
multiSet(data.data);
break;
case METHOD.MERGE:
merge(data.key, data.changes);
break;

case ON_CLEAR:
if (!onClearCallback) {
break;
}
onClearCallback();
break;
default:
break;
}
} else {
switch (data.type) {
case METHOD.SCHEDULE_SUBSCRIBER_UPDATE:
scheduleSubscriberUpdate(data.key, data.value);
break;
case METHOD.SCHEDULE_NOTIFY_COLLECTION_SUBSCRIBERS:
scheduleNotifyCollectionSubscribers(data.key, data.value);
break;
default:
break;
}
}
});
}
Expand Down
Loading