@eleven-am/pondsocket 0.1.4 → 0.1.6
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/package.json +1 -1
- package/pondClient/channel.js +68 -57
- package/pondClient/socket.d.ts +1 -1
- package/pondClient/socket.js +59 -43
- package/pondSocket/pondResponse.d.ts +0 -1
package/package.json
CHANGED
package/pondClient/channel.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Channel = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
4
|
+
var rxjs_1 = require("rxjs");
|
|
5
|
+
var operators_1 = require("rxjs/operators");
|
|
6
|
+
var pondSocket_1 = require("../pondSocket");
|
|
7
|
+
var pondBase_1 = require("../pondBase");
|
|
8
|
+
var Channel = /** @class */ (function () {
|
|
9
|
+
function Channel(channel, params, socket) {
|
|
10
10
|
this._subscriptions = [];
|
|
11
11
|
this._presenceSubject = new rxjs_1.Subject();
|
|
12
12
|
this.channel = channel;
|
|
@@ -15,138 +15,149 @@ class Channel {
|
|
|
15
15
|
this._subject = new rxjs_1.Subject();
|
|
16
16
|
this._connectedSubject = new pondBase_1.Subject(false);
|
|
17
17
|
}
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
18
|
+
Object.defineProperty(Channel.prototype, "isActive", {
|
|
19
|
+
get: function () {
|
|
20
|
+
return this._connectedSubject.value;
|
|
21
|
+
},
|
|
22
|
+
enumerable: false,
|
|
23
|
+
configurable: true
|
|
24
|
+
});
|
|
21
25
|
/**
|
|
22
26
|
* @desc Connects to the channel.
|
|
23
27
|
*/
|
|
24
|
-
join() {
|
|
28
|
+
Channel.prototype.join = function () {
|
|
29
|
+
var _this = this;
|
|
25
30
|
if (this._connectedSubject.value)
|
|
26
31
|
return this;
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
.subscribe(message
|
|
30
|
-
|
|
32
|
+
var observable = this._init();
|
|
33
|
+
var subscription = observable
|
|
34
|
+
.subscribe(function (message) {
|
|
35
|
+
_this._connectedSubject.publish(true);
|
|
31
36
|
if (message.action === "PRESENCE")
|
|
32
|
-
|
|
37
|
+
_this._presenceSubject.next(message.payload.presence);
|
|
33
38
|
else if (message.action === "MESSAGE")
|
|
34
|
-
|
|
39
|
+
_this._subject.next(message);
|
|
35
40
|
else if (message.event === "KICKED_FROM_CHANNEL")
|
|
36
|
-
|
|
41
|
+
_this.leave();
|
|
37
42
|
});
|
|
38
43
|
this._subscriptions.push(subscription);
|
|
39
44
|
return this;
|
|
40
|
-
}
|
|
45
|
+
};
|
|
41
46
|
/**
|
|
42
47
|
* @desc Disconnects from the channel.
|
|
43
48
|
*/
|
|
44
|
-
leave() {
|
|
49
|
+
Channel.prototype.leave = function () {
|
|
45
50
|
void this._connectedSubject.publish(false);
|
|
46
51
|
this._presenceSubject.complete();
|
|
47
|
-
this._subscriptions.forEach(subscription
|
|
52
|
+
this._subscriptions.forEach(function (subscription) { return subscription.unsubscribe(); });
|
|
48
53
|
this._subscriptions = [];
|
|
49
54
|
this._subject.complete();
|
|
50
|
-
}
|
|
55
|
+
};
|
|
51
56
|
/**
|
|
52
57
|
* @desc Monitors the presence state of the channel.
|
|
53
58
|
* @param callback - The callback to call when the presence state changes.
|
|
54
59
|
*/
|
|
55
|
-
onPresenceUpdate(callback) {
|
|
56
|
-
|
|
60
|
+
Channel.prototype.onPresenceUpdate = function (callback) {
|
|
61
|
+
var sub = this._presenceSubject.subscribe(callback);
|
|
57
62
|
this._subscriptions.push(sub);
|
|
58
63
|
return sub;
|
|
59
|
-
}
|
|
64
|
+
};
|
|
60
65
|
/**
|
|
61
66
|
* @desc Monitors the channel for messages.
|
|
62
67
|
* @param callback - The callback to call when a message is received.
|
|
63
68
|
*/
|
|
64
|
-
onMessage(callback) {
|
|
65
|
-
|
|
66
|
-
.pipe((0, operators_1.filter)((message)
|
|
67
|
-
.subscribe(message
|
|
69
|
+
Channel.prototype.onMessage = function (callback) {
|
|
70
|
+
var sub = this._subject
|
|
71
|
+
.pipe((0, operators_1.filter)(function (message) { return message.action === "MESSAGE"; }))
|
|
72
|
+
.subscribe(function (message) { return callback(message.event, message.payload); });
|
|
68
73
|
this._subscriptions.push(sub);
|
|
69
74
|
return sub;
|
|
70
|
-
}
|
|
75
|
+
};
|
|
71
76
|
/**
|
|
72
77
|
* @desc Broadcasts a message to the channel, including yourself.
|
|
73
78
|
* @param event - The event to send.
|
|
74
79
|
* @param payload - The message to send.
|
|
75
80
|
*/
|
|
76
|
-
broadcast(event, payload) {
|
|
77
|
-
|
|
81
|
+
Channel.prototype.broadcast = function (event, payload) {
|
|
82
|
+
var message = {
|
|
78
83
|
channelName: this.channel,
|
|
79
|
-
payload
|
|
84
|
+
payload: payload,
|
|
85
|
+
event: event,
|
|
80
86
|
action: pondSocket_1.ClientActions.BROADCAST
|
|
81
87
|
};
|
|
82
88
|
this._socket.next(message);
|
|
83
|
-
}
|
|
89
|
+
};
|
|
84
90
|
/**
|
|
85
91
|
* @desc Broadcasts a message to every other client in the channel except yourself.
|
|
86
92
|
* @param event - The event to send.
|
|
87
93
|
* @param payload - The message to send.
|
|
88
94
|
*/
|
|
89
|
-
broadcastFrom(event, payload) {
|
|
90
|
-
|
|
95
|
+
Channel.prototype.broadcastFrom = function (event, payload) {
|
|
96
|
+
var message = {
|
|
91
97
|
channelName: this.channel,
|
|
92
|
-
payload
|
|
98
|
+
payload: payload,
|
|
99
|
+
event: event,
|
|
93
100
|
action: pondSocket_1.ClientActions.BROADCAST_FROM
|
|
94
101
|
};
|
|
95
102
|
this._socket.next(message);
|
|
96
|
-
}
|
|
103
|
+
};
|
|
97
104
|
/**
|
|
98
105
|
* @desc Updates the presence state of the current client in the channel.
|
|
99
106
|
* @param presence - The presence state to update.
|
|
100
107
|
*/
|
|
101
|
-
updatePresence(presence) {
|
|
108
|
+
Channel.prototype.updatePresence = function (presence) {
|
|
102
109
|
this._socket.next({
|
|
103
110
|
action: pondSocket_1.ClientActions.UPDATE_PRESENCE,
|
|
104
111
|
channelName: this.channel,
|
|
105
112
|
event: "PRESENCE",
|
|
106
113
|
payload: presence
|
|
107
114
|
});
|
|
108
|
-
}
|
|
115
|
+
};
|
|
109
116
|
/**
|
|
110
117
|
* @desc Sends a message to specific clients in the channel.
|
|
111
118
|
* @param event - The event to send.
|
|
112
119
|
* @param payload - The message to send.
|
|
113
120
|
* @param recipient - The clients to send the message to.
|
|
114
121
|
*/
|
|
115
|
-
sendMessage(event, payload, recipient) {
|
|
116
|
-
|
|
117
|
-
|
|
122
|
+
Channel.prototype.sendMessage = function (event, payload, recipient) {
|
|
123
|
+
var addresses = Array.isArray(recipient) ? recipient : [recipient];
|
|
124
|
+
var message = {
|
|
118
125
|
channelName: this.channel,
|
|
119
|
-
payload
|
|
126
|
+
payload: payload,
|
|
127
|
+
event: event,
|
|
128
|
+
addresses: addresses,
|
|
120
129
|
action: pondSocket_1.ClientActions.SEND_MESSAGE_TO_USER
|
|
121
130
|
};
|
|
122
131
|
this._socket.next(message);
|
|
123
|
-
}
|
|
132
|
+
};
|
|
124
133
|
/**
|
|
125
134
|
* @desc Listens for the connections state of the channel.
|
|
126
135
|
* @param callback - The callback to call when the connection state changes.
|
|
127
136
|
*/
|
|
128
|
-
onConnectionChange(callback) {
|
|
129
|
-
|
|
137
|
+
Channel.prototype.onConnectionChange = function (callback) {
|
|
138
|
+
var sub = this._connectedSubject.subscribe(callback);
|
|
130
139
|
this._subscriptions.push(sub);
|
|
131
140
|
return sub;
|
|
132
|
-
}
|
|
141
|
+
};
|
|
133
142
|
/**
|
|
134
143
|
* @desc Initializes the channel.
|
|
135
144
|
* @private
|
|
136
145
|
*/
|
|
137
|
-
_init() {
|
|
138
|
-
|
|
146
|
+
Channel.prototype._init = function () {
|
|
147
|
+
var _this = this;
|
|
148
|
+
var observable = this._socket.multiplex(function () { return ({
|
|
139
149
|
action: "JOIN_CHANNEL",
|
|
140
|
-
channelName:
|
|
150
|
+
channelName: _this.channel,
|
|
141
151
|
event: "JOIN_CHANNEL",
|
|
142
|
-
payload:
|
|
143
|
-
}), ()
|
|
152
|
+
payload: _this._params
|
|
153
|
+
}); }, function () { return ({
|
|
144
154
|
action: "LEAVE_CHANNEL",
|
|
145
|
-
channelName:
|
|
155
|
+
channelName: _this.channel,
|
|
146
156
|
event: "LEAVE_CHANNEL",
|
|
147
|
-
payload:
|
|
148
|
-
}), message
|
|
157
|
+
payload: _this._params
|
|
158
|
+
}); }, function (message) { return message.channelName === _this.channel; });
|
|
149
159
|
return observable;
|
|
150
|
-
}
|
|
151
|
-
|
|
160
|
+
};
|
|
161
|
+
return Channel;
|
|
162
|
+
}());
|
|
152
163
|
exports.Channel = Channel;
|
package/pondClient/socket.d.ts
CHANGED
|
@@ -25,7 +25,7 @@ export declare class PondClient {
|
|
|
25
25
|
* @param channel - The name of the channel.
|
|
26
26
|
* @param params - The params to send to the server.
|
|
27
27
|
*/
|
|
28
|
-
createChannel(channel: string, params?: ChannelParams): Channel
|
|
28
|
+
createChannel(channel: string, params?: ChannelParams): Channel;
|
|
29
29
|
|
|
30
30
|
/**
|
|
31
31
|
* @desc An event that is triggered when the socket receives a message.
|
package/pondClient/socket.js
CHANGED
|
@@ -1,29 +1,48 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __read = (this && this.__read) || function (o, n) {
|
|
3
|
+
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
4
|
+
if (!m) return o;
|
|
5
|
+
var i = m.call(o), r, ar = [], e;
|
|
6
|
+
try {
|
|
7
|
+
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
8
|
+
}
|
|
9
|
+
catch (error) { e = { error: error }; }
|
|
10
|
+
finally {
|
|
11
|
+
try {
|
|
12
|
+
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
13
|
+
}
|
|
14
|
+
finally { if (e) throw e.error; }
|
|
15
|
+
}
|
|
16
|
+
return ar;
|
|
17
|
+
};
|
|
2
18
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
19
|
exports.PondClient = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
20
|
+
var webSocket_1 = require("rxjs/webSocket");
|
|
21
|
+
var rxjs_1 = require("rxjs");
|
|
22
|
+
var channel_1 = require("./channel");
|
|
23
|
+
var operators_1 = require("rxjs/operators");
|
|
24
|
+
var pondBase_1 = require("../pondBase");
|
|
25
|
+
var PondClient = /** @class */ (function () {
|
|
26
|
+
function PondClient(endpoint, params) {
|
|
11
27
|
this.socketState = "CLOSED";
|
|
12
28
|
/**
|
|
13
29
|
* @desc A retry strategy for the socket.
|
|
14
30
|
* @param maxTries - The maximum number of retries.
|
|
15
31
|
* @param ms - The number of milliseconds to wait before retrying.
|
|
16
32
|
*/
|
|
17
|
-
this._retryStrategy = (maxTries, ms)
|
|
18
|
-
return (0, rxjs_1.pipe)((0, operators_1.retryWhen)(attempts
|
|
19
|
-
|
|
20
|
-
.pipe((0, operators_1.map)(
|
|
21
|
-
|
|
33
|
+
this._retryStrategy = function (maxTries, ms) {
|
|
34
|
+
return (0, rxjs_1.pipe)((0, operators_1.retryWhen)(function (attempts) {
|
|
35
|
+
var observableForRetries = (0, rxjs_1.zip)((0, rxjs_1.range)(1, maxTries), attempts)
|
|
36
|
+
.pipe((0, operators_1.map)(function (_a) {
|
|
37
|
+
var _b = __read(_a, 1), elemFromRange = _b[0];
|
|
38
|
+
return elemFromRange;
|
|
39
|
+
}), (0, operators_1.map)(function (i) { return i * i; }), (0, rxjs_1.switchMap)(function (i) { return (0, rxjs_1.timer)(i * ms); }));
|
|
40
|
+
var observableForFailure = (0, rxjs_1.throwError)(new Error("Could not connect to server"))
|
|
22
41
|
.pipe((0, rxjs_1.materialize)(), (0, rxjs_1.delay)(1000), (0, rxjs_1.dematerialize)());
|
|
23
42
|
return (0, rxjs_1.concat)(observableForRetries, observableForFailure);
|
|
24
43
|
}));
|
|
25
44
|
};
|
|
26
|
-
|
|
45
|
+
var address;
|
|
27
46
|
try {
|
|
28
47
|
address = new URL(endpoint);
|
|
29
48
|
}
|
|
@@ -31,9 +50,9 @@ class PondClient {
|
|
|
31
50
|
address = new URL(window.location.toString());
|
|
32
51
|
address.pathname = endpoint;
|
|
33
52
|
}
|
|
34
|
-
|
|
53
|
+
var query = new URLSearchParams(params);
|
|
35
54
|
address.search = query.toString();
|
|
36
|
-
|
|
55
|
+
var protocol = address.protocol === "https:" ? "wss:" : "ws:";
|
|
37
56
|
if (address.protocol !== "wss:" && address.protocol !== "ws:")
|
|
38
57
|
address.protocol = protocol;
|
|
39
58
|
this.address = address;
|
|
@@ -42,75 +61,72 @@ class PondClient {
|
|
|
42
61
|
/**
|
|
43
62
|
* @desc Connects to the server and returns the socket.
|
|
44
63
|
*/
|
|
45
|
-
connect() {
|
|
64
|
+
PondClient.prototype.connect = function () {
|
|
65
|
+
var _this = this;
|
|
46
66
|
if (this.socketState !== "CLOSED")
|
|
47
67
|
return;
|
|
48
68
|
this.socketState = "CONNECTING";
|
|
49
|
-
|
|
69
|
+
var socket = (0, webSocket_1.webSocket)({
|
|
50
70
|
url: this.address.toString(),
|
|
51
71
|
openObserver: {
|
|
52
|
-
next: ()
|
|
53
|
-
|
|
72
|
+
next: function () {
|
|
73
|
+
_this.socketState = "OPEN";
|
|
54
74
|
}
|
|
55
75
|
},
|
|
56
76
|
closeObserver: {
|
|
57
|
-
next: ()
|
|
58
|
-
|
|
77
|
+
next: function () {
|
|
78
|
+
_this.socketState = "CLOSED";
|
|
59
79
|
}
|
|
60
80
|
},
|
|
61
81
|
closingObserver: {
|
|
62
|
-
next: ()
|
|
63
|
-
|
|
82
|
+
next: function () {
|
|
83
|
+
_this.socketState = "CLOSING";
|
|
64
84
|
}
|
|
65
85
|
}
|
|
66
86
|
});
|
|
67
87
|
this.socket = socket;
|
|
68
88
|
this.subscription = socket.pipe(this._retryStrategy(100, 1000)).subscribe();
|
|
69
89
|
return this;
|
|
70
|
-
}
|
|
90
|
+
};
|
|
71
91
|
/**
|
|
72
92
|
* @desc Returns the current state of the socket.
|
|
73
93
|
*/
|
|
74
|
-
getState() {
|
|
94
|
+
PondClient.prototype.getState = function () {
|
|
75
95
|
return this.socketState;
|
|
76
|
-
}
|
|
96
|
+
};
|
|
77
97
|
/**
|
|
78
98
|
* @desc Creates a channel with the given name and params.
|
|
79
99
|
* @param channel - The name of the channel.
|
|
80
100
|
* @param params - The params to send to the server.
|
|
81
101
|
*/
|
|
82
|
-
createChannel(channel, params) {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
return
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
this.channels.set(channel, newChannel);
|
|
89
|
-
return newChannel;
|
|
90
|
-
}
|
|
91
|
-
return null;
|
|
92
|
-
}
|
|
102
|
+
PondClient.prototype.createChannel = function (channel, params) {
|
|
103
|
+
var _this = this;
|
|
104
|
+
return this.channels.getOrCreate(channel, function () {
|
|
105
|
+
return new channel_1.Channel(channel, params || {}, _this.socket);
|
|
106
|
+
}).doc;
|
|
107
|
+
};
|
|
93
108
|
/**
|
|
94
109
|
* @desc An event that is triggered when the socket receives a message.
|
|
95
110
|
* @param callback - The callback to be called when the event is triggered.
|
|
96
111
|
*/
|
|
97
|
-
onMessage(callback) {
|
|
112
|
+
PondClient.prototype.onMessage = function (callback) {
|
|
98
113
|
var _a;
|
|
99
|
-
(_a = this.socket) === null || _a === void 0 ? void 0 : _a.subscribe(data
|
|
114
|
+
(_a = this.socket) === null || _a === void 0 ? void 0 : _a.subscribe(function (data) {
|
|
100
115
|
if (data.event)
|
|
101
116
|
callback(data.event, data.payload);
|
|
102
117
|
});
|
|
103
|
-
}
|
|
118
|
+
};
|
|
104
119
|
/**
|
|
105
120
|
* @desc Disconnects the socket from the server.
|
|
106
121
|
*/
|
|
107
|
-
disconnect() {
|
|
122
|
+
PondClient.prototype.disconnect = function () {
|
|
108
123
|
var _a, _b, _c;
|
|
109
124
|
(_a = this.socket) === null || _a === void 0 ? void 0 : _a.complete();
|
|
110
125
|
(_b = this.socket) === null || _b === void 0 ? void 0 : _b.unsubscribe();
|
|
111
126
|
(_c = this.subscription) === null || _c === void 0 ? void 0 : _c.unsubscribe();
|
|
112
127
|
this.socket = undefined;
|
|
113
128
|
this.channels = new pondBase_1.PondBase();
|
|
114
|
-
}
|
|
115
|
-
|
|
129
|
+
};
|
|
130
|
+
return PondClient;
|
|
131
|
+
}());
|
|
116
132
|
exports.PondClient = PondClient;
|