From a286703fcb502621a5cc5c853a362e97acd4c4ca Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Wed, 26 Mar 2025 19:22:15 +0800 Subject: [PATCH] fix --- 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 0000000000000..4fb4621547ed5 --- /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 4a84ee9562b93..9575324b5abf1 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; +}