js-apis-emitter.md 4.8 KB
Newer Older
E
esterzhou 已提交
1
# @ohos.events.emitter
E
add doc  
ester.zhou 已提交
2

E
ester.zhou 已提交
3 4 5 6
The **Emitter** module provides APIs for sending and processing in-process events, including the APIs for processing events that are subscribed to in persistent or one-shot manner, unsubscribing from events, and emitting events to the event queue.

> **NOTE**
>
7
> The initial APIs of this module are supported since API version 7.
E
add doc  
ester.zhou 已提交
8 9 10 11 12 13 14 15 16 17 18 19 20

## Modules to Import

```javascript
import emitter from '@ohos.events.emitter'
```

## Required Permissions

None

## emitter.on

E
ester.zhou 已提交
21
on(event: [InnerEvent](#innerevent), callback: Callback\<[EventData](#eventdata)\>): void
E
add doc  
ester.zhou 已提交
22

E
esterzhou 已提交
23 24 25
Subscribes to an event in persistent manner. This API uses a callback to return the event.

**System capability**: SystemCapability.Notification.Emitter
E
add doc  
ester.zhou 已提交
26 27 28

**Parameters**

E
esterzhou 已提交
29 30 31 32
| Name  | Type                               | Mandatory| Description                                   |
| -------- | ----------------------------------- | ---- | --------------------------------------- |
| event    | [InnerEvent](#innerevent)           | Yes  | Event to subscribe to in persistent manner. The **EventPriority** settings do not take effect.|
| callback | Callback\<[EventData](#eventdata)\> | Yes  | Callback used to return the event.               |
E
add doc  
ester.zhou 已提交
33 34 35 36

**Example**

```javascript
E
esterzhou 已提交
37
let innerEvent = {
E
ester.zhou 已提交
38
    eventId: 1
E
add doc  
ester.zhou 已提交
39
};
E
esterzhou 已提交
40
function EmitterCallback(eventData) {
E
add doc  
ester.zhou 已提交
41
    console.info('callback');
E
esterzhou 已提交
42 43
}
emitter.on(innerEvent, EmitterCallback);
E
add doc  
ester.zhou 已提交
44 45 46 47
```

## emitter.once

E
esterzhou 已提交
48
once(event: [InnerEvent](#innerevent), callback: Callback\<[EventData](#eventdata)\>): void
E
add doc  
ester.zhou 已提交
49 50 51

Subscribes to an event in one-shot manner and unsubscribes from it after the event callback is received.

E
esterzhou 已提交
52 53
**System capability**: SystemCapability.Notification.Emitter

E
add doc  
ester.zhou 已提交
54 55
**Parameters**

E
esterzhou 已提交
56 57 58 59
| Name  | Type                               | Mandatory| Description                                   |
| -------- | ----------------------------------- | ---- | --------------------------------------- |
| event    | [InnerEvent](#innerevent)           | Yes  | Event to subscribe to in one-shot manner. The **EventPriority** settings do not take effect.|
| callback | Callback\<[EventData](#eventdata)\> | Yes  | Callback used to return the event.               |
E
add doc  
ester.zhou 已提交
60 61 62 63

**Example**

```javascript
E
esterzhou 已提交
64
let innerEvent = {
E
ester.zhou 已提交
65
    eventId: 1
E
add doc  
ester.zhou 已提交
66
};
E
esterzhou 已提交
67
function EmitterCallback(eventData) {
E
add doc  
ester.zhou 已提交
68 69
    console.info('once callback');
};
E
esterzhou 已提交
70
emitter.once(innerEvent, EmitterCallback);
E
add doc  
ester.zhou 已提交
71 72 73 74 75 76 77 78
```

## emitter.off

off(eventId: number): void

Unsubscribes from an event.

E
esterzhou 已提交
79 80
**System capability**: SystemCapability.Notification.Emitter

E
add doc  
ester.zhou 已提交
81 82
**Parameters**

E
ester.zhou 已提交
83 84 85
| Name | Type  | Mandatory| Description  |
| ------- | ------ | ---- | ------ |
| eventId | number | Yes  | Event ID.|
E
add doc  
ester.zhou 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98

**Example**

```javascript
emitter.off(1);
```

## emitter.emit

emit(event: InnerEvent, data?: EventData): void

Emits an event to the event queue.

E
esterzhou 已提交
99 100
**System capability**: SystemCapability.Notification.Emitter

E
add doc  
ester.zhou 已提交
101 102
**Parameters**

E
ester.zhou 已提交
103 104 105 106
| Name| Type                     | Mandatory| Description          |
| ------ | ------------------------- | ---- | -------------- |
| event  | [InnerEvent](#innerevent) | Yes  | Event to emit.    |
| data   | [EventData](#eventdata)   | No  | Data carried by the event.|
E
add doc  
ester.zhou 已提交
107 108 109 110

**Example**

```javascript
E
esterzhou 已提交
111
let eventData = {
E
add doc  
ester.zhou 已提交
112
    data: {
E
esterzhou 已提交
113
        "content": "c",
E
ester.zhou 已提交
114
        "id": 1,
E
add doc  
ester.zhou 已提交
115
    }};
E
esterzhou 已提交
116
let innerEvent = {
E
ester.zhou 已提交
117
    eventId: 1,
E
add doc  
ester.zhou 已提交
118 119 120 121 122
    priority: emitter.EventPriority.HIGH
};
emitter.emit(innerEvent, eventData);
```

E
esterzhou 已提交
123 124 125 126 127 128 129 130 131 132 133 134 135
## EventPriority

Enumerates the event emit priority levels.

**System capability**: SystemCapability.Notification.Emitter

| Name     | Value  | Description                                               |
| --------- | ---- | --------------------------------------------------- |
| IMMEDIATE | 0    | The event will be emitted immediately.                               |
| HIGH      | 1    | The event will be emitted before low-priority events.                        |
| LOW       | 2    | The event will be emitted before idle-priority events. By default, an event is in LOW priority.|
| IDLE      | 3    | The event will be emitted after all the other events.         |

E
add doc  
ester.zhou 已提交
136 137
## InnerEvent

E
ester.zhou 已提交
138
Describes an in-process event.
E
add doc  
ester.zhou 已提交
139

E
ester.zhou 已提交
140 141 142 143
**System capability**: SystemCapability.Notification.Emitter

| Name    | Type                       | Readable| Writable| Description                              |
| -------- | ------------------------------- | ---- | ---- | ---------------------------------- |
E
esterzhou 已提交
144
| eventId  | number                          | Yes  | Yes  | Event ID.|
E
ester.zhou 已提交
145
| priority | [EventPriority](#eventpriority) | Yes  | Yes  | Emit priority of the event.        |
E
add doc  
ester.zhou 已提交
146 147 148 149 150

## EventData

Describes the data passed in the event.

E
ester.zhou 已提交
151 152 153 154 155
**System capability**: SystemCapability.Notification.Emitter

| Name| Type          | Readable| Writable| Description          |
| ---- | ------------------ | ---- | ---- | -------------- |
| data | [key: string]: any | Yes  | Yes  | Data carried by the event. The data type can be String, Integer, or Boolean.|