Skip to content

Commit 9ac744e

Browse files
committed
refactor: clean up application start
1 parent a95363d commit 9ac744e

File tree

5 files changed

+70
-130
lines changed

5 files changed

+70
-130
lines changed

platform/nativescript/framework.js

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,20 @@ console.keys = function(object) {
4444
// this fixes the issue of resuming the application
4545
// however this might not be the desired functionality
4646
// Todo: figure out if there is a better way to fix application resume.
47-
application.on(application.exitEvent, () => {
48-
const frame = topmost()
49-
if (frame) {
50-
frame.eachChildView(child => {
51-
const vm = child[VUE_VM_REF]
52-
53-
if (vm) {
54-
vm.$destroy()
55-
}
56-
frame._removeView(child)
57-
})
58-
}
59-
})
47+
// application.on(application.exitEvent, () => {
48+
// const frame = topmost()
49+
// if (frame) {
50+
// frame.eachChildView(child => {
51+
// const vm = child[VUE_VM_REF]
52+
//
53+
// if (vm) {
54+
// console.log('DESTROYING ON APPEXITEVENT...')
55+
// vm.$destroy()
56+
// }
57+
// frame._removeView(child)
58+
// })
59+
// }
60+
// })
6061

6162
global.__onLiveSyncCore = () => {
6263
const frame = topmost()

platform/nativescript/plugins/modal-plugin.js

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { isPage } from '../util/index'
2-
import { Page } from 'tns-core-modules/ui/page'
1+
import { ensurePage } from '../util'
32

43
export default {
54
install(Vue) {
@@ -8,28 +7,19 @@ export default {
87
options = { context: null, fullscreen: false }
98
) {
109
return new Promise(resolve => {
11-
const placeholder = this.$document.createComment('placeholder')
12-
1310
const contentComponent = Vue.extend(component)
1411
const vm = new contentComponent(options.context)
1512

13+
vm.$mount()
14+
const modalPage = ensurePage(vm.$el, vm)
15+
1616
contentComponent.prototype.$modal = {
1717
close(data) {
1818
resolve(data)
1919
modalPage.closeModal()
20-
setTimeout(() => {
21-
vm.$destroy()
22-
})
2320
}
2421
}
2522

26-
vm.$mount(placeholder)
27-
const modalPage = isPage(vm.$el) ? vm.$el.nativeView : new Page()
28-
29-
if (!isPage(vm.$el)) {
30-
modalPage.content = vm.$el.nativeView
31-
}
32-
3323
this.$root.$el.nativeView.showModal(
3424
modalPage,
3525
null,

platform/nativescript/plugins/navigator-plugin.js

Lines changed: 11 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
import { VUE_VM_REF } from '../runtime/index'
2-
import { isPage } from '../util/index'
3-
import { start } from 'tns-core-modules/application'
41
import { topmost } from 'tns-core-modules/ui/frame'
5-
import { after } from '../util'
6-
import { Page } from 'tns-core-modules/ui/page'
2+
import { ensurePage } from '../util'
73

84
export default {
95
install(Vue) {
@@ -17,59 +13,21 @@ export default {
1713
pageCb = () => {}
1814
) {
1915
return new Promise(resolve => {
20-
const frame = topmost()
21-
const navigate = frame ? frame.navigate : start
16+
const navigationEntry = {
17+
create() {
18+
const vm = new (Vue.extend(component))(options)
2219

23-
if (isPage(component)) {
24-
return navigate(
25-
Object.assign(options, {
26-
create() {
27-
return component
28-
}
29-
})
30-
)
31-
}
32-
33-
const placeholder = Vue.$document.createComment('placeholder')
34-
35-
let vm = component
36-
if (!component.__is_root__) {
37-
const contentComponent = Vue.extend(component)
38-
vm = new contentComponent(options.context)
39-
vm.$mount(placeholder)
40-
}
20+
vm.$mount()
21+
const pageInstance = ensurePage(vm.$el, vm)
4122

42-
const toPage = isPage(vm.$el) ? vm.$el.nativeView : new Page()
23+
pageCb(pageInstance)
24+
resolve(pageInstance)
4325

44-
if (!isPage(vm.$el)) {
45-
toPage.content = vm.$el.nativeView
26+
return pageInstance
27+
}
4628
}
4729

48-
toPage[VUE_VM_REF] = vm
49-
50-
navigate.call(
51-
frame,
52-
Object.assign(
53-
{
54-
create: () => {
55-
if (frame) {
56-
toPage.disposeNativeView = after(
57-
toPage.disposeNativeView,
58-
toPage,
59-
() => {
60-
vm.$destroy()
61-
}
62-
)
63-
}
64-
65-
pageCb(toPage)
66-
resolve(toPage)
67-
return toPage
68-
}
69-
},
70-
options
71-
)
72-
)
30+
topmost().navigate(Object.assign({}, navigationEntry, options))
7331
})
7432
}
7533
}

platform/nativescript/runtime/index.js

Lines changed: 18 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@ import { patch } from './patch'
44
import { mountComponent } from 'core/instance/lifecycle'
55
import { compileToFunctions } from '../compiler/index'
66
import { registerElement } from '../element-registry'
7-
import { isPage } from '../util/index'
8-
import { Page } from 'tns-core-modules/ui/page'
97

108
import Vue from 'core/index'
119
import DocumentNode from '../renderer/DocumentNode'
1210
import platformComponents from './components/index'
1311
import platformDirectives from './directives/index'
1412

1513
import { mustUseProp, isReservedTag, isUnknownElement } from '../util/index'
14+
import { ensurePage } from '../util'
1615

1716
export const VUE_VM_REF = '__vue_vm_ref__'
1817

@@ -22,47 +21,14 @@ Vue.config.isUnknownElement = isUnknownElement
2221

2322
Vue.$document = Vue.prototype.$document = new DocumentNode()
2423

24+
Vue.compile = compileToFunctions
2525
Vue.registerElement = registerElement
2626

2727
Object.assign(Vue.options.directives, platformDirectives)
2828
Object.assign(Vue.options.components, platformComponents)
2929

3030
Vue.prototype.__patch__ = patch
3131

32-
Vue.prototype.$start = function() {
33-
this.__is_root__ = true
34-
35-
const placeholder = this.$document.createComment('placeholder')
36-
37-
this.$mount(placeholder)
38-
}
39-
40-
const mount = function(el, hydrating) {
41-
if (this.__is_root__ && !this.__started__) {
42-
const self = this
43-
start({
44-
create() {
45-
// Call mountComponent in the create callback when the IOS app loop has started
46-
// https://github.com/rigor789/nativescript-vue/issues/24
47-
mountComponent(self, el, hydrating)
48-
self.__started__ = true
49-
50-
const page = isPage(self.$el) ? self.$el.nativeView : new Page()
51-
52-
if (!isPage(self.$el)) {
53-
page.content = self.$el.nativeView
54-
}
55-
56-
page[VUE_VM_REF] = self
57-
58-
return page
59-
}
60-
})
61-
} else {
62-
return mountComponent(this, el, hydrating)
63-
}
64-
}
65-
6632
Vue.prototype.$mount = function(el, hydrating) {
6733
const options = this.$options
6834
// resolve template/el and convert to render function
@@ -77,30 +43,33 @@ Vue.prototype.$mount = function(el, hydrating) {
7743
const { render, staticRenderFns } = compileToFunctions(
7844
template,
7945
{
80-
delimiters: options.delimiters
46+
delimiters: options.delimiters,
47+
comments: options.comments
8148
},
8249
this
8350
)
8451
options.render = render
8552
options.staticRenderFns = staticRenderFns
8653
}
8754
}
88-
return mount.call(this, el, hydrating)
55+
56+
return mountComponent(this, el, hydrating)
8957
}
9058

91-
Vue.compile = compileToFunctions
92-
Vue.prototype.$renderTemplate = function(template, context, oldVnode) {
93-
let slot = template
94-
if (typeof template !== 'function') {
95-
slot = this.$scopedSlots[template]
96-
? this.$scopedSlots[template]
97-
: this.$scopedSlots.default
98-
}
59+
Vue.prototype.$start = function() {
60+
const self = this
9961

100-
let vnode = slot(context)[0]
101-
this.__patch__(oldVnode, vnode)
62+
start({
63+
create() {
64+
if (self.$el) {
65+
self.$el.nativeView.parent = null
66+
return self.$el.nativeView
67+
}
10268

103-
return vnode
69+
self.$mount()
70+
return ensurePage(self.$el, self)
71+
}
72+
})
10473
}
10574

10675
export default Vue

platform/nativescript/util/index.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { isKnownView, getViewMeta } from '../element-registry'
1+
import { isKnownView, getViewMeta, getViewClass } from '../element-registry'
22
import { makeMap, once } from 'shared/util'
3+
import { VUE_VM_REF } from '../runtime'
34

45
export const isReservedTag = makeMap('template', true)
56

@@ -33,6 +34,27 @@ export function isPage(el) {
3334
return el && el.tagName === 'page'
3435
}
3536

37+
export function ensurePage(el, vm) {
38+
if (!isPage(el)) {
39+
const page = new (getViewClass('page'))()
40+
page.content = el.nativeView
41+
if (vm) {
42+
page[VUE_VM_REF] = vm
43+
page.disposeNativeView = after(page.disposeNativeView, page, () =>
44+
vm.$destroy()
45+
)
46+
}
47+
return page
48+
}
49+
50+
if (vm) {
51+
el.nativeView[VUE_VM_REF] = vm
52+
el.disposeNativeView = after(el.disposeNativeView, el, () => vm.$destroy())
53+
}
54+
55+
return el.nativeView
56+
}
57+
3658
export function query(el, renderer, document) {
3759
// Todo
3860
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy