Skip to content

Commit

Permalink
fix(core): fix GC problem
Browse files Browse the repository at this point in the history
during page navigation, for some reason the page and component instance is not released

re #2712
  • Loading branch information
Gcaufy committed May 7, 2022
1 parent 1c814b5 commit b0c5a92
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 10 deletions.
1 change: 1 addition & 0 deletions packages/core/weapp/class/Base.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export default class Base {
constructor() {
this._events = {};
this._watchers = [];
this.$children = [];
}

$set(target, key, val) {
Expand Down
29 changes: 29 additions & 0 deletions packages/core/weapp/class/WepyComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,16 @@ import Watcher from '../observer/watcher';
import { isArr, isPlainObject } from '../../shared/index';

import { renderNextTick } from '../util/next-tick';
import Dirty from './Dirty';

export default class WepyComponent extends Base {

constructor() {
super();
this.$children = [];
this.$dirty = new Dirty('path');
this.$refs = {};
}
$watch(expOrFn, cb, options) {
let vm = this;
if (isArr(cb)) {
Expand Down Expand Up @@ -52,6 +60,27 @@ export default class WepyComponent extends Base {
$trigger(event, data, option) {
this.$wx.triggerEvent(event, { arguments: [data] }, option);
}

$destroy() {
this.$children.forEach(child => {
if (!child._detached) {
child.$destroy();
}
});
this._watcher.cleanupDeps();
this._watcher.teardown();
this._watchers.forEach(item => {
item.cleanupDeps();
item.teardown();
});
this._watchers = [];
this._events = {};
this._detached = true;
delete this._data.__ob__;
delete this._data;
delete this._watcher;
delete this.$root;
}
}

WepyComponent.prototype.$nextTick = renderNextTick;
20 changes: 10 additions & 10 deletions packages/core/weapp/init/lifecycle.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { initData } from './data';
import { initComputed } from './computed';
import { initMethods } from './methods';
import { isArr, isFunc } from '../../shared/index';
import Dirty from '../class/Dirty';
import {
WEAPP_APP_LIFECYCLE,
WEAPP_PAGE_LIFECYCLE,
Expand All @@ -36,7 +35,7 @@ const callUserMethod = function(vm, userOpt, method, args) {
return result;
};

const getLifecycycle = (defaultLifecycle, rel, type) => {
const getLifecycle = (defaultLifecycle, rel, type) => {
let lifecycle = defaultLifecycle.concat([]);
if (rel && rel.lifecycle && rel.lifecycle[type]) {
let userDefinedLifecycle = [];
Expand Down Expand Up @@ -75,7 +74,7 @@ export function patchAppLifecycle(appConfig, options, rel = {}) {
return callUserMethod(vm, vm.$options, 'onLaunch', args);
};

let lifecycle = getLifecycycle(WEAPP_APP_LIFECYCLE, rel, 'app');
let lifecycle = getLifecycle(WEAPP_APP_LIFECYCLE, rel, 'app');

lifecycle.forEach(k => {
// it's not defined aready && user defined it && it's an array or function
Expand All @@ -92,10 +91,6 @@ export function patchLifecycle(output, options, rel, isComponent) {
const initLifecycle = function(...args) {
let vm = new initClass();

vm.$dirty = new Dirty('path');
vm.$children = [];
vm.$refs = {};

this.$wepy = vm;
vm.$wx = this;
vm.$is = this.is;
Expand Down Expand Up @@ -165,7 +160,7 @@ export function patchLifecycle(output, options, rel, isComponent) {

// 增加组件页面声明周期
output.pageLifetimes = {};
const lifecycle = getLifecycycle(WEAPP_COMPONENT_PAGE_LIFECYCLE, rel, 'component');
const lifecycle = getLifecycle(WEAPP_COMPONENT_PAGE_LIFECYCLE, rel, 'component');

lifecycle.forEach(function(k) {
if (!output.pageLifetimes[k] && options[k] && (isFunc(options[k]) || isArr(options[k]))) {
Expand Down Expand Up @@ -245,7 +240,7 @@ export function patchLifecycle(output, options, rel, isComponent) {
// }
// })

let lifecycle = getLifecycycle(WEAPP_PAGE_LIFECYCLE, rel, 'page');
let lifecycle = getLifecycle(WEAPP_PAGE_LIFECYCLE, rel, 'page');

lifecycle.forEach(k => {
if (!output[k] && options[k] && (isFunc(options[k]) || isArr(options[k]))) {
Expand All @@ -255,7 +250,12 @@ export function patchLifecycle(output, options, rel, isComponent) {
}
});
}
let lifecycle = getLifecycycle(WEAPP_COMPONENT_LIFECYCLE, rel, 'component');
output.detached = function(...args) {
let vm = this.$wepy;
vm.$destroy();
return callUserMethod(vm, vm.$options, 'detached', args);
}
let lifecycle = getLifecycle(WEAPP_COMPONENT_LIFECYCLE, rel, 'component');

lifecycle.forEach(k => {
// beforeCreate is not a real lifecycle
Expand Down

0 comments on commit b0c5a92

Please sign in to comment.