Skip to content

Commit

Permalink
⚡ cache the bounded function in sandbox
Browse files Browse the repository at this point in the history
  • Loading branch information
kuitos committed Aug 6, 2019
1 parent cd75a05 commit 31de427
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export function genSandbox(appName: string) {
let renderSandboxSnapshot: Map<PropertyKey, any> | null = null;
let inAppSandbox = true;

const boundValueSymbol = Symbol('bound value');
const sandbox = new Proxy(window, {

set(target: Window, p: PropertyKey, value: any): boolean {
Expand Down Expand Up @@ -101,9 +102,14 @@ export function genSandbox(appName: string) {
@warning 这里不要随意替换成别的判断方式,因为可能触发一些 edge case(比如在 lodash.isFunction 在 iframe 上下文中可能由于调用了 top window 对象触发的安全异常)
*/
if (typeof value === 'function' && !isConstructable(value)) {
if (value[boundValueSymbol]) {
return value[boundValueSymbol];
}

const boundValue = value.bind(target);
// some callable function has custom fields, we need to copy the enumerable props to boundValue. such as moment function.
Object.keys(value).forEach(key => boundValue[key] = value[key]);
Object.defineProperty(value, boundValueSymbol, { enumerable: false, value: boundValue });
return boundValue;
}

Expand Down

0 comments on commit 31de427

Please sign in to comment.