@lincode/events 1.0.29 → 1.0.30
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/lib/event.d.ts +11 -2
- package/lib/event.js +14 -1
- package/lib-commonjs/event.js +14 -1
- package/package.json +1 -1
- package/src/event.ts +23 -2
package/lib/event.d.ts
CHANGED
|
@@ -1,12 +1,21 @@
|
|
|
1
1
|
import { Cancellable } from "@lincode/promiselikes";
|
|
2
2
|
export declare class Event<Payload = void> {
|
|
3
3
|
private cbs;
|
|
4
|
+
private state?;
|
|
4
5
|
on(cb: (val: Payload) => void): Cancellable;
|
|
5
6
|
once(cb: (val: Payload) => void): Cancellable;
|
|
6
7
|
emit(value: Payload): void;
|
|
8
|
+
setState(value: Payload): void;
|
|
9
|
+
getState(): Payload | undefined;
|
|
7
10
|
}
|
|
8
11
|
type Emit<T> = (val: T, isState?: boolean) => void;
|
|
9
12
|
type On<T> = (cb: (val: T) => void, once?: boolean) => Cancellable;
|
|
10
|
-
|
|
11
|
-
|
|
13
|
+
type GetState<T> = () => T | undefined;
|
|
14
|
+
export type EventFunctions = readonly [
|
|
15
|
+
Emit<void>,
|
|
16
|
+
On<void>,
|
|
17
|
+
Emit<void>,
|
|
18
|
+
GetState<void>
|
|
19
|
+
];
|
|
20
|
+
declare const _default: <T = void>() => readonly [Emit<T>, On<T>, Emit<T>, GetState<T>];
|
|
12
21
|
export default _default;
|
package/lib/event.js
CHANGED
|
@@ -4,6 +4,8 @@ export class Event {
|
|
|
4
4
|
this.cbs = new Set();
|
|
5
5
|
}
|
|
6
6
|
on(cb) {
|
|
7
|
+
if ("state" in this)
|
|
8
|
+
cb(this.state);
|
|
7
9
|
this.cbs.add(cb);
|
|
8
10
|
return new Cancellable(() => this.cbs.delete(cb));
|
|
9
11
|
}
|
|
@@ -19,10 +21,21 @@ export class Event {
|
|
|
19
21
|
for (const cb of this.cbs)
|
|
20
22
|
cb(value);
|
|
21
23
|
}
|
|
24
|
+
setState(value) {
|
|
25
|
+
if ("state" in this && this.state === value)
|
|
26
|
+
return;
|
|
27
|
+
this.state = value;
|
|
28
|
+
this.emit(value);
|
|
29
|
+
}
|
|
30
|
+
getState() {
|
|
31
|
+
return this.state;
|
|
32
|
+
}
|
|
22
33
|
}
|
|
23
34
|
export default () => {
|
|
24
35
|
const event = new Event();
|
|
25
36
|
const emit = (val) => event.emit(val);
|
|
26
37
|
const on = (cb, once) => once ? event.once(cb) : event.on(cb);
|
|
27
|
-
|
|
38
|
+
const emitState = (val) => event.setState(val);
|
|
39
|
+
const getState = () => event.getState();
|
|
40
|
+
return [emit, on, emitState, getState];
|
|
28
41
|
};
|
package/lib-commonjs/event.js
CHANGED
|
@@ -7,6 +7,8 @@ class Event {
|
|
|
7
7
|
this.cbs = new Set();
|
|
8
8
|
}
|
|
9
9
|
on(cb) {
|
|
10
|
+
if ("state" in this)
|
|
11
|
+
cb(this.state);
|
|
10
12
|
this.cbs.add(cb);
|
|
11
13
|
return new promiselikes_1.Cancellable(() => this.cbs.delete(cb));
|
|
12
14
|
}
|
|
@@ -22,11 +24,22 @@ class Event {
|
|
|
22
24
|
for (const cb of this.cbs)
|
|
23
25
|
cb(value);
|
|
24
26
|
}
|
|
27
|
+
setState(value) {
|
|
28
|
+
if ("state" in this && this.state === value)
|
|
29
|
+
return;
|
|
30
|
+
this.state = value;
|
|
31
|
+
this.emit(value);
|
|
32
|
+
}
|
|
33
|
+
getState() {
|
|
34
|
+
return this.state;
|
|
35
|
+
}
|
|
25
36
|
}
|
|
26
37
|
exports.Event = Event;
|
|
27
38
|
exports.default = () => {
|
|
28
39
|
const event = new Event();
|
|
29
40
|
const emit = (val) => event.emit(val);
|
|
30
41
|
const on = (cb, once) => once ? event.once(cb) : event.on(cb);
|
|
31
|
-
|
|
42
|
+
const emitState = (val) => event.setState(val);
|
|
43
|
+
const getState = () => event.getState();
|
|
44
|
+
return [emit, on, emitState, getState];
|
|
32
45
|
};
|
package/package.json
CHANGED
package/src/event.ts
CHANGED
|
@@ -2,8 +2,11 @@ import { Cancellable } from "@lincode/promiselikes"
|
|
|
2
2
|
|
|
3
3
|
export class Event<Payload = void> {
|
|
4
4
|
private cbs = new Set<(val: Payload) => void>()
|
|
5
|
+
private state?: Payload
|
|
5
6
|
|
|
6
7
|
public on(cb: (val: Payload) => void): Cancellable {
|
|
8
|
+
if ("state" in this) cb(this.state!)
|
|
9
|
+
|
|
7
10
|
this.cbs.add(cb)
|
|
8
11
|
return new Cancellable(() => this.cbs.delete(cb))
|
|
9
12
|
}
|
|
@@ -22,17 +25,35 @@ export class Event<Payload = void> {
|
|
|
22
25
|
public emit(value: Payload): void {
|
|
23
26
|
for (const cb of this.cbs) cb(value)
|
|
24
27
|
}
|
|
28
|
+
|
|
29
|
+
public setState(value: Payload): void {
|
|
30
|
+
if ("state" in this && this.state === value) return
|
|
31
|
+
this.state = value
|
|
32
|
+
this.emit(value)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
public getState(): Payload | undefined {
|
|
36
|
+
return this.state
|
|
37
|
+
}
|
|
25
38
|
}
|
|
26
39
|
|
|
27
40
|
type Emit<T> = (val: T, isState?: boolean) => void
|
|
28
41
|
type On<T> = (cb: (val: T) => void, once?: boolean) => Cancellable
|
|
42
|
+
type GetState<T> = () => T | undefined
|
|
29
43
|
|
|
30
|
-
export type EventFunctions = readonly [
|
|
44
|
+
export type EventFunctions = readonly [
|
|
45
|
+
Emit<void>,
|
|
46
|
+
On<void>,
|
|
47
|
+
Emit<void>,
|
|
48
|
+
GetState<void>
|
|
49
|
+
]
|
|
31
50
|
|
|
32
51
|
export default <T = void>() => {
|
|
33
52
|
const event = new Event<T>()
|
|
34
53
|
const emit: Emit<T> = (val: T) => event.emit(val)
|
|
35
54
|
const on: On<T> = (cb: (val: T) => void, once?: boolean) =>
|
|
36
55
|
once ? event.once(cb) : event.on(cb)
|
|
37
|
-
|
|
56
|
+
const emitState: Emit<T> = (val: T) => event.setState(val)
|
|
57
|
+
const getState: GetState<T> = () => event.getState()
|
|
58
|
+
return <const>[emit, on, emitState, getState]
|
|
38
59
|
}
|