@lincode/events 1.0.29 → 1.0.31

Sign up to get free protection for your applications and to get access to all the features.
package/lib/Events.js CHANGED
@@ -20,7 +20,7 @@ export default class {
20
20
  }
21
21
  once(name, cb) {
22
22
  const handle = new Cancellable();
23
- handle.watch(this.on(name, value => {
23
+ handle.watch(this.on(name, (value) => {
24
24
  handle.cancel();
25
25
  cb(value);
26
26
  }));
package/lib/event.d.ts CHANGED
@@ -1,12 +1,21 @@
1
1
  import { Cancellable } from "@lincode/promiselikes";
2
2
  export declare class Event<Payload = void> {
3
3
  private cbs;
4
+ private state?;
4
5
  on(cb: (val: Payload) => void): Cancellable;
5
6
  once(cb: (val: Payload) => void): Cancellable;
6
7
  emit(value: Payload): void;
8
+ setState(value: Payload): void;
9
+ getState(): Payload | undefined;
7
10
  }
8
11
  type Emit<T> = (val: T, isState?: boolean) => void;
9
12
  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>];
13
+ type GetState<T> = () => T | undefined;
14
+ export type EventFunctions = readonly [
15
+ Emit<void>,
16
+ On<void>,
17
+ Emit<void>,
18
+ GetState<void>
19
+ ];
20
+ declare const _default: <T = void>() => readonly [Emit<T>, On<T>, Emit<T>, GetState<T>];
12
21
  export default _default;
package/lib/event.js CHANGED
@@ -4,6 +4,8 @@ export class Event {
4
4
  this.cbs = new Set();
5
5
  }
6
6
  on(cb) {
7
+ if ("state" in this)
8
+ cb(this.state);
7
9
  this.cbs.add(cb);
8
10
  return new Cancellable(() => this.cbs.delete(cb));
9
11
  }
@@ -19,10 +21,21 @@ export class Event {
19
21
  for (const cb of this.cbs)
20
22
  cb(value);
21
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
+ }
22
33
  }
23
34
  export default () => {
24
35
  const event = new Event();
25
36
  const emit = (val) => event.emit(val);
26
37
  const on = (cb, once) => once ? event.once(cb) : event.on(cb);
27
- return [emit, on];
38
+ const emitState = (val) => event.setState(val);
39
+ const getState = () => event.getState();
40
+ return [emit, on, emitState, getState];
28
41
  };
package/lib/index.js CHANGED
@@ -1,2 +1,7 @@
1
1
  export { default, default as Events } from "./Events";
2
2
  export { default as event, Event } from "./event";
3
+ console.log("@lincode/events");
4
+ const w = window;
5
+ "__LINCODE_EVENTS__" in w &&
6
+ console.warn("multiple versions of @lincode/events detected");
7
+ w.__LINCODE_EVENTS__ = true;
@@ -22,7 +22,7 @@ class default_1 {
22
22
  }
23
23
  once(name, cb) {
24
24
  const handle = new promiselikes_1.Cancellable();
25
- handle.watch(this.on(name, value => {
25
+ handle.watch(this.on(name, (value) => {
26
26
  handle.cancel();
27
27
  cb(value);
28
28
  }));
@@ -7,6 +7,8 @@ class Event {
7
7
  this.cbs = new Set();
8
8
  }
9
9
  on(cb) {
10
+ if ("state" in this)
11
+ cb(this.state);
10
12
  this.cbs.add(cb);
11
13
  return new promiselikes_1.Cancellable(() => this.cbs.delete(cb));
12
14
  }
@@ -22,11 +24,22 @@ class Event {
22
24
  for (const cb of this.cbs)
23
25
  cb(value);
24
26
  }
27
+ setState(value) {
28
+ if ("state" in this && this.state === value)
29
+ return;
30
+ this.state = value;
31
+ this.emit(value);
32
+ }
33
+ getState() {
34
+ return this.state;
35
+ }
25
36
  }
26
37
  exports.Event = Event;
27
38
  exports.default = () => {
28
39
  const event = new Event();
29
40
  const emit = (val) => event.emit(val);
30
41
  const on = (cb, once) => once ? event.once(cb) : event.on(cb);
31
- return [emit, on];
42
+ const emitState = (val) => event.setState(val);
43
+ const getState = () => event.getState();
44
+ return [emit, on, emitState, getState];
32
45
  };
@@ -7,3 +7,8 @@ Object.defineProperty(exports, "Events", { enumerable: true, get: function () {
7
7
  var event_1 = require("./event");
8
8
  Object.defineProperty(exports, "event", { enumerable: true, get: function () { return event_1.default; } });
9
9
  Object.defineProperty(exports, "Event", { enumerable: true, get: function () { return event_1.Event; } });
10
+ console.log("@lincode/events");
11
+ const w = window;
12
+ "__LINCODE_EVENTS__" in w &&
13
+ console.warn("multiple versions of @lincode/events detected");
14
+ w.__LINCODE_EVENTS__ = true;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lincode/events",
3
- "version": "1.0.29",
3
+ "version": "1.0.31",
4
4
  "description": "Generated by ambients-cli",
5
5
  "author": "Lai Schwe",
6
6
  "license": "MIT",
@@ -28,5 +28,6 @@
28
28
  "tabWidth": 4,
29
29
  "semi": false,
30
30
  "singleQuote": false
31
- }
31
+ },
32
+ "sideEffects": false
32
33
  }
package/src/Events.ts CHANGED
@@ -1,63 +1,66 @@
1
1
  import { Cancellable } from "@lincode/promiselikes"
2
2
  import { forceGet } from "@lincode/utils"
3
3
 
4
- export default class <Payload = void, Names extends string = string> {
4
+ export default class<Payload = void, Names extends string = string> {
5
5
  private cbsMap = new Map<string, Set<(val: Payload) => void>>()
6
6
  private states?: Map<string, Payload>
7
7
 
8
- public on(name: Names | Array<Names>, cb: (val: Payload) => void): Cancellable {
8
+ public on(name: Names | Array<Names>, cb: (val: Payload) => void) {
9
9
  if (Array.isArray(name)) {
10
10
  const handle = new Cancellable()
11
- for (const n of name)
12
- handle.watch(this.on(n, cb))
11
+ for (const n of name) handle.watch(this.on(n, cb))
13
12
 
14
13
  return handle
15
14
  }
16
- if (this.states?.has(name))
17
- cb(this.states.get(name)!)
15
+ if (this.states?.has(name)) cb(this.states.get(name)!)
16
+
17
+ const cbs = forceGet(
18
+ this.cbsMap,
19
+ name,
20
+ () => new Set<(val: Payload) => void>()
21
+ )
18
22
 
19
- const cbs = forceGet(this.cbsMap, name, () => new Set<(val: Payload) => void>())
20
-
21
23
  cbs.add(cb)
22
24
  return new Cancellable(() => cbs.delete(cb))
23
25
  }
24
-
25
- public once(name: Names | Array<Names>, cb: (val: Payload) => void): Cancellable {
26
+
27
+ public once(name: Names | Array<Names>, cb: (val: Payload) => void) {
26
28
  const handle = new Cancellable()
27
- handle.watch(this.on(name, value => {
28
- handle.cancel()
29
- cb(value)
30
- }))
29
+ handle.watch(
30
+ this.on(name, (value) => {
31
+ handle.cancel()
32
+ cb(value)
33
+ })
34
+ )
31
35
  return handle
32
36
  }
33
37
 
34
- public emit(name: Names, value: Payload): void {
38
+ public emit(name: Names, value: Payload) {
35
39
  if (this.cbsMap.has(name))
36
- for (const cb of this.cbsMap.get(name)!)
37
- cb(value)
40
+ for (const cb of this.cbsMap.get(name)!) cb(value)
38
41
  }
39
42
 
40
43
  public hasState(name: Names): boolean {
41
44
  return !!this.states?.has(name)
42
45
  }
43
46
 
44
- public setState(name: Names, value: Payload): void {
47
+ public setState(name: Names, value: Payload) {
45
48
  this.states ??= new Map()
46
49
  if (this.states.has(name) && this.states.get(name) === value) return
47
50
  this.states.set(name, value)
48
51
  this.emit(name, value)
49
52
  }
50
53
 
51
- public getState(name: Names): Payload | undefined {
54
+ public getState(name: Names) {
52
55
  return this.states?.get(name)
53
56
  }
54
57
 
55
- public deleteState(name: Names): void {
58
+ public deleteState(name: Names) {
56
59
  this.states?.delete(name)
57
60
  }
58
61
 
59
- public clear(): void {
62
+ public clear() {
60
63
  this.cbsMap.clear()
61
64
  this.states?.clear()
62
65
  }
63
- }
66
+ }
package/src/event.ts CHANGED
@@ -2,13 +2,16 @@ import { Cancellable } from "@lincode/promiselikes"
2
2
 
3
3
  export class Event<Payload = void> {
4
4
  private cbs = new Set<(val: Payload) => void>()
5
+ private state?: Payload
6
+
7
+ public on(cb: (val: Payload) => void) {
8
+ if ("state" in this) cb(this.state!)
5
9
 
6
- public on(cb: (val: Payload) => void): Cancellable {
7
10
  this.cbs.add(cb)
8
11
  return new Cancellable(() => this.cbs.delete(cb))
9
12
  }
10
13
 
11
- public once(cb: (val: Payload) => void): Cancellable {
14
+ public once(cb: (val: Payload) => void) {
12
15
  const handle = new Cancellable()
13
16
  handle.watch(
14
17
  this.on((value) => {
@@ -19,20 +22,38 @@ export class Event<Payload = void> {
19
22
  return handle
20
23
  }
21
24
 
22
- public emit(value: Payload): void {
25
+ public emit(value: Payload) {
23
26
  for (const cb of this.cbs) cb(value)
24
27
  }
28
+
29
+ public setState(value: Payload) {
30
+ if ("state" in this && this.state === value) return
31
+ this.state = value
32
+ this.emit(value)
33
+ }
34
+
35
+ public getState() {
36
+ return this.state
37
+ }
25
38
  }
26
39
 
27
40
  type Emit<T> = (val: T, isState?: boolean) => void
28
41
  type On<T> = (cb: (val: T) => void, once?: boolean) => Cancellable
42
+ type GetState<T> = () => T | undefined
29
43
 
30
- export type EventFunctions = readonly [Emit<void>, On<void>]
44
+ export type EventFunctions = readonly [
45
+ Emit<void>,
46
+ On<void>,
47
+ Emit<void>,
48
+ GetState<void>
49
+ ]
31
50
 
32
51
  export default <T = void>() => {
33
52
  const event = new Event<T>()
34
53
  const emit: Emit<T> = (val: T) => event.emit(val)
35
54
  const on: On<T> = (cb: (val: T) => void, once?: boolean) =>
36
55
  once ? event.once(cb) : event.on(cb)
37
- return <const>[emit, on]
56
+ const emitState: Emit<T> = (val: T) => event.setState(val)
57
+ const getState: GetState<T> = () => event.getState()
58
+ return <const>[emit, on, emitState, getState]
38
59
  }
package/src/index.ts CHANGED
@@ -1,2 +1,8 @@
1
1
  export { default, default as Events } from "./Events"
2
2
  export { default as event, EventFunctions, Event } from "./event"
3
+
4
+ console.log("@lincode/events")
5
+ const w = window as any
6
+ "__LINCODE_EVENTS__" in w &&
7
+ console.warn("multiple versions of @lincode/events detected")
8
+ w.__LINCODE_EVENTS__ = true