larasopp 1.0.3 → 1.0.5
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 +8 -0
- package/lib/Core.js +12 -2
- package/lib/Subscribe.d.ts +3 -2
- package/lib/Subscribe.js +3 -0
- package/lib/index.d.ts +4 -1
- package/lib/index.js +29 -1
- package/package.json +1 -1
- package/src/Core.ts +18 -4
- package/src/Subscribe.ts +9 -1
- package/src/index.ts +16 -1
package/lib/Core.d.ts
CHANGED
|
@@ -1,4 +1,11 @@
|
|
|
1
1
|
import { Events } from "easy-event-emitter";
|
|
2
|
+
export declare const SocketEvents: readonly ["open", "close", "error"];
|
|
3
|
+
export type TSocketEvents = typeof SocketEvents[number];
|
|
4
|
+
export declare const ListenerEvents: readonly ["subscribe", "unsubscribe"];
|
|
5
|
+
export type TListenerEvents = typeof ListenerEvents[number];
|
|
6
|
+
export type TListenerCallback = (data: {
|
|
7
|
+
channel: string;
|
|
8
|
+
}) => void;
|
|
2
9
|
export type TPermissions = 'public' | 'protected' | 'private';
|
|
3
10
|
export type TMessage<T> = {
|
|
4
11
|
subscribe?: string;
|
|
@@ -36,6 +43,7 @@ declare abstract class Core {
|
|
|
36
43
|
private onClose;
|
|
37
44
|
private onError;
|
|
38
45
|
private onMessage;
|
|
46
|
+
private emitListener;
|
|
39
47
|
get status(): boolean;
|
|
40
48
|
protected send<T>(message: TMessage<T>): void;
|
|
41
49
|
}
|
package/lib/Core.js
CHANGED
|
@@ -3,7 +3,10 @@ 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;
|
|
6
7
|
const easy_event_emitter_1 = __importDefault(require("easy-event-emitter"));
|
|
8
|
+
exports.SocketEvents = ['open', 'close', 'error'];
|
|
9
|
+
exports.ListenerEvents = ['subscribe', 'unsubscribe'];
|
|
7
10
|
class Core {
|
|
8
11
|
constructor(config) {
|
|
9
12
|
Object.defineProperty(this, "events", {
|
|
@@ -104,16 +107,23 @@ class Core {
|
|
|
104
107
|
onMessage(e) {
|
|
105
108
|
if (this.isJsonString(e.data)) {
|
|
106
109
|
const json = JSON.parse(e.data);
|
|
110
|
+
this.emitListener(json.channel, json.message);
|
|
107
111
|
this.events.emit(json.channel + ':' + json.event, json.message);
|
|
108
112
|
}
|
|
109
113
|
}
|
|
114
|
+
emitListener(method, channel) {
|
|
115
|
+
if (exports.ListenerEvents.includes(method)) {
|
|
116
|
+
this.events.emit(method + ':' + channel, {
|
|
117
|
+
channel
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
}
|
|
110
121
|
get status() {
|
|
111
122
|
return this._status;
|
|
112
123
|
}
|
|
113
124
|
send(message) {
|
|
114
|
-
if (!this.status)
|
|
125
|
+
if (!this.status)
|
|
115
126
|
return;
|
|
116
|
-
}
|
|
117
127
|
this.ws.send(JSON.stringify(message));
|
|
118
128
|
}
|
|
119
129
|
}
|
package/lib/Subscribe.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { TMessage } from "./Core";
|
|
2
|
-
import { Events } from "easy-event-emitter";
|
|
1
|
+
import { TMessage, TListenerEvents, TListenerCallback } from "./Core";
|
|
2
|
+
import { Event, Events } from "easy-event-emitter";
|
|
3
3
|
type TReturn = {
|
|
4
4
|
remove: () => void;
|
|
5
5
|
};
|
|
@@ -19,5 +19,6 @@ declare class Subscribe {
|
|
|
19
19
|
private init;
|
|
20
20
|
bind<T>(event: string, callback: (data: T) => void): TReturn;
|
|
21
21
|
remove(): void;
|
|
22
|
+
addListener(event: TListenerEvents, callback: TListenerCallback): Event | undefined;
|
|
22
23
|
}
|
|
23
24
|
export default Subscribe;
|
package/lib/Subscribe.js
CHANGED
package/lib/index.d.ts
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
|
-
import
|
|
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 {
|
|
4
5
|
constructor(config: IConfig);
|
|
5
6
|
subscribe(channel: string): Subscribe;
|
|
6
7
|
trigger<T>(channel: string, event: string, message: T, permission?: TPermissions): void;
|
|
8
|
+
addListener(event: TSocketEvents, callback: TListenerCallback): Event | undefined;
|
|
7
9
|
}
|
|
10
|
+
export type { Subscribe };
|
|
8
11
|
export default Larasopp;
|
package/lib/index.js
CHANGED
|
@@ -1,9 +1,32 @@
|
|
|
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 =
|
|
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) {
|
|
@@ -27,5 +50,10 @@ class Larasopp extends Core_1.default {
|
|
|
27
50
|
type: permission
|
|
28
51
|
});
|
|
29
52
|
}
|
|
53
|
+
addListener(event, callback) {
|
|
54
|
+
if (!Core_1.SocketEvents.includes(event))
|
|
55
|
+
return;
|
|
56
|
+
return this.events.addListener(event, callback);
|
|
57
|
+
}
|
|
30
58
|
}
|
|
31
59
|
exports.default = Larasopp;
|
package/package.json
CHANGED
package/src/Core.ts
CHANGED
|
@@ -2,6 +2,14 @@ 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
|
+
|
|
8
|
+
export const ListenerEvents = ['subscribe', 'unsubscribe'] as const;
|
|
9
|
+
export type TListenerEvents = typeof ListenerEvents[number];
|
|
10
|
+
|
|
11
|
+
export type TListenerCallback = (data:{channel: string}) => void;
|
|
12
|
+
|
|
5
13
|
export type TPermissions = 'public' | 'protected' | 'private';
|
|
6
14
|
|
|
7
15
|
export type TMessage<T> = {
|
|
@@ -122,19 +130,25 @@ abstract class Core {
|
|
|
122
130
|
private onMessage(e: any): void {
|
|
123
131
|
if (this.isJsonString(e.data)) {
|
|
124
132
|
const json = JSON.parse(e.data);
|
|
125
|
-
|
|
133
|
+
this.emitListener(json.channel, json.message);
|
|
126
134
|
this.events.emit(json.channel + ':' + json.event, json.message);
|
|
127
135
|
}
|
|
128
136
|
}
|
|
129
137
|
|
|
138
|
+
private emitListener(method: TListenerEvents, channel: string): void {
|
|
139
|
+
if (ListenerEvents.includes(method)) {
|
|
140
|
+
this.events.emit(method + ':' + channel, {
|
|
141
|
+
channel
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
130
146
|
public get status(): boolean {
|
|
131
147
|
return this._status;
|
|
132
148
|
}
|
|
133
149
|
|
|
134
150
|
protected send<T>(message: TMessage<T>) {
|
|
135
|
-
if (!this.status)
|
|
136
|
-
return;
|
|
137
|
-
}
|
|
151
|
+
if (!this.status) return;
|
|
138
152
|
this.ws!.send(JSON.stringify(message));
|
|
139
153
|
}
|
|
140
154
|
}
|
package/src/Subscribe.ts
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
import {
|
|
2
|
-
TMessage
|
|
2
|
+
TMessage,
|
|
3
|
+
ListenerEvents,
|
|
4
|
+
TListenerEvents,
|
|
5
|
+
TListenerCallback
|
|
3
6
|
} from "./Core";
|
|
4
7
|
import {
|
|
8
|
+
Event,
|
|
5
9
|
Events
|
|
6
10
|
} from "easy-event-emitter";
|
|
7
11
|
|
|
@@ -67,6 +71,10 @@ class Subscribe {
|
|
|
67
71
|
unsubscribe: this.channel
|
|
68
72
|
});
|
|
69
73
|
}
|
|
74
|
+
|
|
75
|
+
public addListener(event: TListenerEvents, callback: TListenerCallback): Event | undefined {
|
|
76
|
+
return this.events.addListener(event + ':' + this.channel, callback);
|
|
77
|
+
}
|
|
70
78
|
}
|
|
71
79
|
|
|
72
80
|
export default Subscribe;
|
package/src/index.ts
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
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
|
|
|
@@ -30,6 +36,15 @@ class Larasopp extends Core {
|
|
|
30
36
|
type: permission
|
|
31
37
|
});
|
|
32
38
|
}
|
|
39
|
+
|
|
40
|
+
public addListener(event: TSocketEvents, callback: TListenerCallback): Event | undefined {
|
|
41
|
+
if (!SocketEvents.includes(event)) return;
|
|
42
|
+
return this.events.addListener(event, callback);
|
|
43
|
+
}
|
|
33
44
|
}
|
|
34
45
|
|
|
46
|
+
export type {
|
|
47
|
+
Subscribe
|
|
48
|
+
};
|
|
49
|
+
|
|
35
50
|
export default Larasopp;
|