diff --git a/packages/mars-core/src/base/createComponent.js b/packages/mars-core/src/base/createComponent.js index 32c0f7ad..a13c0601 100644 --- a/packages/mars-core/src/base/createComponent.js +++ b/packages/mars-core/src/base/createComponent.js @@ -5,6 +5,7 @@ import {normalizeProps} from '../helper/props'; import {getPageInstance} from '../helper/instance'; +import config from '../config'; export function makeVueCompCreator(getCompMixin) { return function vueCompCreator(options) { @@ -16,6 +17,12 @@ export function makeVueCompCreator(getCompMixin) { export function makeMarkComponent(setData) { return function markComponent(callback) { const page = getPageInstance(this); + + if (!page.$vue && page.$$__createVue__) { + page.$$__createVue__.call(page); + delete page.$$__createVue__; + } + const vms = page.$vue.__vms__; const vmData = vms[this.data.compId]; if (vmData) { @@ -40,7 +47,7 @@ export function makeMarkComponent(setData) { } } - console.warn('[swan instance mismatch]', this.data.compId, this, vms); + // console.warn('[swan instance mismatch]', this.data.compId, this, vms); callback(); }; } @@ -57,7 +64,10 @@ export function makeCreateComponent(handleProxy, handleModel, callHook, hooks = props = Object.assign(props, { compId: String, rootComputed: Object, - rootUID: Number + rootUID: { + type: Number, + value: -1 + } }); return { @@ -80,7 +90,9 @@ export function makeCreateComponent(handleProxy, handleModel, callHook, hooks = }, lifetimes: { created(...args) { - // console.log('===swan created', this.data.compId); + if (process.env.NODE_ENV !== 'production' && config.debug && config.debug.lifetimes) { + console.log('[debug: mp lifetimes] created', this.data.compId); + } if (hooks.created) { hooks.created.call(this, () => { this.__created__ = true; @@ -89,7 +101,6 @@ export function makeCreateComponent(handleProxy, handleModel, callHook, hooks = this.$vue[handleName] && this.$vue[handleName].apply(this.$vue, args); }); } - callHook.call(this, this.$vue, 'comp', 'created', args); }); } @@ -98,11 +109,15 @@ export function makeCreateComponent(handleProxy, handleModel, callHook, hooks = } }, attached(...args) { - // console.log('===swan attached', this, this.data.compId); + if (process.env.NODE_ENV !== 'production' && config.debug && config.debug.lifetimes) { + console.log('[debug: mp lifetimes] attached', this.data.compId); + } callHook.call(this, this.$vue, 'comp', 'attached', args); }, ready(...args) { - // console.log('===swan ready', this, this.data.compId); + if (process.env.NODE_ENV !== 'production' && config.debug && config.debug.lifetimes) { + console.log('[debug: mp lifetimes] ready', this.data.compId); + } if (hooks.ready) { hooks.ready.call(this, () => { callHook.call(this, this.$vue, 'comp', 'ready', args); @@ -113,8 +128,10 @@ export function makeCreateComponent(handleProxy, handleModel, callHook, hooks = } }, detached(...args) { - // console.log('===swan detached', this.data.compId); - callHook.call(this, this.$vue, 'comp', 'dettached', args); + if (process.env.NODE_ENV !== 'production' && config.debug && config.debug.lifetimes) { + console.log('[debug: mp lifetimes] detached', this.data.compId); + } + callHook.call(this, this.$vue, 'comp', 'detached', args); this.$vue && this.$vue.$destroy(); // remove swan binded vue instance from root __vms__ const page = getPageInstance(this); diff --git a/packages/mars-core/src/base/createPage.js b/packages/mars-core/src/base/createPage.js index 402e93ba..da55b5d7 100644 --- a/packages/mars-core/src/base/createPage.js +++ b/packages/mars-core/src/base/createPage.js @@ -3,78 +3,107 @@ * @author zhangwentao */ -/* global Page */ - -/* global getApp */ - +/* global Page, getApp */ /* eslint-disable babel/new-cap */ +/* eslint-disable fecs-camelcase */ + import Vue from './vue/index'; import {mark, measure} from '../helper/perf'; import config from '../config'; import {state} from './state'; +export function createVue(options, args, {setData}) { + const pages = getApp().__pages__; + const uid = this.__uid__ !== undefined ? this.__uid__ : ++pages.uid; + pages[uid] = this; + this.__uid__ = uid; + + if (process.env.NODE_ENV !== 'production' && config.performance && mark) { + const perfTagStart = `${this.route}-start`; + // const perfTagEnd = `${this.route}-end`; + mark(perfTagStart); + } + + if (state.store && !options.store) { + options.store = state.store; + } + + const vm = new Vue(options); + vm.__vms__ = {}; + this.$vue = vm; + vm.$mp = { + scope: this, + query: args[0], + options: args[0] + }; + + vm.$on('vm.mounted', _ => { + setData(vm, this, true); + }); + + vm.$on('vm.updated', _ => { + setData(vm, this, true); + }); + + if (process.env.NODE_ENV !== 'production' && config.performance && mark) { + const perfTagStart = `${this.route}-start`; + const perfTagEnd = `${this.route}-end`; + mark(perfTagEnd); + measure(`${this.route}:new`, perfTagStart, perfTagEnd); + } + + return vm; +} + +export function mountVue(vm) { + if (process.env.NODE_ENV !== 'production' && config.performance && mark) { + const perfTagStart = `${this.route}-start`; + const perfTagEnd = `${this.route}-end`; + mark(perfTagStart); + vm.$mount(); + mark(perfTagEnd); + measure(`${this.route}:mount`, perfTagStart, perfTagEnd); + } + else { + vm.$mount(); + } +} + export function makeCreatePage(pageMixin, {handleProxy, handleModel}, setData, callHook) { return function (options) { options.mixins = [pageMixin]; return { + $$__createVue__() { + const vm = createVue.call(this, options, [], {setData}); + mountVue.call(this, vm); + }, data: {}, handleProxy, handleModel, onLoad(...args) { - const pages = getApp().__pages__; - const uid = this.__uid__ !== undefined ? this.__uid__ : ++pages.uid; - pages[uid] = this; - this.__uid__ = uid; - - if (process.env.NODE_ENV !== 'production' && config.performance && mark) { - const perfTagStart = `${this.route}-start`; - // const perfTagEnd = `${this.route}-end`; - mark(perfTagStart); - } - - if (state.store && !options.store) { - options.store = state.store; - } - - const vm = new Vue(options); - vm.__vms__ = {}; - this.$vue = vm; - vm.$mp = { - scope: this, - query: args[0], - options: args[0] - }; - vm.$on('vm.updated', _ => { - setData(vm, this, true); - }); - vm.$on('vm.mounted', _ => { - setData(vm, this, true); - }); - - if (process.env.NODE_ENV !== 'production' && config.performance && mark) { - const perfTagStart = `${this.route}-start`; - const perfTagEnd = `${this.route}-end`; - mark(perfTagEnd); - measure(`${this.route}:new`, perfTagStart, perfTagEnd); - } - - // 先 callHook 保证数据可以初始化 - const ret = callHook.call(this, this.$vue, 'page', 'onLoad', args); - if (process.env.NODE_ENV !== 'production' && config.performance && mark) { - const perfTagStart = `${this.route}-start`; - const perfTagEnd = `${this.route}-end`; - mark(perfTagStart); - vm.$mount(); - mark(perfTagEnd); - measure(`${this.route}:mount`, perfTagStart, perfTagEnd); + let vm = this.$vue; + let ret; + if (!vm) { + vm = createVue.call(this, options, args, {setData}); + ret = callHook.call(this, this.$vue, 'page', 'onLoad', args); + mountVue.call(this, vm); } else { - vm.$mount(); + const query = args[0]; + vm.$mp.query = query; + vm.$mp.options = query; + ret = callHook.call(this, this.$vue, 'page', 'onLoad', args); + } + if (process.env.NODE_ENV !== 'production' && config.debug && config.debug.lifetimes) { + console.log('[debug: mp pageHooks] onLoad', this.__uid__); } return ret; }, onUnload(...args) { + if (process.env.NODE_ENV !== 'production' && config.debug && config.debug.lifetimes) { + console.log('[debug: mp pageHooks] onUnload', this.__uid__); + } const ret = callHook.call(this, this.$vue, 'page', 'onUnload', args); if (this.$vue) { this.$vue.$destroy(); @@ -90,16 +119,26 @@ export function makeCreatePage(pageMixin, {handleProxy, handleModel}, setData, c } }); + delete this.$vue; + delete this.$$__createVue__; return ret; }, onReady(...args) { - // console.log('[pref] onReady'); + if (process.env.NODE_ENV !== 'production' && config.debug && config.debug.lifetimes) { + console.log('[debug: mp pageHooks] onReady', this.__uid__); + } return callHook.call(this, this.$vue, 'page', 'onReady', args); }, onShow(...args) { + if (process.env.NODE_ENV !== 'production' && config.debug && config.debug.lifetimes) { + console.log('[debug: mp pageHooks] onShow', this.__uid__); + } return callHook.call(this, this.$vue, 'page', 'onShow', args); }, onHide(...args) { + if (process.env.NODE_ENV !== 'production' && config.debug && config.debug.lifetimes) { + console.log('[debug: mp pageHooks] onHide', this.__uid__); + } return callHook.call(this, this.$vue, 'page', 'onHide', args); }, onPullDownRefresh(...args) { diff --git a/packages/mars-core/src/base/mixins.js b/packages/mars-core/src/base/mixins.js index 27a8f56c..81256287 100644 --- a/packages/mars-core/src/base/mixins.js +++ b/packages/mars-core/src/base/mixins.js @@ -5,6 +5,7 @@ import {getPageInstance} from '../helper/instance'; import {setObjectData} from '../helper/util'; +import config from '../config'; export function makePageMixin($api) { return { @@ -55,6 +56,9 @@ export function makeGetCompMixin($api) { }); }, created() { + if (process.env.NODE_ENV !== 'production' && config.debug && config.debug.lifetimes) { + console.log('[debug: Vue] created', this.compId); + } // console.log('===vue created', this.compId); // markComponentInVue.call(this, pms, vms); const vms = this.$root.__vms__; @@ -70,8 +74,10 @@ export function makeGetCompMixin($api) { } export function handleProxy(event) { + if (process.env.NODE_ENV !== 'production' && config.debug && config.debug.events) { + console.log('[debug: handleProxy]', this.data.compId, event); + } // get event dataSet - // console.log('===handleProxy:', this.data.compId, event); const data = event.currentTarget.dataset; const eventType = event.type; @@ -130,6 +136,4 @@ export function handleModel(event) { else if (type === 'change' && tag === 'switch') { setObjectData(this.$vue, model, event.detail.checked); } - - } diff --git a/packages/mars-core/src/config.js b/packages/mars-core/src/config.js index d3983654..6daed09a 100644 --- a/packages/mars-core/src/config.js +++ b/packages/mars-core/src/config.js @@ -4,7 +4,10 @@ */ export default { - performance: false, - debug: false, + performance: true, + debug: { + lifetimes: true, + events: false + }, framework: process.env.MARS_CONFIG_FRAMEWORK -}; \ No newline at end of file +}; diff --git a/packages/mars-core/src/helper/instance.js b/packages/mars-core/src/helper/instance.js index 742d3b94..10ef81df 100644 --- a/packages/mars-core/src/helper/instance.js +++ b/packages/mars-core/src/helper/instance.js @@ -2,12 +2,19 @@ * @file props helper * @author zhangwentao */ -/* global getApp */ +/* global getApp, getCurrentPages */ export function getPageInstance($mp) { const uid = $mp.data.rootUID; - const pages = getApp().__pages__; - const page = pages[uid]; + let page; + if (uid === -1) { + const pages = getCurrentPages(); + page = pages[pages.length - 1]; + } + else { + page = getApp().__pages__[uid]; + } + if (!page) { throw new Error(`cannot find page instance for $mp ${$mp.data.compId}`, $mp); }