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

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

## Modules to Import

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

## Required Permissions

None

## EventPriority

Enumerates the event emit priority levels.

20 21 22 23 24 25
 | Name | Value | Description |
 | --------- | ---- | ------------------------------------------------- |
 | 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 已提交
26 27 28

## emitter.on

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

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

**Parameters**

37 38 39 40
 | 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 已提交
41 42 43 44 45

**Example**

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

## emitter.once

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

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

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

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

64 65 66 67
 | 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 已提交
68 69 70 71 72

**Example**

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

## emitter.off

off(eventId: number): void

Unsubscribes from an event.

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

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

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

**Example**

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

## emitter.emit

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

Emits an event to the event queue.

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

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

111 112 113 114
 | 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 已提交
115 116 117 118 119 120

**Example**

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

## InnerEvent

Describes an intra-process event.

135 136 137 138
 | Name | Type | Readable | Writable | Description |
 | -------- | ------------------------------- | ---- | ---- | ---------------------------------- |
 | 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 已提交
139 140 141 142 143

## EventData

Describes the data passed in the event.

144 145 146
 | 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.<br>**System capability**: SystemCapability.Notification.Emitter |