Skip to content

Commit

Permalink
Improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
bimusiek committed Apr 9, 2024
1 parent 0ce2be7 commit 73b1e51
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions packages/realm-react/src/cachedCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,13 +182,12 @@ export function createCachedCollection<T extends Realm.Object<any>>({
}
};

let wasSetImmediate = false;
let setImmediateId: ReturnType<typeof setImmediate> | undefined = undefined;
if (!isDerived) {
// If we are in a transaction, then push adding the listener to the event loop. This will allow the write transaction to finish.
// see https://github.com/realm/realm-js/issues/4375
if (realm.isInTransaction) {
wasSetImmediate = true;
setImmediate(() => {
setImmediateId = setImmediate(() => {
collection.addListener(listenerCallback, keyPaths);
});
} else {
Expand All @@ -198,13 +197,10 @@ export function createCachedCollection<T extends Realm.Object<any>>({

const tearDown = () => {
if (!isDerived) {
if(wasSetImmediate) {
setImmediate(() => {
collection.removeListener(listenerCallback);
});
} else {
collection.removeListener(listenerCallback);
if (setImmediateId) {
clearImmediate(setImmediateId);
}
collection.removeListener(listenerCallback);
objectCache.clear();
}
};
Expand Down

0 comments on commit 73b1e51

Please sign in to comment.