Skip to content

Commit 4d8fee9

Browse files
committed
feat: add vmSandbox stared and closed hooks
1 parent 0250746 commit 4d8fee9

File tree

3 files changed

+10
-4
lines changed

3 files changed

+10
-4
lines changed

packages/runtime/browser-vm/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ const sandbox = new vmSandbox({
4646
});
4747

4848
sandbox.hooks.usePlugin({
49+
stared(fakeWindow) {},
50+
closed() {},
4951
beforeClearEffect() {},
5052
afterClearEffect() {},
5153
beforeInvoke(url, env, options) {},

packages/runtime/browser-vm/src/lifecycle.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import { SyncHook, PluginSystem } from '@garfish/hooks';
2-
import { ExecScriptOptions } from './types';
2+
import { FakeWindow, ExecScriptOptions } from './types';
33

44
export function sandboxLifecycle() {
55
return new PluginSystem({
6+
closed: new SyncHook<[], void>(),
7+
stared: new SyncHook<[FakeWindow], void>(),
8+
appendNode: new SyncHook<[Element, Element, Element, string], void>(),
69
beforeClearEffect: new SyncHook<[], void>(),
710
afterClearEffect: new SyncHook<[], void>(),
8-
appendNode: new SyncHook<[Element, Element, Element, string], void>(),
911
beforeInvoke: new SyncHook<
1012
[string, Record<string, any>, ExecScriptOptions],
1113
void

packages/runtime/browser-vm/src/sandbox.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,13 @@ const addProxyWindowType = (module: Window, parentModule: Window) => {
6666
};
6767

6868
export class Sandbox {
69-
public version = __VERSION__;
7069
public id = id++;
7170
public type = 'vm';
7271
public closed = true;
73-
public loader: Loader;
7472
public initComplete = false;
73+
public version = __VERSION__;
7574
public global?: Window;
75+
public loader: Loader;
7676
public options: SandboxOptions;
7777
public hooks = sandboxLifecycle();
7878
public replaceGlobalVariables: ReplaceGlobalVariables;
@@ -138,6 +138,7 @@ export class Sandbox {
138138
this.optimizeCode = this.optimizeGlobalMethod();
139139
}
140140
this.initComplete = true;
141+
this.hooks.lifecycle.stared.emit(this.global);
141142
}
142143

143144
close() {
@@ -153,6 +154,7 @@ export class Sandbox {
153154
this.replaceGlobalVariables.prepareList = [];
154155
this.replaceGlobalVariables.recoverList = [];
155156
this.replaceGlobalVariables.overrideList = [];
157+
this.hooks.lifecycle.closed.emit();
156158
}
157159

158160
reset() {

0 commit comments

Comments
 (0)