js-apis-emitter.md 5.5 KB
Newer Older
E
ester.zhou 已提交
1
# @ohos.events.emitter (Emitter)
E
add doc  
ester.zhou 已提交
2

E
ester.zhou 已提交
3
The **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.
E
ester.zhou 已提交
4 5 6

> **NOTE**
>
E
ester.zhou 已提交
7 8 9
> 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.
>
> The APIs of this module can be used in the FA model or stage model.
E
add doc  
ester.zhou 已提交
10 11 12 13 14 15 16 17 18 19 20 21 22

## Modules to Import

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

## Required Permissions

None

## emitter.on

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

E
ester.zhou 已提交
25
Subscribes to an event in persistent manner and executes a callback after the event is received.
E
esterzhou 已提交
26 27

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

**Parameters**

E
ester.zhou 已提交
31 32 33 34
| Name  | Type                               | Mandatory| Description                                                    |
| -------- | ----------------------------------- | ---- | ------------------------------------------------------ |
| event    | [InnerEvent](#innerevent)           | Yes  | Event to subscribe to in persistent manner. The [EventPriority](#eventpriority) settings do not take effect.|
| callback | Callback\<[EventData](#eventdata)\> | Yes  | Callback to execute after the event is received.                      |
E
add doc  
ester.zhou 已提交
35 36 37 38

**Example**

```javascript
E
esterzhou 已提交
39
let innerEvent = {
E
ester.zhou 已提交
40
    eventId: 1
E
add doc  
ester.zhou 已提交
41
};
E
ester.zhou 已提交
42 43 44

// Execute the callback after receiving the event whose eventId is 1.
function emitterCallback() {
E
add doc  
ester.zhou 已提交
45
    console.info('callback');
E
esterzhou 已提交
46
}
E
ester.zhou 已提交
47
emitter.on(innerEvent, emitterCallback);
E
add doc  
ester.zhou 已提交
48 49 50 51
```

## emitter.once

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

E
ester.zhou 已提交
54
Subscribes to an event in one-shot manner and unsubscribes from it after the event callback is executed.
E
add doc  
ester.zhou 已提交
55

E
esterzhou 已提交
56 57
**System capability**: SystemCapability.Notification.Emitter

E
add doc  
ester.zhou 已提交
58 59
**Parameters**

E
ester.zhou 已提交
60 61 62 63
| Name  | Type                               | Mandatory| Description                                                                           |
| -------- | ----------------------------------- | ---- | ------------------------------------------------------------------------------ |
| event    | [InnerEvent](#innerevent)           | Yes  | Event to subscribe to in one-shot manner. The [EventPriority](#eventpriority) settings do not take effect.|
| callback | Callback\<[EventData](#eventdata)\> | Yes  | Callback to execute after the event is received.                                            |
E
add doc  
ester.zhou 已提交
64 65 66 67

**Example**

```javascript
E
esterzhou 已提交
68
let innerEvent = {
E
ester.zhou 已提交
69
    eventId: 1
E
add doc  
ester.zhou 已提交
70
};
E
ester.zhou 已提交
71 72 73

// Execute the callback after receiving the event whose eventId is 1.
function emitterCallback() {
E
add doc  
ester.zhou 已提交
74 75
    console.info('once callback');
};
E
ester.zhou 已提交
76
emitter.once(innerEvent, emitterCallback);
E
add doc  
ester.zhou 已提交
77 78 79 80 81 82 83 84
```

## emitter.off

off(eventId: number): void

Unsubscribes from an event.

E
esterzhou 已提交
85 86
**System capability**: SystemCapability.Notification.Emitter

E
add doc  
ester.zhou 已提交
87 88
**Parameters**

E
ester.zhou 已提交
89 90 91
| Name | Type  | Mandatory| Description  |
| ------- | ------ | ---- | ------ |
| eventId | number | Yes  | Event ID.|
E
add doc  
ester.zhou 已提交
92 93 94 95 96 97 98 99 100

**Example**

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

## emitter.emit

E
ester.zhou 已提交
101
emit(event: [InnerEvent](#innerevent), data?: [EventData](#eventdata)): void
E
add doc  
ester.zhou 已提交
102

E
ester.zhou 已提交
103
Emits an event.
E
add doc  
ester.zhou 已提交
104

E
esterzhou 已提交
105 106
**System capability**: SystemCapability.Notification.Emitter

E
add doc  
ester.zhou 已提交
107 108
**Parameters**

E
ester.zhou 已提交
109
| Name| Type                     | Mandatory| Description          |
E
ester.zhou 已提交
110 111
| ------ | ------------------------- | ---- | ------------- |
| event  | [InnerEvent](#innerevent) | Yes  | Event to emit, where [EventPriority](#eventpriority) specifies the emit priority of the event.|
E
ester.zhou 已提交
112
| data   | [EventData](#eventdata)   | No  | Data carried by the event.|
E
add doc  
ester.zhou 已提交
113 114 115 116

**Example**

```javascript
E
esterzhou 已提交
117
let eventData = {
E
add doc  
ester.zhou 已提交
118
    data: {
E
esterzhou 已提交
119
        "content": "c",
E
ester.zhou 已提交
120
        "id": 1,
E
ester.zhou 已提交
121 122 123
    }
};

E
esterzhou 已提交
124
let innerEvent = {
E
ester.zhou 已提交
125
    eventId: 1,
E
add doc  
ester.zhou 已提交
126 127
    priority: emitter.EventPriority.HIGH
};
E
ester.zhou 已提交
128

E
add doc  
ester.zhou 已提交
129 130 131
emitter.emit(innerEvent, eventData);
```

E
esterzhou 已提交
132 133 134 135 136 137
## EventPriority

Enumerates the event emit priority levels.

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

E
ester.zhou 已提交
138
| Name     | Value   | Description                                               |
E
esterzhou 已提交
139
| --------- | ---- | --------------------------------------------------- |
E
ester.zhou 已提交
140 141 142 143
| 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
esterzhou 已提交
144

E
add doc  
ester.zhou 已提交
145 146
## InnerEvent

E
ester.zhou 已提交
147
Describes an event to subscribe to or emit. The **EventPriority** settings do not take effect under event subscription.
E
add doc  
ester.zhou 已提交
148

E
ester.zhou 已提交
149 150
**System capability**: SystemCapability.Notification.Emitter

E
ester.zhou 已提交
151 152
| Name    | Type                       | Readable| Writable| Description                                |
| -------- | ------------------------------- | ---- | ---- | ------------------------------ |
E
esterzhou 已提交
153
| eventId  | number                          | Yes  | Yes  | Event ID.|
E
ester.zhou 已提交
154
| priority | [EventPriority](#eventpriority) | Yes  | Yes  | Emit priority of the event.            |
E
add doc  
ester.zhou 已提交
155 156 157 158 159

## EventData

Describes the data passed in the event.

E
ester.zhou 已提交
160 161 162 163 164
**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.|