From eb9ed6fba9ca0e3b9622c632a5c244265701a7c8 Mon Sep 17 00:00:00 2001 From: tian Date: Wed, 7 Aug 2024 10:26:05 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E8=87=AA=E5=AE=9A?= =?UTF-8?q?=E4=B9=89=E7=BB=84=E4=BB=B6Subscriber=E4=BF=AE=E9=A5=B0?= =?UTF-8?q?=E5=90=8E=E5=A4=9A=E4=B8=AA=E5=AE=9E=E4=BE=8B=E5=8F=AA=E6=9C=89?= =?UTF-8?q?=E4=B8=80=E4=B8=AA=E5=AE=9E=E4=BE=8B=E6=94=B6=E5=88=B0=E6=B6=88?= =?UTF-8?q?=E6=81=AF=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- entry/src/main/ets/pages/CustView.ets | 16 +++++++++ entry/src/main/ets/pages/Index.ets | 5 +++ library/src/main/ets/Decorators.ts | 47 +++++++++++++++------------ 3 files changed, 47 insertions(+), 21 deletions(-) create mode 100644 entry/src/main/ets/pages/CustView.ets diff --git a/entry/src/main/ets/pages/CustView.ets b/entry/src/main/ets/pages/CustView.ets new file mode 100644 index 0000000..c23b1ad --- /dev/null +++ b/entry/src/main/ets/pages/CustView.ets @@ -0,0 +1,16 @@ +import { Subscriber } from 'EventPost' + +@Component +export struct CustomView { + @State name: string = "CustView" + + build() { + Text(this.name) + } + + @Subscriber("test1") + onReceived(event: object) { + console.error("收到 test1 " + JSON.stringify(event)) + this.name = '收到 test1: ' + event + } +} \ No newline at end of file diff --git a/entry/src/main/ets/pages/Index.ets b/entry/src/main/ets/pages/Index.ets index f205d3e..9189d22 100644 --- a/entry/src/main/ets/pages/Index.ets +++ b/entry/src/main/ets/pages/Index.ets @@ -1,5 +1,6 @@ import { router } from '@kit.ArkUI'; import { EventPost, Subscriber } from 'EventPost/Index'; +import { CustomView } from './CustView'; @Entry @Component @@ -9,6 +10,10 @@ struct Index { build() { Row() { Column() { + CustomView({name: "我是1"}) + CustomView({name: "我是2"}) + CustomView({name: "我是3"}) + CustomView({name: "我是4"}) Text(this.message) .fontSize(50) .fontWeight(FontWeight.Bold) diff --git a/library/src/main/ets/Decorators.ts b/library/src/main/ets/Decorators.ts index 31e1cf3..46061bb 100644 --- a/library/src/main/ets/Decorators.ts +++ b/library/src/main/ets/Decorators.ts @@ -1,34 +1,39 @@ -import { EventPost } from './EventPost' +import { EventPost } from './EventPost'; export function Subscriber(TypeName: string, sticky: boolean = false) { return (target: any, _: string, propertyDescriptor: PropertyDescriptor) => { if (target.rerender) { + const originalMethod = propertyDescriptor.value; + if (target.aboutToDisappear) { - let oldFunction = target.aboutToDisappear - function disappear(){ - EventPost.getDefault().off(TypeName, propertyDescriptor.value) - oldFunction.call(this) - } - target.aboutToDisappear = disappear + const oldFunction = target.aboutToDisappear; + target.aboutToDisappear = function() { + EventPost.getDefault().off(TypeName, this._subscriberCallback); + oldFunction.call(this); + }; } else { - target.aboutToDisappear = () => { - EventPost.getDefault().off(TypeName, propertyDescriptor.value) - } + target.aboutToDisappear = function() { + EventPost.getDefault().off(TypeName, this._subscriberCallback); + }; } if (target.aboutToAppear) { - let oldFunction = target.aboutToAppear - function appear(){ - EventPost.getDefault().on(TypeName, propertyDescriptor.value, sticky, this) - oldFunction.call(this) - } - target.aboutToAppear = appear + const oldFunction = target.aboutToAppear; + target.aboutToAppear = function() { + this._subscriberCallback = (...args: any[]) => { + originalMethod.apply(this, args); + }; + EventPost.getDefault().on(TypeName, this._subscriberCallback, sticky, this); + oldFunction.call(this); + }; } else { - function appear(){ - EventPost.getDefault().on(TypeName, propertyDescriptor.value, sticky, this) - } - target.aboutToAppear = appear + target.aboutToAppear = function() { + this._subscriberCallback = (...args: any[]) => { + originalMethod.apply(this, args); + }; + EventPost.getDefault().on(TypeName, this._subscriberCallback, sticky, this); + }; } } - } + }; } \ No newline at end of file -- Gitee