larasopp 1.2.13 → 1.2.14

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/Listener.d.ts CHANGED
@@ -5,13 +5,17 @@ declare class Listener implements EventListener {
5
5
  private channel;
6
6
  private listeners?;
7
7
  private listener?;
8
+ private cacheEvents;
8
9
  constructor(channel: string, constext: Larasopp);
9
- listen(event: string, callback: (data: any) => void): this;
10
- here(callback: (data: any) => void): this;
11
- joining(callback: (data: any) => void): this;
12
- leaving(callback: (data: any) => void): this;
10
+ listen(event: string, callback: (data: any) => void, withCache?: boolean): this;
11
+ here(callback: (data: any) => void, withCache?: boolean): this;
12
+ joining(callback: (data: any) => void, withCache?: boolean): this;
13
+ leaving(callback: (data: any) => void, withCache?: boolean): this;
13
14
  unsubscribe(): void;
14
15
  remove(): void;
16
+ private hasCache;
17
+ private getCache;
18
+ private pushCache;
15
19
  emit(data: any): void;
16
20
  }
17
21
  export default Listener;
package/lib/Listener.js CHANGED
@@ -26,25 +26,39 @@ class Listener {
26
26
  writable: true,
27
27
  value: void 0
28
28
  });
29
+ Object.defineProperty(this, "cacheEvents", {
30
+ enumerable: true,
31
+ configurable: true,
32
+ writable: true,
33
+ value: void 0
34
+ });
29
35
  this.channel = channel;
30
36
  this.context = constext;
37
+ this.cacheEvents = {};
38
+ this.here(() => { }, true);
31
39
  }
32
- listen(event, callback) {
40
+ listen(event, callback, withCache = false) {
33
41
  if (!this.listeners) {
34
42
  this.listeners = [];
35
43
  }
36
- const listener = this.context.events.addListener(this.channel + ':' + event, callback);
44
+ if (withCache && this.hasCache(event))
45
+ callback(this.getCache(event));
46
+ const listener = this.context.events.addListener(this.channel + ':' + event, (data) => {
47
+ const result = callback(data);
48
+ if (withCache)
49
+ this.pushCache(event, result);
50
+ });
37
51
  this.listeners.push(listener);
38
52
  return this;
39
53
  }
40
- here(callback) {
41
- return this.listen('__HERE', callback);
54
+ here(callback, withCache = true) {
55
+ return this.listen('__HERE', callback, withCache);
42
56
  }
43
- joining(callback) {
44
- return this.listen('__JOIN', callback);
57
+ joining(callback, withCache = false) {
58
+ return this.listen('__JOIN', callback, withCache);
45
59
  }
46
- leaving(callback) {
47
- return this.listen('__LEAVE', callback);
60
+ leaving(callback, withCache = false) {
61
+ return this.listen('__LEAVE', callback, withCache);
48
62
  }
49
63
  unsubscribe() {
50
64
  this.context.unsubscribe(this.channel);
@@ -55,6 +69,15 @@ class Listener {
55
69
  return;
56
70
  this.listeners.forEach((listener) => listener.remove());
57
71
  }
72
+ hasCache(event) {
73
+ return event in this.cacheEvents;
74
+ }
75
+ getCache(event) {
76
+ return this.cacheEvents[event];
77
+ }
78
+ pushCache(event, data) {
79
+ this.cacheEvents[event] = data;
80
+ }
58
81
  emit(data) { }
59
82
  }
60
83
  exports.default = Listener;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "larasopp",
3
- "version": "1.2.13",
3
+ "version": "1.2.14",
4
4
  "description": "Websocket client for laravel",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
package/src/Listener.ts CHANGED
@@ -3,36 +3,49 @@ import {
3
3
  } from "easy-event-emitter";
4
4
  import type Larasopp from ".";
5
5
 
6
+ type TListenerCacheEvents = {
7
+ [event: string]: unknown;
8
+ };
9
+
6
10
  class Listener implements EventListener {
7
11
  private readonly context: Larasopp;
8
12
  private channel: string;
9
13
  private listeners?: EventListener[];
10
14
  private listener?: EventListener;
15
+ private cacheEvents: TListenerCacheEvents;
11
16
 
12
17
  constructor(channel: string, constext: Larasopp) {
13
18
  this.channel = channel;
14
19
  this.context = constext;
20
+ this.cacheEvents = {};
21
+
22
+ this.here(()=>{}, true);
15
23
  }
16
24
 
17
- public listen(event: string, callback: (data: any) => void) {
25
+ public listen(event: string, callback: (data: any) => void, withCache = false) {
18
26
  if (!this.listeners) {
19
27
  this.listeners = [];
20
28
  }
21
- const listener = this.context.events.addListener(this.channel + ':' + event, callback);
29
+
30
+ if (withCache && this.hasCache(event)) callback(this.getCache(event));
31
+ const listener = this.context.events.addListener(this.channel + ':' + event, (data) => {
32
+ const result = callback(data);
33
+ if (withCache) this.pushCache(event, result);
34
+ });
22
35
  this.listeners.push(listener);
23
36
  return this;
24
37
  }
25
38
 
26
- public here(callback: (data: any) => void) {
27
- return this.listen('__HERE', callback);
39
+ public here(callback: (data: any) => void, withCache = true) {
40
+ return this.listen('__HERE', callback, withCache);
28
41
  }
29
42
 
30
- public joining(callback: (data: any) => void) {
31
- return this.listen('__JOIN', callback);
43
+ public joining(callback: (data: any) => void, withCache = false) {
44
+ return this.listen('__JOIN', callback, withCache);
32
45
  }
33
46
 
34
- public leaving(callback: (data: any) => void) {
35
- return this.listen('__LEAVE', callback);
47
+ public leaving(callback: (data: any) => void, withCache = false) {
48
+ return this.listen('__LEAVE', callback, withCache);
36
49
  }
37
50
 
38
51
  public unsubscribe() {
@@ -45,6 +58,18 @@ class Listener implements EventListener {
45
58
  this.listeners.forEach((listener) => listener.remove());
46
59
  }
47
60
 
61
+ private hasCache(event: string) {
62
+ return event in this.cacheEvents;
63
+ }
64
+
65
+ private getCache(event: string) {
66
+ return this.cacheEvents[event];
67
+ }
68
+
69
+ private pushCache(event: string, data: unknown) {
70
+ this.cacheEvents[event] = data;
71
+ }
72
+
48
73
  public emit(data: any): void {}
49
74
  }
50
75