larasopp 1.0.5 → 1.0.7

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.7",
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
@@ -12,6 +12,10 @@ export type TListenerCallback = (data:{channel: string}) => void;
12
12
 
13
13
  export type TPermissions = 'public' | 'protected' | 'private';
14
14
 
15
+ export type TBind = {
16
+ remove: () => void;
17
+ }
18
+
15
19
  export type TMessage<T> = {
16
20
  subscribe?: string;
17
21
  unsubscribe?: string;
@@ -50,7 +54,6 @@ abstract class Core {
50
54
  this.onClose = this.onClose.bind(this);
51
55
  this.onError = this.onError.bind(this);
52
56
  this.onMessage = this.onMessage.bind(this);
53
-
54
57
  }
55
58
 
56
59
  public setConfig(config: IConfig): void {
@@ -136,7 +139,7 @@ abstract class Core {
136
139
  }
137
140
 
138
141
  private emitListener(method: TListenerEvents, channel: string): void {
139
- if (ListenerEvents.includes(method)) {
142
+ if (ListenerEvents.includes(method)) {
140
143
  this.events.emit(method + ':' + channel, {
141
144
  channel
142
145
  });
package/src/Subscribe.ts CHANGED
@@ -2,19 +2,20 @@ import {
2
2
  TMessage,
3
3
  ListenerEvents,
4
4
  TListenerEvents,
5
- TListenerCallback
5
+ TListenerCallback,
6
+ TBind
6
7
  } from "./Core";
7
8
  import {
8
9
  Event,
9
10
  Events
10
11
  } from "easy-event-emitter";
11
12
 
12
- type TReturn = {
13
- remove: () => void;
14
- }
15
13
 
16
14
  interface ISubscribe {
17
15
  events: Events;
16
+ hasChannel: (channel: string) => boolean;
17
+ pushChannel: (channel: string) => void;
18
+ removeChannel: (channel: string) => void;
18
19
  send: <T>(message: TMessage<T>) => void;
19
20
  channel: string;
20
21
  status: boolean;
@@ -22,12 +23,18 @@ interface ISubscribe {
22
23
 
23
24
  class Subscribe {
24
25
  private events: Events;
26
+ private hasChannel: (channel: string) => boolean;
27
+ private pushChannel: (channel: string) => void;
28
+ private removeChannel: (channel: string) => void;
25
29
  private status: boolean;
26
30
  private _channel: string;
27
31
  private send: <T>(message: TMessage<T>) => void;
28
32
 
29
- constructor({events, status, channel, send}: ISubscribe) {
33
+ constructor({events, hasChannel, pushChannel, removeChannel, status, channel, send}: ISubscribe) {
30
34
  this.events = events;
35
+ this.hasChannel = hasChannel;
36
+ this.pushChannel = pushChannel;
37
+ this.removeChannel = removeChannel;
31
38
  this.status = status;
32
39
  this._channel = channel;
33
40
  this.send = send;
@@ -40,6 +47,8 @@ class Subscribe {
40
47
  }
41
48
 
42
49
  private init(): void {
50
+ this.pushChannel(this.channel);
51
+
43
52
  if (this.status) {
44
53
  this.send({
45
54
  subscribe: this.channel
@@ -54,19 +63,19 @@ class Subscribe {
54
63
  }
55
64
  }
56
65
 
57
- public bind<T>(event: string, callback: (data: T) => void): TReturn {
66
+ public bind<T>(event: string, callback: (data: T) => void): TBind {
58
67
  const Event = this.events.addListener(this.channel + ':' + event, callback);
59
68
  return {
60
69
  remove: () => {
61
- this.send({
62
- unsubscribe: this.channel
63
- });
70
+ this.remove();
64
71
  Event.remove();
65
72
  }
66
73
  }
67
74
  }
68
75
 
69
76
  public remove(): void {
77
+ this.removeChannel(this.channel);
78
+ if (this.hasChannel(this.channel)) return;
70
79
  this.send({
71
80
  unsubscribe: this.channel
72
81
  });
package/src/index.ts CHANGED
@@ -6,35 +6,68 @@ import Core,{
6
6
  TPermissions,
7
7
  SocketEvents,
8
8
  TSocketEvents,
9
- TListenerCallback
9
+ TListenerCallback,
10
+ TBind
10
11
  } from "./Core";
11
12
  import Subscribe from "./Subscribe";
12
13
 
13
14
  class Larasopp extends Core {
15
+ private _channels: string[];
14
16
 
15
17
  constructor(config: IConfig) {
16
18
  super(config);
17
19
 
20
+ this._channels = [];
21
+
18
22
  this.subscribe = this.subscribe.bind(this);
19
23
  this.trigger = this.trigger.bind(this);
24
+ this.hasChannel = this.hasChannel.bind(this);
25
+ this.pushChannel = this.pushChannel.bind(this);
26
+ this.removeChannel = this.removeChannel.bind(this);
20
27
  }
21
28
 
22
29
  public subscribe(channel: string): Subscribe {
23
30
  return new Subscribe({
24
31
  events: this.events,
32
+ hasChannel: this.hasChannel,
33
+ pushChannel: this.pushChannel,
34
+ removeChannel: this.removeChannel,
25
35
  status: this.status,
26
36
  send: this.send,
27
37
  channel
28
38
  });
29
39
  }
30
40
 
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
- });
41
+ public trigger<T>(channel: string, event: string, message: T, permission: TPermissions = 'public', waitSubscribe: boolean = false): void {
42
+ const send = () => {
43
+ this.send<T>({
44
+ channel: channel,
45
+ event: event,
46
+ message: message,
47
+ type: permission
48
+ });
49
+ };
50
+
51
+ if (waitSubscribe) this.events.addListener(event + ':' + channel, send);
52
+ send();
53
+ }
54
+
55
+ private pushChannel(channel: string): void {
56
+ if (this._channels.indexOf(channel) >= 0) return;
57
+ this._channels.push(channel);
58
+ }
59
+
60
+ private removeChannel(channel: string): void {
61
+ const index = this._channels.indexOf(channel);
62
+ if (index >= 0) this._channels.splice(index, 1);
63
+ }
64
+
65
+ private get channels(): string[] {
66
+ return this._channels;
67
+ }
68
+
69
+ public hasChannel(channel: string): boolean {
70
+ return this.channels.indexOf(channel) >= 0;
38
71
  }
39
72
 
40
73
  public addListener(event: TSocketEvents, callback: TListenerCallback): Event | undefined {
@@ -44,7 +77,8 @@ class Larasopp extends Core {
44
77
  }
45
78
 
46
79
  export type {
47
- Subscribe
80
+ Subscribe,
81
+ TBind
48
82
  };
49
83
 
50
84
  export default Larasopp;