js-apis-emitter.md 4.6 KB
Newer Older
E
add doc  
ester.zhou 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
# Emitter

> Note: The initial APIs of this module are supported since API version 7.

## Modules to Import

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

## Required Permissions

None

## EventPriority

Enumerates the event emit priority levels.

E
esterzhou 已提交
19
| Name     | Value  | Description                                             |
E
add doc  
ester.zhou 已提交
20
| --------- | ---- | ------------------------------------------------- |
E
esterzhou 已提交
21 22 23 24
| IMMEDIATE | 0    | The event will be emitted immediately.<br>**System capability**: SystemCapability.Notification.Emitter                               |
| HIGH      | 1    | The event will be emitted before low-priority events.<br>**System capability**: SystemCapability.Notification.Emitter                        |
| LOW       | 2    | The event will be emitted before idle-priority events. By default, an event is in LOW priority.<br>**System capability**: SystemCapability.Notification.Emitter|
| IDLE      | 3    | The event will be emitted after all the other events.<br>**System capability**: SystemCapability.Notification.Emitter         |
E
add doc  
ester.zhou 已提交
25 26 27

## emitter.on

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

E
esterzhou 已提交
30 31 32
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 已提交
33 34 35

**Parameters**

E
esterzhou 已提交
36
| Name  | Type                               | Mandatory| Description                    |
E
add doc  
ester.zhou 已提交
37
| -------- | ----------------------------------- | ---- | ------------------------ |
E
esterzhou 已提交
38 39
| 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 已提交
40 41 42 43 44

**Example**

```javascript
var innerEvent = {
E
ester.zhou 已提交
45
    eventId: 1
E
add doc  
ester.zhou 已提交
46 47 48 49 50 51 52 53 54
};
var callback = (eventData) => {
    console.info('callback');
};
emitter.on(innerEvent, callback);
```

## emitter.once

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

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

E
esterzhou 已提交
59 60
**System capability**: SystemCapability.Notification.Emitter

E
add doc  
ester.zhou 已提交
61 62
**Parameters**

E
esterzhou 已提交
63
| Name  | Type                               | Mandatory| Description                    |
E
add doc  
ester.zhou 已提交
64
| -------- | ----------------------------------- | ---- | ------------------------ |
E
esterzhou 已提交
65 66
| 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 已提交
67 68 69 70 71

**Example**

```javascript
var innerEvent = {
E
ester.zhou 已提交
72
    eventId: 1
E
add doc  
ester.zhou 已提交
73 74 75 76 77 78 79 80 81 82 83 84 85
};
var callback = (eventData) => {
    console.info('once callback');
};
emitter.once(innerEvent, callback);
```

## emitter.off

off(eventId: number): void

Unsubscribes from an event.

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

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

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

**Example**

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

## emitter.emit

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

Emits an event to the event queue.

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

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

E
esterzhou 已提交
110
| Name| Type                     | Mandatory| Description          |
E
add doc  
ester.zhou 已提交
111
| ------ | ------------------------- | ---- | -------------- |
E
esterzhou 已提交
112 113
| event  | [InnerEvent](#innerevent) | Yes  | Event to emit.    |
| data   | [EventData](#eventdata)   | No  | Data carried by the event.|
E
add doc  
ester.zhou 已提交
114 115 116 117 118 119

**Example**

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

## InnerEvent

Describes an intra-process event.

E
esterzhou 已提交
134
| Name    | Type                       | Readable| Writable| Description                              |
E
add doc  
ester.zhou 已提交
135
| -------- | ------------------------------- | ---- | ---- | ---------------------------------- |
E
esterzhou 已提交
136 137
| eventId  | number                          | Yes  | Yes  | Event ID, which is used to identify an event.<br>**System capability**: SystemCapability.Notification.Emitter|
| priority | [EventPriority](#eventpriority) | Yes  | Yes  | Emit priority of the event.<br>**System capability**: SystemCapability.Notification.Emitter                |
E
add doc  
ester.zhou 已提交
138 139 140 141 142

## EventData

Describes the data passed in the event.

E
esterzhou 已提交
143
| Name| Type          | Readable| Writable| Description          |
E
add doc  
ester.zhou 已提交
144
| ---- | ------------------ | ---- | ---- | -------------- |
E
esterzhou 已提交
145
| data | [key: string]: any | Yes  | Yes  | Data carried by the event. The data type can be String, Integer, or Boolean.<br>**System capability**: SystemCapability.Notification.Emitter|