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

Fix leaking listeners #6552

Merged
merged 4 commits into from
Jul 4, 2024
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
7 changes: 6 additions & 1 deletion packages/realm-react/src/cachedCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,12 @@ export function createCachedCollection<T extends Realm.Object<any>>({
}
};

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) {
setImmediate(() => {
setImmediateId = setImmediate(() => {
collection.addListener(listenerCallback, keyPaths);
});
} else {
Expand All @@ -196,6 +197,10 @@ export function createCachedCollection<T extends Realm.Object<any>>({

const tearDown = () => {
if (!isDerived) {
if (setImmediateId) {
clearImmediate(setImmediateId);
elle-j marked this conversation as resolved.
Show resolved Hide resolved
setImmediateId = undefined;
}
collection.removeListener(listenerCallback);
objectCache.clear();
}
Expand Down
4 changes: 4 additions & 0 deletions packages/realm-react/src/runtime.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,7 @@ declare type Console = {
declare const console: Console;

declare function setImmediate(cb: (...args: unknown[]) => void);

declare type Timer = unknown;
declare function clearImmediate(timer: Timer): void;
bimusiek marked this conversation as resolved.
Show resolved Hide resolved

Loading