From 9f5401a5b0e79b18a1a8d335b085c03382cdd5de Mon Sep 17 00:00:00 2001 From: Miroslav Jancarik Date: Fri, 10 May 2024 14:52:44 +0200 Subject: [PATCH] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20allow=20async=20getSingl?= =?UTF-8?q?eton=20function?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../integration-custom-element/src/index.js | 26 ++++++++++++------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/packages/integration-custom-element/src/index.js b/packages/integration-custom-element/src/index.js index 9c9b717e..bcb29c81 100644 --- a/packages/integration-custom-element/src/index.js +++ b/packages/integration-custom-element/src/index.js @@ -4,7 +4,7 @@ import { loadAssets } from '@merkur/integration'; async function createSPAWidget(widgetDefinition) { const definition = { ...widgetDefinition, - createWidget: widgetDefinition.createWidget || createMerkurWidget, + createWidget: widgetDefinition.createWidget || createMerkurWidget, // TODO remove createMerkurWidget keep only one way }; getMerkur().register(definition); @@ -37,18 +37,20 @@ function registerCustomElement(options) { constructor() { super(); - const widget = callbacks?.getSingleton?.(); + (async () => { + const shadow = this.attachShadow({ mode: 'open' }); - if (widget && widget.name && widget.version) { - this._widget = widget; + // TODO allow same UI for two custom element + const widget = await callbacks?.getSingleton?.(); - return; - } + if (widget && widget.name && widget.version) { + this._widget = widget; - const shadow = this.attachShadow({ mode: 'open' }); + return; + } - (async () => { try { + // TODO widget root remove widgetDefinition.root = shadow; widgetDefinition.customElement = this; @@ -60,9 +62,10 @@ function registerCustomElement(options) { widgetDefinition.root.appendChild(widgetDefinition.container); this._widget = await createSPAWidget(widgetDefinition); - this._widget.mount(); callbacks?.constructor?.(this._widget); + + await this._widget.mount(); } catch (error) { console.error(error); } @@ -96,6 +99,7 @@ function registerCustomElement(options) { } } +const PROTECTED_FIELDS = ['__proto__', 'prototype', 'constructor']; function deepMerge(target, source) { const isObject = (obj) => !!obj && obj.constructor === Object; @@ -104,6 +108,10 @@ function deepMerge(target, source) { } Object.keys(source).forEach((key) => { + if (PROTECTED_FIELDS.includes(key)) { + return; + } + const targetValue = target[key]; const sourceValue = source[key];