@axijs/emitter 1.1.0 → 1.2.0
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/dist/index.d.mts +20 -10
- package/dist/index.d.ts +20 -10
- package/dist/index.js +10 -0
- package/dist/index.mjs +9 -0
- package/package.json +2 -3
package/dist/index.d.mts
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
type
|
|
1
|
+
type Listener<T> = (value: T) => Promise<void> | void;
|
|
2
2
|
/**
|
|
3
3
|
* Defines the public, read-only contract for an event emitter.
|
|
4
4
|
* It allows subscribing to an event but not emitting it.
|
|
5
5
|
* @template T The type of the emitted value.
|
|
6
6
|
*/
|
|
7
|
-
|
|
7
|
+
interface Subscribable<T> {
|
|
8
8
|
readonly listenerCount: number;
|
|
9
9
|
/**
|
|
10
10
|
* Subscribes a listener to this event.
|
|
11
11
|
* @returns A function to unsubscribe the listener.
|
|
12
12
|
*/
|
|
13
|
-
subscribe(listener:
|
|
14
|
-
unsubscribe(listener:
|
|
13
|
+
subscribe(listener: Listener<T>): Unsubscribable;
|
|
14
|
+
unsubscribe(listener: Listener<T>): boolean;
|
|
15
15
|
clear(): void;
|
|
16
|
-
}
|
|
16
|
+
}
|
|
17
17
|
/**
|
|
18
18
|
* Describes an object that can be unsubscribed from.
|
|
19
19
|
*/
|
|
@@ -55,7 +55,7 @@ declare class Subscription implements Unsubscribable {
|
|
|
55
55
|
* @template T The type of the emitted value.
|
|
56
56
|
*/
|
|
57
57
|
declare class Emitter<T = void> implements Subscribable<T> {
|
|
58
|
-
|
|
58
|
+
protected listeners: Set<Listener<T>>;
|
|
59
59
|
/**
|
|
60
60
|
* Returns the number of listeners.
|
|
61
61
|
*/
|
|
@@ -64,18 +64,18 @@ declare class Emitter<T = void> implements Subscribable<T> {
|
|
|
64
64
|
* Subscribes a listener to this event.
|
|
65
65
|
* @returns A Subscription object to manage the unsubscription.
|
|
66
66
|
*/
|
|
67
|
-
subscribe(listener:
|
|
67
|
+
subscribe(listener: Listener<T>): Subscription;
|
|
68
68
|
/**
|
|
69
69
|
* Subscribes a listener that triggers only once and then automatically unsubscribes.
|
|
70
70
|
* @param listener The callback function to execute once.
|
|
71
71
|
* @returns A Subscription object (can be used to cancel before the event fires).
|
|
72
72
|
*/
|
|
73
|
-
once(listener:
|
|
73
|
+
once(listener: Listener<T>): Subscription;
|
|
74
74
|
/**
|
|
75
75
|
* Manually unsubscribe by listener.
|
|
76
76
|
* @returns returns true if a listener has been removed, or false if it does not exist.
|
|
77
77
|
*/
|
|
78
|
-
unsubscribe(listener:
|
|
78
|
+
unsubscribe(listener: Listener<T>): boolean;
|
|
79
79
|
/**
|
|
80
80
|
* Dispatches the event to all subscribed listeners.
|
|
81
81
|
*/
|
|
@@ -86,6 +86,16 @@ declare class Emitter<T = void> implements Subscribable<T> {
|
|
|
86
86
|
clear(): void;
|
|
87
87
|
}
|
|
88
88
|
|
|
89
|
+
/**
|
|
90
|
+
* An emitter intended for cases where listeners need to finish their work before
|
|
91
|
+
* the next event is handled, such as animations or async side effects.
|
|
92
|
+
* Use with caution when the generation rate of new events can be higher than the
|
|
93
|
+
* time required for a listener to react.
|
|
94
|
+
*/
|
|
95
|
+
declare class AsyncEmitter<T = void> extends Emitter<T> {
|
|
96
|
+
emit(value: T): Promise<void>;
|
|
97
|
+
}
|
|
98
|
+
|
|
89
99
|
interface StateEmitterOptions<T> {
|
|
90
100
|
distinct?: boolean;
|
|
91
101
|
compare?: (prev: T, next: T) => boolean;
|
|
@@ -121,4 +131,4 @@ declare class StateEmitter<T> extends Emitter<T> {
|
|
|
121
131
|
clear(): void;
|
|
122
132
|
}
|
|
123
133
|
|
|
124
|
-
export { Emitter, type
|
|
134
|
+
export { AsyncEmitter, Emitter, type Listener, StateEmitter, type StateEmitterOptions, type Subscribable, Subscription, type Unsubscribable };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
type
|
|
1
|
+
type Listener<T> = (value: T) => Promise<void> | void;
|
|
2
2
|
/**
|
|
3
3
|
* Defines the public, read-only contract for an event emitter.
|
|
4
4
|
* It allows subscribing to an event but not emitting it.
|
|
5
5
|
* @template T The type of the emitted value.
|
|
6
6
|
*/
|
|
7
|
-
|
|
7
|
+
interface Subscribable<T> {
|
|
8
8
|
readonly listenerCount: number;
|
|
9
9
|
/**
|
|
10
10
|
* Subscribes a listener to this event.
|
|
11
11
|
* @returns A function to unsubscribe the listener.
|
|
12
12
|
*/
|
|
13
|
-
subscribe(listener:
|
|
14
|
-
unsubscribe(listener:
|
|
13
|
+
subscribe(listener: Listener<T>): Unsubscribable;
|
|
14
|
+
unsubscribe(listener: Listener<T>): boolean;
|
|
15
15
|
clear(): void;
|
|
16
|
-
}
|
|
16
|
+
}
|
|
17
17
|
/**
|
|
18
18
|
* Describes an object that can be unsubscribed from.
|
|
19
19
|
*/
|
|
@@ -55,7 +55,7 @@ declare class Subscription implements Unsubscribable {
|
|
|
55
55
|
* @template T The type of the emitted value.
|
|
56
56
|
*/
|
|
57
57
|
declare class Emitter<T = void> implements Subscribable<T> {
|
|
58
|
-
|
|
58
|
+
protected listeners: Set<Listener<T>>;
|
|
59
59
|
/**
|
|
60
60
|
* Returns the number of listeners.
|
|
61
61
|
*/
|
|
@@ -64,18 +64,18 @@ declare class Emitter<T = void> implements Subscribable<T> {
|
|
|
64
64
|
* Subscribes a listener to this event.
|
|
65
65
|
* @returns A Subscription object to manage the unsubscription.
|
|
66
66
|
*/
|
|
67
|
-
subscribe(listener:
|
|
67
|
+
subscribe(listener: Listener<T>): Subscription;
|
|
68
68
|
/**
|
|
69
69
|
* Subscribes a listener that triggers only once and then automatically unsubscribes.
|
|
70
70
|
* @param listener The callback function to execute once.
|
|
71
71
|
* @returns A Subscription object (can be used to cancel before the event fires).
|
|
72
72
|
*/
|
|
73
|
-
once(listener:
|
|
73
|
+
once(listener: Listener<T>): Subscription;
|
|
74
74
|
/**
|
|
75
75
|
* Manually unsubscribe by listener.
|
|
76
76
|
* @returns returns true if a listener has been removed, or false if it does not exist.
|
|
77
77
|
*/
|
|
78
|
-
unsubscribe(listener:
|
|
78
|
+
unsubscribe(listener: Listener<T>): boolean;
|
|
79
79
|
/**
|
|
80
80
|
* Dispatches the event to all subscribed listeners.
|
|
81
81
|
*/
|
|
@@ -86,6 +86,16 @@ declare class Emitter<T = void> implements Subscribable<T> {
|
|
|
86
86
|
clear(): void;
|
|
87
87
|
}
|
|
88
88
|
|
|
89
|
+
/**
|
|
90
|
+
* An emitter intended for cases where listeners need to finish their work before
|
|
91
|
+
* the next event is handled, such as animations or async side effects.
|
|
92
|
+
* Use with caution when the generation rate of new events can be higher than the
|
|
93
|
+
* time required for a listener to react.
|
|
94
|
+
*/
|
|
95
|
+
declare class AsyncEmitter<T = void> extends Emitter<T> {
|
|
96
|
+
emit(value: T): Promise<void>;
|
|
97
|
+
}
|
|
98
|
+
|
|
89
99
|
interface StateEmitterOptions<T> {
|
|
90
100
|
distinct?: boolean;
|
|
91
101
|
compare?: (prev: T, next: T) => boolean;
|
|
@@ -121,4 +131,4 @@ declare class StateEmitter<T> extends Emitter<T> {
|
|
|
121
131
|
clear(): void;
|
|
122
132
|
}
|
|
123
133
|
|
|
124
|
-
export { Emitter, type
|
|
134
|
+
export { AsyncEmitter, Emitter, type Listener, StateEmitter, type StateEmitterOptions, type Subscribable, Subscription, type Unsubscribable };
|
package/dist/index.js
CHANGED
|
@@ -20,6 +20,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// src/index.ts
|
|
21
21
|
var index_exports = {};
|
|
22
22
|
__export(index_exports, {
|
|
23
|
+
AsyncEmitter: () => AsyncEmitter,
|
|
23
24
|
Emitter: () => Emitter,
|
|
24
25
|
StateEmitter: () => StateEmitter,
|
|
25
26
|
Subscription: () => Subscription
|
|
@@ -119,6 +120,14 @@ var Emitter = class {
|
|
|
119
120
|
}
|
|
120
121
|
};
|
|
121
122
|
|
|
123
|
+
// src/async-emitter.ts
|
|
124
|
+
var AsyncEmitter = class extends Emitter {
|
|
125
|
+
async emit(value) {
|
|
126
|
+
const results = Array.from(this.listeners).map((listener) => listener(value));
|
|
127
|
+
await Promise.all(results);
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
|
|
122
131
|
// src/state-emitter.ts
|
|
123
132
|
var StateEmitter = class extends Emitter {
|
|
124
133
|
_lastValue;
|
|
@@ -170,6 +179,7 @@ var StateEmitter = class extends Emitter {
|
|
|
170
179
|
};
|
|
171
180
|
// Annotate the CommonJS export names for ESM import in node:
|
|
172
181
|
0 && (module.exports = {
|
|
182
|
+
AsyncEmitter,
|
|
173
183
|
Emitter,
|
|
174
184
|
StateEmitter,
|
|
175
185
|
Subscription
|
package/dist/index.mjs
CHANGED
|
@@ -91,6 +91,14 @@ var Emitter = class {
|
|
|
91
91
|
}
|
|
92
92
|
};
|
|
93
93
|
|
|
94
|
+
// src/async-emitter.ts
|
|
95
|
+
var AsyncEmitter = class extends Emitter {
|
|
96
|
+
async emit(value) {
|
|
97
|
+
const results = Array.from(this.listeners).map((listener) => listener(value));
|
|
98
|
+
await Promise.all(results);
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
|
|
94
102
|
// src/state-emitter.ts
|
|
95
103
|
var StateEmitter = class extends Emitter {
|
|
96
104
|
_lastValue;
|
|
@@ -141,6 +149,7 @@ var StateEmitter = class extends Emitter {
|
|
|
141
149
|
}
|
|
142
150
|
};
|
|
143
151
|
export {
|
|
152
|
+
AsyncEmitter,
|
|
144
153
|
Emitter,
|
|
145
154
|
StateEmitter,
|
|
146
155
|
Subscription
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@axijs/emitter",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "A minimalistic, type-safe library for single-event and state emitting",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://github.com/axijs/tools/tree/main/packages/emitter",
|
|
@@ -9,8 +9,7 @@
|
|
|
9
9
|
"url": "https://github.com/axijs/tools.git",
|
|
10
10
|
"directory": "packages/emitter"
|
|
11
11
|
},
|
|
12
|
-
"keywords": [
|
|
13
|
-
],
|
|
12
|
+
"keywords": [],
|
|
14
13
|
"types": "./dist/index.d.ts",
|
|
15
14
|
"main": "./dist/index.js",
|
|
16
15
|
"module": "./dist/index.mjs",
|