Skip to content

Commit e439ec4

Browse files
committed
fix(core): When application uninstall active release sandbox instance
1 parent 03d3acd commit e439ec4

File tree

5 files changed

+46
-21
lines changed

5 files changed

+46
-21
lines changed

dev/main/src/config.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,12 @@ let defaultConfig: interfaces.Options = {
2222
{
2323
name: 'vue',
2424
activeWhen: '/vue',
25-
// cache: true,
25+
cache: false,
2626
entry: 'http://localhost:2666',
2727
},
2828
{
2929
name: 'vue2',
30+
cache: false,
3031
activeWhen: '/vue2',
3132
entry: 'http://localhost:2777',
3233
},
@@ -61,6 +62,7 @@ let defaultConfig: interfaces.Options = {
6162
GarfishInstance.registerApp({
6263
name: 'react',
6364
activeWhen: '/react',
65+
cache: false,
6466
// basename: '/garfish_master',
6567
entry: 'http://localhost:2444',
6668
// domGetter: ()=>document.querySelector('#submoduleByRouter'),

packages/browser-vm/src/pluginify.ts

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { interfaces } from '@garfish/core';
22
import { warn, isPlainObject } from '@garfish/utils';
33
import { Module } from './types';
44
import { Sandbox } from './sandbox';
5+
import { sandboxMap } from './utils';
56

67
declare module '@garfish/core' {
78
export interface Garfish {
@@ -56,6 +57,7 @@ function rewriteAppAndSandbox(
5657
sandbox: Sandbox,
5758
) {
5859
const originExecScript = sandbox.execScript;
60+
sandboxMap.set(sandbox);
5961
// Rewrite sandbox attributes
6062
sandbox.loader = Garfish.loader;
6163
sandbox.execScript = (code, env, url, options) => {
@@ -135,6 +137,7 @@ function createOptions(Garfish: interfaces.Garfish) {
135137
afterUnmount(_, appInstance) {
136138
if (!appInstance.vmSandbox) return;
137139
appInstance.vmSandbox.reset();
140+
sandboxMap.del(appInstance.vmSandbox);
138141
},
139142

140143
afterMount(_, appInstance) {

packages/browser-vm/src/proxyInterceptor/document.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export function createGetter(sandbox: Sandbox) {
3333
return function (tagName, options) {
3434
const el = value.call(document, tagName, options);
3535
if (isObject(el)) {
36-
sandboxMap.set(el, sandbox);
36+
sandboxMap.setElementTag(el, sandbox);
3737
if (__DEV__) {
3838
el.__SANDBOX__ = true;
3939
}

packages/browser-vm/src/symbolTypes.ts

+1
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ export const __windowBind__ = Symbol.for('garfish.windowBind');
66
export const __sandboxMap__ = Symbol.for('garfish.sandboxMap');
77
export const __documentBind__ = Symbol.for('garfish.documentBind');
88
export const __garfishGlobal__ = Symbol.for('garfish.globalObject');
9+
export const __elementSandboxTag__ = Symbol.for('garfish.elementSandboxTag');

packages/browser-vm/src/utils.ts

+38-19
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
import { hasOwn, makeMap, nextTick } from '@garfish/utils';
22
import { Sandbox } from './sandbox';
33
import { FakeWindow } from './types';
4-
import { __proxyNode__, __sandboxMap__ } from './symbolTypes';
4+
import {
5+
__elementSandboxTag__,
6+
__proxyNode__,
7+
__sandboxMap__,
8+
} from './symbolTypes';
59

610
// https://tc39.es/ecma262/#sec-function-properties-of-the-global-object
7-
const esGlobalMethods = ( // Function properties of the global object // Function properties of the global object
11+
const esGlobalMethods = // Function properties of the global object // Function properties of the global object
12+
(
813
'eval,isFinite,isNaN,parseFloat,parseInt,' +
914
// URL handling functions
1015
'decodeURI,decodeURIComponent,encodeURI,encodeURIComponent,' +
@@ -25,23 +30,37 @@ export const optimizeMethods = [...esGlobalMethods].filter((v) => v !== 'eval');
2530

2631
// The sandbox may be used alone, to ensure that the `sandboxMap` is globally unique,
2732
// because we will only rewrite `appendChild` once
28-
export const sandboxMap = (() => {
29-
if (!(window as FakeWindow)[__sandboxMap__]) {
30-
(window as FakeWindow)[__sandboxMap__] = {
31-
deps: new WeakMap(),
32-
33-
get(element: Element): Sandbox {
34-
return this.deps.get(element);
35-
},
36-
37-
set(element: Element, sandbox: Sandbox) {
38-
if (this.deps.get(element)) return;
39-
this.deps.set(element, sandbox);
40-
},
41-
};
42-
}
43-
return (window as FakeWindow)[__sandboxMap__];
44-
})();
33+
let sandboxList: Map<number, Sandbox> = new Map();
34+
if (!(window as FakeWindow)[__sandboxMap__]) {
35+
(window as FakeWindow)[__sandboxMap__] = sandboxList;
36+
} else {
37+
sandboxList = (window as FakeWindow)[__sandboxMap__];
38+
}
39+
40+
export const sandboxMap = {
41+
sandboxMap: sandboxList,
42+
43+
get(element: Element): Sandbox {
44+
if (!element) return;
45+
const sandboxId = element[__elementSandboxTag__];
46+
if (typeof sandboxId !== 'number') return;
47+
return this.sandboxMap.get(sandboxId);
48+
},
49+
50+
setElementTag(element: Element, sandbox: Sandbox) {
51+
if (!element) return;
52+
element[__elementSandboxTag__] = sandbox.id;
53+
},
54+
55+
set(sandbox: Sandbox) {
56+
if (this.sandboxMap.get(sandbox.id)) return;
57+
this.sandboxMap.set(sandbox.id, sandbox);
58+
},
59+
60+
del(sandbox: Sandbox) {
61+
this.sandboxMap.delete(sandbox.id);
62+
},
63+
};
4564

4665
export function handlerParams(args: IArguments | Array<any>) {
4766
args = Array.isArray(args) ? args : Array.from(args);

0 commit comments

Comments
 (0)