From 35983ac0a82413be1594d786a37532d3819320fe Mon Sep 17 00:00:00 2001 From: Giteabot Date: Thu, 27 Mar 2025 00:13:22 +0800 Subject: [PATCH] Polyfill WeakRef (#34025) (#34028) Backport #34025 by wxiaoguang Fix #33407 Co-authored-by: wxiaoguang --- web_src/js/webcomponents/polyfill.test.ts | 7 +++++++ web_src/js/webcomponents/polyfills.ts | 16 ++++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 web_src/js/webcomponents/polyfill.test.ts diff --git a/web_src/js/webcomponents/polyfill.test.ts b/web_src/js/webcomponents/polyfill.test.ts new file mode 100644 index 0000000000..4fb4621547 --- /dev/null +++ b/web_src/js/webcomponents/polyfill.test.ts @@ -0,0 +1,7 @@ +import {weakRefClass} from './polyfills.ts'; + +test('polyfillWeakRef', () => { + const WeakRef = weakRefClass(); + const r = new WeakRef(123); + expect(r.deref()).toEqual(123); +}); diff --git a/web_src/js/webcomponents/polyfills.ts b/web_src/js/webcomponents/polyfills.ts index 4a84ee9562..9575324b5a 100644 --- a/web_src/js/webcomponents/polyfills.ts +++ b/web_src/js/webcomponents/polyfills.ts @@ -16,3 +16,19 @@ try { return intlNumberFormat(locales, options); }; } + +export function weakRefClass() { + const weakMap = new WeakMap(); + return class { + constructor(target: any) { + weakMap.set(this, target); + } + deref() { + return weakMap.get(this); + } + }; +} + +if (!window.WeakRef) { + window.WeakRef = weakRefClass() as any; +}