-
Notifications
You must be signed in to change notification settings - Fork 2k
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
✨ hijack the dynamic stylesheet injection during development phase #129
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
* @author Kuitos | ||
* @since 2019-04-11 | ||
*/ | ||
import { hijack } from './hijackers'; | ||
import { hijackAtBootstrapping, hijackAtMounting } from './hijackers'; | ||
import { Freer, Rebuilder } from './interfaces'; | ||
import { isConstructable } from './utils'; | ||
|
||
|
@@ -53,7 +53,11 @@ export function genSandbox(appName: string) { | |
// 持续记录更新的(新增和修改的)全局变量的 map,用于在任意时刻做 snapshot | ||
const currentUpdatedPropsValueMapForSnapshot = new Map<PropertyKey, any>(); | ||
|
||
let freers: Freer[] = []; | ||
// some side effect could be be invoked while bootstrapping, such as dynamic stylesheet injection with style-loader, especially during the development phase | ||
const bootstrappingFreers = hijackAtBootstrapping(); | ||
// mounting freers are one-off and should be re-init at every mounting time | ||
let mountingFreers: Freer[] = []; | ||
|
||
let sideEffectsRebuilders: Rebuilder[] = []; | ||
|
||
// render 沙箱的上下文快照 | ||
|
@@ -118,6 +122,14 @@ export function genSandbox(appName: string) { | |
* 也可能是从 unmount 之后再次唤醒进入 mount | ||
*/ | ||
async mount() { | ||
const sideEffectsRebuildersAtBootstrapping = sideEffectsRebuilders.slice(0, bootstrappingFreers.length); | ||
const sideEffectsRebuildersAtMounting = sideEffectsRebuilders.slice(bootstrappingFreers.length); | ||
|
||
// must rebuild the side effects which added at bootstrapping firstly to recovery to nature state | ||
if (sideEffectsRebuildersAtBootstrapping.length) { | ||
sideEffectsRebuildersAtBootstrapping.forEach(rebuild => rebuild()); | ||
} | ||
|
||
/* ------------------------------------------ 因为有上下文依赖(window),以下代码执行顺序不能变 ------------------------------------------ */ | ||
|
||
/* ------------------------------------------ 1. 启动/恢复 快照 ------------------------------------------ */ | ||
|
@@ -131,15 +143,17 @@ export function genSandbox(appName: string) { | |
|
||
/* ------------------------------------------ 2. 开启全局变量补丁 ------------------------------------------*/ | ||
// render 沙箱启动时开始劫持各类全局监听,这就要求应用初始化阶段不应该有 事件监听/定时器 等副作用 | ||
freers.push(...hijack()); | ||
mountingFreers = hijackAtMounting(); | ||
|
||
/* ------------------------------------------ 3. 重置一些初始化时的副作用 ------------------------------------------*/ | ||
// 存在 rebuilder 则表明有些副作用需要重建 | ||
if (sideEffectsRebuilders.length) { | ||
sideEffectsRebuilders.forEach(rebuild => rebuild()); | ||
sideEffectsRebuilders = []; | ||
if (sideEffectsRebuildersAtMounting.length) { | ||
sideEffectsRebuildersAtMounting.forEach(rebuild => rebuild()); | ||
} | ||
|
||
// clean up rebuilders | ||
sideEffectsRebuilders = []; | ||
|
||
inAppSandbox = true; | ||
}, | ||
|
||
|
@@ -155,8 +169,8 @@ export function genSandbox(appName: string) { | |
} | ||
|
||
// record the rebuilders of window side effects (event listeners or timers) | ||
freers.forEach(free => sideEffectsRebuilders.push(free())); | ||
freers = []; | ||
// note that the frees of mounting phase are one-off as it will be re-init at next mounting | ||
sideEffectsRebuilders = [...bootstrappingFreers, ...mountingFreers].map(free => free()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. freers ==> freeres ? |
||
|
||
// renderSandboxSnapshot = snapshot(currentUpdatedPropsValueMapForSnapshot); | ||
// restore global props to initial snapshot | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
为什么开发阶段需要 gc,其他阶段不需要?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
因为 bootstrapping 阶段拦截到的副作用会被多次 rebuild