diff --git a/CHANGELOG.md b/CHANGELOG.md index 69ce1896a5c47baa234d77f209454fa768e43324..0457916b577518ec5cf41d3ed87686c6aec4fda5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,14 @@ # Changelog +## [v2.1.1] 2025.06.14 + +- 底层生命周期感知能力替换成@duke/component-lifecycle库,简化代码,加强可维护性 + +## [v2.1.0] 2025.06.11 + +- 修复装饰器模式下的多个订阅者下,只有一个订阅者会自动反注册的问题 +- 新增 onFromComponent 方法,使用更加灵活 + ## [v2.0.0] 2025.01.17 - 全新升级支持跨线程通知及跨线程粘性事件 diff --git a/README.md b/README.md index 898967d60f56b68d8ccc3416566b1be36f4b4909..5e1a39c89d279026ac31b834cc1e2ab363dc9205 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,7 @@ -# 有没有公司招鸿蒙开发的啊,求职ing - # EventPost ## 简介 +[![openHarmony](https://img.shields.io/badge/openharmony-v2.1.1-brightgreen)](https://gitee.com/Duke_Bit/eventpost/releases/tag/v2.1.1) EventPost是一款消息总线,具有生命周期感知能力,支持Sticky。 @@ -129,4 +128,10 @@ DevEco Studio: 5.0.5.315, SDK: HarmonyOS 5.0.1 Release Ohos_sdk_public 5.0.1.115 ## 开源协议 -本项目基于 [MIT license](https://gitee.com/Duke_Bit/eventpost/blob/master/LICENSE) ,请自由地享受和参与开源。 \ No newline at end of file +本项目基于 [MIT license](https://gitee.com/Duke_Bit/eventpost/blob/master/LICENSE) ,请自由地享受和参与开源。 + +## 其他库 + +- @duke/component-lifecycle 鸿蒙版本的Lifecycle库,支持组件、Router和Navigation: [https://gitee.com/Duke_Bit/component-lifecycle](https://gitee.com/Duke_Bit/component-lifecycle) +- @duke/elf-dialog CustomDialog黑魔法 不依赖promptAction 实现的函数级弹窗,省去复杂的模版代码,让你专注于你的业务,一行代码搞定弹窗: [https://gitee.com/Duke_Bit/elf-dialog](https://gitee.com/Duke_Bit/elf-dialog) +- @duke/elf-refresh OpenHarmony 刷新组件,支持下拉刷新和上拉加载更多,支持各种组件,List、Grid,支持header,footer,目标打造HarmonyOS的SmartRefreshLayout: [https://gitee.com/Duke_Bit/ElfRefresh](https://gitee.com/Duke_Bit/ElfRefresh) \ No newline at end of file diff --git a/build-profile.json5 b/build-profile.json5 index 35c3b395978dadab8d0e670f25f593f5f7d938a0..a0860304030e3865112db44633f865f9011ef841 100644 --- a/build-profile.json5 +++ b/build-profile.json5 @@ -19,8 +19,15 @@ { "name": "default", "signingConfig": "default", - "compatibleSdkVersion": "4.1.0(11)", + "targetSdkVersion": "5.0.5(17)", + "compatibleSdkVersion": "5.0.0(12)", "runtimeOS": "HarmonyOS", + "buildOption": { + "strictMode": { + "caseSensitiveCheck": true, + "useNormalizedOHMUrl": true + } + } } ], "buildModeSet": [ diff --git a/entry/oh-package-lock.json5 b/entry/oh-package-lock.json5 index 99e7939735308a1e55e204806129f64972f79c9e..26e82719981efc4389770ad4035ac8e2494e45fe 100644 --- a/entry/oh-package-lock.json5 +++ b/entry/oh-package-lock.json5 @@ -5,17 +5,26 @@ "lockfileVersion": 3, "ATTENTION": "THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.", "specifiers": { - "EventPost@../library": "EventPost@../library", + "@duke/component-lifecycle@^1.0.2": "@duke/component-lifecycle@1.0.2", + "eventpost@../library": "eventpost@../library", "libnative_event_post.so@../library/src/main/cpp/types/libeventpost": "libnative_event_post.so@../library/src/main/cpp/types/libeventpost" }, "packages": { - "EventPost@../library": { + "@duke/component-lifecycle@1.0.2": { + "name": "@duke/component-lifecycle", + "version": "1.0.2", + "integrity": "sha512-giz0mfwnTzYJ4OfijQv6LAT5uNh8RDRcrwrLjvB6q7KQHFDNXE6lC3gnxOEZjXSs1Npmx0hr2DW9MVJqWueVTA==", + "resolved": "https://ohpm.openharmony.cn/ohpm/@duke/component-lifecycle/-/component-lifecycle-1.0.2.har", + "registryType": "ohpm" + }, + "eventpost@../library": { "name": "eventpost", - "version": "2.1.0", + "version": "2.1.1", "resolved": "../library", "registryType": "local", "dependencies": { - "libnative_event_post.so": "file:./src/main/cpp/types/libeventpost" + "libnative_event_post.so": "file:./src/main/cpp/types/libeventpost", + "@duke/component-lifecycle": "^1.0.2" } }, "libnative_event_post.so@../library/src/main/cpp/types/libeventpost": { diff --git a/entry/oh-package.json5 b/entry/oh-package.json5 index 5f9348c9fc5a05e480ae09b7eabf8d7da94e1473..23fe0fd492186f230bee2add52501c58b11eaaff 100644 --- a/entry/oh-package.json5 +++ b/entry/oh-package.json5 @@ -6,7 +6,8 @@ "author": "", "license": "", "dependencies": { - EventPost: "file:../library" + 'eventpost': "file:../library", + '@duke/component-lifecycle': "^1.0.2" } } diff --git a/entry/src/main/ets/pages/CustView.ets b/entry/src/main/ets/pages/CustView.ets index a7e10dab6d151d68d95de9639cd1dfa9a3a5d5a7..67f39b8062a62fa76cab083b88279062cd88fd8b 100644 --- a/entry/src/main/ets/pages/CustView.ets +++ b/entry/src/main/ets/pages/CustView.ets @@ -1,13 +1,22 @@ -import { Subscriber } from 'EventPost' +import { Subscriber } from 'eventpost' @Component export struct CustomView { @State name: string = "CustView" + aboutToAppear(): void { + console.log("aboutToAppear") + } + build() { Text(this.name) } + onDidBuild(): void { + let a = this as object as Record + console.log("onDidBuild"+a['_ComponentLifecycle']+'') + } + @Subscriber("test1") onReceived(event: object) { console.error("收到 test1 " + JSON.stringify(event)) diff --git a/entry/src/main/ets/pages/Index.ets b/entry/src/main/ets/pages/Index.ets index 237f75370a6663ea921852dcc78994f3dcefa0ad..8315d54bb425c7479595acb2926840dbce4041ec 100644 --- a/entry/src/main/ets/pages/Index.ets +++ b/entry/src/main/ets/pages/Index.ets @@ -1,11 +1,13 @@ import { router } from '@kit.ArkUI'; -import { EventPost, Subscriber } from 'EventPost/Index'; +import { EventPost, Subscriber } from 'eventpost'; import { CustomView } from './CustView'; import { ErrorEvent, MessageEvents, worker } from '@kit.ArkTS'; +import { Lifecycle, LifecycleOwner } from '@duke/component-lifecycle'; @Entry @Component struct Index { + @LifecycleOwner lifecycle: Lifecycle = new Lifecycle(); @State message: string = 'Hello World'; @State showCustom: boolean = true @@ -22,6 +24,12 @@ struct Index { .fontSize(50) .fontWeight(FontWeight.Bold) + Button("重新加载测试") + .margin({ top: 16 }) + .onClick(() => { + this.showCustom = true; + }); + Button("销毁测试") .margin({ top: 16 }) .onClick(() => { diff --git a/entry/src/main/ets/pages/NextPage.ets b/entry/src/main/ets/pages/NextPage.ets index 0a4af130fbbcaf937d900e168c54fa982c140caa..049625839be8efe440218487605cb831046a04d9 100644 --- a/entry/src/main/ets/pages/NextPage.ets +++ b/entry/src/main/ets/pages/NextPage.ets @@ -1,4 +1,4 @@ -import { EventPost, Subscriber } from 'EventPost/Index'; +import { EventPost, Subscriber } from 'eventpost'; @Entry @Component @@ -41,7 +41,7 @@ struct NextPage { console.log("1111") this.message = "fff" EventPost.getDefault().onFromComponent('test1',this,()=>{ - console.log("2222") + console.log("onFromComponent2222") },true) } } \ No newline at end of file diff --git a/entry/src/main/ets/workers/Worker.ets b/entry/src/main/ets/workers/Worker.ets index f78f146919732a82d6ed1766ee2fa4fcac6c20d8..15b9d3d3f1f80c603fb4c26daa77a7bb2ed018bf 100644 --- a/entry/src/main/ets/workers/Worker.ets +++ b/entry/src/main/ets/workers/Worker.ets @@ -1,5 +1,5 @@ import { ErrorEvent, MessageEvents, ThreadWorkerGlobalScope, worker } from '@kit.ArkTS'; -import { EventPost } from 'EventPost'; +import { EventPost } from 'eventpost'; const workerPort: ThreadWorkerGlobalScope = worker.workerPort; diff --git a/library/BuildProfile.ets b/library/BuildProfile.ets index 653292d587d1cae0d404dd729beda53121d68aac..c822afcb603f0df53e2815a0abde15b55d9358ff 100644 --- a/library/BuildProfile.ets +++ b/library/BuildProfile.ets @@ -1,7 +1,7 @@ /** * Use these variables when you tailor your ArkTS code. They must be of the const type. */ -export const HAR_VERSION = '2.0.0'; +export const HAR_VERSION = '2.1.1'; export const BUILD_MODE_NAME = 'debug'; export const DEBUG = true; export const TARGET_NAME = 'default'; diff --git a/library/CHANGELOG.md b/library/CHANGELOG.md index 233305be4d1c918c2f5e45266f7cce53b032bb7e..0457916b577518ec5cf41d3ed87686c6aec4fda5 100644 --- a/library/CHANGELOG.md +++ b/library/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## [v2.1.1] 2025.06.14 + +- 底层生命周期感知能力替换成@duke/component-lifecycle库,简化代码,加强可维护性 + ## [v2.1.0] 2025.06.11 - 修复装饰器模式下的多个订阅者下,只有一个订阅者会自动反注册的问题 diff --git a/library/README.md b/library/README.md index 552d13b24eb8706829266354540b7dd6752115b9..5e1a39c89d279026ac31b834cc1e2ab363dc9205 100644 --- a/library/README.md +++ b/library/README.md @@ -1,6 +1,7 @@ # EventPost ## 简介 +[![openHarmony](https://img.shields.io/badge/openharmony-v2.1.1-brightgreen)](https://gitee.com/Duke_Bit/eventpost/releases/tag/v2.1.1) EventPost是一款消息总线,具有生命周期感知能力,支持Sticky。 @@ -127,4 +128,10 @@ DevEco Studio: 5.0.5.315, SDK: HarmonyOS 5.0.1 Release Ohos_sdk_public 5.0.1.115 ## 开源协议 -本项目基于 [MIT license](https://gitee.com/Duke_Bit/eventpost/blob/master/LICENSE) ,请自由地享受和参与开源。 \ No newline at end of file +本项目基于 [MIT license](https://gitee.com/Duke_Bit/eventpost/blob/master/LICENSE) ,请自由地享受和参与开源。 + +## 其他库 + +- @duke/component-lifecycle 鸿蒙版本的Lifecycle库,支持组件、Router和Navigation: [https://gitee.com/Duke_Bit/component-lifecycle](https://gitee.com/Duke_Bit/component-lifecycle) +- @duke/elf-dialog CustomDialog黑魔法 不依赖promptAction 实现的函数级弹窗,省去复杂的模版代码,让你专注于你的业务,一行代码搞定弹窗: [https://gitee.com/Duke_Bit/elf-dialog](https://gitee.com/Duke_Bit/elf-dialog) +- @duke/elf-refresh OpenHarmony 刷新组件,支持下拉刷新和上拉加载更多,支持各种组件,List、Grid,支持header,footer,目标打造HarmonyOS的SmartRefreshLayout: [https://gitee.com/Duke_Bit/ElfRefresh](https://gitee.com/Duke_Bit/ElfRefresh) \ No newline at end of file diff --git a/library/oh-package.json5 b/library/oh-package.json5 index cc1308a49baef551f61d96200300873bc49d034e..34c4da183883651de08b20170ede122e7d876656 100644 --- a/library/oh-package.json5 +++ b/library/oh-package.json5 @@ -1,6 +1,6 @@ { "name": "eventpost", - "version": "2.1.0", + "version": "2.1.1", "description": "事件分发,支持组件中的lifecycle,在组件中使用自动取消订阅", "main": "Index.ts", "author": "duke", @@ -16,6 +16,7 @@ "repository": "https://gitee.com/Duke_Bit/eventpost", "homepage": "https://gitee.com/Duke_Bit", "dependencies": { - "libnative_event_post.so": "file:./src/main/cpp/types/libeventpost" + "libnative_event_post.so": "file:./src/main/cpp/types/libeventpost", + "@duke/component-lifecycle": "^1.0.2" } } \ No newline at end of file diff --git a/library/src/main/ets/Constants.ts b/library/src/main/ets/Constants.ts index 2b2d357e3532dd77cb99a7f40cfb5f5efa3ee649..747d8a684577e9364e753459253bdfd4f048521d 100644 --- a/library/src/main/ets/Constants.ts +++ b/library/src/main/ets/Constants.ts @@ -1,9 +1 @@ -export const EVENT_COMPONENT_EVENT_CACHE = "EVENT_COMPONENT_EVENT_CACHE" - -export const EVENT_COMPONENT_EVENT_OFF_CACHE = "EVENT_COMPONENT_EVENT_ONCE_HANDLER" - -export const EVENT_COMPONENT_EVENT_ON_HANDLER = "EVENT_COMPONENT_EVENT_ON_HANDLER" - -export const EVENT_COMPONENT_EVENT_OFF_HANDLER = "EVENT_COMPONENT_EVENT_OFF_HANDLER" - -export const EVENT_COMPONENT_EVENT_OFF_2_HANDLER = "EVENT_COMPONENT_EVENT_OFF_HANDLER" \ No newline at end of file +export const EVENT_COMPONENT_EVENT_CACHE = "EVENT_COMPONENT_EVENT_CACHE" \ No newline at end of file diff --git a/library/src/main/ets/Decorators.ts b/library/src/main/ets/Decorators.ts index 076b9458d9bb48cb7a17aeb61615b836348e50e6..33ed129cdc670057ea2b6d7e0a01ab89473e1981 100644 --- a/library/src/main/ets/Decorators.ts +++ b/library/src/main/ets/Decorators.ts @@ -1,67 +1,74 @@ import { - EVENT_COMPONENT_EVENT_CACHE, - EVENT_COMPONENT_EVENT_OFF_HANDLER, - EVENT_COMPONENT_EVENT_ON_HANDLER + EVENT_COMPONENT_EVENT_CACHE } from './Constants'; import { EventOnInfo } from './EventOnInfo'; import { EventPost } from './EventPost'; import { hilog } from '@kit.PerformanceAnalysisKit'; +import { Lifecycle, + LifecycleEventObserver, + LifecycleOwner, + LifecycleState, + LIFECYCLE_DEFAULT, LIFECYCLE_INIT } from '@duke/component-lifecycle'; export function Subscriber(TypeName: string, sticky: boolean = false) { return (target: any, name: string, propertyDescriptor: PropertyDescriptor) => { if (!target.rerender) { - hilog.warn(0x0000, 'EventPost', '%{public}s', 'Subscriber current target is not a component') - return + hilog.warn(0x0000, 'EventPost', '%{public}s', 'Subscriber current target is not a component'); + return; } - let onInfo:EventOnInfo = { typeName: TypeName, sticky: sticky ,callBack:propertyDescriptor.value }; - if (target[EVENT_COMPONENT_EVENT_CACHE]) { - target[EVENT_COMPONENT_EVENT_CACHE].set(name, onInfo) - return + if (!target[EVENT_COMPONENT_EVENT_CACHE]) { + let cache = new Array(); + target[EVENT_COMPONENT_EVENT_CACHE] = cache; + cache.push({ + typeName: TypeName, + methodName: name, + sticky: sticky, + callBack: propertyDescriptor.value + }); } - target[EVENT_COMPONENT_EVENT_CACHE] = new Map(); - target[EVENT_COMPONENT_EVENT_CACHE].set(name, onInfo) - let offs = function () { - this[EVENT_COMPONENT_EVENT_CACHE].forEach((info: EventOnInfo, name: string) => { - EventPost.getDefault().off(info.typeName, this[name]); - }) - delete this[EVENT_COMPONENT_EVENT_OFF_HANDLER]; - delete this[EVENT_COMPONENT_EVENT_ON_HANDLER]; - delete this[EVENT_COMPONENT_EVENT_CACHE]; - }; - let ons = function () { - this[EVENT_COMPONENT_EVENT_CACHE].forEach((info: EventOnInfo, name: string) => { - this[name] = function (...args:any[]) { - info.callBack.apply(this, args); - }; - EventPost.getDefault().on(info.typeName, this[name], info.sticky, this); - }) - }; - if (target.aboutToDisappear) { - const oldFunction = target.aboutToDisappear; - target.aboutToDisappear = function () { - this[EVENT_COMPONENT_EVENT_OFF_HANDLER]() - oldFunction.call(this); - }; - } else { - target.aboutToDisappear = function () { - this[EVENT_COMPONENT_EVENT_OFF_HANDLER]() - }; + else { + let cache = target[EVENT_COMPONENT_EVENT_CACHE] as Array; + cache.push({ + typeName: TypeName, + methodName: name, + sticky: sticky, + callBack: propertyDescriptor.value + }); + //不要重复修改原型方向,减少调用堆栈深度 + return; } - if (target.aboutToAppear) { - const oldFunction = target.aboutToAppear; - target.aboutToAppear = function () { - this[EVENT_COMPONENT_EVENT_ON_HANDLER] = ons; - this[EVENT_COMPONENT_EVENT_OFF_HANDLER] = offs; - this[EVENT_COMPONENT_EVENT_ON_HANDLER]() - oldFunction.call(this); - }; - } else { - target.aboutToAppear = function () { - this[EVENT_COMPONENT_EVENT_ON_HANDLER] = ons; - this[EVENT_COMPONENT_EVENT_OFF_HANDLER] = offs; - this[EVENT_COMPONENT_EVENT_ON_HANDLER]() - }; + if (!target[LIFECYCLE_INIT]) { + LifecycleOwner(target, LIFECYCLE_DEFAULT); } - + let oldFunction = target[LIFECYCLE_INIT]; + target[LIFECYCLE_INIT] = function () { + let lifecycle: Lifecycle; + if (!this[LIFECYCLE_DEFAULT]) { + lifecycle = new Lifecycle(); + this[LIFECYCLE_DEFAULT] = lifecycle; + } + else { + lifecycle = this[LIFECYCLE_DEFAULT]; + } + let observer: LifecycleEventObserver = (source, state) => { + if (state == LifecycleState.ON_APPEAR) { + let cache = target[EVENT_COMPONENT_EVENT_CACHE] as Array; + cache.forEach(item => { + source[item.methodName] = (...args) => { + item.callBack.apply(source, args); + }; + EventPost.getDefault().on(item.typeName, source[item.methodName], item.sticky, source); + }); + } + else if (state == LifecycleState.ON_DISAPPEAR) { + let cache = target[EVENT_COMPONENT_EVENT_CACHE] as Array; + cache.forEach(item => { + EventPost.getDefault().off(item.typeName, source[item.methodName]); + }); + } + }; + lifecycle.addObserver(observer); + oldFunction.apply(this); + }; }; } \ No newline at end of file diff --git a/library/src/main/ets/EventOnInfo.ts b/library/src/main/ets/EventOnInfo.ts index f987a83833919bd89641048222198207e4bd1861..260ce5f76e9d760d20e0f65bea45bef7f6474706 100644 --- a/library/src/main/ets/EventOnInfo.ts +++ b/library/src/main/ets/EventOnInfo.ts @@ -1,5 +1,6 @@ export interface EventOnInfo { typeName: string - sticky?: boolean - callBack:Function + methodName: string + sticky: boolean + callBack: Function } \ No newline at end of file diff --git a/library/src/main/ets/EventPostExtensions.ts b/library/src/main/ets/EventPostExtensions.ts index f76e17f381f142d0171b34d699216e90c7409e32..6ebca14bfe4a5ed6107e718707bb76ea49224f76 100644 --- a/library/src/main/ets/EventPostExtensions.ts +++ b/library/src/main/ets/EventPostExtensions.ts @@ -1,7 +1,10 @@ import { EventPost } from './EventPost' import { hilog } from '@kit.PerformanceAnalysisKit' -import { EVENT_COMPONENT_EVENT_OFF_CACHE, EVENT_COMPONENT_EVENT_OFF_2_HANDLER } from './Constants' +import { Lifecycle, LifecycleOwner, LifecycleState, LIFECYCLE_DEFAULT, LIFECYCLE_INIT } from '@duke/component-lifecycle' +import { EVENT_COMPONENT_EVENT_CACHE } from './Constants' import { EventOnInfo } from './EventOnInfo' +import { systemDateTime } from '@kit.BasicServicesKit' +import { FrameNode, UIContext, uiObserver } from '@kit.ArkUI' EventPost.getDefault().onFromComponent = function (TypeName: string, component: any, callback: Function, sticky: boolean = false) { @@ -9,31 +12,107 @@ EventPost.getDefault().onFromComponent = hilog.warn(0x0000, 'EventPost', '%{public}s', 'onFormComponent component is not a component') return } - this.dispatch.on(TypeName, callback, sticky, component) - let offInfo: EventOnInfo = { typeName: TypeName, callBack: callback } - if (!component[EVENT_COMPONENT_EVENT_OFF_CACHE]) { - component[EVENT_COMPONENT_EVENT_OFF_CACHE] = new Array(); - component[EVENT_COMPONENT_EVENT_OFF_CACHE].push(offInfo) - let offs = function () { - this[EVENT_COMPONENT_EVENT_OFF_CACHE].forEach((info: EventOnInfo) => { - EventPost.getDefault().off(info.typeName, info.callBack); - }) - delete this[EVENT_COMPONENT_EVENT_OFF_CACHE]; - delete this[EVENT_COMPONENT_EVENT_OFF_2_HANDLER]; - }; - component[EVENT_COMPONENT_EVENT_OFF_2_HANDLER] = offs; - if (component.aboutToDisappear) { - const oldFunction = component.aboutToDisappear; - component.aboutToDisappear = function () { - oldFunction.call(this); - this[EVENT_COMPONENT_EVENT_OFF_2_HANDLER]() - }; - } else { - component.aboutToDisappear = function () { - this[EVENT_COMPONENT_EVENT_OFF_2_HANDLER]() - }; + let methodName = `_${systemDateTime.getTime(true)}_${Math.random().toString(36).slice(-8)}` + component[methodName] = callback + if (component[EVENT_COMPONENT_EVENT_CACHE]) { //已经注入过生命周期了 + let cache = component[EVENT_COMPONENT_EVENT_CACHE] as Array + cache.push({ + typeName: TypeName, + methodName: methodName, + sticky, + callBack: callback + }) + let uiContext: UIContext = component.getUIContext() + let uniqueId: number = component.getUniqueId(); + let frameNode: FrameNode = uiContext.getFrameNodeByUniqueId(uniqueId) + if (frameNode) { //aboutToAppear 已经执行完成,加缓存不会自动注册了,需要手动注册 + this.dispatch.on(TypeName, callback, sticky, component) } } else { - component[EVENT_COMPONENT_EVENT_OFF_CACHE].push(offInfo) + this.dispatch.on(TypeName, callback, sticky, component) + let cache = new Array() + component[EVENT_COMPONENT_EVENT_CACHE] = cache + cache.push({ + typeName: TypeName, + methodName: methodName, + sticky, + callBack: callback + }) + if (component[LIFECYCLE_INIT]) { //有生命周期注入,但是没有缓存,则添加缓存 + let keys = Object.keys(component) + for (let i = 0; i < keys.length; i++) { + let property = component[keys[i]] + if (property instanceof Lifecycle) { + property.addObserver((_, state) => { + if (state == LifecycleState.ON_DISAPPEAR) { + let cache = component[EVENT_COMPONENT_EVENT_CACHE] as Array + cache.forEach(item => { + EventPost.getDefault().off(item.typeName, component[item.methodName]); + }) + } + }) + break + } + } + } else { //没有生命周期注入,则添加生命周期 + let lifecycle = new Lifecycle() + lifecycle.addObserver((_, state) => { + if (state == LifecycleState.ON_DISAPPEAR) { + let cache = component[EVENT_COMPONENT_EVENT_CACHE] as Array + cache.forEach(item => { + EventPost.getDefault().off(item.typeName, component[item.methodName]); + }) + } + }) + component[LIFECYCLE_DEFAULT] = lifecycle + let oldAppear = component.aboutToAppear + let oldDidBuild = component.onDidBuild + LifecycleOwner(component, LIFECYCLE_DEFAULT) + //已经错误时机,需要恢复,同时手动注入 + component.aboutToAppear = oldAppear + component[LIFECYCLE_INIT].call(component) + let uiContext: UIContext = component.getUIContext() + let uniqueId: number = component.getUniqueId(); + let frameNode: FrameNode = uiContext.getFrameNodeByUniqueId(uniqueId) + if (frameNode) { //获取到frameNode 已经错过注册事件,需要把didBuild还原 + component.onDidBuild = oldDidBuild + component.lifecycleRegister.call(component) + //需要补充错过的生命周期 + if (lifecycle.isPage) { + let pageInfo = uiContext.getPageInfoByUniqueId(uniqueId) + if (lifecycle.isRouterPage) { + let pageState = pageInfo.routerPageInfo.state + if (pageState == uiObserver.RouterPageState.ON_PAGE_SHOW || + pageState == uiObserver.RouterPageState.ON_BACK_PRESS) { + lifecycle['handler'](LifecycleState.ON_SHOWED) + } else if (pageState == uiObserver.RouterPageState.ON_PAGE_HIDE) { + lifecycle['handler'](LifecycleState.ON_DISAPPEAR) + } + } else if (lifecycle.isNavigation) { + let pageState = pageInfo.navDestinationInfo.state + if (pageState == uiObserver.NavDestinationState.ON_SHOWN || + pageState == uiObserver.NavDestinationState.ON_BACKPRESS) { + lifecycle['handler'](LifecycleState.ON_SHOWED) + } else if (pageState == uiObserver.NavDestinationState.ON_HIDDEN) { + lifecycle['handler'](LifecycleState.ON_DISAPPEAR) + } else if (pageState == uiObserver.NavDestinationState.ON_WILL_SHOW) { + lifecycle['handler'](LifecycleState.ON_WILL_SHOW) + } else if (pageState == uiObserver.NavDestinationState.ON_WILL_HIDE) { + lifecycle['handler'](LifecycleState.ON_WILL_HIDE) + } else if (pageState == uiObserver.NavDestinationState.ON_ACTIVE) { + lifecycle['handler'](LifecycleState.ON_ACTIVE) + } else if (pageState == uiObserver.NavDestinationState.ON_INACTIVE) { + lifecycle['handler'](LifecycleState.ON_INACTIVE) + } + } + } else { + if (frameNode.isVisible()) { + lifecycle['handler'](LifecycleState.ON_SHOWED) + } else { + lifecycle['handler'](LifecycleState.ON_HIDDEN) + } + } + } + } } } \ No newline at end of file