@lincode/events 1.0.24 → 1.0.26
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 +3 -3
- package/lib/event.js +1 -1
- package/lib/index.d.ts +1 -1
- package/lib-commonjs/event.js +1 -1
- package/package.json +31 -25
- package/src/event.ts +16 -15
- package/src/index.ts +1 -1
package/lib/event.d.ts
CHANGED
@@ -1,6 +1,6 @@
|
|
1
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;
|
2
|
+
export declare type Emit<T> = (val: T, isState?: boolean) => void;
|
3
|
+
export declare type On<T> = (cb: (val: T) => void, once?: boolean) => Cancellable;
|
4
|
+
export declare type GetState<T> = () => T | undefined;
|
5
5
|
declare const _default: <T = void>() => readonly [Emit<T>, On<T>, Emit<T>, GetState<T>];
|
6
6
|
export default _default;
|
package/lib/event.js
CHANGED
package/lib/index.d.ts
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, Emit, On, GetState } from "./event";
|
package/lib-commonjs/event.js
CHANGED
package/package.json
CHANGED
@@ -1,27 +1,33 @@
|
|
1
1
|
{
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
2
|
+
"name": "@lincode/events",
|
3
|
+
"version": "1.0.26",
|
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
|
+
"prettier": {
|
28
|
+
"trailingComma": "none",
|
29
|
+
"tabWidth": 4,
|
30
|
+
"semi": false,
|
31
|
+
"singleQuote": false
|
32
|
+
}
|
27
33
|
}
|
package/src/event.ts
CHANGED
@@ -5,25 +5,25 @@ class Event<Payload = void> {
|
|
5
5
|
private state?: Payload
|
6
6
|
|
7
7
|
public on(cb: (val: Payload) => void): Cancellable {
|
8
|
-
if ("state" in this)
|
9
|
-
|
10
|
-
|
8
|
+
if ("state" in this) cb(this.state!)
|
9
|
+
|
11
10
|
this.cbs.add(cb)
|
12
11
|
return new Cancellable(() => this.cbs.delete(cb))
|
13
12
|
}
|
14
|
-
|
13
|
+
|
15
14
|
public once(cb: (val: Payload) => void): Cancellable {
|
16
15
|
const handle = new Cancellable()
|
17
|
-
handle.watch(
|
18
|
-
|
19
|
-
|
20
|
-
|
16
|
+
handle.watch(
|
17
|
+
this.on((value) => {
|
18
|
+
handle.cancel()
|
19
|
+
cb(value)
|
20
|
+
})
|
21
|
+
)
|
21
22
|
return handle
|
22
23
|
}
|
23
24
|
|
24
25
|
public emit(value: Payload): void {
|
25
|
-
for (const cb of this.cbs)
|
26
|
-
cb(value)
|
26
|
+
for (const cb of this.cbs) cb(value)
|
27
27
|
}
|
28
28
|
|
29
29
|
public setState(value: Payload): void {
|
@@ -37,15 +37,16 @@ class Event<Payload = void> {
|
|
37
37
|
}
|
38
38
|
}
|
39
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
|
40
|
+
export type Emit<T> = (val: T, isState?: boolean) => void
|
41
|
+
export type On<T> = (cb: (val: T) => void, once?: boolean) => Cancellable
|
42
|
+
export type GetState<T> = () => T | undefined
|
43
43
|
|
44
44
|
export default <T = void>() => {
|
45
45
|
const event = new Event<T>()
|
46
46
|
const emit: Emit<T> = (val: T) => event.emit(val)
|
47
|
-
const on: On<T> = (cb: (val: T) => void, once?: boolean) =>
|
47
|
+
const on: On<T> = (cb: (val: T) => void, once?: boolean) =>
|
48
|
+
once ? event.once(cb) : event.on(cb)
|
48
49
|
const emitState: Emit<T> = (val: T) => event.setState(val)
|
49
50
|
const getState: GetState<T> = () => event.getState()
|
50
51
|
return <const>[emit, on, emitState, getState]
|
51
|
-
}
|
52
|
+
}
|
package/src/index.ts
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, Emit, On, GetState } from "./event"
|