larasopp 1.0.2 → 1.0.4

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,11 +1,16 @@
1
1
  import { Events } from "easy-event-emitter";
2
+ export declare const ListenerEvents: readonly ["subscribe", "unsubscribe"];
3
+ export type TListenerEvents = typeof ListenerEvents[number];
4
+ export type TListenerCallback = (data: {
5
+ channel: string;
6
+ }) => void;
2
7
  export type TPermissions = 'public' | 'protected' | 'private';
3
- type TMessage = {
8
+ export type TMessage<T> = {
4
9
  subscribe?: string;
5
10
  unsubscribe?: string;
6
11
  channel?: string;
7
12
  event?: string;
8
- message?: any;
13
+ message?: T;
9
14
  type?: TPermissions;
10
15
  };
11
16
  export interface IConfig {
@@ -16,7 +21,7 @@ export interface IConfig {
16
21
  declare abstract class Core {
17
22
  protected events: Events;
18
23
  private ws?;
19
- protected status: boolean;
24
+ protected _status: boolean;
20
25
  private config;
21
26
  constructor(config: IConfig);
22
27
  setConfig(config: IConfig): void;
@@ -36,6 +41,8 @@ declare abstract class Core {
36
41
  private onClose;
37
42
  private onError;
38
43
  private onMessage;
39
- protected send(message: TMessage): void;
44
+ private emitListener;
45
+ get status(): boolean;
46
+ protected send<T>(message: TMessage<T>): void;
40
47
  }
41
48
  export default Core;
package/lib/Core.js CHANGED
@@ -3,7 +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
7
  const easy_event_emitter_1 = __importDefault(require("easy-event-emitter"));
8
+ exports.ListenerEvents = ['subscribe', 'unsubscribe'];
7
9
  class Core {
8
10
  constructor(config) {
9
11
  Object.defineProperty(this, "events", {
@@ -18,7 +20,7 @@ class Core {
18
20
  writable: true,
19
21
  value: void 0
20
22
  });
21
- Object.defineProperty(this, "status", {
23
+ Object.defineProperty(this, "_status", {
22
24
  enumerable: true,
23
25
  configurable: true,
24
26
  writable: true,
@@ -32,7 +34,8 @@ class Core {
32
34
  });
33
35
  this.events = new easy_event_emitter_1.default;
34
36
  this.config = Object.assign({ tls: false }, config);
35
- this.status = false;
37
+ this._status = false;
38
+ this.send = this.send.bind(this);
36
39
  this.setConfig = this.setConfig.bind(this);
37
40
  this.setToken = this.setToken.bind(this);
38
41
  this.connect = this.connect.bind(this);
@@ -64,10 +67,6 @@ class Core {
64
67
  this.onError('Socket exception');
65
68
  this.onClose(e);
66
69
  }
67
- // this.handleTimeout = setTimeout(() => {
68
- // this.ws?.close(1000, 'timeout');
69
- // },this.timeout);
70
- // this.startReconnect();
71
70
  this.ws.onopen = this.onOpen;
72
71
  this.ws.onclose = this.onClose;
73
72
  this.ws.onerror = this.onError;
@@ -94,24 +93,11 @@ class Core {
94
93
  return true;
95
94
  }
96
95
  onOpen(e) {
97
- this.status = true;
96
+ this._status = true;
98
97
  this.events.emit("open", e);
99
98
  }
100
99
  onClose(e) {
101
- // if (e.wasClean) {
102
- // console.log('Соединение закрыто чисто');
103
- // } else {
104
- // if (Larasopp.stepReconnect >= 5) {
105
- // console.log('connect error');
106
- // }else{
107
- // setTimeout(() => {
108
- // console.log('try reconnect...');
109
- // Larasopp.connect(params);
110
- // ++Larasopp.stepReconnect;
111
- // }, 3000);
112
- // }
113
- // }
114
- this.status = false;
100
+ this._status = false;
115
101
  this.events.emit("close", e);
116
102
  }
117
103
  onError(e) {
@@ -120,9 +106,20 @@ class Core {
120
106
  onMessage(e) {
121
107
  if (this.isJsonString(e.data)) {
122
108
  const json = JSON.parse(e.data);
109
+ this.emitListener(json.channel, json.message);
123
110
  this.events.emit(json.channel + ':' + json.event, json.message);
124
111
  }
125
112
  }
113
+ emitListener(method, channel) {
114
+ if (exports.ListenerEvents.includes(method)) {
115
+ this.events.emit(method + ':' + channel, {
116
+ channel
117
+ });
118
+ }
119
+ }
120
+ get status() {
121
+ return this._status;
122
+ }
126
123
  send(message) {
127
124
  if (!this.status) {
128
125
  return;
@@ -0,0 +1,24 @@
1
+ import { TMessage, TListenerEvents, TListenerCallback } from "./Core";
2
+ import { Event, Events } from "easy-event-emitter";
3
+ type TReturn = {
4
+ remove: () => void;
5
+ };
6
+ interface ISubscribe {
7
+ events: Events;
8
+ send: <T>(message: TMessage<T>) => void;
9
+ channel: string;
10
+ status: boolean;
11
+ }
12
+ declare class Subscribe {
13
+ private events;
14
+ private status;
15
+ private _channel;
16
+ private send;
17
+ constructor({ events, status, channel, send }: ISubscribe);
18
+ get channel(): string;
19
+ private init;
20
+ bind<T>(event: string, callback: (data: T) => void): TReturn;
21
+ remove(): void;
22
+ addListener(event: TListenerEvents, callback: TListenerCallback): Event | undefined;
23
+ }
24
+ export default Subscribe;
@@ -0,0 +1,73 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ class Subscribe {
4
+ constructor({ events, 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, "status", {
12
+ enumerable: true,
13
+ configurable: true,
14
+ writable: true,
15
+ value: void 0
16
+ });
17
+ Object.defineProperty(this, "_channel", {
18
+ enumerable: true,
19
+ configurable: true,
20
+ writable: true,
21
+ value: void 0
22
+ });
23
+ Object.defineProperty(this, "send", {
24
+ enumerable: true,
25
+ configurable: true,
26
+ writable: true,
27
+ value: void 0
28
+ });
29
+ this.events = events;
30
+ this.status = status;
31
+ this._channel = channel;
32
+ this.send = send;
33
+ this.init();
34
+ }
35
+ get channel() {
36
+ return this._channel;
37
+ }
38
+ init() {
39
+ if (this.status) {
40
+ this.send({
41
+ subscribe: this.channel
42
+ });
43
+ }
44
+ else {
45
+ const event = this.events.addListener('open', () => {
46
+ this.send({
47
+ subscribe: this.channel
48
+ });
49
+ event.remove();
50
+ });
51
+ }
52
+ }
53
+ bind(event, callback) {
54
+ const Event = this.events.addListener(this.channel + ':' + event, callback);
55
+ return {
56
+ remove: () => {
57
+ this.send({
58
+ unsubscribe: this.channel
59
+ });
60
+ Event.remove();
61
+ }
62
+ };
63
+ }
64
+ remove() {
65
+ this.send({
66
+ unsubscribe: this.channel
67
+ });
68
+ }
69
+ addListener(event, callback) {
70
+ return this.events.addListener(event + ':' + this.channel, callback);
71
+ }
72
+ }
73
+ exports.default = Subscribe;
package/lib/index.d.ts CHANGED
@@ -1,11 +1,8 @@
1
1
  import Core, { IConfig, TPermissions } from "./Core";
2
- export default class Larasopp extends Core {
2
+ import Subscribe from "./Subscribe";
3
+ declare class Larasopp extends Core {
3
4
  constructor(config: IConfig);
4
- subscribe(channel: string): {
5
- bind: (event: string, callback: (data: any) => void) => {
6
- remove: () => void;
7
- };
8
- remove: () => void;
9
- };
10
- trigger(channel: string, event: string, message: any, permission?: TPermissions): void;
5
+ subscribe(channel: string): Subscribe;
6
+ trigger<T>(channel: string, event: string, message: T, permission?: TPermissions): void;
11
7
  }
8
+ export default Larasopp;
package/lib/index.js CHANGED
@@ -4,6 +4,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const Core_1 = __importDefault(require("./Core"));
7
+ const Subscribe_1 = __importDefault(require("./Subscribe"));
7
8
  class Larasopp extends Core_1.default {
8
9
  constructor(config) {
9
10
  super(config);
@@ -11,37 +12,12 @@ class Larasopp extends Core_1.default {
11
12
  this.trigger = this.trigger.bind(this);
12
13
  }
13
14
  subscribe(channel) {
14
- if (this.status) {
15
- this.send({
16
- subscribe: channel
17
- });
18
- }
19
- else {
20
- const event = this.events.addListener('open', () => {
21
- this.send({
22
- subscribe: channel
23
- });
24
- event.remove();
25
- });
26
- }
27
- return {
28
- bind: (event, callback) => {
29
- const retEvent = this.events.addListener(channel + ':' + event, callback);
30
- return {
31
- remove: () => {
32
- this.send({
33
- unsubscribe: channel
34
- });
35
- retEvent.remove();
36
- }
37
- };
38
- },
39
- remove: () => {
40
- this.send({
41
- unsubscribe: channel
42
- });
43
- }
44
- };
15
+ return new Subscribe_1.default({
16
+ events: this.events,
17
+ status: this.status,
18
+ send: this.send,
19
+ channel
20
+ });
45
21
  }
46
22
  trigger(channel, event, message, permission = 'public') {
47
23
  this.send({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "larasopp",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
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
@@ -2,14 +2,19 @@ import EventEmitter,{
2
2
  Events
3
3
  } from "easy-event-emitter";
4
4
 
5
+ export const ListenerEvents = ['subscribe', 'unsubscribe'] as const;
6
+ export type TListenerEvents = typeof ListenerEvents[number];
7
+
8
+ export type TListenerCallback = (data:{channel: string}) => void;
9
+
5
10
  export type TPermissions = 'public' | 'protected' | 'private';
6
11
 
7
- type TMessage = {
12
+ export type TMessage<T> = {
8
13
  subscribe?: string;
9
14
  unsubscribe?: string;
10
15
  channel?: string;
11
16
  event?: string;
12
- message?: any;
17
+ message?: T;
13
18
  type?: TPermissions;
14
19
  }
15
20
 
@@ -22,7 +27,7 @@ export interface IConfig {
22
27
  abstract class Core {
23
28
  protected events: Events;
24
29
  private ws?: WebSocket;
25
- protected status: boolean;
30
+ protected _status: boolean;
26
31
  private config: IConfig;
27
32
 
28
33
  constructor(config: IConfig) {
@@ -32,8 +37,9 @@ abstract class Core {
32
37
  tls: false,
33
38
  ...config,
34
39
  };
35
- this.status = false;
40
+ this._status = false;
36
41
 
42
+ this.send = this.send.bind(this);
37
43
  this.setConfig = this.setConfig.bind(this);
38
44
  this.setToken = this.setToken.bind(this);
39
45
  this.connect = this.connect.bind(this);
@@ -77,12 +83,6 @@ abstract class Core {
77
83
  this.onClose(e);
78
84
  }
79
85
 
80
- // this.handleTimeout = setTimeout(() => {
81
- // this.ws?.close(1000, 'timeout');
82
- // },this.timeout);
83
-
84
- // this.startReconnect();
85
-
86
86
  this.ws!.onopen = this.onOpen;
87
87
  this.ws!.onclose = this.onClose;
88
88
  this.ws!.onerror = this.onError;
@@ -111,25 +111,12 @@ abstract class Core {
111
111
  }
112
112
 
113
113
  private onOpen(e: any): void {
114
- this.status = true;
114
+ this._status = true;
115
115
  this.events.emit("open", e);
116
116
  }
117
117
 
118
118
  private onClose(e: any): void {
119
- // if (e.wasClean) {
120
- // console.log('Соединение закрыто чисто');
121
- // } else {
122
- // if (Larasopp.stepReconnect >= 5) {
123
- // console.log('connect error');
124
- // }else{
125
- // setTimeout(() => {
126
- // console.log('try reconnect...');
127
- // Larasopp.connect(params);
128
- // ++Larasopp.stepReconnect;
129
- // }, 3000);
130
- // }
131
- // }
132
- this.status = false;
119
+ this._status = false;
133
120
  this.events.emit("close", e);
134
121
  }
135
122
 
@@ -140,12 +127,24 @@ abstract class Core {
140
127
  private onMessage(e: any): void {
141
128
  if (this.isJsonString(e.data)) {
142
129
  const json = JSON.parse(e.data);
143
-
130
+ this.emitListener(json.channel, json.message);
144
131
  this.events.emit(json.channel + ':' + json.event, json.message);
145
132
  }
146
133
  }
147
134
 
148
- protected send(message: TMessage) {
135
+ private emitListener(method: TListenerEvents, channel: string): void {
136
+ if (ListenerEvents.includes(method)) {
137
+ this.events.emit(method + ':' + channel, {
138
+ channel
139
+ });
140
+ }
141
+ }
142
+
143
+ public get status(): boolean {
144
+ return this._status;
145
+ }
146
+
147
+ protected send<T>(message: TMessage<T>) {
149
148
  if (!this.status) {
150
149
  return;
151
150
  }
@@ -0,0 +1,80 @@
1
+ import {
2
+ TMessage,
3
+ ListenerEvents,
4
+ TListenerEvents,
5
+ TListenerCallback
6
+ } from "./Core";
7
+ import {
8
+ Event,
9
+ Events
10
+ } from "easy-event-emitter";
11
+
12
+ type TReturn = {
13
+ remove: () => void;
14
+ }
15
+
16
+ interface ISubscribe {
17
+ events: Events;
18
+ send: <T>(message: TMessage<T>) => void;
19
+ channel: string;
20
+ status: boolean;
21
+ }
22
+
23
+ class Subscribe {
24
+ private events: Events;
25
+ private status: boolean;
26
+ private _channel: string;
27
+ private send: <T>(message: TMessage<T>) => void;
28
+
29
+ constructor({events, status, channel, send}: ISubscribe) {
30
+ this.events = events;
31
+ this.status = status;
32
+ this._channel = channel;
33
+ this.send = send;
34
+
35
+ this.init();
36
+ }
37
+
38
+ public get channel(): string {
39
+ return this._channel;
40
+ }
41
+
42
+ private init(): void {
43
+ if (this.status) {
44
+ this.send({
45
+ subscribe: this.channel
46
+ });
47
+ }else{
48
+ const event = this.events.addListener('open',() => {
49
+ this.send({
50
+ subscribe: this.channel
51
+ });
52
+ event.remove();
53
+ });
54
+ }
55
+ }
56
+
57
+ public bind<T>(event: string, callback: (data: T) => void): TReturn {
58
+ const Event = this.events.addListener(this.channel + ':' + event, callback);
59
+ return {
60
+ remove: () => {
61
+ this.send({
62
+ unsubscribe: this.channel
63
+ });
64
+ Event.remove();
65
+ }
66
+ }
67
+ }
68
+
69
+ public remove(): void {
70
+ this.send({
71
+ unsubscribe: this.channel
72
+ });
73
+ }
74
+
75
+ public addListener(event: TListenerEvents, callback: TListenerCallback): Event | undefined {
76
+ return this.events.addListener(event + ':' + this.channel, callback);
77
+ }
78
+ }
79
+
80
+ export default Subscribe;
package/src/index.ts CHANGED
@@ -2,56 +2,34 @@ import Core,{
2
2
  IConfig,
3
3
  TPermissions
4
4
  } from "./Core";
5
+ import Subscribe from "./Subscribe";
5
6
 
6
- export default class Larasopp extends Core {
7
+ class Larasopp extends Core {
7
8
 
8
9
  constructor(config: IConfig) {
9
10
  super(config);
10
-
11
+
11
12
  this.subscribe = this.subscribe.bind(this);
12
13
  this.trigger = this.trigger.bind(this);
13
14
  }
14
15
 
15
- public subscribe(channel: string) {
16
- if (this.status) {
17
- this.send({
18
- subscribe: channel
19
- });
20
- }else{
21
- const event = this.events.addListener('open',() => {
22
- this.send({
23
- subscribe: channel
24
- });
25
- event.remove();
26
- });
27
- }
28
-
29
- return {
30
- bind: (event: string, callback: (data: any) => void) => {
31
- const retEvent = this.events.addListener(channel + ':' + event, callback);
32
- return {
33
- remove: () => {
34
- this.send({
35
- unsubscribe: channel
36
- });
37
- retEvent.remove();
38
- }
39
- }
40
- },
41
- remove: () => {
42
- this.send({
43
- unsubscribe: channel
44
- });
45
- }
46
- }
16
+ public subscribe(channel: string): Subscribe {
17
+ return new Subscribe({
18
+ events: this.events,
19
+ status: this.status,
20
+ send: this.send,
21
+ channel
22
+ });
47
23
  }
48
24
 
49
- public trigger(channel: string, event: string, message: any, permission: TPermissions = 'public'): void {
50
- this.send({
25
+ public trigger<T>(channel: string, event: string, message: T, permission: TPermissions = 'public'): void {
26
+ this.send<T>({
51
27
  channel: channel,
52
28
  event: event,
53
29
  message: message,
54
30
  type: permission
55
31
  });
56
32
  }
57
- }
33
+ }
34
+
35
+ export default Larasopp;