Skip to content

Commit 7a1144c

Browse files
author
Rishabh Karnad
committed
Added implementation of $destroy to Vue object and called it in componentWillUnmount of ReactVueComponent
Fixes #163
1 parent 5f1eb34 commit 7a1144c

File tree

6 files changed

+66
-12
lines changed

6 files changed

+66
-12
lines changed

src/core/instance/index.js

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,4 @@ eventsMixin(Vue)
4040
// lifecycleMixin(Vue)
4141
// renderMixin(Vue)
4242

43-
/**
44-
* react-vue change
45-
*/
46-
Vue.prototype.$nextTick = function (fn) {
47-
return nextTick(fn, this)
48-
}
49-
50-
Vue.prototype.$destroy = function (fn) {
51-
// nothing
52-
}
53-
5443
export default Vue

src/platforms/vue-native/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/** @flow */
22

3-
import Vue from 'core/index'
3+
import Vue from './runtime/index'
44
import observer from './observer'
55

66
Vue.observer = observer

src/platforms/vue-native/runtime/components/buildNativeComponent.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ export function buildNativeComponent (render, options, config) {
147147
}
148148
componentWillUnmount () {
149149
this.beforeDestroy.forEach(v => v.call(this.vm))
150+
this.vm.$destroy()
150151
}
151152
UNSAFE_componentWillReceiveProps (nextProps) {
152153
this.vm._props && Object.assign(this.vm._props, nextProps)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import Vue from 'core/index'
2+
3+
import { lifeCycleMixin } from './lifeCycle';
4+
import { renderMixin } from './render';
5+
6+
lifeCycleMixin(Vue)
7+
renderMixin(Vue)
8+
9+
export default Vue
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// @flow
2+
3+
import { remove } from 'core/util/index'
4+
5+
export function lifeCycleMixin (Vue: Class<Component>) {
6+
Vue.prototype.$destroy = function (fn: Function) {
7+
const vm: Component = this
8+
if (vm._isBeingDestroyed) {
9+
return
10+
}
11+
// callHook(vm, 'beforeDestroy')
12+
vm._isBeingDestroyed = true
13+
// remove self from parent
14+
const parent = vm.$parent
15+
if (parent && !parent._isBeingDestroyed && !vm.$options.abstract) {
16+
remove(parent.$children, vm)
17+
}
18+
// teardown watchers
19+
if (vm._watcher) {
20+
vm._watcher.teardown()
21+
}
22+
let i = vm._watchers.length
23+
while (i--) {
24+
vm._watchers[i].teardown()
25+
}
26+
// remove reference from data ob
27+
// frozen object may not have observer.
28+
if (vm._data.__ob__) {
29+
vm._data.__ob__.vmCount--
30+
}
31+
// call the last hook...
32+
vm._isDestroyed = true
33+
// invoke destroy hooks on current rendered tree
34+
// vm.__patch__(vm._vnode, null)
35+
// fire destroyed hook
36+
// callHook(vm, 'destroyed')
37+
// turn off all instance listeners.
38+
vm.$off()
39+
// remove __vue__ reference
40+
if (vm.$el) {
41+
vm.$el.__vue__ = null
42+
}
43+
// remove reference to DOM nodes (prevents leak)
44+
vm.$options._parentElm = vm.$options._refElm = null
45+
}
46+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// @flow
2+
3+
import { nextTick } from 'core/util/index'
4+
5+
export function renderMixin (Vue: Class<Component>) {
6+
Vue.prototype.$nextTick = function (fn: Function) {
7+
return nextTick(fn, this)
8+
}
9+
}

0 commit comments

Comments
 (0)