@lincode/events 1.0.27 → 1.0.29
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 +10 -10
- package/lib/event.js +2 -15
- package/lib/index.d.ts +1 -1
- package/lib/index.js +1 -1
- package/lib-commonjs/event.js +3 -14
- package/lib-commonjs/index.js +2 -1
- package/package.json +4 -5
- package/src/event.ts +3 -24
- package/src/index.ts +1 -1
package/lib/event.d.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { Cancellable } from "@lincode/promiselikes";
|
|
2
|
-
declare
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
];
|
|
11
|
-
declare const _default: <T = void>() => readonly [Emit<T>, On<T
|
|
2
|
+
export declare class Event<Payload = void> {
|
|
3
|
+
private cbs;
|
|
4
|
+
on(cb: (val: Payload) => void): Cancellable;
|
|
5
|
+
once(cb: (val: Payload) => void): Cancellable;
|
|
6
|
+
emit(value: Payload): void;
|
|
7
|
+
}
|
|
8
|
+
type Emit<T> = (val: T, isState?: boolean) => void;
|
|
9
|
+
type On<T> = (cb: (val: T) => void, once?: boolean) => Cancellable;
|
|
10
|
+
export type EventFunctions = readonly [Emit<void>, On<void>];
|
|
11
|
+
declare const _default: <T = void>() => readonly [Emit<T>, On<T>];
|
|
12
12
|
export default _default;
|
package/lib/event.js
CHANGED
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
import { Cancellable } from "@lincode/promiselikes";
|
|
2
|
-
class Event {
|
|
2
|
+
export class Event {
|
|
3
3
|
constructor() {
|
|
4
4
|
this.cbs = new Set();
|
|
5
5
|
}
|
|
6
6
|
on(cb) {
|
|
7
|
-
if ("state" in this)
|
|
8
|
-
cb(this.state);
|
|
9
7
|
this.cbs.add(cb);
|
|
10
8
|
return new Cancellable(() => this.cbs.delete(cb));
|
|
11
9
|
}
|
|
@@ -21,21 +19,10 @@ class Event {
|
|
|
21
19
|
for (const cb of this.cbs)
|
|
22
20
|
cb(value);
|
|
23
21
|
}
|
|
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
|
-
}
|
|
33
22
|
}
|
|
34
23
|
export default () => {
|
|
35
24
|
const event = new Event();
|
|
36
25
|
const emit = (val) => event.emit(val);
|
|
37
26
|
const on = (cb, once) => once ? event.once(cb) : event.on(cb);
|
|
38
|
-
|
|
39
|
-
const getState = () => event.getState();
|
|
40
|
-
return [emit, on, emitState, getState];
|
|
27
|
+
return [emit, on];
|
|
41
28
|
};
|
package/lib/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export { default, default as Events } from "./Events";
|
|
2
|
-
export { default as event, EventFunctions } from "./event";
|
|
2
|
+
export { default as event, EventFunctions, Event } from "./event";
|
package/lib/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export { default, default as Events } from "./Events";
|
|
2
|
-
export { default as event } from "./event";
|
|
2
|
+
export { default as event, Event } from "./event";
|
package/lib-commonjs/event.js
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Event = void 0;
|
|
3
4
|
const promiselikes_1 = require("@lincode/promiselikes");
|
|
4
5
|
class Event {
|
|
5
6
|
constructor() {
|
|
6
7
|
this.cbs = new Set();
|
|
7
8
|
}
|
|
8
9
|
on(cb) {
|
|
9
|
-
if ("state" in this)
|
|
10
|
-
cb(this.state);
|
|
11
10
|
this.cbs.add(cb);
|
|
12
11
|
return new promiselikes_1.Cancellable(() => this.cbs.delete(cb));
|
|
13
12
|
}
|
|
@@ -23,21 +22,11 @@ class Event {
|
|
|
23
22
|
for (const cb of this.cbs)
|
|
24
23
|
cb(value);
|
|
25
24
|
}
|
|
26
|
-
setState(value) {
|
|
27
|
-
if ("state" in this && this.state === value)
|
|
28
|
-
return;
|
|
29
|
-
this.state = value;
|
|
30
|
-
this.emit(value);
|
|
31
|
-
}
|
|
32
|
-
getState() {
|
|
33
|
-
return this.state;
|
|
34
|
-
}
|
|
35
25
|
}
|
|
26
|
+
exports.Event = Event;
|
|
36
27
|
exports.default = () => {
|
|
37
28
|
const event = new Event();
|
|
38
29
|
const emit = (val) => event.emit(val);
|
|
39
30
|
const on = (cb, once) => once ? event.once(cb) : event.on(cb);
|
|
40
|
-
|
|
41
|
-
const getState = () => event.getState();
|
|
42
|
-
return [emit, on, emitState, getState];
|
|
31
|
+
return [emit, on];
|
|
43
32
|
};
|
package/lib-commonjs/index.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.event = exports.Events = exports.default = void 0;
|
|
3
|
+
exports.Event = exports.event = exports.Events = exports.default = void 0;
|
|
4
4
|
var Events_1 = require("./Events");
|
|
5
5
|
Object.defineProperty(exports, "default", { enumerable: true, get: function () { return Events_1.default; } });
|
|
6
6
|
Object.defineProperty(exports, "Events", { enumerable: true, get: function () { return Events_1.default; } });
|
|
7
7
|
var event_1 = require("./event");
|
|
8
8
|
Object.defineProperty(exports, "event", { enumerable: true, get: function () { return event_1.default; } });
|
|
9
|
+
Object.defineProperty(exports, "Event", { enumerable: true, get: function () { return event_1.Event; } });
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lincode/events",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.29",
|
|
4
4
|
"description": "Generated by ambients-cli",
|
|
5
5
|
"author": "Lai Schwe",
|
|
6
6
|
"license": "MIT",
|
|
@@ -8,12 +8,11 @@
|
|
|
8
8
|
"ambients"
|
|
9
9
|
],
|
|
10
10
|
"dependencies": {
|
|
11
|
-
"@lincode/promiselikes": "
|
|
12
|
-
"@lincode/utils": "
|
|
11
|
+
"@lincode/promiselikes": "^1.0.24",
|
|
12
|
+
"@lincode/utils": "^1.3.5"
|
|
13
13
|
},
|
|
14
|
-
"peerDependencies": {},
|
|
15
14
|
"devDependencies": {
|
|
16
|
-
"typescript": "
|
|
15
|
+
"typescript": "^4.9.4"
|
|
17
16
|
},
|
|
18
17
|
"scripts": {
|
|
19
18
|
"build": "ambients clean && yarn tsc && yarn tsc -p tsconfig-commonjs.json",
|
package/src/event.ts
CHANGED
|
@@ -1,12 +1,9 @@
|
|
|
1
1
|
import { Cancellable } from "@lincode/promiselikes"
|
|
2
2
|
|
|
3
|
-
class Event<Payload = void> {
|
|
3
|
+
export class Event<Payload = void> {
|
|
4
4
|
private cbs = new Set<(val: Payload) => void>()
|
|
5
|
-
private state?: Payload
|
|
6
5
|
|
|
7
6
|
public on(cb: (val: Payload) => void): Cancellable {
|
|
8
|
-
if ("state" in this) cb(this.state!)
|
|
9
|
-
|
|
10
7
|
this.cbs.add(cb)
|
|
11
8
|
return new Cancellable(() => this.cbs.delete(cb))
|
|
12
9
|
}
|
|
@@ -25,35 +22,17 @@ class Event<Payload = void> {
|
|
|
25
22
|
public emit(value: Payload): void {
|
|
26
23
|
for (const cb of this.cbs) cb(value)
|
|
27
24
|
}
|
|
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
|
-
}
|
|
38
25
|
}
|
|
39
26
|
|
|
40
27
|
type Emit<T> = (val: T, isState?: boolean) => void
|
|
41
28
|
type On<T> = (cb: (val: T) => void, once?: boolean) => Cancellable
|
|
42
|
-
type GetState<T> = () => T | undefined
|
|
43
29
|
|
|
44
|
-
export type EventFunctions = readonly [
|
|
45
|
-
Emit<void>,
|
|
46
|
-
On<void>,
|
|
47
|
-
Emit<void>,
|
|
48
|
-
GetState<void>
|
|
49
|
-
]
|
|
30
|
+
export type EventFunctions = readonly [Emit<void>, On<void>]
|
|
50
31
|
|
|
51
32
|
export default <T = void>() => {
|
|
52
33
|
const event = new Event<T>()
|
|
53
34
|
const emit: Emit<T> = (val: T) => event.emit(val)
|
|
54
35
|
const on: On<T> = (cb: (val: T) => void, once?: boolean) =>
|
|
55
36
|
once ? event.once(cb) : event.on(cb)
|
|
56
|
-
|
|
57
|
-
const getState: GetState<T> = () => event.getState()
|
|
58
|
-
return <const>[emit, on, emitState, getState]
|
|
37
|
+
return <const>[emit, on]
|
|
59
38
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export { default, default as Events } from "./Events"
|
|
2
|
-
export { default as event, EventFunctions } from "./event"
|
|
2
|
+
export { default as event, EventFunctions, Event } from "./event"
|