Skip to content

Commit

Permalink
memory leak issue (umijs#715)
Browse files Browse the repository at this point in the history
* memory leak issue

* fix memory leak

* use noop

* Update src/sandbox/patchers/dynamicAppend.ts

Co-authored-by: Kuitos <kuitos.lau@gmail.com>
  • Loading branch information
lkiarest and kuitos committed Jul 4, 2020
1 parent 99dd475 commit da03ce3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
5 changes: 4 additions & 1 deletion src/sandbox/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ export function getProxyPropertyGetter(proxy: WindowProxy, property: PropertyKey
return undefined;
}

export function setProxyPropertyGetter(proxy: WindowProxy, property: PropertyKey, getter: () => any) {
export function setProxyPropertyGetter(proxy: WindowProxy, property: PropertyKey, getter: () => any): Function {
const prevGetters = proxyPropertyGetterMap.get(proxy) || {};
proxyPropertyGetterMap.set(proxy, { ...prevGetters, [property]: getter });
return function deleteProxyPropertyGetter() {
proxyPropertyGetterMap.delete(proxy);
};
}
7 changes: 5 additions & 2 deletions src/sandbox/patchers/dynamicAppend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @since 2019-10-21
*/
import { execScripts } from 'import-html-entry';
import { isFunction } from 'lodash';
import { isFunction, noop } from 'lodash';
import { checkActivityFunctions } from 'single-spa';
import { frameworkConfiguration } from '../../apis';
import { Freer } from '../../interfaces';
Expand Down Expand Up @@ -321,9 +321,10 @@ export default function patch(
scopedCSS = false,
): Freer {
let dynamicStyleSheetElements: Array<HTMLLinkElement | HTMLStyleElement> = [];
let deleteProxyPropertyGetter: Function = noop;

if (!singular) {
setProxyPropertyGetter(proxy, 'document', () => {
deleteProxyPropertyGetter = setProxyPropertyGetter(proxy, 'document', () => {
return new Proxy(document, {
get(target: Document, property: PropertyKey): any {
if (property === 'createElement') {
Expand Down Expand Up @@ -442,6 +443,8 @@ export default function patch(
// the dynamic style sheet would be removed automatically while unmoutting
});

deleteProxyPropertyGetter();

return function rebuild() {
dynamicStyleSheetElements.forEach(stylesheetElement => {
// re-append the dynamic stylesheet to sub-app container
Expand Down

0 comments on commit da03ce3

Please sign in to comment.