-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
useObserver
registry refactor (#3598)
- Loading branch information
Showing
12 changed files
with
155 additions
and
349 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"mobx-react-lite": patch | ||
--- | ||
|
||
refactor reaction tracking |
15 changes: 7 additions & 8 deletions
15
packages/mobx-react-lite/__tests__/strictAndConcurrentModeUsingFinalizationRegistry.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 0 additions & 12 deletions
12
packages/mobx-react-lite/src/utils/FinalizationRegistryWrapper.ts
This file was deleted.
Oops, something went wrong.
62 changes: 62 additions & 0 deletions
62
packages/mobx-react-lite/src/utils/UniversalFinalizationRegistry.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
export declare class FinalizationRegistryType<T> { | ||
constructor(finalize: (value: T) => void) | ||
register(target: object, value: T, token?: object): void | ||
unregister(token: object): void | ||
} | ||
|
||
declare const FinalizationRegistry: typeof FinalizationRegistryType | undefined | ||
|
||
export const REGISTRY_FINALIZE_AFTER = 10_000 | ||
export const REGISTRY_SWEEP_INTERVAL = 10_000 | ||
|
||
export class TimerBasedFinalizationRegistry<T> implements FinalizationRegistryType<T> { | ||
private registrations: Map<unknown, { value: T; registeredAt: number }> = new Map() | ||
private sweepTimeout: ReturnType<typeof setTimeout> | undefined | ||
|
||
constructor(private readonly finalize: (value: T) => void) {} | ||
|
||
// Token is actually required with this impl | ||
register(target: object, value: T, token?: object) { | ||
this.registrations.set(token, { | ||
value, | ||
registeredAt: Date.now() | ||
}) | ||
this.scheduleSweep() | ||
} | ||
|
||
unregister(token: unknown) { | ||
this.registrations.delete(token) | ||
} | ||
|
||
// Bound so it can be used directly as setTimeout callback. | ||
sweep = (maxAge = REGISTRY_FINALIZE_AFTER) => { | ||
// cancel timeout so we can force sweep anytime | ||
clearTimeout(this.sweepTimeout) | ||
this.sweepTimeout = undefined | ||
|
||
const now = Date.now() | ||
this.registrations.forEach((registration, token) => { | ||
if (now - registration.registeredAt >= maxAge) { | ||
this.finalize(registration.value) | ||
this.registrations.delete(token) | ||
} | ||
}) | ||
|
||
if (this.registrations.size > 0) { | ||
this.scheduleSweep() | ||
} | ||
} | ||
|
||
// Bound so it can be exported directly as clearTimers test utility. | ||
finalizeAllImmediately = () => { | ||
this.sweep(0) | ||
} | ||
|
||
private scheduleSweep() { | ||
if (this.sweepTimeout === undefined) { | ||
this.sweepTimeout = setTimeout(this.sweep, REGISTRY_SWEEP_INTERVAL) | ||
} | ||
} | ||
} | ||
|
||
export const UniversalFinalizationRegistry = FinalizationRegistry ?? TimerBasedFinalizationRegistry |
Oops, something went wrong.