@eleven-am/pondsocket 0.1.7 → 0.1.9

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.
@@ -1,34 +1,28 @@
1
- import {Subscription} from "rxjs";
2
- import {PondAssigns, PondMessage, PondPresence} from "../pondSocket";
1
+ import {ClientMessage, PondAssigns, PondMessage, PondPresence, ServerMessage} from "../pondSocket";
2
+ import {Broadcast, Subscription} from "../pondBase";
3
3
 
4
4
  export declare type ChannelParams = PondAssigns;
5
5
 
6
6
  export declare class Channel {
7
- readonly channel: string;
8
7
 
9
- get isActive(): boolean;
8
+ constructor(name: string, receiver: Broadcast<ServerMessage, void>, broadcaster: Broadcast<ClientMessage, void>, params?: ChannelParams);
10
9
 
11
10
  /**
12
11
  * @desc Connects to the channel.
13
12
  */
14
- join(): this;
13
+ join(): void;
15
14
 
16
15
  /**
17
16
  * @desc Disconnects from the channel.
18
17
  */
19
18
  leave(): void;
20
19
 
21
- /**
22
- * @desc Monitors the presence state of the channel.
23
- * @param callback - The callback to call when the presence state changes.
24
- */
25
- onPresenceUpdate(callback: (presence: PondPresence[]) => void): Subscription;
26
-
27
20
  /**
28
21
  * @desc Monitors the channel for messages.
22
+ * @param event - The event to monitor.
29
23
  * @param callback - The callback to call when a message is received.
30
24
  */
31
- onMessage(callback: (event: string, message: PondMessage) => void): Subscription;
25
+ onMessage(event: string, callback: (message: PondMessage) => void): Subscription;
32
26
 
33
27
  /**
34
28
  * @desc Broadcasts a message to the channel, including yourself.
@@ -44,6 +38,14 @@ export declare class Channel {
44
38
  */
45
39
  broadcastFrom(event: string, payload: PondMessage): void;
46
40
 
41
+ /**
42
+ * @desc Sends a message to specific clients in the channel.
43
+ * @param event - The event to send.
44
+ * @param payload - The message to send.
45
+ * @param recipient - The clients to send the message to.
46
+ */
47
+ sendMessage(event: string, payload: PondMessage, recipient: string[]): void;
48
+
47
49
  /**
48
50
  * @desc Updates the presence state of the current client in the channel.
49
51
  * @param presence - The presence state to update.
@@ -51,16 +53,14 @@ export declare class Channel {
51
53
  updatePresence(presence: PondPresence): void;
52
54
 
53
55
  /**
54
- * @desc Sends a message to specific clients in the channel.
55
- * @param event - The event to send.
56
- * @param payload - The message to send.
57
- * @param recipient - The clients to send the message to.
56
+ * @desc Monitors the presence state of the channel.
57
+ * @param callback - The callback to call when the presence state changes.
58
58
  */
59
- sendMessage(event: string, payload: PondMessage, recipient: string[]): void;
59
+ onPresence(callback: (change: PondPresence | null, presence: PondPresence[]) => void): Subscription;
60
60
 
61
61
  /**
62
- * @desc Listens for the connections state of the channel.
62
+ * @desc Monitors the connection state of the channel.
63
63
  * @param callback - The callback to call when the connection state changes.
64
64
  */
65
- onConnectionChange(callback: (connected: boolean) => void): import("../pondBase").Subscription;
65
+ onConnectionChange(callback: (connected: boolean) => void): Subscription;
66
66
  }
@@ -1,77 +1,72 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Channel = void 0;
4
- var rxjs_1 = require("rxjs");
5
- var operators_1 = require("rxjs/operators");
6
4
  var pondSocket_1 = require("../pondSocket");
7
5
  var pondBase_1 = require("../pondBase");
