js-apis-emitter.md 4.6 KB
Newer Older
E
add doc  
ester.zhou 已提交
1 2
# Emitter

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 21 22

## Modules to Import

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

## Required Permissions

None

## EventPriority

Enumerates the event emit priority levels.

E
ester.zhou 已提交
23 24 25 26 27 28 29 30
**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 已提交
31 32 33

## emitter.on

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

E
esterzhou 已提交
36 37 38
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 已提交
39 40 41

**Parameters**

E
ester.zhou 已提交
42 43 44 45
| Name  | Type                               | Mandatory| Description                    |
| -------- | ----------------------------------- | ---- | ------------------------ |
| event    | [InnerEvent](#innerevent)           | Yes  | Event to subscribe to in persistent manner.          |
| callback | Callback\<[EventData](#eventdata)\> | Yes  | Callback used to return the event.|
E
add doc  
ester.zhou 已提交
46 47 48 49 50

**Example**

```javascript
var innerEvent = {
E
ester.zhou 已提交
51
    eventId: 1
E
add doc  
ester.zhou 已提交
52 53 54 55 56 57 58 59 60
};
var callback = (eventData) => {
    console.info('callback');
};
emitter.on(innerEvent, callback);
```

## emitter.once

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

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

E
esterzhou 已提交
65 66
**System capability**: SystemCapability.Notification.Emitter

E
add doc  
ester.zhou 已提交
67 68
**Parameters**

E
ester.zhou 已提交
69 70 71 72
| Name  | Type                               | Mandatory| Description                    |
| -------- | ----------------------------------- | ---- | ------------------------ |
| event    | [InnerEvent](#innerevent)           | Yes  | Event to subscribe to in one-shot manner.          |
| callback | Callback\<[EventData](#eventdata)\> | Yes  | Callback used to return the event.|
E
add doc  
ester.zhou 已提交
73 74 75 76 77

**Example**

```javascript
var innerEvent = {
E
ester.zhou 已提交
78
    eventId: 1
E
add doc  
ester.zhou 已提交
79 80 81 82 83 84 85 86 87 88 89 90 91
};
var callback = (eventData) => {
    console.info('once callback');
};
emitter.once(innerEvent, callback);
```

## emitter.off

off(eventId: number): void

Unsubscribes from an event.

E
esterzhou 已提交
92 93
**System capability**: SystemCapability.Notification.Emitter

E
add doc  
ester.zhou 已提交
94 95
**Parameters**

E
ester.zhou 已提交
96 97 98
| Name | Type  | Mandatory| Description  |
| ------- | ------ | ---- | ------ |
| eventId | number | Yes  | Event ID.|
E
add doc  
ester.zhou 已提交
99 100 101 102 103 104 105 106 107 108 109 110 111

**Example**

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

## emitter.emit

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

Emits an event to the event queue.

E
esterzhou 已提交
112 113
**System capability**: SystemCapability.Notification.Emitter

E
add doc  
ester.zhou 已提交
114 115
**Parameters**

E
ester.zhou 已提交
116 117 118 119
| 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 已提交
120 121 122 123 124 125

**Example**

```javascript
var eventData = {
    data: {
E
esterzhou 已提交
126
        "content": "c",
E
ester.zhou 已提交
127
        "id": 1,
E
add doc  
ester.zhou 已提交
128 129
    }};
var innerEvent = {
E
ester.zhou 已提交
130
    eventId: 1,
E
add doc  
ester.zhou 已提交
131 132 133 134 135 136 137
    priority: emitter.EventPriority.HIGH
};
emitter.emit(innerEvent, eventData);
```

## InnerEvent

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

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

| Name    | Type                       | Readable| Writable| Description                              |
| -------- | ------------------------------- | ---- | ---- | ---------------------------------- |
| eventId  | number                          | Yes  | Yes  | Event ID, which is used to identify an event.|
| 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.|