Skip to content

Commit

Permalink
fix: 修复appendChild执行顺序的问题 (#470)
Browse files Browse the repository at this point in the history
close #465
  • Loading branch information
yiludege authored Mar 24, 2023
1 parent a1a4b4a commit af054da
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 22 deletions.
45 changes: 24 additions & 21 deletions packages/wujie-core/src/effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ function patchStylesheetElement(
});
}

let dynamicScriptExecStack = Promise.resolve();
function rewriteAppendOrInsertChild(opts: {
rawDOMAppendOrInsertBefore: <T extends Node>(newChild: T, refChild?: Node | null) => T;
wujieId: string;
Expand Down Expand Up @@ -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(() =>
Expand Down
3 changes: 2 additions & 1 deletion packages/wujie-core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down

0 comments on commit af054da

Please sign in to comment.