js-apis-emitter.md 3.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 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
# Emitter

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

## Modules to Import

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

## System Capabilities

```javascript
SystemCapability.Notification.Emitter
```

## Required Permissions

None

## EventPriority

Enumerates the event emit priority levels.

| 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.|

## emitter.on

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

Subscribes to an event in persistent manner. This method uses a callback to return the event.

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | ----------------------------------- | ---- | ------------------------ |
E
ester.zhou 已提交
42 43
| 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 已提交
44 45 46 47 48

**Example**

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

## emitter.once

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

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

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | ----------------------------------- | ---- | ------------------------ |
E
ester.zhou 已提交
67 68
| 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 已提交
69 70 71 72 73

**Example**

```javascript
var innerEvent = {
E
ester.zhou 已提交
74
    eventId: 1
E
add doc  
ester.zhou 已提交
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
};
var callback = (eventData) => {
    console.info('once callback');
};
emitter.once(innerEvent, callback);
```

## emitter.off

off(eventId: number): void

Unsubscribes from an event.

**Parameters**

| Name| Type| Mandatory| Description|
| ------- | ------ | ---- | -------------- |
| eventId | number | Yes| Event ID.|

**Example**

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

## emitter.emit

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

Emits an event to the event queue.

**Parameters**

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

**Example**

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

## InnerEvent

Describes an intra-process event.

| Name| Type| Readable| Writable| Description|
| -------- | ------------------------------- | ---- | ---- | ---------------------------------- |
| eventId  | number                          | Yes| Yes| Event ID, which is used to identify an event.|
E
ester.zhou 已提交
135
| priority | [EventPriority](#eventpriority) | Yes| Yes| Emit priority of the event.|
E
add doc  
ester.zhou 已提交
136 137 138 139 140 141 142 143

## EventData

Describes the data passed in the event.

| Name| Type| Readable| Writable| Description|
| ---- | ------------------ | ---- | ---- | -------------- |
| data | [key: string]: any | Yes| Yes| Data carried by the event.|