Skip to content

Commit

Permalink
🎨 rename hijackers to sandbox patchers (#344)
Browse files Browse the repository at this point in the history
  • Loading branch information
kuitos authored Mar 27, 2020
1 parent 4918ffa commit 085ba5b
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 30 deletions.
19 changes: 0 additions & 19 deletions src/hijackers/index.ts

This file was deleted.

6 changes: 3 additions & 3 deletions src/sandbox/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @author Kuitos
* @since 2019-04-11
*/
import { hijackAtBootstrapping, hijackAtMounting } from '../hijackers';
import { patchAtBootstrapping, patchAtMounting } from './patchers';
import { Freer, Rebuilder, SandBox } from '../interfaces';
import ProxySandbox from './proxySandbox';
import SnapshotSandbox from './snapshotSandbox';
Expand Down Expand Up @@ -36,7 +36,7 @@ export function genSandbox(appName: string) {
}

// 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(appName, sandbox.proxy);
const bootstrappingFreers = patchAtBootstrapping(appName, sandbox.proxy);

return {
sandbox: sandbox.proxy,
Expand All @@ -62,7 +62,7 @@ export function genSandbox(appName: string) {

/* ------------------------------------------ 2. 开启全局变量补丁 ------------------------------------------*/
// render 沙箱启动时开始劫持各类全局监听,尽量不要在应用初始化阶段有 事件监听/定时器 等副作用
mountingFreers = hijackAtMounting(appName, sandbox.proxy);
mountingFreers = patchAtMounting(appName, sandbox.proxy);

/* ------------------------------------------ 3. 重置一些初始化时的副作用 ------------------------------------------*/
// 存在 rebuilder 则表明有些副作用需要重建
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import { execScripts } from 'import-html-entry';
import { isFunction } from 'lodash';
import { checkActivityFunctions } from 'single-spa';
import { Freer } from '../interfaces';
import { getWrapperId } from '../utils';
import { Freer } from '../../interfaces';
import { getWrapperId } from '../../utils';

const styledComponentSymbol = Symbol('styled-component');

Expand Down Expand Up @@ -60,7 +60,7 @@ function getWrapperElement(appName: string) {
* @param proxy
* @param mounting
*/
export default function hijack(appName: string, proxy: Window, mounting = true): Freer {
export default function patch(appName: string, proxy: Window, mounting = true): Freer {
let dynamicStyleSheetElements: Array<HTMLLinkElement | HTMLStyleElement> = [];

HTMLHeadElement.prototype.appendChild = function appendChild<T extends Node>(this: HTMLHeadElement, newChild: T) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import { isFunction, noop } from 'lodash';

export default function hijack() {
export default function patch() {
// FIXME umi unmount feature request
// eslint-disable-next-line @typescript-eslint/no-unused-vars
let rawHistoryListen = (_: any) => noop;
Expand Down
19 changes: 19 additions & 0 deletions src/sandbox/patchers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* @author Kuitos
* @since 2019-04-11
*/

import { noop } from 'lodash';
import { Freer } from '../../interfaces';
import patchDynamicAppend from './dynamicHeadAppend';
import patchHistoryListener from './historyListener';
import patchTimer from './timer';
import patchWindowListener from './windowListener';

export function patchAtMounting(appName: string, proxy: Window): Freer[] {
return [patchTimer(), patchWindowListener(), patchHistoryListener(), patchDynamicAppend(appName, proxy)];
}

export function patchAtBootstrapping(appName: string, proxy: Window): Freer[] {
return [process.env.NODE_ENV === 'development' ? patchDynamicAppend(appName, proxy, false) : () => () => noop];
}
4 changes: 2 additions & 2 deletions src/hijackers/timer.ts → src/sandbox/patchers/timer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
*/

import { noop } from 'lodash';
import { sleep } from '../utils';
import { sleep } from '../../utils';

const rawWindowInterval = window.setInterval;
const rawWindowTimeout = window.setTimeout;

export default function hijack() {
export default function patch() {
const timerIds: number[] = [];
const intervalIds: number[] = [];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { noop } from 'lodash';
const rawAddEventListener = window.addEventListener;
const rawRemoveEventListener = window.removeEventListener;

export default function hijack() {
export default function patch() {
const listenerMap = new Map<string, EventListenerOrEventListenerObject[]>();

window.addEventListener = (
Expand Down
4 changes: 3 additions & 1 deletion src/sandbox/snapshotSandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import { SandBox } from '../interfaces';
function iter(obj: object, callbackFn: (prop: any) => void) {
// eslint-disable-next-line guard-for-in, no-restricted-syntax
for (const prop in obj) {
callbackFn(prop);
if (obj.hasOwnProperty(prop)) {
callbackFn(prop);
}
}
}

Expand Down

0 comments on commit 085ba5b

Please sign in to comment.