@lincode/events 1.0.22
Sign up to get free protection for your applications and to get access to all the features.
- package/.ambientsrc +14 -0
- package/lib/Events.d.ts +13 -0
- package/lib/Events.js +59 -0
- package/lib/event.d.ts +6 -0
- package/lib/event.js +40 -0
- package/lib/index.d.ts +2 -0
- package/lib/index.js +2 -0
- package/lib-commonjs/Events.js +62 -0
- package/lib-commonjs/event.js +42 -0
- package/lib-commonjs/index.js +8 -0
- package/lib-commonjs/test.js +8 -0
- package/package.json +27 -0
- package/src/Events.ts +63 -0
- package/src/event.ts +50 -0
- package/src/index.ts +2 -0
- package/src/test.ts +7 -0
- package/tsconfig-commonjs.json +17 -0
- package/tsconfig.json +20 -0
package/.ambientsrc
ADDED
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>, GetState<T>];
|
6
|
+
export default _default;
|
package/lib/event.js
ADDED
@@ -0,0 +1,40 @@
|
|
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, isState) => isState ? event.setState(val) : event.emit(val);
|
37
|
+
const on = (cb, once) => once ? event.once(cb) : event.on(cb);
|
38
|
+
const getState = () => event.getState();
|
39
|
+
return [emit, on, getState];
|
40
|
+
};
|
package/lib/index.d.ts
ADDED
package/lib/index.js
ADDED
@@ -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,42 @@
|
|
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, isState) => isState ? event.setState(val) : event.emit(val);
|
39
|
+
const on = (cb, once) => once ? event.once(cb) : event.on(cb);
|
40
|
+
const getState = () => event.getState();
|
41
|
+
return [emit, on, getState];
|
42
|
+
};
|
@@ -0,0 +1,8 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
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
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
{
|
2
|
+
"name": "@lincode/events",
|
3
|
+
"version": "1.0.22",
|
4
|
+
"description": "Generated by ambients-cli",
|
5
|
+
"author": "Lai Schwe",
|
6
|
+
"license": "MIT",
|
7
|
+
"keywords": [
|
8
|
+
"ambients"
|
9
|
+
],
|
10
|
+
"dependencies": {
|
11
|
+
"@lincode/promiselikes": "*",
|
12
|
+
"@lincode/utils": "*"
|
13
|
+
},
|
14
|
+
"peerDependencies": {},
|
15
|
+
"devDependencies": {
|
16
|
+
"typescript": "*"
|
17
|
+
},
|
18
|
+
"scripts": {
|
19
|
+
"build": "ambients clean && yarn tsc && yarn tsc -p tsconfig-commonjs.json",
|
20
|
+
"start": "node lib-commonjs/test.js",
|
21
|
+
"dev": "yarn build && yarn start dev"
|
22
|
+
},
|
23
|
+
"private": false,
|
24
|
+
"main": "lib-commonjs/index.js",
|
25
|
+
"module": "lib/index.js",
|
26
|
+
"types": "lib/index.d.ts"
|
27
|
+
}
|
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,50 @@
|
|
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, isState?: boolean) => isState ? event.setState(val) : event.emit(val)
|
47
|
+
const on: On<T> = (cb: (val: T) => void, once?: boolean) => once ? event.once(cb) : event.on(cb)
|
48
|
+
const getState: GetState<T> = () => event.getState()
|
49
|
+
return <const>[emit, on, getState]
|
50
|
+
}
|
package/src/index.ts
ADDED
package/src/test.ts
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
{
|
2
|
+
"compilerOptions": {
|
3
|
+
"module": "commonjs",
|
4
|
+
"moduleResolution": "node",
|
5
|
+
"strict": true,
|
6
|
+
"noImplicitOverride": true,
|
7
|
+
"skipLibCheck": true,
|
8
|
+
"allowSyntheticDefaultImports": true,
|
9
|
+
"experimentalDecorators": true,
|
10
|
+
"target": "es2019",
|
11
|
+
"declaration": false,
|
12
|
+
"outDir": "lib-commonjs"
|
13
|
+
},
|
14
|
+
"include": [
|
15
|
+
"src"
|
16
|
+
]
|
17
|
+
}
|
package/tsconfig.json
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
{
|
2
|
+
"compilerOptions": {
|
3
|
+
"module": "esnext",
|
4
|
+
"moduleResolution": "node",
|
5
|
+
"strict": true,
|
6
|
+
"noImplicitOverride": true,
|
7
|
+
"skipLibCheck": true,
|
8
|
+
"allowSyntheticDefaultImports": true,
|
9
|
+
"experimentalDecorators": true,
|
10
|
+
"target": "es2019",
|
11
|
+
"declaration": true,
|
12
|
+
"outDir": "lib"
|
13
|
+
},
|
14
|
+
"include": [
|
15
|
+
"src"
|
16
|
+
],
|
17
|
+
"exclude": [
|
18
|
+
"src/test.ts"
|
19
|
+
]
|
20
|
+
}
|