From d70be9d0fe648a7b593c4df46ac61e18d4166d07 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Wed, 26 Mar 2025 23:56:25 +0800 Subject: [PATCH] Polyfill WeakRef (#34025) Fix #33407 --- 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; +}