Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 修复appendChild执行顺序的问题 #470

Merged
merged 1 commit into from
Mar 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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