-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix GC problem #2797
base: master
Are you sure you want to change the base?
Fix GC problem #2797
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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)) { | ||
|
@@ -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 => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
item.cleanupDeps(); | ||
item.teardown(); | ||
}); | ||
this._watchers = []; | ||
this._events = {}; | ||
this._detached = true; | ||
delete this._data.__ob__; | ||
delete this._data; | ||
delete this._watcher; | ||
Comment on lines
+79
to
+81
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这几个应该都不用删。测了下,删这几个 |
||
delete this.$root; | ||
} | ||
} | ||
|
||
WepyComponent.prototype.$nextTick = renderNextTick; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
|
@@ -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 = []; | ||
|
@@ -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 | ||
|
@@ -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; | ||
|
@@ -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]))) { | ||
|
@@ -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]))) { | ||
|
@@ -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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个应该在 callUserMethod(vm, vm.$options, 'detached', args); 之后执行吧 |
||
return callUserMethod(vm, vm.$options, 'detached', args); | ||
} | ||
let lifecycle = getLifecycle(WEAPP_COMPONENT_LIFECYCLE, rel, 'component'); | ||
|
||
lifecycle.forEach(k => { | ||
// beforeCreate is not a real lifecycle | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个是不是没必要,组件自己也是会调用 detached 进行析构的