Skip to content

Commit

Permalink
fix FinalizationRegistry implementation through WeakRef
Browse files Browse the repository at this point in the history
  • Loading branch information
cray0000 committed Sep 13, 2024
1 parent c6f36be commit f47b67d
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions packages/teamplay/utils/MockFinalizationRegistry.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export const REGISTRY_SWEEP_INTERVAL = 10_000
export const MOCK_FINALIZATION_TIMEOUT = 10_000
const PERMANENT = false

// This is a mock implementation of FinalizationRegistry which doesn't actually
Expand Down Expand Up @@ -29,6 +30,7 @@ export class PermanentFinalizationRegistry {
// track the target objects. It's used in environments where FinalizationRegistry
// is not available but WeakRef is (e.g. React Native >=0.75 on New Architecture).
export class WeakRefBasedFinalizationRegistry {
counter = 0
registrations = new Map()
sweepTimeout

Expand All @@ -38,26 +40,32 @@ export class WeakRefBasedFinalizationRegistry {

// Token is actually required with this impl
register (target, value, token) {
this.registrations.set(token, {
this.registrations.set(this.counter, {
targetRef: new WeakRef(target),
tokenRef: token && new WeakRef(token),
value
})
this.counter++
this.scheduleSweep()
}

unregister (token) {
this.registrations.delete(token)
this.registrations.forEach((registration, key) => {
if (registration?.tokenRef.deref() !== token) return
this.registrations.delete(key)
})
}

// Bound so it can be used directly as setTimeout callback.
sweep = () => {
clearTimeout(this.sweepTimeout)
this.sweepTimeout = undefined

this.registrations.forEach((registration, token) => {
this.registrations.forEach((registration, key) => {
if (registration.targetRef.deref() !== undefined) return
this.finalize(registration.value)
this.registrations.delete(token)
const value = registration.value
this.registrations.delete(key)
setTimeout(() => this.finalize(value), MOCK_FINALIZATION_TIMEOUT)
})

if (this.registrations.size > 0) this.scheduleSweep()
Expand Down

0 comments on commit f47b67d

Please sign in to comment.