1# @ohos.events.emitter (Emitter) 2 3The **Emitter** module provides the capabilities of sending and processing inter- or intra-thread events in a process. You can use the APIs of this module to subscribe to an event in persistent or one-shot manner, unsubscribe from an event, or emit an event to the event queue. 4 5> **NOTE** 6> 7> The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version. 8 9## Modules to Import 10 11```ts 12import { emitter } from '@kit.BasicServicesKit'; 13``` 14 15## Required Permissions 16 17None. 18 19## emitter.on 20 21on(event: InnerEvent, callback: Callback\<EventData\>): void 22 23Subscribes to an event in persistent manner and executes a callback after the event is received. 24 25**Atomic service API**: This API can be used in atomic services since API version 11. 26 27**System capability**: SystemCapability.Notification.Emitter 28 29**Parameters** 30 31| Name | Type | Mandatory| Description | 32| -------- | ----------------------------------- | ---- | ------------------------------------------------------------ | 33| event | [InnerEvent](#innerevent) | Yes | Event to subscribe to in persistent manner. The [EventPriority](#eventpriority) parameter is not required and does not take effect.| 34| callback | Callback\<[EventData](#eventdata)\> | Yes | Callback to be executed when the event is received. | 35 36**Example** 37 38```ts 39import { Callback } from '@kit.BasicServicesKit'; 40 41let innerEvent: emitter.InnerEvent = { 42 eventId: 1 43}; 44 45let callback: Callback<emitter.EventData> = (eventData: emitter.EventData) => { 46 console.info(`eventData: ${JSON.stringify(eventData)}`); 47} 48 49// Execute the callback after receiving the event whose eventId is 1. 50emitter.on(innerEvent, callback); 51``` 52 53## emitter.on<sup>11+</sup> 54 55on(eventId: string, callback: Callback\<EventData\>): void 56 57Subscribes to an event in persistent manner and executes a callback after the event is received. 58 59**Atomic service API**: This API can be used in atomic services since API version 11. 60 61**System capability**: SystemCapability.Notification.Emitter 62 63**Parameters** 64 65| Name | Type | Mandatory| Description | 66| -------- | ----------------------------------- | ---- | -------------------------------------- | 67| eventId | string | Yes | Event to subscribe to in persistent manner. The value cannot be an empty string and exceed 10240 bytes. | 68| callback | Callback\<[EventData](#eventdata)\> | Yes | Callback to be executed when the event is received.| 69 70**Example** 71 72```ts 73import { Callback } from '@kit.BasicServicesKit'; 74 75let callback: Callback<emitter.EventData> = (eventData: emitter.EventData) => { 76 console.info(`eventData: ${JSON.stringify(eventData)}`); 77} 78// Execute the callback after receiving the event whose eventId is eventId. 79emitter.on(`eventId`, callback); 80``` 81 82## emitter.on<sup>12+</sup> 83 84on<T\>(eventId: string, callback: Callback\<GenericEventData<T\>\>): void 85 86Subscribes to an event in persistent manner and executes a callback after the event is received. 87 88**Atomic service API**: This API can be used in atomic services since API version 12. 89 90**System capability**: SystemCapability.Notification.Emitter 91 92**Parameters** 93 94| Name | Type | Mandatory| Description | 95| -------- | ----------------------------------- | ---- | -------------------------------------- | 96| eventId | string | Yes | Event to subscribe to in persistent manner. The value cannot be an empty string and exceed 10240 bytes. | 97| callback | Callback\<[GenericEventData<T\>](#genericeventdatat12)\> | Yes | Callback to be executed when the event is received.| 98 99**Example** 100 101```ts 102import { Callback } from '@kit.BasicServicesKit'; 103 104@Sendable 105class Sample { 106 constructor() { 107 this.count = 100; 108 } 109 printCount() { 110 console.info('Print count : ' + this.count); 111 } 112 count: number; 113} 114 115let callback: Callback<emitter.GenericEventData<Sample>> = (eventData: emitter.GenericEventData<Sample>): void => { 116 console.info(`eventData: ${JSON.stringify(eventData?.data)}`); 117 if (eventData?.data instanceof Sample) { 118 eventData?.data?.printCount(); 119 } 120} 121// Execute the callback after receiving the event whose eventId is eventId. 122emitter.on("eventId", callback); 123``` 124 125## emitter.once 126 127once(event: InnerEvent, callback: Callback\<EventData\>): void 128 129Subscribes to an event in one-shot manner and unsubscribes from it after the event callback is executed. 130 131**Atomic service API**: This API can be used in atomic services since API version 11. 132 133**System capability**: SystemCapability.Notification.Emitter 134 135**Parameters** 136 137| Name | Type | Mandatory| Description | 138| -------- | ----------------------------------- | ---- | ------------------------------------------------------------ | 139| event | [InnerEvent](#innerevent) | Yes | Event to subscribe to in one-shot manner. The [EventPriority](#eventpriority) parameter is not required and does not take effect.| 140| callback | Callback\<[EventData](#eventdata)\> | Yes | Callback to be executed when the event is received. | 141 142**Example** 143 144```ts 145import { Callback } from '@kit.BasicServicesKit'; 146 147let innerEvent: emitter.InnerEvent = { 148 eventId: 1 149}; 150 151let callback: Callback<emitter.EventData> = (eventData: emitter.EventData) => { 152 console.info(`eventData: ${JSON.stringify(eventData)}`); 153} 154// Execute the callback after receiving the event whose eventId is 1. 155emitter.once(innerEvent, callback); 156``` 157 158## emitter.once<sup>11+</sup> 159 160once(eventId: string, callback: Callback\<EventData\>): void 161 162Subscribes to an event in one-shot manner and unsubscribes from it after the event callback is executed. 163 164**Atomic service API**: This API can be used in atomic services since API version 11. 165 166**System capability**: SystemCapability.Notification.Emitter 167 168**Parameters** 169 170| Name | Type | Mandatory| Description | 171| -------- | ----------------------------------- | ---- | -------------------------------------- | 172| eventId | string | Yes | Event to subscribe to in one-shot manner. The value cannot be an empty string and exceed 10240 bytes. | 173| callback | Callback\<[EventData](#eventdata)\> | Yes | Callback to be executed when the event is received.| 174 175**Example** 176 177```ts 178import { Callback } from '@kit.BasicServicesKit'; 179 180let callback: Callback<emitter.EventData> = (eventData: emitter.EventData) => { 181 console.info(`eventData: ${JSON.stringify(eventData)}`); 182} 183// Execute the callback after receiving the event whose eventId is eventId. 184emitter.once("eventId", callback); 185``` 186 187## emitter.once<sup>12+</sup> 188 189once<T\>(eventId: string, callback: Callback\<GenericEventData<T\>\>): void 190 191Subscribes to an event in one-shot manner and unsubscribes from it after the event callback is executed. 192 193**Atomic service API**: This API can be used in atomic services since API version 12. 194 195**System capability**: SystemCapability.Notification.Emitter 196 197**Parameters** 198 199| Name | Type | Mandatory| Description | 200| -------- | ----------------------------------- | ---- | -------------------------------------- | 201| eventId | string | Yes | Event to subscribe to in one-shot manner. The value cannot be an empty string and exceed 10240 bytes. | 202| callback | Callback\<[GenericEventData<T\>](#genericeventdatat12)\> | Yes | Callback to be executed when the event is received.| 203 204**Example** 205 206```ts 207import { Callback } from '@kit.BasicServicesKit'; 208 209@Sendable 210class Sample { 211 constructor() { 212 this.count = 100; 213 } 214 printCount() { 215 console.info('Print count : ' + this.count); 216 } 217 count: number; 218} 219 220let callback: Callback<emitter.GenericEventData<Sample>> = (eventData: emitter.GenericEventData<Sample>): void => { 221 console.info(`eventData: ${JSON.stringify(eventData?.data)}`); 222 if (eventData?.data instanceof Sample) { 223 eventData?.data?.printCount(); 224 } 225} 226// Execute the callback after receiving the event whose eventId is eventId. 227emitter.once("eventId", callback); 228``` 229 230## emitter.off 231 232off(eventId: number): void 233 234Unsubscribes from all events with the specified event ID. 235 236After this API is used to unsubscribe from an event, the event that has been published through the [emit](#emitteremit) API but has not been executed will be unsubscribed. 237 238**Atomic service API**: This API can be used in atomic services since API version 11. 239 240**System capability**: SystemCapability.Notification.Emitter 241 242**Parameters** 243 244| Name | Type | Mandatory| Description | 245| ------- | ------ | ---- | -------- | 246| eventId | number | Yes | Event ID.| 247 248**Example** 249 250```ts 251// Unregister the callbacks of all events whose eventID is 1. 252emitter.off(1); 253``` 254 255## emitter.off<sup>11+</sup> 256 257off(eventId: string): void 258 259Unsubscribes from all events with the specified event ID. 260 261After this API is used to unsubscribe from an event, the event that has been published through the [emit](#emitteremit11) API but has not been executed will be unsubscribed. 262 263**Atomic service API**: This API can be used in atomic services since API version 11. 264 265**System capability**: SystemCapability.Notification.Emitter 266 267**Parameters** 268 269| Name | Type | Mandatory| Description | 270| ------- | ------ | ---- | -------- | 271| eventId | string | Yes | Event ID. The value cannot be an empty string and exceed 10240 bytes.| 272 273**Example** 274 275```ts 276// Unregister the callbacks of all events whose eventID is eventId. 277emitter.off("eventId"); 278``` 279 280## emitter.off<sup>10+</sup> 281 282off(eventId: number, callback: Callback\<EventData\>): void 283 284Unsubscribes from an event with the specified event ID and processed by the specified callback. This API takes effect only when **Callback\<EventData>** has been registered through the [on](#emitteron) or [once](#emitteronce) API. Otherwise, no processing is performed. 285 286After this API is used to unsubscribe from an event, the event that has been published through the [emit](#emitteremit) API but has not been executed will be unsubscribed. 287 288**Atomic service API**: This API can be used in atomic services since API version 11. 289 290**System capability**: SystemCapability.Notification.Emitter 291 292**Parameters** 293 294| Name | Type | Mandatory| Description | 295| ------- | ------ | ---- | ------ | 296| eventId | number | Yes | Event ID.| 297| callback | Callback\<[EventData](#eventdata)\> | Yes | Callback to unregister. | 298 299**Example** 300 301```ts 302import { Callback } from '@kit.BasicServicesKit'; 303 304let callback: Callback<emitter.EventData> = (eventData: emitter.EventData) => { 305 console.info(`eventData: ${JSON.stringify(eventData)}`); 306} 307// Unregister the callback of the event whose eventID is 1. The callback object must be the registered object. 308// If the callback has not been registered, no processing is performed. 309emitter.off(1, callback); 310``` 311 312## emitter.off<sup>11+</sup> 313 314off(eventId: string, callback: Callback\<EventData\>): void 315 316Unsubscribes from an event with the specified event ID and processed by the specified callback. This API takes effect only when **Callback\<EventData>** has been registered through the [on](#emitteron11) or [once](#emitteronce11) API. Otherwise, no processing is performed. 317 318After this API is used to unsubscribe from an event, the event that has been published through the [emit](#emitteremit11) API but has not been executed will be unsubscribed. 319 320**Atomic service API**: This API can be used in atomic services since API version 11. 321 322**System capability**: SystemCapability.Notification.Emitter 323 324**Parameters** 325 326| Name | Type | Mandatory| Description | 327| -------- | ----------------------------------- | ---- | -------------------------- | 328| eventId | string | Yes | Event ID. The value cannot be an empty string and exceed 10240 bytes. | 329| callback | Callback\<[EventData](#eventdata)\> | Yes | Callback to unregister.| 330 331**Example** 332 333```ts 334import { Callback } from '@kit.BasicServicesKit'; 335 336let callback: Callback<emitter.EventData> = (eventData: emitter.EventData) => { 337 console.info(`eventData: ${JSON.stringify(eventData)}`); 338} 339// Unregister the callback of the event whose eventID is eventId. The callback object must be the registered object. 340// If the callback has not been registered, no processing is performed. 341emitter.off("eventId", callback); 342``` 343 344## emitter.off<sup>12+</sup> 345 346off<T\>(eventId: string, callback: Callback\<GenericEventData<T\>\>): void 347 348Unsubscribes from an event with the specified event ID and processed by the specified callback. This API takes effect only when **Callback\<EventData>** has been registered through the [on](#emitteron12) or [once](#emitteronce12) API. Otherwise, no processing is performed. 349 350After this API is used to unsubscribe from an event, the event that has been published through the [emit](#emitteremit12) API but has not been executed will be unsubscribed. 351 352**Atomic service API**: This API can be used in atomic services since API version 12. 353 354**System capability**: SystemCapability.Notification.Emitter 355 356**Parameters** 357 358| Name | Type | Mandatory| Description | 359| -------- | ----------------------------------- | ---- | -------------------------- | 360| eventId | string | Yes | Event ID. The value cannot be an empty string and exceed 10240 bytes. | 361| callback | Callback\<[GenericEventData<T\>](#genericeventdatat12)\> | Yes | Callback to unregister.| 362 363**Example** 364 365```ts 366import { Callback } from '@kit.BasicServicesKit'; 367 368@Sendable 369class Sample { 370 constructor() { 371 this.count = 100; 372 } 373 printCount() { 374 console.info('Print count : ' + this.count); 375 } 376 count: number; 377} 378 379let callback: Callback<emitter.GenericEventData<Sample>> = (eventData: emitter.GenericEventData<Sample>): void => { 380 console.info(`eventData: ${JSON.stringify(eventData?.data)}`); 381 if (eventData?.data instanceof Sample) { 382 eventData?.data?.printCount(); 383 } 384} 385// Unregister the callback of the event whose eventID is eventId. The callback object must be the registered object. 386// If the callback has not been registered, no processing is performed. 387emitter.off("eventId", callback); 388``` 389 390## emitter.emit 391 392emit(event: InnerEvent, data?: EventData): void 393 394Emits the specified event. 395 396This API can be used to emit data objects across threads. The data objects must meet the specifications specified in [Overview of Inter-thread Communication Objects](../../arkts-utils/serializable-overview.md). Currently, complex data decorated by decorators such as [@State](../../ui/state-management/arkts-state.md) and [@Observed](../../ui/state-management/arkts-observed-and-objectlink.md) is not supported. 397 398**Atomic service API**: This API can be used in atomic services since API version 11. 399 400**System capability**: SystemCapability.Notification.Emitter 401 402**Parameters** 403 404| Name| Type | Mandatory| Description | 405| ------ | ------------------------- | ---- | ------------- | 406| event | [InnerEvent](#innerevent) | Yes | Event to emit, where [EventPriority](#eventpriority) specifies the emit priority of the event.| 407| data | [EventData](#eventdata) | No | Data passed in the event.| 408 409**Example** 410 411```ts 412let eventData: emitter.EventData = { 413 data: { 414 "content": "content", 415 "id": 1, 416 } 417}; 418 419let innerEvent: emitter.InnerEvent = { 420 eventId: 1, 421 priority: emitter.EventPriority.HIGH 422}; 423 424emitter.emit(innerEvent, eventData); 425``` 426 427## emitter.emit<sup>11+</sup> 428 429emit(eventId: string, data?: EventData): void 430 431Emits the specified event. 432 433This API can be used to emit data objects across threads. The data objects must meet the specifications specified in [Overview of Inter-thread Communication Objects](../../arkts-utils/serializable-overview.md). Currently, complex data decorated by decorators such as [@State](../../ui/state-management/arkts-state.md) and [@Observed](../../ui/state-management/arkts-observed-and-objectlink.md) is not supported. 434 435**Atomic service API**: This API can be used in atomic services since API version 11. 436 437**System capability**: SystemCapability.Notification.Emitter 438 439**Parameters** 440 441| Name | Type | Mandatory| Description | 442| ------- | ----------------------- | ---- | ---------------- | 443| eventId | string | Yes | ID of the event to emit. The value cannot be an empty string and exceed 10240 bytes. | 444| data | [EventData](#eventdata) | No | Data passed in the event.| 445 446**Example** 447 448```ts 449let eventData: emitter.EventData = { 450 data: { 451 "content": "content", 452 "id": 1, 453 } 454}; 455 456emitter.emit("eventId", eventData); 457``` 458 459## emitter.emit<sup>12+</sup> 460 461emit<T\>(eventId: string, data?: GenericEventData<T\>): void 462 463Emits the specified event. 464 465This API can be used to emit data objects across threads. The data objects must meet the specifications specified in [Overview of Inter-thread Communication Objects](../../arkts-utils/serializable-overview.md). Currently, complex data decorated by decorators such as [@State](../../ui/state-management/arkts-state.md) and [@Observed](../../ui/state-management/arkts-observed-and-objectlink.md) is not supported. 466 467**Atomic service API**: This API can be used in atomic services since API version 12. 468 469**System capability**: SystemCapability.Notification.Emitter 470 471**Parameters** 472 473| Name | Type | Mandatory| Description | 474| ------- | ----------------------- | ---- | ---------------- | 475| eventId | string | Yes | ID of the event to emit. The value cannot be an empty string and exceed 10240 bytes. | 476| data | [GenericEventData<T\>](#genericeventdatat12) | No | Data passed in the event.| 477 478**Example** 479 480```ts 481@Sendable 482class Sample { 483 constructor() { 484 this.count = 100; 485 } 486 printCount() { 487 console.info('Print count : ' + this.count); 488 } 489 count: number; 490} 491 492let eventData: emitter.GenericEventData<Sample> = { 493 data: new Sample() 494}; 495emitter.emit("eventId", eventData); 496``` 497 498## emitter.emit<sup>11+</sup> 499 500emit(eventId: string, options: Options, data?: EventData): void 501 502Emits an event of a specified priority. 503 504This API can be used to emit data objects across threads. The data objects must meet the specifications specified in [Overview of Inter-thread Communication Objects](../../arkts-utils/serializable-overview.md). Currently, complex data decorated by decorators such as [@State](../../ui/state-management/arkts-state.md) and [@Observed](../../ui/state-management/arkts-observed-and-objectlink.md) is not supported. 505 506**Atomic service API**: This API can be used in atomic services since API version 11. 507 508**System capability**: SystemCapability.Notification.Emitter 509 510**Parameters** 511 512| Name | Type | Mandatory| Description | 513| ------- | ----------------------- | ---- | ---------------- | 514| eventId | string | Yes | ID of the event to emit. The value cannot be an empty string and exceed 10240 bytes. | 515| options | [Options](#options11) | Yes | Event emit priority. | 516| data | [EventData](#eventdata) | No | Data passed in the event.| 517 518**Example** 519 520```ts 521let eventData: emitter.EventData = { 522 data: { 523 "content": "content", 524 "id": 1, 525 } 526}; 527 528let options: emitter.Options = { 529 priority: emitter.EventPriority.HIGH 530}; 531 532emitter.emit("eventId", options, eventData); 533``` 534 535## emitter.emit<sup>12+</sup> 536 537emit<T\>(eventId: string, options: Options, data?: GenericEventData<T\>): void 538 539Emits an event of a specified priority. 540 541This API can be used to emit data objects across threads. The data objects must meet the specifications specified in [Overview of Inter-thread Communication Objects](../../arkts-utils/serializable-overview.md). Currently, complex data decorated by decorators such as [@State](../../ui/state-management/arkts-state.md) and [@Observed](../../ui/state-management/arkts-observed-and-objectlink.md) is not supported. 542 543**Atomic service API**: This API can be used in atomic services since API version 12. 544 545**System capability**: SystemCapability.Notification.Emitter 546 547**Parameters** 548 549| Name | Type | Mandatory| Description | 550| ------- | ----------------------- | ---- | ---------------- | 551| eventId | string | Yes | ID of the event to emit. The value cannot be an empty string and exceed 10240 bytes. | 552| options | [Options](#options11) | Yes | Event emit priority. | 553| data | [GenericEventData<T\>](#genericeventdatat12) | No | Data passed in the event.| 554 555**Example** 556 557```ts 558@Sendable 559class Sample { 560 constructor() { 561 this.count = 100; 562 } 563 printCount() { 564 console.info('Print count : ' + this.count); 565 } 566 count: number; 567} 568 569let options: emitter.Options = { 570 priority: emitter.EventPriority.HIGH 571}; 572let eventData: emitter.GenericEventData<Sample> = { 573 data: new Sample() 574}; 575 576emitter.emit("eventId", options, eventData); 577``` 578 579## emitter.getListenerCount<sup>11+</sup> 580 581getListenerCount(eventId: number | string): number 582 583Obtains the number of subscriptions to a specified event. 584 585**Atomic service API**: This API can be used in atomic services since API version 11. 586 587**System capability**: SystemCapability.Notification.Emitter 588 589**Parameters** 590 591| Name | Type | Mandatory| Description | 592| ------- | -------------- | ---- | -------- | 593| eventId | number \| string | Yes | Event ID, which is a custom string. The value cannot be an empty string and exceed 10240 bytes.| 594 595**Returns** 596 597| Type | Description | 598| ------- |------------| 599| number | Number of subscriptions to a specified event.| 600 601 602**Example** 603 604```ts 605let count = emitter.getListenerCount("eventId"); 606``` 607 608## EventPriority 609 610Enumerates the event priorities. 611 612**Atomic service API**: This API can be used in atomic services since API version 11. 613 614**System capability**: SystemCapability.Notification.Emitter 615 616| Name | Value | Description | 617| --------- | ---- | --------------------------------------------------- | 618| IMMEDIATE | 0 | The event will be emitted immediately. | 619| HIGH | 1 | The event will be emitted before low-priority events. | 620| LOW | 2 | The event will be emitted before idle-priority events. By default, an event is in LOW priority. | 621| IDLE | 3 | The event will be emitted after all the other events. | 622 623## InnerEvent 624 625Describes an event to subscribe to or emit. The **EventPriority** settings do not take effect under event subscription. 626 627**Atomic service API**: This API can be used in atomic services since API version 11. 628 629**System capability**: SystemCapability.Notification.Emitter 630 631| Name | Type | Read Only| Optional| Description | 632| -------- | ------------------------------- | ---- | ---- | ------------------------------ | 633| eventId | number | No | No | Event ID.| 634| priority | [EventPriority](#eventpriority) | No | Yes | Event priority. The default value is **EventPriority.LOW**. | 635 636## EventData 637 638Describes the data passed in the event. 639 640**Atomic service API**: This API can be used in atomic services since API version 11. 641 642**System capability**: SystemCapability.Notification.Emitter 643 644| Name| Type | Read Only| Optional| Description | 645| ---- | ------------------ | ---- | ---- | -------------- | 646| data | { [key: string]: any } | No | Yes | Data passed in the event. The value can be in any of the following types: Array, ArrayBuffer, Boolean, DataView, Date, Error, Map, Number, Object, Primitive (except symbol), RegExp, Set, String, and TypedArray. The maximum data size is 16 MB.| 647 648## Options<sup>11+</sup> 649 650Describes the event emit priority. 651 652**Atomic service API**: This API can be used in atomic services since API version 12. 653 654**System capability**: SystemCapability.Notification.Emitter 655 656| Name | Type | Read Only| Optional| Description | 657| -------- | ------------------------------- | ---- | ---- | -------------- | 658| priority | [EventPriority](#eventpriority) | No | Yes | Event priority. The default value is **EventPriority.LOW**.| 659 660## GenericEventData<T\><sup>12+</sup> 661 662Describes the generic data passed in the event. 663 664**Atomic service API**: This API can be used in atomic services since API version 12. 665 666**System capability**: SystemCapability.Notification.Emitter 667 668| Name | Type | Read Only| Optional| Description | 669| -------- | ------------------------------- | ---- | ---- | -------------- | 670| data | T | No | Yes | Data passed in the event. **T**: generic type.| 671