Skip to content

Commit bd1d8e5

Browse files
ikokostyasandersn
andauthored
Add definitions for WeakRef and FinalizationRegistry (#38232)
* Add definitions for WeakRef and FinalizationRegistry Fixes #32393 * Mark callback parameter in FinalizationRegistry#cleanupSome() as optional * Make FinalizationRegistry.prototype.cleanupSome optional * Remove FinalizationRegistry.prototype.cleanupSome() Co-authored-by: Nathan Shively-Sanders <[email protected]>
1 parent fc0eed3 commit bd1d8e5

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

src/lib/esnext.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
/// <reference lib="esnext.intl" />
33
/// <reference lib="esnext.string" />
44
/// <reference lib="esnext.promise" />
5+
/// <reference lib="esnext.weakref" />

src/lib/esnext.weakref.d.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
interface WeakRef<T extends object> {
2+
readonly [Symbol.toStringTag]: "WeakRef";
3+
4+
/**
5+
* Returns the WeakRef instance's target object, or undefined if the target object has been
6+
* reclaimed.
7+
*/
8+
deref(): T | undefined;
9+
}
10+
11+
interface WeakRefConstructor {
12+
readonly prototype: WeakRef<any>;
13+
14+
/**
15+
* Creates a WeakRef instance for the given target object.
16+
* @param target The target object for the WeakRef instance.
17+
*/
18+
new<T extends object>(target?: T): WeakRef<T>;
19+
}
20+
21+
declare var WeakRef: WeakRefConstructor;
22+
23+
interface FinalizationRegistry {
24+
readonly [Symbol.toStringTag]: "FinalizationRegistry";
25+
26+
/**
27+
* Registers an object with the registry.
28+
* @param target The target object to register.
29+
* @param heldValue The value to pass to the finalizer for this object. This cannot be the
30+
* target object.
31+
* @param unregisterToken The token to pass to the unregister method to unregister the target
32+
* object. If provided (and not undefined), this must be an object. If not provided, the target
33+
* cannot be unregistered.
34+
*/
35+
register(target: object, heldValue: any, unregisterToken?: object): void;
36+
37+
/**
38+
* Unregisters an object from the registry.
39+
* @param unregisterToken The token that was used as the unregisterToken argument when calling
40+
* register to register the target object.
41+
*/
42+
unregister(unregisterToken: object): void;
43+
}
44+
45+
interface FinalizationRegistryConstructor {
46+
readonly prototype: FinalizationRegistry;
47+
48+
/**
49+
* Creates a finalization registry with an associated cleanup callback
50+
* @param cleanupCallback The callback to call after an object in the registry has been reclaimed.
51+
*/
52+
new(cleanupCallback: (heldValue: any) => void): FinalizationRegistry;
53+
}
54+
55+
declare var FinalizationRegistry: FinalizationRegistryConstructor;

0 commit comments

Comments
 (0)