8
6
  var Channel = /** @class */ (function () {
9
- function Channel(channel, params, socket) {
10
- this._subscriptions = [];
11
- this._presenceSubject = new rxjs_1.Subject();
12
- this.channel = channel;
13
- this._params = params;
14
- this._socket = socket;
15
- this._subject = new rxjs_1.Subject();
16
- this._connectedSubject = new pondBase_1.Subject(false);
7
+ function Channel(name, receiver, broadcaster, params) {
8
+ var _this = this;
9
+ this._name = name;
10
+ this._joinParams = params || {};
11
+ this._broadcaster = broadcaster;
12
+ this._connection = new pondBase_1.Subject(false);
13
+ this._receiver = new pondBase_1.Broadcast();
14
+ this._subscription = receiver.subscribe(function (data) {
15
+ if (data.channelName === name) {
16
+ _this._receiver.publish(data);
17
+ _this._connection.publish(true);
18
+ }
19
+ });
20
+ this._presence = new pondBase_1.Subject({
21
+ change: null,
22
+ presence: []
23
+ });
17
24
  }
18
- Object.defineProperty(Channel.prototype, "isActive", {
19
- get: function () {
20
- return this._connectedSubject.value;
21
- },
22
- enumerable: false,
23
- configurable: true
24
- });
25
25
  /**
26
26
  * @desc Connects to the channel.
27
27
  */
28
28
  Channel.prototype.join = function () {
29
29
  var _this = this;
30
- if (this._connectedSubject.value)
31
- return this;
32
- var observable = this._init();
33
- var subscription = observable
34
- .subscribe(function (message) {
35
- _this._connectedSubject.publish(true);
36
- if (message.action === "PRESENCE")
37
- _this._presenceSubject.next(message.payload.presence);
38
- else if (message.action === "MESSAGE")
39
- _this._subject.next(message);
40
- else if (message.event === "KICKED_FROM_CHANNEL")
41
- _this.leave();
30
+ var joinMessage = {
31
+ action: pondSocket_1.ClientActions.JOIN_CHANNEL,
32
+ channelName: this._name,
33
+ event: pondSocket_1.ClientActions.JOIN_CHANNEL,
34
+ payload: this._joinParams
35
+ };
36
+ this._receiver.subscribe(function (data) {
37
+ if (data.action === pondSocket_1.ServerActions.PRESENCE) {
38
+ _this._presence.publish(data.payload);
39
+ }
42
40
  });
43
- this._subscriptions.push(subscription);
44
- return this;
41
+ this._broadcaster.publish(joinMessage);
45
42
  };
46
43
  /**
47
44
  * @desc Disconnects from the channel.
48
45
  */
49
46
  Channel.prototype.leave = function () {
50
- void this._connectedSubject.publish(false);
51
- this._presenceSubject.complete();
52
- this._subscriptions.forEach(function (subscription) { return subscription.unsubscribe(); });
53
- this._subscriptions = [];
54
- this._subject.complete();
55
- };
56
- /**
57
- * @desc Monitors the presence state of the channel.
58
- * @param callback - The callback to call when the presence state changes.
59
- */
60
- Channel.prototype.onPresenceUpdate = function (callback) {
61
- var sub = this._presenceSubject.subscribe(callback);
62
- this._subscriptions.push(sub);
63
- return sub;
47
+ var leaveMessage = {
48
+ action: pondSocket_1.ClientActions.LEAVE_CHANNEL,
49
+ channelName: this._name,
50
+ event: pondSocket_1.ClientActions.LEAVE_CHANNEL,
51
+ payload: {}
52
+ };
53
+ this._broadcaster.publish(leaveMessage);
54
+ this._connection.publish(false);
55
+ this._subscription.unsubscribe();
56
+ this._connection.clear();
57
+ this._receiver.clear();
58
+ this._presence.clear();
64
59
  };
65
60
  /**
66
61
  * @desc Monitors the channel for messages.
62
+ * @param event - The event to monitor.
67
63
  * @param callback - The callback to call when a message is received.
68
64
  */
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); });
73
- this._subscriptions.push(sub);
74
- return sub;
65
+ Channel.prototype.onMessage = function (event, callback) {
66
+ return this._receiver.subscribe(function (data) {
67
+ if (data.action === pondSocket_1.ServerActions.MESSAGE && data.event === event)
68
+ callback(data.payload);
69
+ });
75
70
  };
76
71
  /**
77
72
  * @desc Broadcasts a message to the channel, including yourself.
@@ -80,12 +75,9 @@ var Channel = /** @class */ (function () {
80
75
  */
81
76
  Channel.prototype.broadcast = function (event, payload) {
82
77
  var message = {
83
- channelName: this.channel,
84
- payload: payload,
85
- event: event,
86
- action: pondSocket_1.ClientActions.BROADCAST
78
+ action: pondSocket_1.ClientActions.BROADCAST, channelName: this._name, payload: payload, event: event
87
79
  };
88
- this._socket.next(message);
80
+ this._broadcaster.publish(message);
89
81
  };
90
82
  /**
91
83
  * @desc Broadcasts a message to every other client in the channel except yourself.
@@ -94,24 +86,9 @@ var Channel = /** @class */ (function () {
94
86
  */
95
87
  Channel.prototype.broadcastFrom = function (event, payload) {
96
88
  var message = {
97
- channelName: this.channel,
98
- payload: payload,
99
- event: event,
100
- action: pondSocket_1.ClientActions.BROADCAST_FROM
89
+ action: pondSocket_1.ClientActions.BROADCAST_FROM, channelName: this._name, payload: payload, event: event
101
90
  };
102
- this._socket.next(message);
103
- };
104
- /**
105
- * @desc Updates the presence state of the current client in the channel.
106
- * @param presence - The presence state to update.
107
- */
108
- Channel.prototype.updatePresence = function (presence) {
109
- this._socket.next({
110
- action: pondSocket_1.ClientActions.UPDATE_PRESENCE,
111
- channelName: this.channel,
112
- event: "PRESENCE",
113
- payload: presence
114
- });
91
+ this._broadcaster.publish(message);
115
92
  };
116
93
  /**
117
94
  * @desc Sends a message to specific clients in the channel.
@@ -120,43 +97,45 @@ var Channel = /** @class */ (function () {
120
97
  * @param recipient - The clients to send the message to.
121
98
  */
122
99
  Channel.prototype.sendMessage = function (event, payload, recipient) {
123
- var addresses = Array.isArray(recipient) ? recipient : [recipient];
124
100
  var message = {
125
- channelName: this.channel,
101
+ action: pondSocket_1.ClientActions.SEND_MESSAGE_TO_USER,
102
+ channelName: this._name,
126
103
  payload: payload,
127
104
  event: event,
128
- addresses: addresses,
129
- action: pondSocket_1.ClientActions.SEND_MESSAGE_TO_USER
105
+ addresses: recipient
130
106
  };
131
- this._socket.next(message);
107
+ this._broadcaster.publish(message);
132
108
  };
133
109
  /**
134
- * @desc Listens for the connections state of the channel.
135
- * @param callback - The callback to call when the connection state changes.
110
+ * @desc Updates the presence state of the current client in the channel.
111
+ * @param presence - The presence state to update.
136
112
  */
137
- Channel.prototype.onConnectionChange = function (callback) {
138
- var sub = this._connectedSubject.subscribe(callback);
139
- this._subscriptions.push(sub);
140
- return sub;
113
+ Channel.prototype.updatePresence = function (presence) {
114
+ var message = {
115
+ action: pondSocket_1.ClientActions.UPDATE_PRESENCE,
116
+ channelName: this._name,
117
+ payload: presence,
118
+ event: pondSocket_1.ClientActions.UPDATE_PRESENCE
119
+ };
120
+ this._broadcaster.publish(message);
141
121
  };
142
122
  /**
143
- * @desc Initializes the channel.
144
- * @private
123
+ * @desc Monitors the presence state of the channel.
124
+ * @param callback - The callback to call when the presence state changes.
145
125
  */
146
- Channel.prototype._init = function () {
147
- var _this = this;
148
- var observable = this._socket.multiplex(function () { return ({
149
- action: "JOIN_CHANNEL",
150
- channelName: _this.channel,
151
- event: "JOIN_CHANNEL",
152
- payload: _this._params
153
- }); }, function () { return ({
154
- action: "LEAVE_CHANNEL",
155
- channelName: _this.channel,
156
- event: "LEAVE_CHANNEL",
157
- payload: _this._params
158
- }); }, function (message) { return message.channelName === _this.channel; });
159
- return observable;
126
+ Channel.prototype.onPresence = function (callback) {
127
+ return this._presence.subscribe(function (data) {
128
+ callback(data.change, data.presence);
129
+ });
130
+ };
131
+ /**
132
+ * @desc Monitors the connection state of the channel.
133
+ * @param callback - The callback to call when the connection state changes.
134
+ */
135
+ Channel.prototype.onConnectionChange = function (callback) {
136
+ return this._connection.subscribe(function (data) {
137
+ callback(data);
138
+ });
160
139
  };
161
140
  return Channel;
162
141
  }());
@@ -1,5 +1,5 @@
1
1
  import {Channel, ChannelParams} from "./channel";
2
- import {default_t} from "../pondBase";
2
+ import {PondMessage} from "../pondSocket";
3
3
 
4
4
  declare type PondParams = {
5
5
  [key: string]: string;
@@ -11,32 +11,31 @@ export declare class PondClient {
11
11
  constructor(endpoint: string, params?: PondParams);
12
12
 
13
13
  /**
14
- * @desc Connects to the server and returns the socket.
14
+ * @desc Returns the current state of the socket.
15
15
  */
16
- connect(): this | undefined;
16
+ getState(): PondState;
17
17
 
18
18
  /**
19
- * @desc Returns the current state of the socket.
19
+ * @desc Connects to the server and returns the socket.
20
20
  */
21
- getState(): PondState;
21
+ connect(backoff?: number): void;
22
22
 
23
23
  /**
24
- * @desc Creates a channel with the given name and params.
25
- * @param channel - The name of the channel.
26
- * @param params - The params to send to the server.
24
+ * @desc Disconnects the socket from the server.
27
25
  */
28
- createChannel(channel: string, params?: ChannelParams): Channel;
26
+ disconnect(): void;
29
27
 
30
28
  /**
31
29
  * @desc An event that is triggered when the socket receives a message.
30
+ * @param event - The event to subscribe to.
32
31
  * @param callback - The callback to be called when the event is triggered.
33
32
  */
34
- onMessage(callback: (event: string, message: default_t) => void): void;
33
+ onMessage(event: string, callback: (message: PondMessage) => void): import("../pondBase").Subscription;
35
34
 
36
35
  /**
37
- * @desc Disconnects the socket from the server.
36
+ * @desc Creates a channel with the given name and params.
37
+ * @param name - The name of the channel.
38
+ * @param params - The params to send to the server.
38
39
  */
39
- disconnect(): void;
40
+ createChannel(name: string, params?: ChannelParams): Channel;
40
41
  }
41
-
42
- export {};
@@ -1,47 +1,11 @@
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
- };
18
2
  Object.defineProperty(exports, "__esModule", { value: true });
19
3
  exports.PondClient = void 0;
20
- var webSocket_1 = require("rxjs/webSocket");
21
- var rxjs_1 = require("rxjs");
22
4
  var channel_1 = require("./channel");
23
- var operators_1 = require("rxjs/operators");
24
5
  var pondBase_1 = require("../pondBase");
6
+ var pondSocket_1 = require("../pondSocket");
25
7
  var PondClient = /** @class */ (function () {
26
8
  function PondClient(endpoint, params) {
27
- this.socketState = "CLOSED";
28
- /**
29
- * @desc A retry strategy for the socket.
30
- * @param maxTries - The maximum number of retries.
31
- * @param ms - The number of milliseconds to wait before retrying.
32
- */
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"))
41
- .pipe((0, rxjs_1.materialize)(), (0, rxjs_1.delay)(1000), (0, rxjs_1.dematerialize)());
42
- return (0, rxjs_1.concat)(observableForRetries, observableForFailure);
43
- }));
44
- };
45
9
  var address;
46
10
  try {
47
11
  address = new URL(endpoint);
@@ -56,76 +20,81 @@ var PondClient = /** @class */ (function () {
56
20
  if (address.protocol !== "wss:" && address.protocol !== "ws:")
57
21
  address.protocol = protocol;
58
22
  this.address = address;
59
- this.channels = new pondBase_1.PondBase();
23
+ this._socketState = "CLOSED";
24
+ this._channels = {};
25
+ this._broadcaster = new pondBase_1.Broadcast();
26
+ this._receiver = new pondBase_1.Broadcast();
60
27
  }
61
- /**
62
- * @desc Connects to the server and returns the socket.
63
- */
64
- PondClient.prototype.connect = function () {
65
- var _this = this;
66
- if (this.socketState !== "CLOSED")
67
- return;
68
- this.socketState = "CONNECTING";
69
- var socket = (0, webSocket_1.webSocket)({
70
- url: this.address.toString(),
71
- openObserver: {
72
- next: function () {
73
- _this.socketState = "OPEN";
74
- }
75
- },
76
- closeObserver: {
77
- next: function () {
78
- _this.socketState = "CLOSED";
79
- }
80
- },
81
- closingObserver: {
82
- next: function () {
83
- _this.socketState = "CLOSING";
84
- }
85
- }
86
- });
87
- this.socket = socket;
88
- this.subscription = socket.pipe(this._retryStrategy(100, 1000)).subscribe();
89
- return this;
90
- };
91
28
  /**
92
29
  * @desc Returns the current state of the socket.
93
30
  */
94
31
  PondClient.prototype.getState = function () {
95
- return this.socketState;
32
+ return this._socketState;
96
33
  };
97
34
  /**
98
- * @desc Creates a channel with the given name and params.
99
- * @param channel - The name of the channel.
100
- * @param params - The params to send to the server.
35
+ * @desc Connects to the server and returns the socket.
101
36
  */
102
- PondClient.prototype.createChannel = function (channel, params) {
37
+ PondClient.prototype.connect = function (backoff) {
103
38
  var _this = this;
104
- return this.channels.getOrCreate(channel, function () {
105
- return new channel_1.Channel(channel, params || {}, _this.socket);
106
- }).doc;
39
+ if (backoff === void 0) { backoff = 1; }
40
+ var socket = new WebSocket(this.address.toString());
41
+ var sub = this._receiver.subscribe(function (message) {
42
+ socket.send(JSON.stringify(message));
43
+ });
44
+ socket.onopen = function () {
45
+ _this._socketState = "OPEN";
46
+ };
47
+ socket.onclose = function () {
48
+ _this._socketState = "CLOSED";
49
+ sub.unsubscribe();
50
+ };
51
+ socket.onmessage = function (message) {
52
+ var data = JSON.parse(message.data);
53
+ _this._broadcaster.publish(data);
54
+ };
55
+ socket.onerror = function () {
56
+ _this._socketState = "CLOSED";
57
+ sub.unsubscribe();
58
+ setTimeout(function () {
59
+ _this.connect(backoff * 2);
60
+ }, backoff * 1000);
61
+ };
62
+ this._socket = socket;
63
+ };
64
+ /**
65
+ * @desc Disconnects the socket from the server.
66
+ */
67
+ PondClient.prototype.disconnect = function () {
68
+ var _a;
69
+ Object.values(this._channels).forEach(function (channel) { return channel.leave(); });
70
+ this._socketState = "CLOSED";
71
+ this._broadcaster.clear();
72
+ this._receiver.clear();
73
+ (_a = this._socket) === null || _a === void 0 ? void 0 : _a.close();
74
+ this._channels = {};
107
75
  };
108
76
  /**
109
77
  * @desc An event that is triggered when the socket receives a message.
78
+ * @param event - The event to subscribe to.
110
79
  * @param callback - The callback to be called when the event is triggered.
111
80
  */
112
- PondClient.prototype.onMessage = function (callback) {
113
- var _a;
114
- (_a = this.socket) === null || _a === void 0 ? void 0 : _a.subscribe(function (data) {
115
- if (data.event)
116
- callback(data.event, data.payload);
81
+ PondClient.prototype.onMessage = function (event, callback) {
82
+ return this._broadcaster.subscribe(function (data) {
83
+ if (data.action === pondSocket_1.ServerActions.MESSAGE && data.event === event)
84
+ callback(data.payload);
117
85
  });
118
86
  };
119
87
  /**
120
- * @desc Disconnects the socket from the server.
88
+ * @desc Creates a channel with the given name and params.
89
+ * @param name - The name of the channel.
90
+ * @param params - The params to send to the server.
121
91
  */
122
- PondClient.prototype.disconnect = function () {
123
- var _a, _b, _c;
124
- (_a = this.socket) === null || _a === void 0 ? void 0 : _a.complete();
125
- (_b = this.socket) === null || _b === void 0 ? void 0 : _b.unsubscribe();
126
- (_c = this.subscription) === null || _c === void 0 ? void 0 : _c.unsubscribe();
127
- this.socket = undefined;
128
- this.channels = new pondBase_1.PondBase();
92
+ PondClient.prototype.createChannel = function (name, params) {
93
+ if (this._channels[name])
94
+ return this._channels[name];
95
+ var channel = new channel_1.Channel(name, this._broadcaster, this._receiver, params);
96
+ this._channels[name] = channel;
97
+ return channel;
129
98
  };
130
99
  return PondClient;
131
100
  }());
@@ -1,4 +1,4 @@
1
- import {BaseClass, default_t, PondDocument, Subscription} from "../pondBase";
1
+ import {default_t, Subscription} from "../pondBase";
2
2
  import {NewUser, PondAssigns, PondChannelData, PondMessage, PondPresence, ServerMessage} from "./types";
3
3
  import {PondSenders, ServerActions} from "./enums";
4
4
  import {ChannelHandler} from "./channelMiddleWare";
@@ -10,12 +10,7 @@ export interface ChannelInfo {
10
10
  assigns: PondPresence[];
11
11
  }
12
12
 
13
- export interface PondUser {
14
- presence: PondDocument<PondPresence>;
15
- assigns: PondDocument<PondAssigns>;
16
- }
17
-
18
- export declare class Channel extends BaseClass {
13
+ export declare class Channel {
19
14
  readonly name: string;
20
15
 
21
16
  /**