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