asljs-eventful 0.2.2 → 0.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +123 -3
- package/eventful.js +2 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -11,16 +11,136 @@ Lightweight event helper adding on/off/emit to any object.
|
|
|
11
11
|
npm install asljs-eventful
|
|
12
12
|
```
|
|
13
13
|
|
|
14
|
+
NPM Package: [asljs-eventful](https://www.npmjs.com/package/asljs-eventful)
|
|
15
|
+
|
|
14
16
|
## Usage
|
|
15
17
|
|
|
16
|
-
### Basic
|
|
18
|
+
### Basic (JavaScript)
|
|
19
|
+
|
|
20
|
+
Adding events to an object, add listeners, and emit events:
|
|
17
21
|
|
|
18
22
|
```js
|
|
19
23
|
import { eventful } from 'asljs-eventful';
|
|
20
24
|
|
|
21
25
|
const obj = eventful({ name: 'Alice' });
|
|
22
|
-
|
|
23
|
-
obj.
|
|
26
|
+
|
|
27
|
+
obj.on('greet',
|
|
28
|
+
msg => console.log(`${msg}, ${obj.name}!`));
|
|
29
|
+
|
|
30
|
+
// writes "Hello, Alice!" to console
|
|
31
|
+
obj.emit('greet', 'Hello');
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
### Basic (TypeScript)
|
|
35
|
+
|
|
36
|
+
```ts
|
|
37
|
+
import { eventful, type Eventful } from 'asljs-eventful';
|
|
38
|
+
|
|
39
|
+
const obj: Eventful<{ name: string }> =
|
|
40
|
+
eventful({ name: 'Alice' });
|
|
41
|
+
|
|
42
|
+
obj.on('greet',
|
|
43
|
+
msg => console.log(`${msg}, ${obj.name}!`));
|
|
44
|
+
|
|
45
|
+
// writes "Hello, Alice!" to console
|
|
46
|
+
obj.emit('greet', 'Hello');
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### Inheritance (JavaScript)
|
|
50
|
+
|
|
51
|
+
Adding events to a class via inheritance:
|
|
52
|
+
|
|
53
|
+
```js
|
|
54
|
+
import { EventfulBase } from 'asljs-eventful';
|
|
55
|
+
|
|
56
|
+
class MyClass extends EventfulBase {
|
|
57
|
+
constructor(name) {
|
|
58
|
+
super();
|
|
59
|
+
|
|
60
|
+
this.name = name;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
greet() {
|
|
64
|
+
this.emit(
|
|
65
|
+
'greet',
|
|
66
|
+
`Hello, ${this.name}`);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
### Inheritance (TypeScript)
|
|
72
|
+
|
|
73
|
+
```ts
|
|
74
|
+
import { EventfulBase } from 'asljs-eventful';
|
|
75
|
+
class MyClass extends EventfulBase {
|
|
76
|
+
name: string;
|
|
77
|
+
|
|
78
|
+
constructor(name: string) {
|
|
79
|
+
super();
|
|
80
|
+
|
|
81
|
+
this.name = name;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
greet() {
|
|
85
|
+
this.emit(
|
|
86
|
+
'greet',
|
|
87
|
+
`Hello, ${this.name}`);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### Construction (JavaScript)
|
|
93
|
+
|
|
94
|
+
Adding events to an existing class during construction:
|
|
95
|
+
|
|
96
|
+
```js
|
|
97
|
+
import { eventful } from 'asljs-eventful';
|
|
98
|
+
|
|
99
|
+
export class MyClass {
|
|
100
|
+
constructor(name) {
|
|
101
|
+
eventful(this);
|
|
102
|
+
|
|
103
|
+
this.name = name;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
greet() {
|
|
107
|
+
this.emit(
|
|
108
|
+
'greet',
|
|
109
|
+
`Hello, ${this.name}`);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
### Construction (TypeScript)
|
|
115
|
+
|
|
116
|
+
```ts
|
|
117
|
+
import { eventful, type Eventful } from 'asljs-eventful';
|
|
118
|
+
|
|
119
|
+
type MyClassEvents =
|
|
120
|
+
{ greet: [message: string]; };
|
|
121
|
+
|
|
122
|
+
export class MyClass implements Eventful<MyClassEvents> {
|
|
123
|
+
name: string;
|
|
124
|
+
|
|
125
|
+
declare on: Eventful<MyClassEvents>['on'];
|
|
126
|
+
declare once: Eventful<MyClassEvents>['once'];
|
|
127
|
+
declare off: Eventful<MyClassEvents>['off'];
|
|
128
|
+
declare emit: Eventful<MyClassEvents>['emit'];
|
|
129
|
+
declare emitAsync: Eventful<MyClassEvents>['emitAsync'];
|
|
130
|
+
declare has: Eventful<MyClassEvents>['has'];
|
|
131
|
+
|
|
132
|
+
constructor(name: string) {
|
|
133
|
+
eventful(this);
|
|
134
|
+
|
|
135
|
+
this.name = name;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
greet() {
|
|
139
|
+
this.emit(
|
|
140
|
+
'greet',
|
|
141
|
+
`Hello, ${this.name}`);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
24
144
|
```
|
|
25
145
|
|
|
26
146
|
### Advanced Options
|
package/eventful.js
CHANGED