larasopp 1.0.5 → 1.0.6

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.
@@ -5,16 +5,22 @@ type TReturn = {
5
5
  };
6
6
  interface ISubscribe {
7
7
  events: Events;
8
+ hasChannel: (channel: string) => boolean;
9
+ pushChannel: (channel: string) => void;
10
+ removeChannel: (channel: string) => void;
8
11
  send: <T>(message: TMessage<T>) => void;
9
12
  channel: string;
10
13
  status: boolean;
11
14
  }
12
15
  declare class Subscribe {
13
16
  private events;
17
+ private hasChannel;
18
+ private pushChannel;
19
+ private removeChannel;
14
20
  private status;
15
21
  private _channel;
16
22
  private send;
17
- constructor({ events, status, channel, send }: ISubscribe);
23
+ constructor({ events, hasChannel, pushChannel, removeChannel, status, channel, send }: ISubscribe);
18
24
  get channel(): string;
19
25
  private init;
20
26
  bind<T>(event: string, callback: (data: T) => void): TReturn;
package/lib/Subscribe.js CHANGED
@@ -1,13 +1,31 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  class Subscribe {
4
- constructor({ events, status, channel, send }) {
4
+ constructor({ events, hasChannel, pushChannel, removeChannel, status, channel, send }) {
5
5
  Object.defineProperty(this, "events", {
6
6
  enumerable: true,
7
7
  configurable: true,
8
8
  writable: true,
9
9
  value: void 0
10
10
  });
11
+ Object.defineProperty(this, "hasChannel", {
12
+ enumerable: true,
13
+ configurable: true,
14
+ writable: true,
15
+ value: void 0
16
+ });
17
+ Object.defineProperty(this, "pushChannel", {
18
+ enumerable: true,
19
+ configurable: true,
20
+ writable: true,
21
+ value: void 0
22
+ });
23
+ Object.defineProperty(this, "removeChannel", {
24
+ enumerable: true,
25
+ configurable: true,
26
+ writable: true,
27
+ value: void 0
28
+ });
11
29
  Object.defineProperty(this, "status", {
12
30
  enumerable: true,
13
31
  configurable: true,
@@ -27,6 +45,9 @@ class Subscribe {
27
45
  value: void 0
28
46
  });
29
47
  this.events = events;
48
+ this.hasChannel = hasChannel;
49
+ this.pushChannel = pushChannel;
50
+ this.removeChannel = removeChannel;
30
51
  this.status = status;
31
52
  this._channel = channel;
32
53
  this.send = send;
@@ -36,6 +57,7 @@ class Subscribe {
36
57
  return this._channel;
37
58
  }
38
59
  init() {
60
+ this.pushChannel(this.channel);
39
61
  if (this.status) {
40
62
  this.send({
41
63
  subscribe: this.channel
@@ -54,14 +76,15 @@ class Subscribe {
54
76
  const Event = this.events.addListener(this.channel + ':' + event, callback);
55
77
  return {
56
78
  remove: () => {
57
- this.send({
58
- unsubscribe: this.channel
59
- });
79
+ this.remove();
60
80
  Event.remove();
61
81
  }
62
82
  };
63
83
  }
64
84
  remove() {
85
+ this.removeChannel(this.channel);
86
+ if (this.hasChannel(this.channel))
87
+ return;
65
88
  this.send({
66
89
  unsubscribe: this.channel
67
90
  });
package/lib/index.d.ts CHANGED
@@ -2,9 +2,14 @@ import { Event } from "easy-event-emitter";
2
2
  import Core, { IConfig, TPermissions, TSocketEvents, TListenerCallback } from "./Core";
3
3
  import Subscribe from "./Subscribe";
4
4
  declare class Larasopp extends Core {
5
+ private _channels;
5
6
  constructor(config: IConfig);
6
7
  subscribe(channel: string): Subscribe;
7
- trigger<T>(channel: string, event: string, message: T, permission?: TPermissions): void;
8
+ trigger<T>(channel: string, event: string, message: T, permission?: TPermissions, waitSubscribe?: boolean): void;
9
+ private pushChannel;
10
+ private removeChannel;
11
+ private get channels();
12
+ hasChannel(channel: string): boolean;
8
13
  addListener(event: TSocketEvents, callback: TListenerCallback): Event | undefined;
9
14
  }
10
15
  export type { Subscribe };
package/lib/index.js CHANGED
@@ -31,24 +31,58 @@ const Subscribe_1 = __importDefault(require("./Subscribe"));
31
31
  class Larasopp extends Core_1.default {
32
32
  constructor(config) {
33
33
  super(config);
34
+ Object.defineProperty(this, "_channels", {
35
+ enumerable: true,
36
+ configurable: true,
37
+ writable: true,
38
+ value: void 0
39
+ });
40
+ this._channels = [];
34
41
  this.subscribe = this.subscribe.bind(this);
35
42
  this.trigger = this.trigger.bind(this);
43
+ this.hasChannel = this.hasChannel.bind(this);
44
+ this.pushChannel = this.pushChannel.bind(this);
45
+ this.removeChannel = this.removeChannel.bind(this);
36
46
  }
37
47
  subscribe(channel) {
38
48
  return new Subscribe_1.default({
39
49
  events: this.events,
50
+ hasChannel: this.hasChannel,
51
+ pushChannel: this.pushChannel,
52
+ removeChannel: this.removeChannel,
40
53
  status: this.status,
41
54
  send: this.send,
42
55
  channel
43
56
  });
44
57
  }
45
- trigger(channel, event, message, permission = 'public') {
46
- this.send({
47
- channel: channel,
48
- event: event,
49
- message: message,
50
- type: permission
51
- });
58
+ trigger(channel, event, message, permission = 'public', waitSubscribe = false) {
59
+ const send = () => {
60
+ this.send({
61
+ channel: channel,
62
+ event: event,
63
+ message: message,
64
+ type: permission
65
+ });
66
+ };
67
+ if (waitSubscribe)
68
+ this.events.addListener(event + ':' + channel, send);
69
+ send();
70
+ }
71
+ pushChannel(channel) {
72
+ if (this._channels.indexOf(channel) >= 0)
73
+ return;
74
+ this._channels.push(channel);
75
+ }
76
+ removeChannel(channel) {
77
+ const index = this._channels.indexOf(channel);
78
+ if (index >= 0)
79
+ this._channels.splice(index, 1);
80
+ }
81
+ get channels() {
82
+ return this._channels;
83
+ }
84
+ hasChannel(channel) {
85
+ return this.channels.indexOf(channel) >= 0;
52
86
  }
53
87
  addListener(event, callback) {
54
88
  if (!Core_1.SocketEvents.includes(event))
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "larasopp",
3
- "version": "1.0.5",
3
+ "version": "1.0.6",
4
4
  "description": "Websocket client for laravel",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -37,6 +37,6 @@
37
37
  "typescript": "^5.1.6"
38
38
  },
39
39
  "dependencies": {
40
- "easy-event-emitter": "^1.0.6"
40
+ "easy-event-emitter": "^1.0.7"
41
41
  }
42
42
  }
package/src/Core.ts CHANGED
@@ -50,7 +50,6 @@ abstract class Core {
50
50
  this.onClose = this.onClose.bind(this);
51
51
  this.onError = this.onError.bind(this);
52
52
  this.onMessage = this.onMessage.bind(this);
53
-
54
53
  }
55
54
 
56
55
  public setConfig(config: IConfig): void {
@@ -136,7 +135,7 @@ abstract class Core {
136
135
  }
137
136
 
138
137
  private emitListener(method: TListenerEvents, channel: string): void {
139
- if (ListenerEvents.includes(method)) {
138
+ if (ListenerEvents.includes(method)) {
140
139
  this.events.emit(method + ':' + channel, {
141
140
  channel
142
141
  });
package/src/Subscribe.ts CHANGED
@@ -15,6 +15,9 @@ type TReturn = {
15
15
 
16
16
  interface ISubscribe {
17
17
  events: Events;
18
+ hasChannel: (channel: string) => boolean;
19
+ pushChannel: (channel: string) => void;
20
+ removeChannel: (channel: string) => void;
18
21
  send: <T>(message: TMessage<T>) => void;
19
22
  channel: string;
20
23
  status: boolean;
@@ -22,12 +25,18 @@ interface ISubscribe {
22
25
 
23
26
  class Subscribe {
24
27
  private events: Events;
28
+ private hasChannel: (channel: string) => boolean;
29
+ private pushChannel: (channel: string) => void;
30
+ private removeChannel: (channel: string) => void;
25
31
  private status: boolean;
26
32
  private _channel: string;
27
33
  private send: <T>(message: TMessage<T>) => void;
28
34
 
29
- constructor({events, status, channel, send}: ISubscribe) {
35
+ constructor({events, hasChannel, pushChannel, removeChannel, status, channel, send}: ISubscribe) {
30
36
  this.events = events;
37
+ this.hasChannel = hasChannel;
38
+ this.pushChannel = pushChannel;
39
+ this.removeChannel = removeChannel;
31
40
  this.status = status;
32
41
  this._channel = channel;
33
42
  this.send = send;
@@ -40,6 +49,8 @@ class Subscribe {
40
49
  }
41
50
 
42
51
  private init(): void {
52
+ this.pushChannel(this.channel);
53
+
43
54
  if (this.status) {
44
55
  this.send({
45
56
  subscribe: this.channel
@@ -58,15 +69,15 @@ class Subscribe {
58
69
  const Event = this.events.addListener(this.channel + ':' + event, callback);
59
70
  return {
60
71
  remove: () => {
61
- this.send({
62
- unsubscribe: this.channel
63
- });
72
+ this.remove();
64
73
  Event.remove();
65
74
  }
66
75
  }
67
76
  }
68
77
 
69
78
  public remove(): void {
79
+ this.removeChannel(this.channel);
80
+ if (this.hasChannel(this.channel)) return;
70
81
  this.send({
71
82
  unsubscribe: this.channel
72
83
  });
package/src/index.ts CHANGED
@@ -11,30 +11,62 @@ import Core,{
11
11
  import Subscribe from "./Subscribe";
12
12
 
13
13
  class Larasopp extends Core {
14
+ private _channels: string[];
14
15
 
15
16
  constructor(config: IConfig) {
16
17
  super(config);
17
18
 
19
+ this._channels = [];
20
+
18
21
  this.subscribe = this.subscribe.bind(this);
19
22
  this.trigger = this.trigger.bind(this);
23
+ this.hasChannel = this.hasChannel.bind(this);
24
+ this.pushChannel = this.pushChannel.bind(this);
25
+ this.removeChannel = this.removeChannel.bind(this);
20
26
  }
21
27
 
22
28
  public subscribe(channel: string): Subscribe {
23
29
  return new Subscribe({
24
30
  events: this.events,
31
+ hasChannel: this.hasChannel,
32
+ pushChannel: this.pushChannel,
33
+ removeChannel: this.removeChannel,
25
34
  status: this.status,
26
35
  send: this.send,
27
36
  channel
28
37
  });
29
38
  }
30
39
 
31
- public trigger<T>(channel: string, event: string, message: T, permission: TPermissions = 'public'): void {
32
- this.send<T>({
33
- channel: channel,
34
- event: event,
35
- message: message,
36
- type: permission
37
- });
40
+ public trigger<T>(channel: string, event: string, message: T, permission: TPermissions = 'public', waitSubscribe: boolean = false): void {
41
+ const send = () => {
42
+ this.send<T>({
43
+ channel: channel,
44
+ event: event,
45
+ message: message,
46
+ type: permission
47
+ });
48
+ };
49
+
50
+ if (waitSubscribe) this.events.addListener(event + ':' + channel, send);
51
+ send();
52
+ }
53
+
54
+ private pushChannel(channel: string): void {
55
+ if (this._channels.indexOf(channel) >= 0) return;
56
+ this._channels.push(channel);
57
+ }
58
+
59
+ private removeChannel(channel: string): void {
60
+ const index = this._channels.indexOf(channel);
61
+ if (index >= 0) this._channels.splice(index, 1);
62
+ }
63
+
64
+ private get channels(): string[] {
65
+ return this._channels;
66
+ }
67
+
68
+ public hasChannel(channel: string): boolean {
69
+ return this.channels.indexOf(channel) >= 0;
38
70
  }
39
71
 
40
72
  public addListener(event: TSocketEvents, callback: TListenerCallback): Event | undefined {