larasopp 1.0.4 → 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.
package/lib/Core.d.ts CHANGED
@@ -1,4 +1,6 @@
1
1
  import { Events } from "easy-event-emitter";
2
+ export declare const SocketEvents: readonly ["open", "close", "error"];
3
+ export type TSocketEvents = typeof SocketEvents[number];
2
4
  export declare const ListenerEvents: readonly ["subscribe", "unsubscribe"];
3
5
  export type TListenerEvents = typeof ListenerEvents[number];
4
6
  export type TListenerCallback = (data: {
package/lib/Core.js CHANGED
@@ -3,8 +3,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.ListenerEvents = void 0;
6
+ exports.ListenerEvents = exports.SocketEvents = void 0;
7
7
  const easy_event_emitter_1 = __importDefault(require("easy-event-emitter"));
8
+ exports.SocketEvents = ['open', 'close', 'error'];
8
9
  exports.ListenerEvents = ['subscribe', 'unsubscribe'];
9
10
  class Core {
10
11
  constructor(config) {
@@ -121,9 +122,8 @@ class Core {
121
122
  return this._status;
122
123
  }
123
124
  send(message) {
124
- if (!this.status) {
125
+ if (!this.status)
125
126
  return;
126
- }
127
127
  this.ws.send(JSON.stringify(message));
128
128
  }
129
129
  }
@@ -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
@@ -1,8 +1,16 @@
1
- import Core, { IConfig, TPermissions } from "./Core";
1
+ import { Event } from "easy-event-emitter";
2
+ import Core, { IConfig, TPermissions, TSocketEvents, TListenerCallback } from "./Core";
2
3
  import Subscribe from "./Subscribe";
3
4
  declare class Larasopp extends Core {
5
+ private _channels;
4
6
  constructor(config: IConfig);
5
7
  subscribe(channel: string): Subscribe;
6
- 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;
13
+ addListener(event: TSocketEvents, callback: TListenerCallback): Event | undefined;
7
14
  }
15
+ export type { Subscribe };
8
16
  export default Larasopp;
package/lib/index.js CHANGED
@@ -1,31 +1,93 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
2
25
  var __importDefault = (this && this.__importDefault) || function (mod) {
3
26
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
27
  };
5
28
  Object.defineProperty(exports, "__esModule", { value: true });
6
- const Core_1 = __importDefault(require("./Core"));
29
+ const Core_1 = __importStar(require("./Core"));
7
30
  const Subscribe_1 = __importDefault(require("./Subscribe"));
8
31
  class Larasopp extends Core_1.default {
9
32
  constructor(config) {
10
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 = [];
11
41
  this.subscribe = this.subscribe.bind(this);
12
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);
13
46
  }
14
47
  subscribe(channel) {
15
48
  return new Subscribe_1.default({
16
49
  events: this.events,
50
+ hasChannel: this.hasChannel,
51
+ pushChannel: this.pushChannel,
52
+ removeChannel: this.removeChannel,
17
53
  status: this.status,
18
54
  send: this.send,
19
55
  channel
20
56
  });
21
57
  }
22
- trigger(channel, event, message, permission = 'public') {
23
- this.send({
24
- channel: channel,
25
- event: event,
26
- message: message,
27
- type: permission
28
- });
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;
86
+ }
87
+ addListener(event, callback) {
88
+ if (!Core_1.SocketEvents.includes(event))
89
+ return;
90
+ return this.events.addListener(event, callback);
29
91
  }
30
92
  }
31
93
  exports.default = Larasopp;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "larasopp",
3
- "version": "1.0.4",
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
@@ -2,6 +2,9 @@ import EventEmitter,{
2
2
  Events
3
3
  } from "easy-event-emitter";
4
4
 
5
+ export const SocketEvents = ['open', 'close', 'error'] as const;
6
+ export type TSocketEvents = typeof SocketEvents[number];
7
+
5
8
  export const ListenerEvents = ['subscribe', 'unsubscribe'] as const;
6
9
  export type TListenerEvents = typeof ListenerEvents[number];
7
10
 
@@ -47,7 +50,6 @@ abstract class Core {
47
50
  this.onClose = this.onClose.bind(this);
48
51
  this.onError = this.onError.bind(this);
49
52
  this.onMessage = this.onMessage.bind(this);
50
-
51
53
  }
52
54
 
53
55
  public setConfig(config: IConfig): void {
@@ -133,7 +135,7 @@ abstract class Core {
133
135
  }
134
136
 
135
137
  private emitListener(method: TListenerEvents, channel: string): void {
136
- if (ListenerEvents.includes(method)) {
138
+ if (ListenerEvents.includes(method)) {
137
139
  this.events.emit(method + ':' + channel, {
138
140
  channel
139
141
  });
@@ -145,9 +147,7 @@ abstract class Core {
145
147
  }
146
148
 
147
149
  protected send<T>(message: TMessage<T>) {
148
- if (!this.status) {
149
- return;
150
- }
150
+ if (!this.status) return;
151
151
  this.ws!.send(JSON.stringify(message));
152
152
  }
153
153
  }
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
@@ -1,35 +1,82 @@
1
+ import {
2
+ Event
3
+ } from "easy-event-emitter";
1
4
  import Core,{
2
5
  IConfig,
3
- TPermissions
6
+ TPermissions,
7
+ SocketEvents,
8
+ TSocketEvents,
9
+ TListenerCallback
4
10
  } from "./Core";
5
11
  import Subscribe from "./Subscribe";
6
12
 
7
13
  class Larasopp extends Core {
14
+ private _channels: string[];
8
15
 
9
16
  constructor(config: IConfig) {
10
17
  super(config);
11
18
 
19
+ this._channels = [];
20
+
12
21
  this.subscribe = this.subscribe.bind(this);
13
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);
14
26
  }
15
27
 
16
28
  public subscribe(channel: string): Subscribe {
17
29
  return new Subscribe({
18
30
  events: this.events,
31
+ hasChannel: this.hasChannel,
32
+ pushChannel: this.pushChannel,
33
+ removeChannel: this.removeChannel,
19
34
  status: this.status,
20
35
  send: this.send,
21
36
  channel
22
37
  });
23
38
  }
24
39
 
25
- public trigger<T>(channel: string, event: string, message: T, permission: TPermissions = 'public'): void {
26
- this.send<T>({
27
- channel: channel,
28
- event: event,
29
- message: message,
30
- type: permission
31
- });
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;
70
+ }
71
+
72
+ public addListener(event: TSocketEvents, callback: TListenerCallback): Event | undefined {
73
+ if (!SocketEvents.includes(event)) return;
74
+ return this.events.addListener(event, callback);
32
75
  }
33
76
  }
34
77
 
78
+ export type {
79
+ Subscribe
80
+ };
81
+
35
82
  export default Larasopp;