Skip to content
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

support swan lifecycle onInit #197

Open
wants to merge 2 commits into
base: 0.3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions packages/mars-build/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

## [0.3.15](https://github.com/keyiran/Mars/compare/@marsjs/[email protected]...@marsjs/[email protected]) (2020-09-17)


### Features

* **build & core:** support swan lifecycle onInit ([d83d1ea](https://github.com/keyiran/Mars/commit/d83d1ea))





## [0.3.14](https://github.com/max-team/Mars/compare/@marsjs/[email protected]...@marsjs/[email protected]) (2019-11-14)


Expand Down
2 changes: 1 addition & 1 deletion packages/mars-build/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@marsjs/build",
"version": "0.3.14",
"version": "0.3.15",
"scripts": {
"test": "jest --coverage"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
/* eslint-disable fecs-camelcase */

/* eslint-disable babel/new-cap */
/* global Set */

// 小程序page生命周期
const PAGE_LIFECYCLE_HOOKS = {
onInit: true,
onLoad: true,
onReady: true,
onShow: true,
Expand All @@ -23,7 +26,19 @@ const PAGE_LIFECYCLE_HOOKS = {

};

// 小程序和Vue App级生命周期映射关系
const APP_LIFE_MAP_VUE = {
onLaunch: 'created',
onShow: 'mounted',
onHide: 'destroyed'
// onLoad: true,
// onReady: true,
// onUnload: true
};

// 小程序和Vue Page级生命周期映射关系(如果多个小程序生命周期映射到同一个vue生命周期,请严格保证书写顺序和生命周期调用顺序一致,例如onInit和onLoad)
const PAGE_LIFE_MAP_VUE = {
onInit: 'created',
onLoad: 'created',
onReady: 'mounted',
onUnload: 'destroyed',
Expand Down Expand Up @@ -63,14 +78,6 @@ const COMP_LIFE_MAP_VUE = {
type: 'pageLifetimes'
}
};
const APP_PAOGE_LIFE_MAP_VUE = {
onLaunch: 'created',
onShow: 'mounted',
onHide: 'destroyed'
// onLoad: true,
// onReady: true,
// onUnload: true
};

// MAP: AOP 注册的生命周期方法的映射
const AOP_MAP = {
Expand Down Expand Up @@ -219,10 +226,10 @@ function judgeComponent(properties) {
* @return {Object} 处理的lifeItem
*/
function bindAOPEvents(lifeItem, t, key, isApp) {
const lifeTime = isApp ? APP_PAOGE_LIFE_MAP_VUE[key] : PAGE_LIFE_MAP_VUE[key];
if (!lifeTime) {
// 非该页面生命周期则返回
return;
const isAOPLifeTime = key in (isApp ? AOP_MAP.App : AOP_MAP.Page);
// 非当前页面AOP生命周期,不做处理返回
if (!isAOPLifeTime) {
return lifeItem;
}
// 在生命周期内调用相应AOP事件
const AOPEventHookExpression = t.expressionStatement(
Expand Down Expand Up @@ -412,39 +419,41 @@ module.exports = function getVisitor(options = {}) {

// 如果有区别级别的 mpConfig 从 mpConfig 获取是否为组件
const isComponent = mpConfig ? mpConfig.component : judgeComponent(properties);
// 处理swan page 生命周期:onLoad 、onReady 、onUnload、onShow 、onHide
// 收集 app.vue 周期:onLaunch 、 onShow 、 onHide
['onLoad', 'onReady', 'onUnload', 'onLaunch', 'onShow', 'onHide'].forEach(key => {
let lifeItem = properties.find(item => item.key.name === key);
let lifeMapKey = isApp ? APP_PAOGE_LIFE_MAP_VUE[key] : PAGE_LIFE_MAP_VUE[key];
// 判断当前 vue sfc 是否是组件component
if (isComponent) {
return;
}
// 处理生命周期的AOP
if (useAOP) {
lifeItem = bindAOPEvents(lifeItem, t, key, isApp);
}
mapSwanLifeTime(properties, t, key, lifeItem, lifeMapKey, options);
});
// 处理swan component 生命周期:created、 attached 、ready 、detached、show、hide
Object.keys(COMP_LIFE_MAP_VUE).forEach(key => {
const {
life: lifeMapKey,
type: lifeType
} = COMP_LIFE_MAP_VUE[key];
const lifeTypeItem = properties.find(item => item.key.name === lifeType);
if (!lifeTypeItem) {
return;
}
if (isComponent) {
// 将小程序组件级生命周期映射到Vue
Object.keys(COMP_LIFE_MAP_VUE).forEach(key => {
const {
life: lifeMapKey,
type: lifeType
} = COMP_LIFE_MAP_VUE[key];
const lifeTypeItem = properties.find(item => item.key.name === lifeType);
if (!lifeTypeItem) {
return;
}

const lifeItem = lifeTypeItem.value.properties.find(item => item.key.name === key);
if (!lifeItem) {
return;
}
const lifeItem = lifeTypeItem.value.properties.find(item => item.key.name === key);
if (!lifeItem) {
return;
}

mapSwanLifeTime(properties, t, key, lifeItem, lifeMapKey, options);
});
mapSwanLifeTime(properties, t, key, lifeItem, lifeMapKey, options);
});
}
else {
// 将小程序App和Page级生命周期映射到Vue
[
...new Set(Object.keys(PAGE_LIFE_MAP_VUE).concat(Object.keys(APP_LIFE_MAP_VUE)))
].forEach(key => {
let lifeItem = properties.find(item => item.key.name === key);
let lifeMapKey = isApp ? APP_LIFE_MAP_VUE[key] : PAGE_LIFE_MAP_VUE[key];
// 处理生命周期的AOP
if (useAOP) {
lifeItem = bindAOPEvents(lifeItem, t, key, isApp);
}

mapSwanLifeTime(properties, t, key, lifeItem, lifeMapKey, options);
});
}
/* eslint-disable */
path.traverse(Property(t, options));
}
Expand Down
11 changes: 11 additions & 0 deletions packages/mars-core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

## [0.3.9](https://github.com/keyiran/Mars/compare/@marsjs/[email protected]...@marsjs/[email protected]) (2020-09-17)


### Features

* **build & core:** support swan lifecycle onInit ([d83d1ea](https://github.com/keyiran/Mars/commit/d83d1ea))





## [0.3.8](https://github.com/max-team/Mars/compare/@marsjs/[email protected]...@marsjs/[email protected]) (2020-03-12)


Expand Down
2 changes: 1 addition & 1 deletion packages/mars-core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@marsjs/core",
"version": "0.3.8",
"version": "0.3.9",
"scripts": {
"test": "jest --coverage"
},
Expand Down
18 changes: 18 additions & 0 deletions packages/mars-core/src/base/createPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,24 @@ export function makeCreatePage(pageMixin, {handleProxy, handleModel}, setData, c
data: initData,
handleProxy,
handleModel,
onInit(...args) {
let vm = this.$vue;
if (!vm) {
vm = createVue.call(this, options, args, {setData});
}
else {
const query = args[0];
vm.$mp.query = query;
vm.$mp.options = query;
}

if (process.env.NODE_ENV !== 'production' && config.debug && config.debug.lifetimes) {
console.log('[debug: mp pageHooks] onInit', this.__uid__);
}
// 先 callHook 保证数据可以初始化
const ret = callHook.call(this, this.$vue, 'page', 'onInit', args);
return ret;
},
onLoad(...args) {
let vm = this.$vue;
if (!vm) {
Expand Down