From 2f25d9a709cf35ab6af6c781701f6bebef2fedeb Mon Sep 17 00:00:00 2001 From: Mike Hernas Date: Thu, 14 Mar 2024 21:55:38 +0100 Subject: [PATCH 1/4] Fix leaking listeners (cherry picked from commit 2fe52402a7076210fa48a37baba4b3f7f7521e0c) --- packages/realm-react/src/cachedCollection.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/packages/realm-react/src/cachedCollection.ts b/packages/realm-react/src/cachedCollection.ts index 49ecc456b8..3e9404802e 100644 --- a/packages/realm-react/src/cachedCollection.ts +++ b/packages/realm-react/src/cachedCollection.ts @@ -182,10 +182,12 @@ export function createCachedCollection>({ } }; + let wasSetImmediate = false; 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(() => { collection.addListener(listenerCallback, keyPaths); }); @@ -196,7 +198,13 @@ export function createCachedCollection>({ const tearDown = () => { if (!isDerived) { - collection.removeListener(listenerCallback); + if(wasSetImmediate) { + setImmediate(() => { + collection.removeListener(listenerCallback); + }); + } else { + collection.removeListener(listenerCallback); + } objectCache.clear(); } }; From 9177da6a9d79f520d7ac9e842cfc2f994d54177a Mon Sep 17 00:00:00 2001 From: Mike Hernas Date: Mon, 8 Apr 2024 10:29:49 +0200 Subject: [PATCH 2/4] Improvement --- packages/realm-react/src/cachedCollection.ts | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/packages/realm-react/src/cachedCollection.ts b/packages/realm-react/src/cachedCollection.ts index 3e9404802e..7027b8f5bb 100644 --- a/packages/realm-react/src/cachedCollection.ts +++ b/packages/realm-react/src/cachedCollection.ts @@ -182,13 +182,12 @@ export function createCachedCollection>({ } }; - let wasSetImmediate = false; + let setImmediateId: ReturnType | 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 { @@ -198,13 +197,10 @@ export function createCachedCollection>({ const tearDown = () => { if (!isDerived) { - if(wasSetImmediate) { - setImmediate(() => { - collection.removeListener(listenerCallback); - }); - } else { - collection.removeListener(listenerCallback); + if (setImmediateId) { + clearImmediate(setImmediateId); } + collection.removeListener(listenerCallback); objectCache.clear(); } }; From 4b49d74e5f90896ddec1e311826f1646899609af Mon Sep 17 00:00:00 2001 From: Mike Hernas Date: Mon, 17 Jun 2024 13:31:41 +0200 Subject: [PATCH 3/4] Clear setImmediateId + add runtime types for setImmediate function --- packages/realm-react/src/cachedCollection.ts | 1 + packages/realm-react/src/runtime.d.ts | 3 +++ 2 files changed, 4 insertions(+) diff --git a/packages/realm-react/src/cachedCollection.ts b/packages/realm-react/src/cachedCollection.ts index 7027b8f5bb..806be0071e 100644 --- a/packages/realm-react/src/cachedCollection.ts +++ b/packages/realm-react/src/cachedCollection.ts @@ -199,6 +199,7 @@ export function createCachedCollection>({ if (!isDerived) { if (setImmediateId) { clearImmediate(setImmediateId); + setImmediateId = undefined; } collection.removeListener(listenerCallback); objectCache.clear(); diff --git a/packages/realm-react/src/runtime.d.ts b/packages/realm-react/src/runtime.d.ts index fe8d1c0211..d566d30825 100644 --- a/packages/realm-react/src/runtime.d.ts +++ b/packages/realm-react/src/runtime.d.ts @@ -25,3 +25,6 @@ declare type Console = { declare const console: Console; declare function setImmediate(cb: (...args: unknown[]) => void); + +declare type Timer = unknown; +declare function clearImmediate(timer: Timer): void; From be8b588af09196ccfda72280c05f9560041671ff Mon Sep 17 00:00:00 2001 From: Michal Hernas Date: Mon, 17 Jun 2024 16:46:19 +0200 Subject: [PATCH 4/4] Update packages/realm-react/src/runtime.d.ts Co-authored-by: LJ <81748770+elle-j@users.noreply.github.com> --- packages/realm-react/src/runtime.d.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/realm-react/src/runtime.d.ts b/packages/realm-react/src/runtime.d.ts index d566d30825..70b5b96b61 100644 --- a/packages/realm-react/src/runtime.d.ts +++ b/packages/realm-react/src/runtime.d.ts @@ -28,3 +28,4 @@ declare function setImmediate(cb: (...args: unknown[]) => void); declare type Timer = unknown; declare function clearImmediate(timer: Timer): void; +