@lincode/events 1.0.30 → 1.0.32

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/index.js CHANGED
@@ -1,2 +1,6 @@
1
1
  export { default, default as Events } from "./Events";
2
2
  export { default as event, Event } from "./event";
3
+ const w = window;
4
+ "__LINCODE_EVENTS__" in w &&
5
+ console.warn("multiple versions of @lincode/events detected");
6
+ 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,3 +7,7 @@ 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
+ const w = window;
11
+ "__LINCODE_EVENTS__" in w &&
12
+ console.warn("multiple versions of @lincode/events detected");
13
+ w.__LINCODE_EVENTS__ = true;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lincode/events",
3
- "version": "1.0.30",
3
+ "version": "1.0.32",
4
4
  "description": "Generated by ambients-cli",
5
5
  "author": "Lai Schwe",
6
6
  "license": "MIT",
@@ -8,8 +8,8 @@
8
8
  "ambients"
9
9
  ],
10
10
  "dependencies": {
11
- "@lincode/promiselikes": "^1.0.24",
12
- "@lincode/utils": "^1.3.5"
11
+ "@lincode/promiselikes": "^1.0.25",
12
+ "@lincode/utils": "^1.3.7"
13
13
  },
14
14
  "devDependencies": {
15
15
  "typescript": "^4.9.4"
@@ -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
@@ -4,14 +4,14 @@ export class Event<Payload = void> {
4
4
  private cbs = new Set<(val: Payload) => void>()
5
5
  private state?: Payload
6
6
 
7
- public on(cb: (val: Payload) => void): Cancellable {
7
+ public on(cb: (val: Payload) => void) {
8
8
  if ("state" in this) cb(this.state!)
9
9
 
10
10
  this.cbs.add(cb)
11
11
  return new Cancellable(() => this.cbs.delete(cb))
12
12
  }
13
13
 
14
- public once(cb: (val: Payload) => void): Cancellable {
14
+ public once(cb: (val: Payload) => void) {
15
15
  const handle = new Cancellable()
16
16
  handle.watch(
17
17
  this.on((value) => {
@@ -22,17 +22,17 @@ export class Event<Payload = void> {
22
22
  return handle
23
23
  }
24
24
 
25
- public emit(value: Payload): void {
25
+ public emit(value: Payload) {
26
26
  for (const cb of this.cbs) cb(value)
27
27
  }
28
28
 
29
- public setState(value: Payload): void {
29
+ public setState(value: Payload) {
30
30
  if ("state" in this && this.state === value) return
31
31
  this.state = value
32
32
  this.emit(value)
33
33
  }
34
34
 
35
- public getState(): Payload | undefined {
35
+ public getState() {
36
36
  return this.state
37
37
  }
38
38
  }
package/src/index.ts CHANGED
@@ -1,2 +1,7 @@
1
1
  export { default, default as Events } from "./Events"
2
2
  export { default as event, EventFunctions, Event } from "./event"
3
+
4
+ const w = window as any
5
+ "__LINCODE_EVENTS__" in w &&
6
+ console.warn("multiple versions of @lincode/events detected")
7
+ w.__LINCODE_EVENTS__ = true