larasopp 1.0.2 → 1.0.3

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,11 @@
1
1
  import { Events } from "easy-event-emitter";
2
2
  export type TPermissions = 'public' | 'protected' | 'private';
3
- type TMessage = {
3
+ export type TMessage<T> = {
4
4
  subscribe?: string;
5
5
  unsubscribe?: string;
6
6
  channel?: string;
7
7
  event?: string;
8
- message?: any;
8
+ message?: T;
9
9
  type?: TPermissions;
10
10
  };
11
11
  export interface IConfig {
@@ -16,7 +16,7 @@ export interface IConfig {
16
16
  declare abstract class Core {
17
17
  protected events: Events;
18
18
  private ws?;
19
- protected status: boolean;
19
+ protected _status: boolean;
20
20
  private config;
21
21
  constructor(config: IConfig);
22
22
  setConfig(config: IConfig): void;
@@ -36,6 +36,7 @@ declare abstract class Core {
36
36
  private onClose;
37
37
  private onError;
38
38
  private onMessage;
39
- protected send(message: TMessage): void;
39
+ get status(): boolean;
40
+ protected send<T>(message: TMessage<T>): void;
40
41
  }
41
42
  export default Core;
package/lib/Core.js CHANGED
@@ -18,7 +18,7 @@ class Core {
18
18
  writable: true,
19
19
  value: void 0
20
20
  });
21
- Object.defineProperty(this, "status", {
21
+ Object.defineProperty(this, "_status", {
22
22
  enumerable: true,
23
23
  configurable: true,
24
24
  writable: true,
@@ -32,7 +32,8 @@ class Core {
32
32
  });
33
33
  this.events = new easy_event_emitter_1.default;
34
34
  this.config = Object.assign({ tls: false }, config);
35
- this.status = false;
35
+ this._status = false;
36
+ this.send = this.send.bind(this);
36
37
  this.setConfig = this.setConfig.bind(this);
37
38
  this.setToken = this.setToken.bind(this);
38
39
  this.connect = this.connect.bind(this);
@@ -64,10 +65,6 @@ class Core {
64
65
  this.onError('Socket exception');
65
66
  this.onClose(e);
66
67
  }
67
- // this.handleTimeout = setTimeout(() => {
68
- // this.ws?.close(1000, 'timeout');
69
- // },this.timeout);
70
- // this.startReconnect();
71
68
  this.ws.onopen = this.onOpen;
72
69
  this.ws.onclose = this.onClose;
73
70
  this.ws.onerror = this.onError;
@@ -94,24 +91,11 @@ class Core {
94
91
  return true;
95
92
  }
96
93
  onOpen(e) {
97
- this.status = true;
94
+ this._status = true;
98
95
  this.events.emit("open", e);
99
96
  }
100
97
  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;
98
+ this._status = false;
115
99
  this.events.emit("close", e);
116
100
  }
117
101
  onError(e) {
@@ -123,6 +107,9 @@ class Core {
123
107
  this.events.emit(json.channel + ':' + json.event, json.message);
124
108
  }
125
109
  }
110
+ get status() {
111
+ return this._status;
112
+ }
126
113
  send(message) {
127
114
  if (!this.status) {
128
115
  return;
@@ -0,0 +1,23 @@
1
+ import { TMessage } from "./Core";
2
+ import { 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
+ }
23
+ export default Subscribe;
@@ -0,0 +1,70 @@
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
+ }
70
+ 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.3",
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
@@ -4,12 +4,12 @@ import EventEmitter,{
4
4
 
5
5
  export type TPermissions = 'public' | 'protected' | 'private';
6
6
 
7
- type TMessage = {
7
+ export type TMessage<T> = {
8
8
  subscribe?: string;
9
9
  unsubscribe?: string;
10
10
  channel?: string;
11
11
  event?: string;
12
- message?: any;
12
+ message?: T;
13
13
  type?: TPermissions;
14
14
  }
15
15
 
@@ -22,7 +22,7 @@ export interface IConfig {
22
22
  abstract class Core {
23
23
  protected events: Events;
24
24
  private ws?: WebSocket;
25
- protected status: boolean;
25
+ protected _status: boolean;
26
26
  private config: IConfig;
27
27
 
28
28
  constructor(config: IConfig) {
@@ -32,8 +32,9 @@ abstract class Core {
32
32
  tls: false,
33
33
  ...config,
34
34
  };
35
- this.status = false;
35
+ this._status = false;
36
36
 
37
+ this.send = this.send.bind(this);
37
38
  this.setConfig = this.setConfig.bind(this);
38
39
  this.setToken = this.setToken.bind(this);
39
40
  this.connect = this.connect.bind(this);
@@ -77,12 +78,6 @@ abstract class Core {
77
78
  this.onClose(e);
78
79
  }
79
80
 
80
- // this.handleTimeout = setTimeout(() => {
81
- // this.ws?.close(1000, 'timeout');
82
- // },this.timeout);
83
-
84
- // this.startReconnect();
85
-
86
81
  this.ws!.onopen = this.onOpen;
87
82
  this.ws!.onclose = this.onClose;
88
83
  this.ws!.onerror = this.onError;
@@ -111,25 +106,12 @@ abstract class Core {
111
106
  }
112
107
 
113
108
  private onOpen(e: any): void {
114
- this.status = true;
109
+ this._status = true;
115
110
  this.events.emit("open", e);
116
111
  }
117
112
 
118
113
  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;
114
+ this._status = false;
133
115
  this.events.emit("close", e);
134
116
  }
135
117
 
@@ -145,7 +127,11 @@ abstract class Core {
145
127
  }
146
128
  }
147
129
 
148
- protected send(message: TMessage) {
130
+ public get status(): boolean {
131
+ return this._status;
132
+ }
133
+
134
+ protected send<T>(message: TMessage<T>) {
149
135
  if (!this.status) {
150
136
  return;
151
137
  }
@@ -0,0 +1,72 @@
1
+ import {
2
+ TMessage
3
+ } from "./Core";
4
+ import {
5
+ Events
6
+ } from "easy-event-emitter";
7
+
8
+ type TReturn = {
9
+ remove: () => void;
10
+ }
11
+
12
+ interface ISubscribe {
13
+ events: Events;
14
+ send: <T>(message: TMessage<T>) => void;
15
+ channel: string;
16
+ status: boolean;
17
+ }
18
+
19
+ class Subscribe {
20
+ private events: Events;
21
+ private status: boolean;
22
+ private _channel: string;
23
+ private send: <T>(message: TMessage<T>) => void;
24
+
25
+ constructor({events, status, channel, send}: ISubscribe) {
26
+ this.events = events;
27
+ this.status = status;
28
+ this._channel = channel;
29
+ this.send = send;
30
+
31
+ this.init();
32
+ }
33
+
34
+ public get channel(): string {
35
+ return this._channel;
36
+ }
37
+
38
+ private init(): void {
39
+ if (this.status) {
40
+ this.send({
41
+ subscribe: this.channel
42
+ });
43
+ }else{
44
+ const event = this.events.addListener('open',() => {
45
+ this.send({
46
+ subscribe: this.channel
47
+ });
48
+ event.remove();
49
+ });
50
+ }
51
+ }
52
+
53
+ public bind<T>(event: string, callback: (data: T) => void): TReturn {
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
+
65
+ public remove(): void {
66
+ this.send({
67
+ unsubscribe: this.channel
68
+ });
69
+ }
70
+ }
71
+
72
+ 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;