larasopp 1.2.8 → 1.2.10
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 +3 -33
- package/lib/Core.js +24 -4
- package/lib/constants.d.ts +2 -0
- package/lib/constants.js +5 -0
- package/lib/index.d.ts +2 -1
- package/lib/index.js +3 -25
- package/lib/types.d.ts +34 -0
- package/lib/types.js +3 -0
- package/package.json +1 -1
- package/src/Core.ts +24 -41
- package/src/constants.ts +3 -0
- package/src/index.ts +10 -9
- package/src/types.ts +44 -0
package/lib/Core.d.ts
CHANGED
|
@@ -1,42 +1,11 @@
|
|
|
1
1
|
import { type Events } from "easy-event-emitter";
|
|
2
|
-
import type
|
|
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
|
-
|
|
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);
|
|
@@ -54,6 +59,8 @@ class Core {
|
|
|
54
59
|
* @returns {this}
|
|
55
60
|
*/
|
|
56
61
|
connect(token) {
|
|
62
|
+
if (this.status)
|
|
63
|
+
return this;
|
|
57
64
|
try {
|
|
58
65
|
const host = this.config.host + '/token=' + (token !== null && token !== void 0 ? token : this.config.token);
|
|
59
66
|
this.ws = new WebSocket(encodeURI(host));
|
|
@@ -89,11 +96,24 @@ class Core {
|
|
|
89
96
|
}
|
|
90
97
|
return true;
|
|
91
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
|
+
}
|
|
92
111
|
handleOpen(e) {
|
|
93
112
|
this.events.emit("open", e);
|
|
94
113
|
}
|
|
95
114
|
handleClose(e) {
|
|
96
115
|
this.events.emit("close", e);
|
|
116
|
+
this.tryReconnect();
|
|
97
117
|
}
|
|
98
118
|
handleError(e) {
|
|
99
119
|
this.events.emit("error", e);
|
|
@@ -130,7 +150,7 @@ class Core {
|
|
|
130
150
|
}
|
|
131
151
|
}
|
|
132
152
|
emitListener(method, channel) {
|
|
133
|
-
if (
|
|
153
|
+
if (constants_1.ListenerEvents.includes(method)) {
|
|
134
154
|
this.events.emit(method + ':' + channel, {
|
|
135
155
|
channel
|
|
136
156
|
});
|
package/lib/constants.js
ADDED
package/lib/index.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Event } from "easy-event-emitter";
|
|
2
|
-
import Core
|
|
2
|
+
import Core from "./Core";
|
|
3
3
|
import Listener from "./Listener";
|
|
4
|
+
import type { IConfig, TPermissions, TSocketEvents, TListenerCallback, TListen } from "./types";
|
|
4
5
|
declare class Larasopp extends Core {
|
|
5
6
|
private readonly channels;
|
|
6
7
|
constructor(config: IConfig);
|
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 =
|
|
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) {
|
|
@@ -84,7 +62,7 @@ class Larasopp extends Core_1.default {
|
|
|
84
62
|
this.channels[channel].push(listener);
|
|
85
63
|
}
|
|
86
64
|
addListener(event, callback) {
|
|
87
|
-
if (!
|
|
65
|
+
if (!constants_1.SocketEvents.includes(event))
|
|
88
66
|
return;
|
|
89
67
|
return this.events.addListener(event, callback);
|
|
90
68
|
}
|
package/lib/types.d.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
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 TListen = {
|
|
10
|
+
remove: () => void;
|
|
11
|
+
};
|
|
12
|
+
export type TMessage<T> = {
|
|
13
|
+
subscribe?: string;
|
|
14
|
+
unsubscribe?: string;
|
|
15
|
+
channel?: string;
|
|
16
|
+
event?: string;
|
|
17
|
+
token?: string;
|
|
18
|
+
message?: T;
|
|
19
|
+
type?: TPermissions;
|
|
20
|
+
};
|
|
21
|
+
export type TConfigDataReviver = {
|
|
22
|
+
[index: string]: (value: any) => any;
|
|
23
|
+
};
|
|
24
|
+
export interface IConfig {
|
|
25
|
+
host: string;
|
|
26
|
+
token?: string;
|
|
27
|
+
reviver?: (this: any, key: string, value: any) => any;
|
|
28
|
+
dataReviver?: TConfigDataReviver;
|
|
29
|
+
reconnect?: number;
|
|
30
|
+
reconnectDelay?: number;
|
|
31
|
+
}
|
|
32
|
+
export type TChannels = {
|
|
33
|
+
[channel: string]: Listener[];
|
|
34
|
+
};
|
package/lib/types.js
ADDED
package/package.json
CHANGED
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
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
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);
|
|
@@ -78,6 +45,7 @@ abstract class Core {
|
|
|
78
45
|
* @returns {this}
|
|
79
46
|
*/
|
|
80
47
|
public connect(token?: string): this {
|
|
48
|
+
if (this.status) return this;
|
|
81
49
|
try {
|
|
82
50
|
const host = this.config.host + '/token=' + (token ?? this.config.token);
|
|
83
51
|
this.ws = new WebSocket(encodeURI(host));
|
|
@@ -114,12 +82,27 @@ abstract class Core {
|
|
|
114
82
|
return true;
|
|
115
83
|
}
|
|
116
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
|
+
|
|
117
99
|
private handleOpen(e: Event): void {
|
|
118
100
|
this.events.emit("open", e);
|
|
119
101
|
}
|
|
120
102
|
|
|
121
103
|
private handleClose(e: CloseEvent): void {
|
|
122
104
|
this.events.emit("close", e);
|
|
105
|
+
this.tryReconnect();
|
|
123
106
|
}
|
|
124
107
|
|
|
125
108
|
private handleError(e: Event): void {
|
package/src/constants.ts
ADDED
package/src/index.ts
CHANGED
|
@@ -1,16 +1,17 @@
|
|
|
1
1
|
import {
|
|
2
2
|
Event
|
|
3
3
|
} from "easy-event-emitter";
|
|
4
|
-
import Core
|
|
5
|
-
|
|
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
|
+
TListen,
|
|
13
|
+
TChannels
|
|
14
|
+
} from "./types";
|
|
14
15
|
|
|
15
16
|
class Larasopp extends Core {
|
|
16
17
|
private readonly channels: TChannels;
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
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 TListen = {
|
|
16
|
+
remove: () => void;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export type TMessage<T> = {
|
|
20
|
+
subscribe?: string;
|
|
21
|
+
unsubscribe?: string;
|
|
22
|
+
channel?: string;
|
|
23
|
+
event?: string;
|
|
24
|
+
token?: string;
|
|
25
|
+
message?: T;
|
|
26
|
+
type?: TPermissions;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export type TConfigDataReviver = {
|
|
30
|
+
[index: string]: (value: any) => any;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export interface IConfig {
|
|
34
|
+
host: string;
|
|
35
|
+
token?: string;
|
|
36
|
+
reviver?: (this: any, key: string, value: any) => any;
|
|
37
|
+
dataReviver?: TConfigDataReviver;
|
|
38
|
+
reconnect?: number;
|
|
39
|
+
reconnectDelay?: number;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export type TChannels = {
|
|
43
|
+
[channel: string]: Listener[];
|
|
44
|
+
};
|