-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support defer scripts and keep the executing order to consist w…
…ith browser (#2811)
- Loading branch information
Showing
7 changed files
with
108 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
"@qiankunjs/loader": patch | ||
"@qiankunjs/sandbox": patch | ||
"@qiankunjs/shared": patch | ||
--- | ||
|
||
feat: support defer scripts and keep the executing order to consist with browser |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { Deferred, waitUntilSettled } from '../utils'; | ||
|
||
export function prepareScriptForQueue( | ||
scriptQueue: HTMLScriptElement[], | ||
scriptDeferredWeakMap: WeakMap<HTMLScriptElement, Deferred<void>>, | ||
): { scriptDeferred: Deferred<void>; prevScriptDeferred?: Deferred<void>; queue: (script: HTMLScriptElement) => void } { | ||
const queueLength = scriptQueue.length; | ||
const prevScript = queueLength ? scriptQueue[scriptQueue.length - 1] : undefined; | ||
const prevScriptDeferred = prevScript ? scriptDeferredWeakMap.get(prevScript) : undefined; | ||
const scriptDeferred = new Deferred<void>(); | ||
|
||
return { | ||
scriptDeferred, | ||
prevScriptDeferred, | ||
queue: (script: HTMLScriptElement) => { | ||
// the script have no src attribute, indicating that the script needs to wait for the src to be filled | ||
if (!script.hasAttribute('src')) { | ||
scriptQueue.push(script); | ||
scriptDeferredWeakMap.set(script, scriptDeferred); | ||
|
||
// clear the memory regardless the script loaded or failed | ||
void waitUntilSettled(scriptDeferred.promise).then(() => { | ||
const scriptIndex = scriptQueue.indexOf(script); | ||
scriptQueue.splice(scriptIndex, 1); | ||
scriptDeferredWeakMap.delete(script); | ||
}); | ||
} | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters