@lincode/events 1.0.23 → 1.0.25

Sign up to get free protection for your applications and to get access to all the features.
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
@@ -11,7 +11,7 @@ class Event {
11
11
  }
12
12
  once(cb) {
13
13
  const handle = new Cancellable();
14
- handle.watch(this.on(value => {
14
+ handle.watch(this.on((value) => {
15
15
  handle.cancel();
16
16
  cb(value);
17
17
  }));
@@ -13,7 +13,7 @@ class Event {
13
13
  }
14
14
  once(cb) {
15
15
  const handle = new promiselikes_1.Cancellable();
16
- handle.watch(this.on(value => {
16
+ handle.watch(this.on((value) => {
17
17
  handle.cancel();
18
18
  cb(value);
19
19
  }));
package/package.json CHANGED
@@ -1,27 +1,33 @@
1
1
  {
2
- "name": "@lincode/events",
3
- "version": "1.0.23",
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"
2
+ "name": "@lincode/events",
3
+ "version": "1.0.25",
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
- cb(this.state!)
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(this.on(value => {
18
- handle.cancel()
19
- cb(value)
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) => once ? event.once(cb) : event.on(cb)
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
+ }