Skip to content

Commit d70be9d

Browse files
authored
Polyfill WeakRef (#34025)
Fix #33407
1 parent d28a7f9 commit d70be9d

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import {weakRefClass} from './polyfills.ts';
2+
3+
test('polyfillWeakRef', () => {
4+
const WeakRef = weakRefClass();
5+
const r = new WeakRef(123);
6+
expect(r.deref()).toEqual(123);
7+
});

web_src/js/webcomponents/polyfills.ts

+16
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,19 @@ try {
1616
return intlNumberFormat(locales, options);
1717
};
1818
}
19+
20+
export function weakRefClass() {
21+
const weakMap = new WeakMap();
22+
return class {
23+
constructor(target: any) {
24+
weakMap.set(this, target);
25+
}
26+
deref() {
27+
return weakMap.get(this);
28+
}
29+
};
30+
}
31+
32+
if (!window.WeakRef) {
33+
window.WeakRef = weakRefClass() as any;
34+
}

0 commit comments

Comments
 (0)