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 39let innerEvent: emitter.InnerEvent = { 40 eventId: 1 41}; 42 43// Execute the callback after receiving the event whose eventId is 1. 44emitter.on(innerEvent, () => { 45 console.info('callback'); 46}); 47``` 48 49## emitter.on<sup>11+</sup> 50 51on(eventId: string, callback: Callback\<EventData\>): void 52 53Subscribes to an event in persistent manner and executes a callback after the event is received. 54 55**Atomic service API**: This API can be used in atomic services since API version 11. 56 57**System capability**: SystemCapability.Notification.Emitter 58 59**Parameters** 60 61| Name | Type | Mandatory| Description | 62| -------- | ----------------------------------- | ---- | -------------------------------------- | 63| eventId | string | Yes | Event to subscribe to in persistent manner. The value cannot be an empty string and exceed 10240 bytes. | 64| callback | Callback\<[EventData](#eventdata)\> | Yes | Callback to be executed when the event is received.| 65 66**Example** 67 68```ts 69// Execute the callback after receiving the event whose eventId is eventId. 70emitter.on("eventId", () => { 71 console.info('callback'); 72}); 73``` 74 75## emitter.on<sup>12+</sup> 76 77on<T\>(eventId: string, callback: Callback\<GenericEventData<T\>\>): void 78 79Subscribes to an event in persistent manner and executes a callback after the event is received. 80 81**Atomic service API**: This API can be used in atomic services since API version 12. 82 83**System capability**: SystemCapability.Notification.Emitter 84 85**Parameters** 86 87| Name | Type | Mandatory| Description | 88| -------- | ----------------------------------- | ---- | -------------------------------------- | 89| eventId | string | Yes | Event to subscribe to in persistent manner. The value cannot be an empty string and exceed 10240 bytes. | 90| callback | Callback\<[GenericEventData<T\>](#genericeventdatat12)\> | Yes | Callback to be executed when the event is received.| 91 92**Example** 93 94```ts 95@Sendable 96class Sample { 97 constructor() { 98 this.count = 100; 99 } 100 printCount() { 101 console.info('Print count : ' + this.count); 102 } 103 count: number; 104} 105 106let callback = (eventData: emitter.GenericEventData<Sample>): void => { 107 let storage: Sample = eventData.data!; 108 storage.printCount(); 109} 110// Execute the callback after receiving the event whose eventId is eventId. 111emitter.on("eventId", callback); 112``` 113 114## emitter.once 115 116once(event: InnerEvent, callback: Callback\<EventData\>): void 117 118Subscribes to an event in one-shot manner and unsubscribes from it after the event callback is executed. 119 120**Atomic service API**: This API can be used in atomic services since API version 11. 121 122**System capability**: SystemCapability.Notification.Emitter 123 124**Parameters** 125 126| Name | Type | Mandatory| Description | 127| -------- | ----------------------------------- | ---- | ------------------------------------------------------------ | 128| event | [InnerEvent](#innerevent) | Yes | Event to subscribe to in one-shot manner. The [EventPriority](#eventpriority) parameter is not required and does not take effect.| 129| callback | Callback\<[EventData](#eventdata)\> | Yes | Callback to be executed when the event is received. | 130 131**Example** 132 133```ts 134let innerEvent: emitter.InnerEvent = { 135 eventId: 1 136}; 137 138// Execute the callback after receiving the event whose eventId is 1. 139emitter.once(innerEvent, () => { 140 console.info('once callback'); 141}); 142``` 143 144## emitter.once<sup>11+</sup> 145 146once(eventId: string, callback: Callback\<EventData\>): void 147 148Subscribes to an event in one-shot manner and unsubscribes from it after the event callback is executed. 149 150**Atomic service API**: This API can be used in atomic services since API version 11. 151 152**System capability**: SystemCapability.Notification.Emitter 153 154**Parameters** 155 156| Name | Type | Mandatory| Description | 157| -------- | ----------------------------------- | ---- | -------------------------------------- | 158| eventId | string | Yes | Event to subscribe to in one-shot manner. The value cannot be an empty string and exceed 10240 bytes. | 159| callback | Callback\<[EventData](#eventdata)\> | Yes | Callback to be executed when the event is received.| 160 161**Example** 162 163```ts 164// Execute the callback after receiving the event whose eventId is eventId. 165emitter.once("eventId", () => { 166 console.info('once callback'); 167}); 168``` 169 170## emitter.once<sup>12+</sup> 171 172once<T\>(eventId: string, callback: Callback\<GenericEventData<T\>\>): void 173 174Subscribes to an event in one-shot manner and unsubscribes from it after the event callback is executed. 175 176**Atomic service API**: This API can be used in atomic services since API version 12. 177 178**System capability**: SystemCapability.Notification.Emitter 179 180**Parameters** 181 182| Name | Type | Mandatory| Description | 183| -------- | ----------------------------------- | ---- | -------------------------------------- | 184| eventId | string | Yes | Event to subscribe to in one-shot manner. The value cannot be an empty string and exceed 10240 bytes. | 185| callback | Callback\<[GenericEventData<T\>](#genericeventdatat12)\> | Yes | Callback to be executed when the event is received.| 186 187**Example** 188 189```ts 190@Sendable 191class Sample { 192 constructor() { 193 this.count = 100; 194 } 195 printCount() { 196 console.info('Print count : ' + this.count); 197 } 198 count: number; 199} 200 201let callback = (eventData: emitter.GenericEventData<Sample>): void => { 202 let storage: Sample = eventData.data!; 203 storage.printCount(); 204} 205// Execute the callback after receiving the event whose eventId is eventId. 206emitter.once("eventId", callback); 207``` 208 209## emitter.off 210 211off(eventId: number): void 212 213Unsubscribes from an event. 214 215**Atomic service API**: This API can be used in atomic services since API version 11. 216 217**System capability**: SystemCapability.Notification.Emitter 218 219**Parameters** 220 221| Name | Type | Mandatory| Description | 222| ------- | ------ | ---- | -------- | 223| eventId | number | Yes | Event ID.| 224 225**Example** 226 227```ts 228// Unregister the callbacks of all events whose eventID is 1. 229emitter.off(1); 230``` 231 232## emitter.off<sup>11+</sup> 233 234off(eventId: string): void 235 236Unsubscribes from an event. 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 | string | Yes | Event ID. The value cannot be an empty string and exceed 10240 bytes.| 247 248**Example** 249 250```ts 251// Unregister the callbacks of all events whose eventID is eventId. 252emitter.off("eventId"); 253``` 254 255## emitter.off<sup>10+</sup> 256 257off(eventId: number, callback: Callback\<EventData\>): void 258 259Unsubscribes from an event. If **Callback\<EventData\>** has been registered through the **on** or **once** API, it is unregistered. Otherwise, no processing is performed. 260 261**Atomic service API**: This API can be used in atomic services since API version 11. 262 263**System capability**: SystemCapability.Notification.Emitter 264 265**Parameters** 266 267| Name | Type | Mandatory| Description | 268| ------- | ------ | ---- | ------ | 269| eventId | number | Yes | Event ID.| 270| callback | Callback\<[EventData](#eventdata)\> | Yes |Callback to unregister. | 271 272**Example** 273 274```ts 275// Unregister the callbacks of all events whose eventID is 1. 276// If the callback has not been registered, no processing is performed. 277emitter.off(1, () => { 278 console.info('callback'); 279}); 280``` 281 282## emitter.off<sup>11+</sup> 283 284off(eventId: string, callback: Callback\<EventData\>): void 285 286Unsubscribes from an event. If **Callback\<EventData\>** has been registered through the **on** or **once** API, it is unregistered. Otherwise, no processing is performed. 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 | string | Yes | Event ID. The value cannot be an empty string and exceed 10240 bytes. | 297| callback | Callback\<[EventData](#eventdata)\> | Yes | Callback to unregister.| 298 299**Example** 300 301```ts 302// Unregister the callback of the event whose eventID is eventId. 303// If the callback has not been registered, no processing is performed. 304emitter.off("eventId", () => { 305 console.info('callback'); 306}); 307``` 308 309## emitter.off<sup>12+</sup> 310 311off<T\>(eventId: string, callback: Callback\<GenericEventData<T\>\>): void 312 313Unsubscribes from an event. If **Callback\<GenericEventData<T\>\>** has been registered through the **on** or **once** API, it is unregistered. Otherwise, no processing is performed. 314 315**Atomic service API**: This API can be used in atomic services since API version 12. 316 317**System capability**: SystemCapability.Notification.Emitter 318 319**Parameters** 320 321| Name | Type | Mandatory| Description | 322| -------- | ----------------------------------- | ---- | -------------------------- | 323| eventId | string | Yes | Event ID. The value cannot be an empty string and exceed 10240 bytes. | 324| callback | Callback\<[GenericEventData<T\>](#genericeventdatat12)\> | Yes | Callback to unregister.| 325 326**Example** 327 328```ts 329@Sendable 330class Sample { 331 constructor() { 332 this.count = 100; 333 } 334 printCount() { 335 console.info('Print count : ' + this.count); 336 } 337 count: number; 338} 339 340let callback = (eventData: emitter.GenericEventData<Sample>): void => { 341 let storage: Sample = eventData.data!; 342 storage.printCount(); 343} 344// Unregister the callback of the event whose eventID is eventId. 345// If the callback has not been registered, no processing is performed. 346emitter.off("eventId", callback); 347``` 348 349## emitter.emit 350 351emit(event: InnerEvent, data?: EventData): void 352 353Emits the specified event. 354 355**Atomic service API**: This API can be used in atomic services since API version 11. 356 357**System capability**: SystemCapability.Notification.Emitter 358 359**Parameters** 360 361| Name| Type | Mandatory| Description | 362| ------ | ------------------------- | ---- | ------------- | 363| event | [InnerEvent](#innerevent) | Yes | Event to emit, where [EventPriority](#eventpriority) specifies the emit priority of the event.| 364| data | [EventData](#eventdata) | No | Data passed in the event.| 365 366**Example** 367 368```ts 369let eventData: emitter.EventData = { 370 data: { 371 "content": "content", 372 "id": 1, 373 } 374}; 375 376let innerEvent: emitter.InnerEvent = { 377 eventId: 1, 378 priority: emitter.EventPriority.HIGH 379}; 380 381emitter.emit(innerEvent, eventData); 382``` 383 384## emitter.emit<sup>11+</sup> 385 386emit(eventId: string, data?: EventData): void 387 388Emits the specified event. 389 390**Atomic service API**: This API can be used in atomic services since API version 11. 391 392**System capability**: SystemCapability.Notification.Emitter 393 394**Parameters** 395 396| Name | Type | Mandatory| Description | 397| ------- | ----------------------- | ---- | ---------------- | 398| eventId | string | Yes | ID of the event to emit. The value cannot be an empty string and exceed 10240 bytes. | 399| data | [EventData](#eventdata) | No | Data passed in the event.| 400 401**Example** 402 403```ts 404let eventData: emitter.EventData = { 405 data: { 406 "content": "content", 407 "id": 1, 408 } 409}; 410 411emitter.emit("eventId", eventData); 412``` 413 414## emitter.emit<sup>12+</sup> 415 416emit<T\>(eventId: string, data?: GenericEventData<T\>): void 417 418Emits the specified event. 419 420**Atomic service API**: This API can be used in atomic services since API version 12. 421 422**System capability**: SystemCapability.Notification.Emitter 423 424**Parameters** 425 426| Name | Type | Mandatory| Description | 427| ------- | ----------------------- | ---- | ---------------- | 428| eventId | string | Yes | ID of the event to emit. The value cannot be an empty string and exceed 10240 bytes. | 429| data | [GenericEventData<T\>](#genericeventdatat12) | No | Data passed in the event.| 430 431**Example** 432 433```ts 434@Sendable 435class Sample { 436 constructor() { 437 this.count = 100; 438 } 439 printCount() { 440 console.info('Print count : ' + this.count); 441 } 442 count: number; 443} 444 445class SelfEventData implements emitter.EventData { 446 data: Sample = new Sample(); 447} 448 449let eventData = new SelfEventData(); 450emitter.emit("eventId", eventData); 451``` 452 453## emitter.emit<sup>11+</sup> 454 455emit(eventId: string, options: Options, data?: EventData): void 456 457Emits an event of a specified priority. 458 459**Atomic service API**: This API can be used in atomic services since API version 11. 460 461**System capability**: SystemCapability.Notification.Emitter 462 463**Parameters** 464 465| Name | Type | Mandatory| Description | 466| ------- | ----------------------- | ---- | ---------------- | 467| eventId | string | Yes | ID of the event to emit. The value cannot be an empty string and exceed 10240 bytes. | 468| options | [Options](#options11) | Yes | Event emit priority. | 469| data | [EventData](#eventdata) | No | Data passed in the event.| 470 471**Example** 472 473```ts 474let eventData: emitter.EventData = { 475 data: { 476 "content": "content", 477 "id": 1, 478 } 479}; 480 481let options: emitter.Options = { 482 priority: emitter.EventPriority.HIGH 483}; 484 485emitter.emit("eventId", options, eventData); 486``` 487 488## emitter.emit<sup>12+</sup> 489 490emit<T\>(eventId: string, options: Options, data?: GenericEventData<T\>): void 491 492Emits an event of a specified priority. 493 494**Atomic service API**: This API can be used in atomic services since API version 12. 495 496**System capability**: SystemCapability.Notification.Emitter 497 498**Parameters** 499 500| Name | Type | Mandatory| Description | 501| ------- | ----------------------- | ---- | ---------------- | 502| eventId | string | Yes | ID of the event to emit. The value cannot be an empty string and exceed 10240 bytes. | 503| options | [Options](#options11) | Yes | Event emit priority. | 504| data | [GenericEventData<T\>](#genericeventdatat12) | No | Data passed in the event.| 505 506**Example** 507 508```ts 509@Sendable 510class Sample { 511 constructor() { 512 this.count = 100; 513 } 514 printCount() { 515 console.info('Print count : ' + this.count); 516 } 517 count: number; 518} 519 520class SelfEventData implements emitter.EventData { 521 data: Sample = new Sample(); 522} 523 524let options: emitter.Options = { 525 priority: emitter.EventPriority.HIGH 526}; 527 528let eventData = new SelfEventData(); 529emitter.emit("eventId", options, eventData); 530``` 531 532## emitter.getListenerCount<sup>11+</sup> 533 534getListenerCount(eventId: number | string): number 535 536Obtains the number of subscriptions to a specified event. 537 538**Atomic service API**: This API can be used in atomic services since API version 11. 539 540**System capability**: SystemCapability.Notification.Emitter 541 542**Parameters** 543 544| Name | Type | Mandatory| Description | 545| ------- | -------------- | ---- | -------- | 546| eventId | number \| string | Yes | Event ID. The value of the string type cannot be an empty string.| 547 548**Example** 549 550```ts 551let count = emitter.getListenerCount("eventId"); 552``` 553 554## EventPriority 555 556Enumerates the event emit priority levels. 557 558**Atomic service API**: This API can be used in atomic services since API version 11. 559 560**System capability**: SystemCapability.Notification.Emitter 561 562| Name | Value | Description | 563| --------- | ---- | --------------------------------------------------- | 564| IMMEDIATE | 0 | The event will be emitted immediately. | 565| HIGH | 1 | The event will be emitted before low-priority events. | 566| LOW | 2 | The event will be emitted before idle-priority events. By default, an event is in LOW priority. | 567| IDLE | 3 | The event will be emitted after all the other events. | 568 569## InnerEvent 570 571Describes an event to subscribe to or emit. The **EventPriority** settings do not take effect under event subscription. 572 573**Atomic service API**: This API can be used in atomic services since API version 11. 574 575**System capability**: SystemCapability.Notification.Emitter 576 577| Name | Type | Read Only| Optional| Description | 578| -------- | ------------------------------- | ---- | ---- | ------------------------------ | 579| eventId | number | No | No | Event ID.| 580| priority | [EventPriority](#eventpriority) | No | Yes | Emit priority of the event. | 581 582## EventData 583 584Describes data passed in the event. 585 586**Atomic service API**: This API can be used in atomic services since API version 11. 587 588**System capability**: SystemCapability.Notification.Emitter 589 590| Name| Type | Read Only| Optional| Description | 591| ---- | ------------------ | ---- | ---- | -------------- | 592| 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.| 593 594## Options<sup>11+</sup> 595 596Describes the event emit priority. 597 598**Atomic service API**: This API can be used in atomic services since API version 11. 599 600**System capability**: SystemCapability.Notification.Emitter 601 602| Name | Type | Read Only| Optional| Description | 603| -------- | ------------------------------- | ---- | ---- | -------------- | 604| priority | [EventPriority](#eventpriority) | No | Yes | Event priority.| 605 606## GenericEventData<T\><sup>12+</sup> 607 608Describes the generic data passed in the event. 609 610**Atomic service API**: This API can be used in atomic services since API version 12. 611 612**System capability**: SystemCapability.Notification.Emitter 613 614| Name | Type | Read Only| Optional| Description | 615| -------- | ------------------------------- | ---- | ---- | -------------- | 616| data | T | No | Yes | Data passed in the event. **T**: generic type.| 617