Skip to content

refactor: app and page lifecycle#1927

Merged
imsobear merged 10 commits intomasterfrom
refactor-lifecycle
Jul 23, 2020
Merged

refactor: app and page lifecycle#1927
imsobear merged 10 commits intomasterfrom
refactor-lifecycle

Conversation

@SoloJiang
Copy link
Collaborator

@SoloJiang SoloJiang commented Jun 22, 2020

  1. 支持应用级生命周期:
  • 小程序
  • Web framework App: onLaunch/onError
  • PHA
  1. 支持 withPageLifeCycle
  • 小程序
  • Web framework App
  • PHA
  1. 支持小程序运行时 withRouter

@SoloJiang
Copy link
Collaborator Author

应用级使用方法:

// app.js
import { runApp } from 'rax-app';
import appConfig from './app.json';

runApp(appConfig, {
  dynamicConfig: true,
  onLaunch(options) {
    
  },
  onShow() {
    
  },
  onHide() {
    
  },
  onError(err) {
    
  },
  onTabItem({ from, path, text, index }) {
    
  }
});

@SoloJiang
Copy link
Collaborator Author

页面级使用方法:

// 函数组件
import { createElement } from 'rax';
import { usePageShow } from 'rax-app';

function Home() {
  usePageShow(() => {
    // 首次调用,回调函数会在组件完成渲染后执行
  });
  usePageHide(() => {
    
  });
}

// Class Component
import { createElement, Component } from 'rax';
import { withPageLifeCycle } from 'rax-app';

class Home extends Component {
  onShow() {
    // 首次调用,函数在组件 constructor 之后调用
  }
}

export default withPageLifeCycle(Home);

@SoloJiang SoloJiang changed the title refactor: app and page lifecycle [WIP]refactor: app and page lifecycle Jun 22, 2020
@SoloJiang SoloJiang changed the title [WIP]refactor: app and page lifecycle refactor: app and page lifecycle Jul 14, 2020
// In MiniApp platform show event will trigger after addPageLifeCycle, so it needn't be execute in constructor
this.onShow();
}
addPageLifeCycle(SHOW, this.onShow.bind(this));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果是 !isMiniAppPlatform 会执行两边 onShow?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

时机不同,在 !isMiniAppPlatform 的环境下,constructor 的时候已经错过了页面 onShow 的时机,这里是最早执行 onShow 的时候了

@SoloJiang
Copy link
Collaborator Author

以下功能下一期添加:

  1. 默认保活和超过 5 个保活页面自动销毁机制
  2. tabbar 的 onTabItemClick 事件

@@ -0,0 +1,12 @@
import { isMiniAppPlatform } from './env';

let history;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

路由这块可以考虑将 createHistory 的逻辑从 runApp 也抽离到这个文件,是否更合理一些


let withRouter;

if (isMiniApp || isWeChatMiniProgram || isByteDanceMicroApp) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -0,0 +1,30 @@
import { createElement } from 'rax';
import { withRouter as webSPAWithRouter } from 'rax-use-router';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个 router 不只是针对 webSPA 的吧 ?如果是感觉不太语义。

import { withRouter as webSPAWithRouter } from 'rax-use-router';
import { isMiniApp, isWeChatMiniProgram, isByteDanceMicroApp } from 'universal-env';

let withRouter;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

直接 let withRouter = webSPAWithRouter 下面逻辑就不用 else 了

throw new Error('Error: the runApp method second param can only be Object.');
}

const pageProps = handleDynamicConfig(dynamicConfig);
Copy link
Collaborator

@chenbin92 chenbin92 Jul 20, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里有点没理解,如果 dynamicConfig 为 false,为什么要把 dynamicConfig 当做 pageProps,场景是 ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个是兼容以前的逻辑,以前 runApp 第二个参数是 pageProps,后面 raxjs 就直接干掉这个逻辑就好了


// In MiniApp, it needn't return App Component
if (isMiniApp || isWeChatMiniProgram) {
} else if (isMiniApp || isWeChatMiniProgram || isByteDanceMicroApp) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

使用 isMiniAppPlatform,其他地方也可用统一改下

Copy link
Collaborator

@chenbin92 chenbin92 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed

window.addEventListener('appshare', ({ context, shareInfo, options }) => {
emit(SHARE, context, shareInfo, options);
});
window.addEventListener('tabitemclick', ({ options, context }) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

window 在 SSR 下是无效的,需要考虑下吗

// Emit app hide
emit(HIDE);
// Emit page hide
pageEmit(HIDE, router.current.path);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个文件相关的 pageEmit 是不是放到 page.js 合理些 ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个是 history.listen 回调的逻辑,可能没法拆过去

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

要在 app 启动之后开始监听

}
}
});
// Emit Web lifeCycles
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Emit error lifeCycles ?


for (let i = 0, l = routes.length; i < l; i++) {
if (fullpath === routes[i].path || routes[i].regexp && routes[i].regexp.test(fullpath)) {
initialComponent = routes[i].component;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

下一行如果是 class 组件直接跳过了 ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这边所有的页面都包了一层 Wrapper :
const importComponent = `() => () => interopRequire(require('${getDepPath(route.source, this.rootContext)}'))`;

// Get history
const history = getHistory();
// The app switches from foreground to background
if (router.current && history.pathname === router.current.path) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

history 上应该没有 pathname,这里是 history.location.pathname ?

// Flow router info
router.prev = router.current;
router.current = {
path: location.pathname,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里改成的 key 是 pathname 会不会更语义点,在对比的时候也是 history.location.pathname === router.current.pathname ?

https://github.com/alibaba/rax/pull/1927/files#diff-b636603ce2a0fe89646dcd00e6d265ceR181

console.log('@weex-module/globalEvent error: ' + err);
}
} else if (!isUndef(document) && !isUndef(window)) {
document.addEventListener('visibilitychange', function() {
Copy link
Collaborator

@chenbin92 chenbin92 Jul 20, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

visibilitychange 事件现在还是 Recommendation 阶段,Safari 并未完全按照规范实现,即在 Safari 切换 Tab 时不会触发

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Safari 后续再兼容


// Emit app launch cycle.
emit('launch');
appEmit(LAUNCH);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

小程序自己有 app lanuch 的钩子,我用 window trigger 的

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

看到了~

image


export default function runApp(appConfig, pageProps = {}) {
function handleDynamicConfig(config) {
if (config.dynamicConfig) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

没有 config.dynamicConfig 就先 return 吧

@imsobear imsobear merged commit 984b0ec into master Jul 23, 2020
@imsobear imsobear deleted the refactor-lifecycle branch July 23, 2020 06:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants