larasopp 1.2.10 → 1.2.11

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
@@ -1,11 +1,12 @@
1
- import { type Event } from "easy-event-emitter";
1
+ import { type Event as EventListener } from "easy-event-emitter";
2
2
  import type Larasopp from ".";
3
3
  declare class Listener {
4
4
  private readonly context;
5
5
  private channel;
6
6
  private listeners?;
7
7
  constructor(channel: string, constext: Larasopp);
8
- listen(event: string, callback: (data: any) => void): Event;
8
+ listen(event: string, callback: (data: any) => void): EventListener;
9
+ unsubscribe(): void;
9
10
  remove(): void;
10
11
  }
11
12
  export default Listener;
package/lib/Listener.js CHANGED
@@ -31,6 +31,10 @@ class Listener {
31
31
  this.listeners.push(listener);
32
32
  return listener;
33
33
  }
34
+ unsubscribe() {
35
+ this.context.unsubscribe(this.channel);
36
+ this.remove();
37
+ }
34
38
  remove() {
35
39
  if (!this.listeners)
36
40
  return;
@@ -1,29 +1,29 @@
1
- import { TMessage, TListenerEvents, TListenerCallback, TListen } from "./Core";
2
- import type { Event, Events } from "easy-event-emitter";
3
- interface ISubscribe {
4
- events: Events;
5
- hasChannel: (channel: string) => boolean;
6
- pushChannel: (channel: string) => void;
7
- removeChannel: (channel: string) => void;
8
- send: <T>(message: TMessage<T>) => void;
9
- channel: string;
10
- status: boolean;
11
- }
12
- declare class Subscribe {
13
- private events;
14
- private currentEvents;
15
- private hasChannel;
16
- private pushChannel;
17
- private removeChannel;
18
- private status;
19
- private _channel;
20
- private send;
21
- constructor({ events, hasChannel, pushChannel, removeChannel, status, channel, send }: ISubscribe);
22
- get channel(): string;
23
- private init;
24
- listen<T>(event: string, callback: (data: T) => void): TListen;
25
- private clearEvents;
26
- remove(): void;
27
- addListener(event: TListenerEvents, callback: TListenerCallback): Event | undefined;
28
- }
29
- export default Subscribe;
1
+ import { TMessage, TListenerEvents, TListenerCallback, TListen } from "./Core";
2
+ import type { Event, Events } from "easy-event-emitter";
3
+ interface ISubscribe {
4
+ events: Events;
5
+ hasChannel: (channel: string) => boolean;
6
+ pushChannel: (channel: string) => void;
7
+ removeChannel: (channel: string) => void;
8
+ send: <T>(message: TMessage<T>) => void;
9
+ channel: string;
10
+ status: boolean;
11
+ }
12
+ declare class Subscribe {
13
+ private events;
14
+ private currentEvents;
15
+ private hasChannel;
16
+ private pushChannel;
17
+ private removeChannel;
18
+ private status;
19
+ private _channel;
20
+ private send;
21
+ constructor({ events, hasChannel, pushChannel, removeChannel, status, channel, send }: ISubscribe);
22
+ get channel(): string;
23
+ private init;
24
+ listen<T>(event: string, callback: (data: T) => void): TListen;
25
+ private clearEvents;
26
+ remove(): void;
27
+ addListener(event: TListenerEvents, callback: TListenerCallback): Event | undefined;
28
+ }
29
+ export default Subscribe;
package/lib/Subscribe.js CHANGED
@@ -1,107 +1,107 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- class Subscribe {
4
- constructor({ events, hasChannel, pushChannel, removeChannel, status, channel, send }) {
5
- Object.defineProperty(this, "events", {
6
- enumerable: true,
7
- configurable: true,
8
- writable: true,
9
- value: void 0
10
- });
11
- Object.defineProperty(this, "currentEvents", {
12
- enumerable: true,
13
- configurable: true,
14
- writable: true,
15
- value: void 0
16
- });
17
- Object.defineProperty(this, "hasChannel", {
18
- enumerable: true,
19
- configurable: true,
20
- writable: true,
21
- value: void 0
22
- });
23
- Object.defineProperty(this, "pushChannel", {
24
- enumerable: true,
25
- configurable: true,
26
- writable: true,
27
- value: void 0
28
- });
29
- Object.defineProperty(this, "removeChannel", {
30
- enumerable: true,
31
- configurable: true,
32
- writable: true,
33
- value: void 0
34
- });
35
- Object.defineProperty(this, "status", {
36
- enumerable: true,
37
- configurable: true,
38
- writable: true,
39
- value: void 0
40
- });
41
- Object.defineProperty(this, "_channel", {
42
- enumerable: true,
43
- configurable: true,
44
- writable: true,
45
- value: void 0
46
- });
47
- Object.defineProperty(this, "send", {
48
- enumerable: true,
49
- configurable: true,
50
- writable: true,
51
- value: void 0
52
- });
53
- this.events = events;
54
- this.currentEvents = [];
55
- this.hasChannel = hasChannel;
56
- this.pushChannel = pushChannel;
57
- this.removeChannel = removeChannel;
58
- this.status = status;
59
- this._channel = channel;
60
- this.send = send;
61
- this.init();
62
- }
63
- get channel() {
64
- return this._channel;
65
- }
66
- init() {
67
- this.pushChannel(this.channel);
68
- if (this.status) {
69
- this.send({
70
- subscribe: this.channel
71
- });
72
- }
73
- else {
74
- const event = this.events.addListener('open', () => {
75
- this.send({
76
- subscribe: this.channel
77
- });
78
- event.remove();
79
- });
80
- }
81
- }
82
- listen(event, callback) {
83
- const Event = this.events.addListener(this.channel + ':' + event, callback);
84
- this.currentEvents.push(Event);
85
- return {
86
- remove: () => {
87
- Event.remove();
88
- }
89
- };
90
- }
91
- clearEvents() {
92
- this.currentEvents.forEach((event) => event.remove());
93
- }
94
- remove() {
95
- this.clearEvents();
96
- this.removeChannel(this.channel);
97
- if (this.hasChannel(this.channel))
98
- return;
99
- this.send({
100
- unsubscribe: this.channel
101
- });
102
- }
103
- addListener(event, callback) {
104
- return this.events.addListener(event + ':' + this.channel, callback);
105
- }
106
- }
107
- exports.default = Subscribe;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ class Subscribe {
4
+ constructor({ events, hasChannel, pushChannel, removeChannel, status, channel, send }) {
5
+ Object.defineProperty(this, "events", {
6
+ enumerable: true,
7
+ configurable: true,
8
+ writable: true,
9
+ value: void 0
10
+ });
11
+ Object.defineProperty(this, "currentEvents", {
12
+ enumerable: true,
13
+ configurable: true,
14
+ writable: true,
15
+ value: void 0
16
+ });
17
+ Object.defineProperty(this, "hasChannel", {
18
+ enumerable: true,
19
+ configurable: true,
20
+ writable: true,
21
+ value: void 0
22
+ });
23
+ Object.defineProperty(this, "pushChannel", {
24
+ enumerable: true,
25
+ configurable: true,
26
+ writable: true,
27
+ value: void 0
28
+ });
29
+ Object.defineProperty(this, "removeChannel", {
30
+ enumerable: true,
31
+ configurable: true,
32
+ writable: true,
33
+ value: void 0
34
+ });
35
+ Object.defineProperty(this, "status", {
36
+ enumerable: true,
37
+ configurable: true,
38
+ writable: true,
39
+ value: void 0
40
+ });
41
+ Object.defineProperty(this, "_channel", {
42
+ enumerable: true,
43
+ configurable: true,
44
+ writable: true,
45
+ value: void 0
46
+ });
47
+ Object.defineProperty(this, "send", {
48
+ enumerable: true,
49
+ configurable: true,
50
+ writable: true,
51
+ value: void 0
52
+ });
53
+ this.events = events;
54
+ this.currentEvents = [];
55
+ this.hasChannel = hasChannel;
56
+ this.pushChannel = pushChannel;
57
+ this.removeChannel = removeChannel;
58
+ this.status = status;
59
+ this._channel = channel;
60
+ this.send = send;
61
+ this.init();
62
+ }
63
+ get channel() {
64
+ return this._channel;
65
+ }
66
+ init() {
67
+ this.pushChannel(this.channel);
68
+ if (this.status) {
69
+ this.send({
70
+ subscribe: this.channel
71
+ });
72
+ }
73
+ else {
74
+ const event = this.events.addListener('open', () => {
75
+ this.send({
76
+ subscribe: this.channel
77
+ });
78
+ event.remove();
79
+ });
80
+ }
81
+ }
82
+ listen(event, callback) {
83
+ const Event = this.events.addListener(this.channel + ':' + event, callback);
84
+ this.currentEvents.push(Event);
85
+ return {
86
+ remove: () => {
87
+ Event.remove();
88
+ }
89
+ };
90
+ }
91
+ clearEvents() {
92
+ this.currentEvents.forEach((event) => event.remove());
93
+ }
94
+ remove() {
95
+ this.clearEvents();
96
+ this.removeChannel(this.channel);
97
+ if (this.hasChannel(this.channel))
98
+ return;
99
+ this.send({
100
+ unsubscribe: this.channel
101
+ });
102
+ }
103
+ addListener(event, callback) {
104
+ return this.events.addListener(event + ':' + this.channel, callback);
105
+ }
106
+ }
107
+ exports.default = Subscribe;
package/lib/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
- import { Event } from "easy-event-emitter";
1
+ import { type Event as EventListener } from "easy-event-emitter";
2
2
  import Core from "./Core";
3
3
  import Listener from "./Listener";
4
- import type { IConfig, TPermissions, TSocketEvents, TListenerCallback, TListen } from "./types";
4
+ import type { IConfig, TPermissions, TSocketEvents, TListenerCallback } from "./types";
5
5
  declare class Larasopp extends Core {
6
6
  private readonly channels;
7
7
  constructor(config: IConfig);
@@ -10,7 +10,8 @@ declare class Larasopp extends Core {
10
10
  unsubscribe(channel: string): void;
11
11
  trigger<T>(channel: string, event: string, message: T, permission?: TPermissions, waitSubscribe?: boolean): void;
12
12
  private pushListener;
13
- addListener(event: TSocketEvents, callback: TListenerCallback): Event | undefined;
13
+ countListeners(channel: string): number;
14
+ addListener(event: TSocketEvents, callback: TListenerCallback): EventListener | undefined;
14
15
  }
15
- export type { TListen };
16
+ export type { Listener, EventListener };
16
17
  export default Larasopp;
package/lib/index.js CHANGED
@@ -55,12 +55,17 @@ class Larasopp extends Core_1.default {
55
55
  pushListener(channel, listener) {
56
56
  if (!this.channels[channel]) {
57
57
  this.channels[channel] = [];
58
+ this.send({
59
+ subscribe: channel
60
+ });
58
61
  }
59
- this.send({
60
- subscribe: channel
61
- });
62
62
  this.channels[channel].push(listener);
63
63
  }
64
+ countListeners(channel) {
65
+ if (!this.channels[channel])
66
+ return -1;
67
+ return this.channels[channel].length;
68
+ }
64
69
  addListener(event, callback) {
65
70
  if (!constants_1.SocketEvents.includes(event))
66
71
  return;
package/lib/types.d.ts CHANGED
@@ -6,9 +6,6 @@ export type TListenerCallback = (data: {
6
6
  channel: string;
7
7
  }) => void;
8
8
  export type TPermissions = 'public' | 'protected' | 'private';
9
- export type TListen = {
10
- remove: () => void;
11
- };
12
9
  export type TMessage<T> = {
13
10
  subscribe?: string;
14
11
  unsubscribe?: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "larasopp",
3
- "version": "1.2.10",
3
+ "version": "1.2.11",
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
@@ -1,12 +1,12 @@
1
1
  import {
2
- type Event
2
+ type Event as EventListener
3
3
  } from "easy-event-emitter";
4
4
  import type Larasopp from ".";
5
5
 
6
6
  class Listener {
7
7
  private readonly context: Larasopp;
8
8
  private channel: string;
9
- private listeners?: Event[];
9
+ private listeners?: EventListener[];
10
10
 
11
11
  constructor(channel: string, constext: Larasopp) {
12
12
  this.channel = channel;
@@ -22,6 +22,11 @@ class Listener {
22
22
  return listener;
23
23
  }
24
24
 
25
+ public unsubscribe() {
26
+ this.context.unsubscribe(this.channel);
27
+ this.remove();
28
+ }
29
+
25
30
  public remove() {
26
31
  if (!this.listeners) return;
27
32
  this.listeners.forEach((listener) => listener.remove());
package/src/index.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import {
2
- Event
2
+ type Event as EventListener
3
3
  } from "easy-event-emitter";
4
4
  import Core from "./Core";
5
5
  import { SocketEvents } from "./constants";
@@ -9,7 +9,6 @@ import type {
9
9
  TPermissions,
10
10
  TSocketEvents,
11
11
  TListenerCallback,
12
- TListen,
13
12
  TChannels
14
13
  } from "./types";
15
14
 
@@ -64,21 +63,27 @@ class Larasopp extends Core {
64
63
  private pushListener(channel: string, listener: Listener): void {
65
64
  if (!this.channels[channel]) {
66
65
  this.channels[channel] = [];
66
+ this.send({
67
+ subscribe: channel
68
+ });
67
69
  }
68
- this.send({
69
- subscribe: channel
70
- });
71
70
  this.channels[channel].push(listener);
72
71
  }
73
72
 
74
- public addListener(event: TSocketEvents, callback: TListenerCallback): Event | undefined {
73
+ public countListeners(channel: string) {
74
+ if (!this.channels[channel]) return -1;
75
+ return this.channels[channel].length;
76
+ }
77
+
78
+ public addListener(event: TSocketEvents, callback: TListenerCallback): EventListener | undefined {
75
79
  if (!SocketEvents.includes(event)) return;
76
80
  return this.events.addListener(event, callback);
77
81
  }
78
82
  }
79
83
 
80
84
  export type {
81
- TListen
85
+ Listener,
86
+ EventListener
82
87
  };
83
88
 
84
89
  export default Larasopp;
package/src/types.ts CHANGED
@@ -12,10 +12,6 @@ export type TListenerCallback = (data:{channel: string}) => void;
12
12
 
13
13
  export type TPermissions = 'public' | 'protected' | 'private';
14
14
 
15
- export type TListen = {
16
- remove: () => void;
17
- };
18
-
19
15
  export type TMessage<T> = {
20
16
  subscribe?: string;
21
17
  unsubscribe?: string;