From af054da4e2431dc9f010db49593ff2b584299419 Mon Sep 17 00:00:00 2001 From: yiludege Date: Fri, 24 Mar 2023 11:51:25 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8DappendChild=E6=89=A7?= =?UTF-8?q?=E8=A1=8C=E9=A1=BA=E5=BA=8F=E7=9A=84=E9=97=AE=E9=A2=98=20(#470)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit close #465 --- packages/wujie-core/src/effect.ts | 45 ++++++++++++++++--------------- packages/wujie-core/src/utils.ts | 3 ++- 2 files changed, 26 insertions(+), 22 deletions(-) diff --git a/packages/wujie-core/src/effect.ts b/packages/wujie-core/src/effect.ts index 9f70920c4..7137b0ed8 100644 --- a/packages/wujie-core/src/effect.ts +++ b/packages/wujie-core/src/effect.ts @@ -161,6 +161,7 @@ function patchStylesheetElement( }); } +let dynamicScriptExecStack = Promise.resolve(); function rewriteAppendOrInsertChild(opts: { rawDOMAppendOrInsertBefore: (newChild: T, refChild?: Node | null) => T; wujieId: string; @@ -274,27 +275,29 @@ function rewriteAppendOrInsertChild(opts: { crossoriginType: crossOrigin || "", ignore: isMatchUrl(src, getEffectLoaders("jsIgnores", plugins)), } as ScriptObject; - getExternalScripts([scriptOptions], fetch, lifecycles.loadError, fiber).forEach((scriptResult) => - scriptResult.contentPromise.then( - (content) => { - if (sandbox.execQueue === null) return warn(WUJIE_TIPS_REPEAT_RENDER); - const execQueueLength = sandbox.execQueue?.length; - sandbox.execQueue.push(() => - fiber - ? requestIdleCallback(() => { - execScript({ ...scriptResult, content }); - }) - : execScript({ ...scriptResult, content }) - ); - // 同步脚本如果都执行完了,需要手动触发执行 - if (!execQueueLength) sandbox.execQueue.shift()(); - }, - () => { - manualInvokeElementEvent(element, "error"); - element = null; - } - ) - ); + getExternalScripts([scriptOptions], fetch, lifecycles.loadError, fiber).forEach((scriptResult) => { + dynamicScriptExecStack = dynamicScriptExecStack.then(() => + scriptResult.contentPromise.then( + (content) => { + if (sandbox.execQueue === null) return warn(WUJIE_TIPS_REPEAT_RENDER); + const execQueueLength = sandbox.execQueue?.length; + sandbox.execQueue.push(() => + fiber + ? requestIdleCallback(() => { + execScript({ ...scriptResult, content }); + }) + : execScript({ ...scriptResult, content }) + ); + // 同步脚本如果都执行完了,需要手动触发执行 + if (!execQueueLength) sandbox.execQueue.shift()(); + }, + () => { + manualInvokeElementEvent(element, "error"); + element = null; + } + ) + ); + }); } else { const execQueueLength = sandbox.execQueue?.length; sandbox.execQueue.push(() => diff --git a/packages/wujie-core/src/utils.ts b/packages/wujie-core/src/utils.ts index 62d4a2917..50bff72bf 100644 --- a/packages/wujie-core/src/utils.ts +++ b/packages/wujie-core/src/utils.ts @@ -290,9 +290,10 @@ export function isScriptElement(element: HTMLElement): boolean { return element.tagName?.toUpperCase() === "SCRIPT"; } +let count = 1; export function setTagToScript(element: HTMLScriptElement, tag?: string): void { if (isScriptElement(element)) { - const scriptTag = tag || `${new Date().valueOf()}`; + const scriptTag = tag || String(count++); element.setAttribute(WUJIE_SCRIPT_ID, scriptTag); } }