@lincode/events 1.0.21 → 1.0.24
Sign up to get free protection for your applications and to get access to all the features.
- package/.ambientsrc +2 -1
- package/lib/Events.d.ts +13 -0
- package/lib/Events.js +59 -0
- package/lib/event.d.ts +6 -0
- package/lib/event.js +41 -0
- package/lib/index.d.ts +2 -19
- package/lib/index.js +2 -99
- package/lib-commonjs/Events.js +62 -0
- package/lib-commonjs/event.js +43 -0
- package/lib-commonjs/index.js +6 -102
- package/package.json +1 -2
- package/src/Events.ts +63 -0
- package/src/event.ts +51 -0
- package/src/index.ts +2 -113
- package/tsconfig-commonjs.json +2 -1
- package/tsconfig.json +2 -1
package/.ambientsrc
CHANGED
package/lib/Events.d.ts
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
import { Cancellable } from "@lincode/promiselikes";
|
2
|
+
export default class<Payload = void, Names extends string = string> {
|
3
|
+
private cbsMap;
|
4
|
+
private states?;
|
5
|
+
on(name: Names | Array<Names>, cb: (val: Payload) => void): Cancellable;
|
6
|
+
once(name: Names | Array<Names>, cb: (val: Payload) => void): Cancellable;
|
7
|
+
emit(name: Names, value: Payload): void;
|
8
|
+
hasState(name: Names): boolean;
|
9
|
+
setState(name: Names, value: Payload): void;
|
10
|
+
getState(name: Names): Payload | undefined;
|
11
|
+
deleteState(name: Names): void;
|
12
|
+
clear(): void;
|
13
|
+
}
|
package/lib/Events.js
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
import { Cancellable } from "@lincode/promiselikes";
|
2
|
+
import { forceGet } from "@lincode/utils";
|
3
|
+
export default class {
|
4
|
+
constructor() {
|
5
|
+
this.cbsMap = new Map();
|
6
|
+
}
|
7
|
+
on(name, cb) {
|
8
|
+
var _a;
|
9
|
+
if (Array.isArray(name)) {
|
10
|
+
const handle = new Cancellable();
|
11
|
+
for (const n of name)
|
12
|
+
handle.watch(this.on(n, cb));
|
13
|
+
return handle;
|
14
|
+
}
|
15
|
+
if ((_a = this.states) === null || _a === void 0 ? void 0 : _a.has(name))
|
16
|
+
cb(this.states.get(name));
|
17
|
+
const cbs = forceGet(this.cbsMap, name, () => new Set());
|
18
|
+
cbs.add(cb);
|
19
|
+
return new Cancellable(() => cbs.delete(cb));
|
20
|
+
}
|
21
|
+
once(name, cb) {
|
22
|
+
const handle = new Cancellable();
|
23
|
+
handle.watch(this.on(name, value => {
|
24
|
+
handle.cancel();
|
25
|
+
cb(value);
|
26
|
+
}));
|
27
|
+
return handle;
|
28
|
+
}
|
29
|
+
emit(name, value) {
|
30
|
+
if (this.cbsMap.has(name))
|
31
|
+
for (const cb of this.cbsMap.get(name))
|
32
|
+
cb(value);
|
33
|
+
}
|
34
|
+
hasState(name) {
|
35
|
+
var _a;
|
36
|
+
return !!((_a = this.states) === null || _a === void 0 ? void 0 : _a.has(name));
|
37
|
+
}
|
38
|
+
setState(name, value) {
|
39
|
+
var _a;
|
40
|
+
(_a = this.states) !== null && _a !== void 0 ? _a : (this.states = new Map());
|
41
|
+
if (this.states.has(name) && this.states.get(name) === value)
|
42
|
+
return;
|
43
|
+
this.states.set(name, value);
|
44
|
+
this.emit(name, value);
|
45
|
+
}
|
46
|
+
getState(name) {
|
47
|
+
var _a;
|
48
|
+
return (_a = this.states) === null || _a === void 0 ? void 0 : _a.get(name);
|
49
|
+
}
|
50
|
+
deleteState(name) {
|
51
|
+
var _a;
|
52
|
+
(_a = this.states) === null || _a === void 0 ? void 0 : _a.delete(name);
|
53
|
+
}
|
54
|
+
clear() {
|
55
|
+
var _a;
|
56
|
+
this.cbsMap.clear();
|
57
|
+
(_a = this.states) === null || _a === void 0 ? void 0 : _a.clear();
|
58
|
+
}
|
59
|
+
}
|
package/lib/event.d.ts
ADDED
@@ -0,0 +1,6 @@
|
|
1
|
+
import { Cancellable } from "@lincode/promiselikes";
|
2
|
+
declare type Emit<T> = (val: T, isState?: boolean) => void;
|
3
|
+
declare type On<T> = (cb: (val: T) => void, once?: boolean) => Cancellable;
|
4
|
+
declare type GetState<T> = () => T | undefined;
|
5
|
+
declare const _default: <T = void>() => readonly [Emit<T>, On<T>, Emit<T>, GetState<T>];
|
6
|
+
export default _default;
|
package/lib/event.js
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
import { Cancellable } from "@lincode/promiselikes";
|
2
|
+
class Event {
|
3
|
+
constructor() {
|
4
|
+
this.cbs = new Set();
|
5
|
+
}
|
6
|
+
on(cb) {
|
7
|
+
if ("state" in this)
|
8
|
+
cb(this.state);
|
9
|
+
this.cbs.add(cb);
|
10
|
+
return new Cancellable(() => this.cbs.delete(cb));
|
11
|
+
}
|
12
|
+
once(cb) {
|
13
|
+
const handle = new Cancellable();
|
14
|
+
handle.watch(this.on(value => {
|
15
|
+
handle.cancel();
|
16
|
+
cb(value);
|
17
|
+
}));
|
18
|
+
return handle;
|
19
|
+
}
|
20
|
+
emit(value) {
|
21
|
+
for (const cb of this.cbs)
|
22
|
+
cb(value);
|
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
|
+
}
|
33
|
+
}
|
34
|
+
export default () => {
|
35
|
+
const event = new Event();
|
36
|
+
const emit = (val) => event.emit(val);
|
37
|
+
const on = (cb, once) => once ? event.once(cb) : event.on(cb);
|
38
|
+
const emitState = (val) => event.setState(val);
|
39
|
+
const getState = () => event.getState();
|
40
|
+
return [emit, on, emitState, getState];
|
41
|
+
};
|
package/lib/index.d.ts
CHANGED
@@ -1,19 +1,2 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
export default class Events<Payload = void, Names extends string = string> {
|
4
|
-
private cbsMap;
|
5
|
-
private states?;
|
6
|
-
on(name: Names | Array<Names>, cb: Callback<Payload>): Cancellable;
|
7
|
-
once(name: Names | Array<Names>, cb: Callback<Payload>): Cancellable;
|
8
|
-
emit(name: Names, value: Payload): void;
|
9
|
-
hasState(name: Names): boolean;
|
10
|
-
setState(name: Names, value: Payload): void;
|
11
|
-
getState(name: Names): Payload | undefined;
|
12
|
-
deleteState(name: Names): void;
|
13
|
-
clear(): void;
|
14
|
-
}
|
15
|
-
export { Events };
|
16
|
-
declare type Emit<T> = (val: T, isState?: boolean) => void;
|
17
|
-
declare type On<T> = (cb: (val: T) => void, once?: boolean) => Cancellable;
|
18
|
-
declare type GetState<T> = () => T | undefined;
|
19
|
-
export declare const event: <T = void>() => readonly [Emit<T>, On<T>, GetState<T>];
|
1
|
+
export { default, default as Events } from "./Events";
|
2
|
+
export { default as event } from "./event";
|
package/lib/index.js
CHANGED
@@ -1,99 +1,2 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
export default class Events {
|
4
|
-
constructor() {
|
5
|
-
this.cbsMap = new Map();
|
6
|
-
}
|
7
|
-
on(name, cb) {
|
8
|
-
var _a;
|
9
|
-
if (Array.isArray(name)) {
|
10
|
-
const handle = new Cancellable();
|
11
|
-
for (const n of name)
|
12
|
-
handle.watch(this.on(n, cb));
|
13
|
-
return handle;
|
14
|
-
}
|
15
|
-
if ((_a = this.states) === null || _a === void 0 ? void 0 : _a.has(name))
|
16
|
-
cb(this.states.get(name));
|
17
|
-
const cbs = forceGet(this.cbsMap, name, () => new Set());
|
18
|
-
cbs.add(cb);
|
19
|
-
return new Cancellable(() => cbs.delete(cb));
|
20
|
-
}
|
21
|
-
once(name, cb) {
|
22
|
-
const handle = new Cancellable();
|
23
|
-
handle.watch(this.on(name, value => {
|
24
|
-
handle.cancel();
|
25
|
-
cb(value);
|
26
|
-
}));
|
27
|
-
return handle;
|
28
|
-
}
|
29
|
-
emit(name, value) {
|
30
|
-
if (this.cbsMap.has(name))
|
31
|
-
for (const cb of this.cbsMap.get(name))
|
32
|
-
cb(value);
|
33
|
-
}
|
34
|
-
hasState(name) {
|
35
|
-
var _a;
|
36
|
-
return !!((_a = this.states) === null || _a === void 0 ? void 0 : _a.has(name));
|
37
|
-
}
|
38
|
-
setState(name, value) {
|
39
|
-
var _a;
|
40
|
-
(_a = this.states) !== null && _a !== void 0 ? _a : (this.states = new Map());
|
41
|
-
if (this.states.has(name) && this.states.get(name) === value)
|
42
|
-
return;
|
43
|
-
this.states.set(name, value);
|
44
|
-
this.emit(name, value);
|
45
|
-
}
|
46
|
-
getState(name) {
|
47
|
-
var _a;
|
48
|
-
return (_a = this.states) === null || _a === void 0 ? void 0 : _a.get(name);
|
49
|
-
}
|
50
|
-
deleteState(name) {
|
51
|
-
var _a;
|
52
|
-
(_a = this.states) === null || _a === void 0 ? void 0 : _a.delete(name);
|
53
|
-
}
|
54
|
-
clear() {
|
55
|
-
var _a;
|
56
|
-
this.cbsMap.clear();
|
57
|
-
(_a = this.states) === null || _a === void 0 ? void 0 : _a.clear();
|
58
|
-
}
|
59
|
-
}
|
60
|
-
export { Events };
|
61
|
-
class Event {
|
62
|
-
constructor() {
|
63
|
-
this.cbs = new Set();
|
64
|
-
}
|
65
|
-
on(cb) {
|
66
|
-
if ("state" in this)
|
67
|
-
cb(this.state);
|
68
|
-
this.cbs.add(cb);
|
69
|
-
return new Cancellable(() => this.cbs.delete(cb));
|
70
|
-
}
|
71
|
-
once(cb) {
|
72
|
-
const handle = new Cancellable();
|
73
|
-
handle.watch(this.on(value => {
|
74
|
-
handle.cancel();
|
75
|
-
cb(value);
|
76
|
-
}));
|
77
|
-
return handle;
|
78
|
-
}
|
79
|
-
emit(value) {
|
80
|
-
for (const cb of this.cbs)
|
81
|
-
cb(value);
|
82
|
-
}
|
83
|
-
setState(value) {
|
84
|
-
if ("state" in this && this.state === value)
|
85
|
-
return;
|
86
|
-
this.state = value;
|
87
|
-
this.emit(value);
|
88
|
-
}
|
89
|
-
getState() {
|
90
|
-
return this.state;
|
91
|
-
}
|
92
|
-
}
|
93
|
-
export const event = () => {
|
94
|
-
const event = new Event();
|
95
|
-
const emit = (val, isState) => isState ? event.setState(val) : event.emit(val);
|
96
|
-
const on = (cb, once) => once ? event.once(cb) : event.on(cb);
|
97
|
-
const getState = () => event.getState();
|
98
|
-
return [emit, on, getState];
|
99
|
-
};
|
1
|
+
export { default, default as Events } from "./Events";
|
2
|
+
export { default as event } from "./event";
|
@@ -0,0 +1,62 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
const promiselikes_1 = require("@lincode/promiselikes");
|
4
|
+
const utils_1 = require("@lincode/utils");
|
5
|
+
class default_1 {
|
6
|
+
constructor() {
|
7
|
+
this.cbsMap = new Map();
|
8
|
+
}
|
9
|
+
on(name, cb) {
|
10
|
+
var _a;
|
11
|
+
if (Array.isArray(name)) {
|
12
|
+
const handle = new promiselikes_1.Cancellable();
|
13
|
+
for (const n of name)
|
14
|
+
handle.watch(this.on(n, cb));
|
15
|
+
return handle;
|
16
|
+
}
|
17
|
+
if ((_a = this.states) === null || _a === void 0 ? void 0 : _a.has(name))
|
18
|
+
cb(this.states.get(name));
|
19
|
+
const cbs = (0, utils_1.forceGet)(this.cbsMap, name, () => new Set());
|
20
|
+
cbs.add(cb);
|
21
|
+
return new promiselikes_1.Cancellable(() => cbs.delete(cb));
|
22
|
+
}
|
23
|
+
once(name, cb) {
|
24
|
+
const handle = new promiselikes_1.Cancellable();
|
25
|
+
handle.watch(this.on(name, value => {
|
26
|
+
handle.cancel();
|
27
|
+
cb(value);
|
28
|
+
}));
|
29
|
+
return handle;
|
30
|
+
}
|
31
|
+
emit(name, value) {
|
32
|
+
if (this.cbsMap.has(name))
|
33
|
+
for (const cb of this.cbsMap.get(name))
|
34
|
+
cb(value);
|
35
|
+
}
|
36
|
+
hasState(name) {
|
37
|
+
var _a;
|
38
|
+
return !!((_a = this.states) === null || _a === void 0 ? void 0 : _a.has(name));
|
39
|
+
}
|
40
|
+
setState(name, value) {
|
41
|
+
var _a;
|
42
|
+
(_a = this.states) !== null && _a !== void 0 ? _a : (this.states = new Map());
|
43
|
+
if (this.states.has(name) && this.states.get(name) === value)
|
44
|
+
return;
|
45
|
+
this.states.set(name, value);
|
46
|
+
this.emit(name, value);
|
47
|
+
}
|
48
|
+
getState(name) {
|
49
|
+
var _a;
|
50
|
+
return (_a = this.states) === null || _a === void 0 ? void 0 : _a.get(name);
|
51
|
+
}
|
52
|
+
deleteState(name) {
|
53
|
+
var _a;
|
54
|
+
(_a = this.states) === null || _a === void 0 ? void 0 : _a.delete(name);
|
55
|
+
}
|
56
|
+
clear() {
|
57
|
+
var _a;
|
58
|
+
this.cbsMap.clear();
|
59
|
+
(_a = this.states) === null || _a === void 0 ? void 0 : _a.clear();
|
60
|
+
}
|
61
|
+
}
|
62
|
+
exports.default = default_1;
|
@@ -0,0 +1,43 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
const promiselikes_1 = require("@lincode/promiselikes");
|
4
|
+
class Event {
|
5
|
+
constructor() {
|
6
|
+
this.cbs = new Set();
|
7
|
+
}
|
8
|
+
on(cb) {
|
9
|
+
if ("state" in this)
|
10
|
+
cb(this.state);
|
11
|
+
this.cbs.add(cb);
|
12
|
+
return new promiselikes_1.Cancellable(() => this.cbs.delete(cb));
|
13
|
+
}
|
14
|
+
once(cb) {
|
15
|
+
const handle = new promiselikes_1.Cancellable();
|
16
|
+
handle.watch(this.on(value => {
|
17
|
+
handle.cancel();
|
18
|
+
cb(value);
|
19
|
+
}));
|
20
|
+
return handle;
|
21
|
+
}
|
22
|
+
emit(value) {
|
23
|
+
for (const cb of this.cbs)
|
24
|
+
cb(value);
|
25
|
+
}
|
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
|
+
}
|
36
|
+
exports.default = () => {
|
37
|
+
const event = new Event();
|
38
|
+
const emit = (val) => event.emit(val);
|
39
|
+
const on = (cb, once) => once ? event.once(cb) : event.on(cb);
|
40
|
+
const emitState = (val) => event.setState(val);
|
41
|
+
const getState = () => event.getState();
|
42
|
+
return [emit, on, emitState, getState];
|
43
|
+
};
|
package/lib-commonjs/index.js
CHANGED
@@ -1,104 +1,8 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.event = exports.Events = void 0;
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
}
|
10
|
-
on(name, cb) {
|
11
|
-
var _a;
|
12
|
-
if (Array.isArray(name)) {
|
13
|
-
const handle = new promiselikes_1.Cancellable();
|
14
|
-
for (const n of name)
|
15
|
-
handle.watch(this.on(n, cb));
|
16
|
-
return handle;
|
17
|
-
}
|
18
|
-
if ((_a = this.states) === null || _a === void 0 ? void 0 : _a.has(name))
|
19
|
-
cb(this.states.get(name));
|
20
|
-
const cbs = utils_1.forceGet(this.cbsMap, name, () => new Set());
|
21
|
-
cbs.add(cb);
|
22
|
-
return new promiselikes_1.Cancellable(() => cbs.delete(cb));
|
23
|
-
}
|
24
|
-
once(name, cb) {
|
25
|
-
const handle = new promiselikes_1.Cancellable();
|
26
|
-
handle.watch(this.on(name, value => {
|
27
|
-
handle.cancel();
|
28
|
-
cb(value);
|
29
|
-
}));
|
30
|
-
return handle;
|
31
|
-
}
|
32
|
-
emit(name, value) {
|
33
|
-
if (this.cbsMap.has(name))
|
34
|
-
for (const cb of this.cbsMap.get(name))
|
35
|
-
cb(value);
|
36
|
-
}
|
37
|
-
hasState(name) {
|
38
|
-
var _a;
|
39
|
-
return !!((_a = this.states) === null || _a === void 0 ? void 0 : _a.has(name));
|
40
|
-
}
|
41
|
-
setState(name, value) {
|
42
|
-
var _a;
|
43
|
-
(_a = this.states) !== null && _a !== void 0 ? _a : (this.states = new Map());
|
44
|
-
if (this.states.has(name) && this.states.get(name) === value)
|
45
|
-
return;
|
46
|
-
this.states.set(name, value);
|
47
|
-
this.emit(name, value);
|
48
|
-
}
|
49
|
-
getState(name) {
|
50
|
-
var _a;
|
51
|
-
return (_a = this.states) === null || _a === void 0 ? void 0 : _a.get(name);
|
52
|
-
}
|
53
|
-
deleteState(name) {
|
54
|
-
var _a;
|
55
|
-
(_a = this.states) === null || _a === void 0 ? void 0 : _a.delete(name);
|
56
|
-
}
|
57
|
-
clear() {
|
58
|
-
var _a;
|
59
|
-
this.cbsMap.clear();
|
60
|
-
(_a = this.states) === null || _a === void 0 ? void 0 : _a.clear();
|
61
|
-
}
|
62
|
-
}
|
63
|
-
exports.default = Events;
|
64
|
-
exports.Events = Events;
|
65
|
-
class Event {
|
66
|
-
constructor() {
|
67
|
-
this.cbs = new Set();
|
68
|
-
}
|
69
|
-
on(cb) {
|
70
|
-
if ("state" in this)
|
71
|
-
cb(this.state);
|
72
|
-
this.cbs.add(cb);
|
73
|
-
return new promiselikes_1.Cancellable(() => this.cbs.delete(cb));
|
74
|
-
}
|
75
|
-
once(cb) {
|
76
|
-
const handle = new promiselikes_1.Cancellable();
|
77
|
-
handle.watch(this.on(value => {
|
78
|
-
handle.cancel();
|
79
|
-
cb(value);
|
80
|
-
}));
|
81
|
-
return handle;
|
82
|
-
}
|
83
|
-
emit(value) {
|
84
|
-
for (const cb of this.cbs)
|
85
|
-
cb(value);
|
86
|
-
}
|
87
|
-
setState(value) {
|
88
|
-
if ("state" in this && this.state === value)
|
89
|
-
return;
|
90
|
-
this.state = value;
|
91
|
-
this.emit(value);
|
92
|
-
}
|
93
|
-
getState() {
|
94
|
-
return this.state;
|
95
|
-
}
|
96
|
-
}
|
97
|
-
const event = () => {
|
98
|
-
const event = new Event();
|
99
|
-
const emit = (val, isState) => isState ? event.setState(val) : event.emit(val);
|
100
|
-
const on = (cb, once) => once ? event.once(cb) : event.on(cb);
|
101
|
-
const getState = () => event.getState();
|
102
|
-
return [emit, on, getState];
|
103
|
-
};
|
104
|
-
exports.event = event;
|
3
|
+
exports.event = exports.Events = exports.default = void 0;
|
4
|
+
var Events_1 = require("./Events");
|
5
|
+
Object.defineProperty(exports, "default", { enumerable: true, get: function () { return Events_1.default; } });
|
6
|
+
Object.defineProperty(exports, "Events", { enumerable: true, get: function () { return Events_1.default; } });
|
7
|
+
var event_1 = require("./event");
|
8
|
+
Object.defineProperty(exports, "event", { enumerable: true, get: function () { return event_1.default; } });
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@lincode/events",
|
3
|
-
"version": "1.0.
|
3
|
+
"version": "1.0.24",
|
4
4
|
"description": "Generated by ambients-cli",
|
5
5
|
"author": "Lai Schwe",
|
6
6
|
"license": "MIT",
|
@@ -13,7 +13,6 @@
|
|
13
13
|
},
|
14
14
|
"peerDependencies": {},
|
15
15
|
"devDependencies": {
|
16
|
-
"@types/node": "*",
|
17
16
|
"typescript": "*"
|
18
17
|
},
|
19
18
|
"scripts": {
|
package/src/Events.ts
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
import { Cancellable } from "@lincode/promiselikes"
|
2
|
+
import { forceGet } from "@lincode/utils"
|
3
|
+
|
4
|
+
export default class <Payload = void, Names extends string = string> {
|
5
|
+
private cbsMap = new Map<string, Set<(val: Payload) => void>>()
|
6
|
+
private states?: Map<string, Payload>
|
7
|
+
|
8
|
+
public on(name: Names | Array<Names>, cb: (val: Payload) => void): Cancellable {
|
9
|
+
if (Array.isArray(name)) {
|
10
|
+
const handle = new Cancellable()
|
11
|
+
for (const n of name)
|
12
|
+
handle.watch(this.on(n, cb))
|
13
|
+
|
14
|
+
return handle
|
15
|
+
}
|
16
|
+
if (this.states?.has(name))
|
17
|
+
cb(this.states.get(name)!)
|
18
|
+
|
19
|
+
const cbs = forceGet(this.cbsMap, name, () => new Set<(val: Payload) => void>())
|
20
|
+
|
21
|
+
cbs.add(cb)
|
22
|
+
return new Cancellable(() => cbs.delete(cb))
|
23
|
+
}
|
24
|
+
|
25
|
+
public once(name: Names | Array<Names>, cb: (val: Payload) => void): Cancellable {
|
26
|
+
const handle = new Cancellable()
|
27
|
+
handle.watch(this.on(name, value => {
|
28
|
+
handle.cancel()
|
29
|
+
cb(value)
|
30
|
+
}))
|
31
|
+
return handle
|
32
|
+
}
|
33
|
+
|
34
|
+
public emit(name: Names, value: Payload): void {
|
35
|
+
if (this.cbsMap.has(name))
|
36
|
+
for (const cb of this.cbsMap.get(name)!)
|
37
|
+
cb(value)
|
38
|
+
}
|
39
|
+
|
40
|
+
public hasState(name: Names): boolean {
|
41
|
+
return !!this.states?.has(name)
|
42
|
+
}
|
43
|
+
|
44
|
+
public setState(name: Names, value: Payload): void {
|
45
|
+
this.states ??= new Map()
|
46
|
+
if (this.states.has(name) && this.states.get(name) === value) return
|
47
|
+
this.states.set(name, value)
|
48
|
+
this.emit(name, value)
|
49
|
+
}
|
50
|
+
|
51
|
+
public getState(name: Names): Payload | undefined {
|
52
|
+
return this.states?.get(name)
|
53
|
+
}
|
54
|
+
|
55
|
+
public deleteState(name: Names): void {
|
56
|
+
this.states?.delete(name)
|
57
|
+
}
|
58
|
+
|
59
|
+
public clear(): void {
|
60
|
+
this.cbsMap.clear()
|
61
|
+
this.states?.clear()
|
62
|
+
}
|
63
|
+
}
|
package/src/event.ts
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
import { Cancellable } from "@lincode/promiselikes"
|
2
|
+
|
3
|
+
class Event<Payload = void> {
|
4
|
+
private cbs = new Set<(val: Payload) => void>()
|
5
|
+
private state?: Payload
|
6
|
+
|
7
|
+
public on(cb: (val: Payload) => void): Cancellable {
|
8
|
+
if ("state" in this)
|
9
|
+
cb(this.state!)
|
10
|
+
|
11
|
+
this.cbs.add(cb)
|
12
|
+
return new Cancellable(() => this.cbs.delete(cb))
|
13
|
+
}
|
14
|
+
|
15
|
+
public once(cb: (val: Payload) => void): Cancellable {
|
16
|
+
const handle = new Cancellable()
|
17
|
+
handle.watch(this.on(value => {
|
18
|
+
handle.cancel()
|
19
|
+
cb(value)
|
20
|
+
}))
|
21
|
+
return handle
|
22
|
+
}
|
23
|
+
|
24
|
+
public emit(value: Payload): void {
|
25
|
+
for (const cb of this.cbs)
|
26
|
+
cb(value)
|
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
|
+
}
|
38
|
+
}
|
39
|
+
|
40
|
+
type Emit<T> = (val: T, isState?: boolean) => void
|
41
|
+
type On<T> = (cb: (val: T) => void, once?: boolean) => Cancellable
|
42
|
+
type GetState<T> = () => T | undefined
|
43
|
+
|
44
|
+
export default <T = void>() => {
|
45
|
+
const event = new Event<T>()
|
46
|
+
const emit: Emit<T> = (val: T) => event.emit(val)
|
47
|
+
const on: On<T> = (cb: (val: T) => void, once?: boolean) => once ? event.once(cb) : event.on(cb)
|
48
|
+
const emitState: Emit<T> = (val: T) => event.setState(val)
|
49
|
+
const getState: GetState<T> = () => event.getState()
|
50
|
+
return <const>[emit, on, emitState, getState]
|
51
|
+
}
|
package/src/index.ts
CHANGED
@@ -1,113 +1,2 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
export default class Events<Payload = void, Names extends string = string> {
|
5
|
-
private cbsMap = new Map<string, Set<Callback<Payload>>>()
|
6
|
-
private states?: Map<string, Payload>
|
7
|
-
|
8
|
-
public on(name: Names | Array<Names>, cb: Callback<Payload>): Cancellable {
|
9
|
-
if (Array.isArray(name)) {
|
10
|
-
const handle = new Cancellable()
|
11
|
-
for (const n of name)
|
12
|
-
handle.watch(this.on(n, cb))
|
13
|
-
|
14
|
-
return handle
|
15
|
-
}
|
16
|
-
if (this.states?.has(name))
|
17
|
-
cb(this.states.get(name)!)
|
18
|
-
|
19
|
-
const cbs = forceGet(this.cbsMap, name, () => new Set<Callback<Payload>>())
|
20
|
-
|
21
|
-
cbs.add(cb)
|
22
|
-
return new Cancellable(() => cbs.delete(cb))
|
23
|
-
}
|
24
|
-
|
25
|
-
public once(name: Names | Array<Names>, cb: Callback<Payload>): Cancellable {
|
26
|
-
const handle = new Cancellable()
|
27
|
-
handle.watch(this.on(name, value => {
|
28
|
-
handle.cancel()
|
29
|
-
cb(value)
|
30
|
-
}))
|
31
|
-
return handle
|
32
|
-
}
|
33
|
-
|
34
|
-
public emit(name: Names, value: Payload): void {
|
35
|
-
if (this.cbsMap.has(name))
|
36
|
-
for (const cb of this.cbsMap.get(name)!)
|
37
|
-
cb(value)
|
38
|
-
}
|
39
|
-
|
40
|
-
public hasState(name: Names): boolean {
|
41
|
-
return !!this.states?.has(name)
|
42
|
-
}
|
43
|
-
|
44
|
-
public setState(name: Names, value: Payload): void {
|
45
|
-
this.states ??= new Map()
|
46
|
-
if (this.states.has(name) && this.states.get(name) === value) return
|
47
|
-
this.states.set(name, value)
|
48
|
-
this.emit(name, value)
|
49
|
-
}
|
50
|
-
|
51
|
-
public getState(name: Names): Payload | undefined {
|
52
|
-
return this.states?.get(name)
|
53
|
-
}
|
54
|
-
|
55
|
-
public deleteState(name: Names): void {
|
56
|
-
this.states?.delete(name)
|
57
|
-
}
|
58
|
-
|
59
|
-
public clear(): void {
|
60
|
-
this.cbsMap.clear()
|
61
|
-
this.states?.clear()
|
62
|
-
}
|
63
|
-
}
|
64
|
-
export { Events }
|
65
|
-
|
66
|
-
class Event<Payload = void> {
|
67
|
-
private cbs = new Set<Callback<Payload>>()
|
68
|
-
private state?: Payload
|
69
|
-
|
70
|
-
public on(cb: Callback<Payload>): Cancellable {
|
71
|
-
if ("state" in this)
|
72
|
-
cb(this.state!)
|
73
|
-
|
74
|
-
this.cbs.add(cb)
|
75
|
-
return new Cancellable(() => this.cbs.delete(cb))
|
76
|
-
}
|
77
|
-
|
78
|
-
public once(cb: Callback<Payload>): Cancellable {
|
79
|
-
const handle = new Cancellable()
|
80
|
-
handle.watch(this.on(value => {
|
81
|
-
handle.cancel()
|
82
|
-
cb(value)
|
83
|
-
}))
|
84
|
-
return handle
|
85
|
-
}
|
86
|
-
|
87
|
-
public emit(value: Payload): void {
|
88
|
-
for (const cb of this.cbs)
|
89
|
-
cb(value)
|
90
|
-
}
|
91
|
-
|
92
|
-
public setState(value: Payload): void {
|
93
|
-
if ("state" in this && this.state === value) return
|
94
|
-
this.state = value
|
95
|
-
this.emit(value)
|
96
|
-
}
|
97
|
-
|
98
|
-
public getState(): Payload | undefined {
|
99
|
-
return this.state
|
100
|
-
}
|
101
|
-
}
|
102
|
-
|
103
|
-
type Emit<T> = (val: T, isState?: boolean) => void
|
104
|
-
type On<T> = (cb: (val: T) => void, once?: boolean) => Cancellable
|
105
|
-
type GetState<T> = () => T | undefined
|
106
|
-
|
107
|
-
export const event = <T = void>() => {
|
108
|
-
const event = new Event<T>()
|
109
|
-
const emit: Emit<T> = (val: T, isState?: boolean) => isState ? event.setState(val) : event.emit(val)
|
110
|
-
const on: On<T> = (cb: (val: T) => void, once?: boolean) => once ? event.once(cb) : event.on(cb)
|
111
|
-
const getState: GetState<T> = () => event.getState()
|
112
|
-
return <const>[emit, on, getState]
|
113
|
-
}
|
1
|
+
export { default, default as Events } from "./Events"
|
2
|
+
export { default as event } from "./event"
|
package/tsconfig-commonjs.json
CHANGED
@@ -3,10 +3,11 @@
|
|
3
3
|
"module": "commonjs",
|
4
4
|
"moduleResolution": "node",
|
5
5
|
"strict": true,
|
6
|
+
"noImplicitOverride": true,
|
6
7
|
"skipLibCheck": true,
|
7
8
|
"allowSyntheticDefaultImports": true,
|
8
9
|
"experimentalDecorators": true,
|
9
|
-
"target": "
|
10
|
+
"target": "es2019",
|
10
11
|
"declaration": false,
|
11
12
|
"outDir": "lib-commonjs"
|
12
13
|
},
|
package/tsconfig.json
CHANGED
@@ -3,10 +3,11 @@
|
|
3
3
|
"module": "esnext",
|
4
4
|
"moduleResolution": "node",
|
5
5
|
"strict": true,
|
6
|
+
"noImplicitOverride": true,
|
6
7
|
"skipLibCheck": true,
|
7
8
|
"allowSyntheticDefaultImports": true,
|
8
9
|
"experimentalDecorators": true,
|
9
|
-
"target": "
|
10
|
+
"target": "es2019",
|
10
11
|
"declaration": true,
|
11
12
|
"outDir": "lib"
|
12
13
|
},
|