larasopp 1.2.9 → 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/Core.d.ts CHANGED
@@ -1,42 +1,11 @@
1
1
  import { type Events } from "easy-event-emitter";
2
- import type Listener from "./Listener";
3
- export declare const SocketEvents: readonly ["open", "close", "error"];
4
- export type TSocketEvents = typeof SocketEvents[number];
5
- export declare const ListenerEvents: readonly ["subscribe", "unsubscribe"];
6
- export type TListenerEvents = typeof ListenerEvents[number];
7
- export type TListenerCallback = (data: {
8
- channel: string;
9
- }) => void;
10
- export type TPermissions = 'public' | 'protected' | 'private';
11
- export type TListen = {
12
- remove: () => void;
13
- };
14
- export type TMessage<T> = {
15
- subscribe?: string;
16
- unsubscribe?: string;
17
- channel?: string;
18
- event?: string;
19
- token?: string;
20
- message?: T;
21
- type?: TPermissions;
22
- };
23
- export type TConfigDataReviver = {
24
- [index: string]: (value: any) => any;
25
- };
26
- export interface IConfig {
27
- host: string;
28
- token?: string;
29
- reviver?: (this: any, key: string, value: any) => any;
30
- dataReviver?: TConfigDataReviver;
31
- }
32
- export type TChannels = {
33
- [channel: string]: Listener[];
34
- };
2
+ import type { IConfig, TMessage } from "./types";
35
3
  declare abstract class Core {
36
4
  readonly events: Events;
37
5
  private ws?;
38
6
  private _socketId?;
39
7
  private config;
8
+ private reconnectCount;
40
9
  constructor(config: IConfig);
41
10
  setConfig(config: IConfig): void;
42
11
  setToken(token: string): void;
@@ -51,6 +20,7 @@ declare abstract class Core {
51
20
  */
52
21
  disconnect(): void;
53
22
  protected isJson(str: string): boolean;
23
+ private tryReconnect;
54
24
  private handleOpen;
55
25
  private handleClose;
56
26
  private handleError;
package/lib/Core.js CHANGED
@@ -3,10 +3,8 @@ 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 = exports.SocketEvents = void 0;
7
6
  const easy_event_emitter_1 = __importDefault(require("easy-event-emitter"));
8
- exports.SocketEvents = ['open', 'close', 'error'];
9
- exports.ListenerEvents = ['subscribe', 'unsubscribe'];
7
+ const constants_1 = require("./constants");
10
8
  class Core {
11
9
  constructor(config) {
12
10
  Object.defineProperty(this, "events", {
@@ -33,7 +31,14 @@ class Core {
33
31
  writable: true,
34
32
  value: void 0
35
33
  });
34
+ Object.defineProperty(this, "reconnectCount", {
35
+ enumerable: true,
36
+ configurable: true,
37
+ writable: true,
38
+ value: void 0
39
+ });
36
40
  this.events = new easy_event_emitter_1.default;
41
+ this.reconnectCount = 0;
37
42
  this.config = config;
38
43
  this.handleOpen = this.handleOpen.bind(this);
39
44
  this.handleClose = this.handleClose.bind(this);
@@ -91,11 +96,24 @@ class Core {
91
96
  }
92
97
  return true;
93
98
  }
99
+ tryReconnect() {
100
+ var _a, _b;
101
+ if (typeof this.config.reconnect === 'undefined' ||
102
+ this.reconnectCount >= this.config.reconnect ||
103
+ typeof this.ws === 'undefined' ||
104
+ this.status ||
105
+ ((_a = this.ws) === null || _a === void 0 ? void 0 : _a.readyState) === this.ws.CONNECTING)
106
+ return;
107
+ ++this.reconnectCount;
108
+ this.connect();
109
+ setTimeout(() => this.tryReconnect(), (_b = this.config.reconnectDelay) !== null && _b !== void 0 ? _b : 1000);
110
+ }
94
111
  handleOpen(e) {
95
112
  this.events.emit("open", e);
96
113
  }
97
114
  handleClose(e) {
98
115
  this.events.emit("close", e);
116
+ this.tryReconnect();
99
117
  }
100
118
  handleError(e) {
101
119
  this.events.emit("error", e);
@@ -132,7 +150,7 @@ class Core {
132
150
  }
133
151
  }
134
152
  emitListener(method, channel) {
135
- if (exports.ListenerEvents.includes(method)) {
153
+ if (constants_1.ListenerEvents.includes(method)) {
136
154
  this.events.emit(method + ':' + channel, {
137
155
  channel
138
156
  });
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;
@@ -0,0 +1,2 @@
1
+ export declare const SocketEvents: readonly ["open", "close", "error"];
2
+ export declare const ListenerEvents: readonly ["subscribe", "unsubscribe"];
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ListenerEvents = exports.SocketEvents = void 0;
4
+ exports.SocketEvents = ['open', 'close', 'error'];
5
+ exports.ListenerEvents = ['subscribe', 'unsubscribe'];
package/lib/index.d.ts CHANGED
@@ -1,6 +1,7 @@
1
- import { Event } from "easy-event-emitter";
2
- import Core, { type IConfig, type TPermissions, type TSocketEvents, type TListenerCallback, type TListen } from "./Core";
1
+ import { type Event as EventListener } from "easy-event-emitter";
2
+ import Core from "./Core";
3
3
  import Listener from "./Listener";
4
+ import type { IConfig, TPermissions, TSocketEvents, TListenerCallback } from "./types";
4
5
  declare class Larasopp extends Core {
5
6
  private readonly channels;
6
7
  constructor(config: IConfig);
@@ -9,7 +10,8 @@ declare class Larasopp extends Core {
9
10
  unsubscribe(channel: string): void;
10
11
  trigger<T>(channel: string, event: string, message: T, permission?: TPermissions, waitSubscribe?: boolean): void;
11
12
  private pushListener;
12
- addListener(event: TSocketEvents, callback: TListenerCallback): Event | undefined;
13
+ countListeners(channel: string): number;
14
+ addListener(event: TSocketEvents, callback: TListenerCallback): EventListener | undefined;
13
15
  }
14
- export type { TListen };
16
+ export type { Listener, EventListener };
15
17
  export default Larasopp;
package/lib/index.js CHANGED
@@ -1,32 +1,10 @@
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
- };
25
2
  var __importDefault = (this && this.__importDefault) || function (mod) {
26
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
27
4
  };
28
5
  Object.defineProperty(exports, "__esModule", { value: true });
29
- const Core_1 = __importStar(require("./Core"));
6
+ const Core_1 = __importDefault(require("./Core"));
7
+ const constants_1 = require("./constants");
30
8
  const Listener_1 = __importDefault(require("./Listener"));
31
9
  class Larasopp extends Core_1.default {
32
10
  constructor(config) {
@@ -77,14 +55,19 @@ class Larasopp extends Core_1.default {
77
55
  pushListener(channel, listener) {
78
56
  if (!this.channels[channel]) {
79
57
  this.channels[channel] = [];
58
+ this.send({
59
+ subscribe: channel
60
+ });
80
61
  }
81
- this.send({
82
- subscribe: channel
83
- });
84
62
  this.channels[channel].push(listener);
85
63
  }
64
+ countListeners(channel) {
65
+ if (!this.channels[channel])
66
+ return -1;
67
+ return this.channels[channel].length;
68
+ }
86
69
  addListener(event, callback) {
87
- if (!Core_1.SocketEvents.includes(event))
70
+ if (!constants_1.SocketEvents.includes(event))
88
71
  return;
89
72
  return this.events.addListener(event, callback);
90
73
  }
package/lib/types.d.ts ADDED
@@ -0,0 +1,31 @@
1
+ import type Listener from "./Listener";
2
+ import { SocketEvents, ListenerEvents } from "./constants";
3
+ export type TSocketEvents = typeof SocketEvents[number];
4
+ export type TListenerEvents = typeof ListenerEvents[number];
5
+ export type TListenerCallback = (data: {
6
+ channel: string;
7
+ }) => void;
8
+ export type TPermissions = 'public' | 'protected' | 'private';
9
+ export type TMessage<T> = {
10
+ subscribe?: string;
11
+ unsubscribe?: string;
12
+ channel?: string;
13
+ event?: string;
14
+ token?: string;
15
+ message?: T;
16
+ type?: TPermissions;
17
+ };
18
+ export type TConfigDataReviver = {
19
+ [index: string]: (value: any) => any;
20
+ };
21
+ export interface IConfig {
22
+ host: string;
23
+ token?: string;
24
+ reviver?: (this: any, key: string, value: any) => any;
25
+ dataReviver?: TConfigDataReviver;
26
+ reconnect?: number;
27
+ reconnectDelay?: number;
28
+ }
29
+ export type TChannels = {
30
+ [channel: string]: Listener[];
31
+ };
package/lib/types.js ADDED
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ ;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "larasopp",
3
- "version": "1.2.9",
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/Core.ts CHANGED
@@ -1,56 +1,23 @@
1
1
  import EventEmitter,{
2
2
  type Events
3
3
  } from "easy-event-emitter";
4
- import type Listener from "./Listener";
5
-
6
- export const SocketEvents = ['open', 'close', 'error'] as const;
7
- export type TSocketEvents = typeof SocketEvents[number];
8
-
9
- export const ListenerEvents = ['subscribe', 'unsubscribe'] as const;
10
- export type TListenerEvents = typeof ListenerEvents[number];
11
-
12
- export type TListenerCallback = (data:{channel: string}) => void;
13
-
14
- export type TPermissions = 'public' | 'protected' | 'private';
15
-
16
- export type TListen = {
17
- remove: () => void;
18
- }
19
-
20
- export type TMessage<T> = {
21
- subscribe?: string;
22
- unsubscribe?: string;
23
- channel?: string;
24
- event?: string;
25
- token?: string;
26
- message?: T;
27
- type?: TPermissions;
28
- }
29
-
30
- export type TConfigDataReviver = {
31
- [index: string]: (value: any) => any;
32
- };
33
-
34
- export interface IConfig {
35
- host: string;
36
- token?: string;
37
- reviver?: (this: any, key: string, value: any) => any;
38
- dataReviver?: TConfigDataReviver;
39
- }
40
-
41
- export type TChannels = {
42
- [channel: string]: Listener[];
43
- };
4
+ import { ListenerEvents } from "./constants";
5
+ import type {
6
+ IConfig,
7
+ TListenerEvents,
8
+ TMessage
9
+ } from "./types";
44
10
 
45
11
  abstract class Core {
46
12
  public readonly events: Events;
47
13
  private ws?: WebSocket;
48
14
  private _socketId?: string;
49
15
  private config: IConfig;
16
+ private reconnectCount: number;
50
17
 
51
18
  constructor(config: IConfig) {
52
19
  this.events = new EventEmitter;
53
-
20
+ this.reconnectCount = 0;
54
21
  this.config = config;
55
22
 
56
23
  this.handleOpen = this.handleOpen.bind(this);
@@ -115,12 +82,27 @@ abstract class Core {
115
82
  return true;
116
83
  }
117
84
 
85
+ private tryReconnect() {
86
+ if (
87
+ typeof this.config.reconnect === 'undefined' ||
88
+ this.reconnectCount >= this.config.reconnect ||
89
+ typeof this.ws === 'undefined' ||
90
+ this.status ||
91
+ this.ws?.readyState === this.ws.CONNECTING
92
+ ) return;
93
+
94
+ ++this.reconnectCount;
95
+ this.connect();
96
+ setTimeout(() => this.tryReconnect(), this.config.reconnectDelay ?? 1000);
97
+ }
98
+
118
99
  private handleOpen(e: Event): void {
119
100
  this.events.emit("open", e);
120
101
  }
121
102
 
122
103
  private handleClose(e: CloseEvent): void {
123
104
  this.events.emit("close", e);
105
+ this.tryReconnect();
124
106
  }
125
107
 
126
108
  private handleError(e: Event): void {
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());
@@ -0,0 +1,3 @@
1
+ export const SocketEvents = ['open', 'close', 'error'] as const;
2
+
3
+ export const ListenerEvents = ['subscribe', 'unsubscribe'] as const;
package/src/index.ts CHANGED
@@ -1,16 +1,16 @@
1
1
  import {
2
- Event
2
+ type Event as EventListener
3
3
  } from "easy-event-emitter";
4
- import Core,{
5
- type IConfig,
6
- type TPermissions,
7
- SocketEvents,
8
- type TSocketEvents,
9
- type TListenerCallback,
10
- type TListen,
11
- type TChannels
12
- } from "./Core";
4
+ import Core from "./Core";
5
+ import { SocketEvents } from "./constants";
13
6
  import Listener from "./Listener";
7
+ import type {
8
+ IConfig,
9
+ TPermissions,
10
+ TSocketEvents,
11
+ TListenerCallback,
12
+ TChannels
13
+ } from "./types";
14
14
 
15
15
  class Larasopp extends Core {
16
16
  private readonly channels: TChannels;
@@ -63,21 +63,27 @@ class Larasopp extends Core {
63
63
  private pushListener(channel: string, listener: Listener): void {
64
64
  if (!this.channels[channel]) {
65
65
  this.channels[channel] = [];
66
+ this.send({
67
+ subscribe: channel
68
+ });
66
69
  }
67
- this.send({
68
- subscribe: channel
69
- });
70
70
  this.channels[channel].push(listener);
71
71
  }
72
72
 
73
- 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 {
74
79
  if (!SocketEvents.includes(event)) return;
75
80
  return this.events.addListener(event, callback);
76
81
  }
77
82
  }
78
83
 
79
84
  export type {
80
- TListen
85
+ Listener,
86
+ EventListener
81
87
  };
82
88
 
83
89
  export default Larasopp;
package/src/types.ts ADDED
@@ -0,0 +1,40 @@
1
+ import type Listener from "./Listener";
2
+ import {
3
+ SocketEvents,
4
+ ListenerEvents
5
+ } from "./constants";
6
+
7
+ export type TSocketEvents = typeof SocketEvents[number];
8
+
9
+ export type TListenerEvents = typeof ListenerEvents[number];
10
+
11
+ export type TListenerCallback = (data:{channel: string}) => void;
12
+
13
+ export type TPermissions = 'public' | 'protected' | 'private';
14
+
15
+ export type TMessage<T> = {
16
+ subscribe?: string;
17
+ unsubscribe?: string;
18
+ channel?: string;
19
+ event?: string;
20
+ token?: string;
21
+ message?: T;
22
+ type?: TPermissions;
23
+ };
24
+
25
+ export type TConfigDataReviver = {
26
+ [index: string]: (value: any) => any;
27
+ };
28
+
29
+ export interface IConfig {
30
+ host: string;
31
+ token?: string;
32
+ reviver?: (this: any, key: string, value: any) => any;
33
+ dataReviver?: TConfigDataReviver;
34
+ reconnect?: number;
35
+ reconnectDelay?: number;
36
+ };
37
+
38
+ export type TChannels = {
39
+ [channel: string]: Listener[];
40
+ };