Skip to content

Commit

Permalink
Added implementation of $destroy to Vue object and called it in compo…
Browse files Browse the repository at this point in the history
…nentWillUnmount of ReactVueComponent

Fixes #163
  • Loading branch information
Rishabh Karnad committed Aug 20, 2019
1 parent 5f1eb34 commit 7a1144c
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 12 deletions.
11 changes: 0 additions & 11 deletions src/core/instance/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,4 @@ eventsMixin(Vue)
// lifecycleMixin(Vue)
// renderMixin(Vue)

/**
* react-vue change
*/
Vue.prototype.$nextTick = function (fn) {
return nextTick(fn, this)
}

Vue.prototype.$destroy = function (fn) {
// nothing
}

export default Vue
2 changes: 1 addition & 1 deletion src/platforms/vue-native/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/** @flow */

import Vue from 'core/index'
import Vue from './runtime/index'
import observer from './observer'

Vue.observer = observer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ export function buildNativeComponent (render, options, config) {
}
componentWillUnmount () {
this.beforeDestroy.forEach(v => v.call(this.vm))
this.vm.$destroy()
}
UNSAFE_componentWillReceiveProps (nextProps) {
this.vm._props && Object.assign(this.vm._props, nextProps)
Expand Down
9 changes: 9 additions & 0 deletions src/platforms/vue-native/runtime/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Vue from 'core/index'

import { lifeCycleMixin } from './lifeCycle';
import { renderMixin } from './render';

lifeCycleMixin(Vue)
renderMixin(Vue)

export default Vue
46 changes: 46 additions & 0 deletions src/platforms/vue-native/runtime/lifeCycle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// @flow

import { remove } from 'core/util/index'

export function lifeCycleMixin (Vue: Class<Component>) {
Vue.prototype.$destroy = function (fn: Function) {
const vm: Component = this
if (vm._isBeingDestroyed) {
return
}
// callHook(vm, 'beforeDestroy')
vm._isBeingDestroyed = true
// remove self from parent
const parent = vm.$parent
if (parent && !parent._isBeingDestroyed && !vm.$options.abstract) {
remove(parent.$children, vm)
}
// teardown watchers
if (vm._watcher) {
vm._watcher.teardown()
}
let i = vm._watchers.length
while (i--) {
vm._watchers[i].teardown()
}
// remove reference from data ob
// frozen object may not have observer.
if (vm._data.__ob__) {
vm._data.__ob__.vmCount--
}
// call the last hook...
vm._isDestroyed = true
// invoke destroy hooks on current rendered tree
// vm.__patch__(vm._vnode, null)
// fire destroyed hook
// callHook(vm, 'destroyed')
// turn off all instance listeners.
vm.$off()
// remove __vue__ reference
if (vm.$el) {
vm.$el.__vue__ = null
}
// remove reference to DOM nodes (prevents leak)
vm.$options._parentElm = vm.$options._refElm = null
}
}
9 changes: 9 additions & 0 deletions src/platforms/vue-native/runtime/render.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// @flow

import { nextTick } from 'core/util/index'

export function renderMixin (Vue: Class<Component>) {
Vue.prototype.$nextTick = function (fn: Function) {
return nextTick(fn, this)
}
}

0 comments on commit 7a1144c

Please sign in to comment.