oorja 1.5.0 → 1.6.2

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.
Files changed (46) hide show
  1. package/README.md +10 -4
  2. package/bin/oorja +5 -0
  3. package/lib/commands/signout.js +2 -2
  4. package/lib/commands/teletype/index.js +3 -3
  5. package/lib/lib/config.d.ts +5 -6
  6. package/lib/lib/config.js +12 -7
  7. package/lib/lib/encryption.d.ts +1 -1
  8. package/lib/lib/encryption.js +12 -7
  9. package/lib/lib/index.js +1 -0
  10. package/lib/lib/oorja/index.js +14 -13
  11. package/lib/lib/oorja/preflight.d.ts +1 -1
  12. package/lib/lib/oorja/preflight.js +17 -11
  13. package/lib/lib/surya/index.d.ts +2 -2
  14. package/lib/lib/surya/index.js +15 -13
  15. package/lib/lib/surya/types.d.ts +10 -10
  16. package/lib/lib/surya/vendor/phoenix/ajax.d.ts +8 -0
  17. package/lib/lib/surya/vendor/phoenix/ajax.js +85 -0
  18. package/lib/lib/surya/vendor/phoenix/channel.d.ts +154 -0
  19. package/lib/lib/surya/vendor/phoenix/channel.js +311 -0
  20. package/lib/lib/surya/vendor/phoenix/constants.d.ts +33 -0
  21. package/lib/lib/surya/vendor/phoenix/constants.js +32 -0
  22. package/lib/lib/surya/vendor/phoenix/index.d.ts +199 -0
  23. package/lib/lib/surya/vendor/phoenix/index.js +207 -0
  24. package/lib/lib/surya/vendor/phoenix/longpoll.d.ts +12 -0
  25. package/lib/lib/surya/vendor/phoenix/longpoll.js +129 -0
  26. package/lib/lib/surya/vendor/phoenix/presence.d.ts +44 -0
  27. package/lib/lib/surya/vendor/phoenix/presence.js +155 -0
  28. package/lib/lib/surya/vendor/phoenix/push.d.ts +57 -0
  29. package/lib/lib/surya/vendor/phoenix/push.js +125 -0
  30. package/lib/lib/surya/vendor/phoenix/serializer.d.ts +53 -0
  31. package/lib/lib/surya/vendor/phoenix/serializer.js +102 -0
  32. package/lib/lib/surya/vendor/phoenix/socket.d.ts +222 -0
  33. package/lib/lib/surya/vendor/phoenix/socket.js +544 -0
  34. package/lib/lib/surya/vendor/phoenix/timer.d.ts +25 -0
  35. package/lib/lib/surya/vendor/phoenix/timer.js +43 -0
  36. package/lib/lib/surya/vendor/phoenix/utils.d.ts +1 -0
  37. package/lib/lib/surya/vendor/phoenix/utils.js +15 -0
  38. package/lib/lib/teletype/auxiliary.d.ts +1 -1
  39. package/lib/lib/teletype/auxiliary.js +8 -4
  40. package/lib/lib/teletype/index.d.ts +1 -1
  41. package/lib/lib/teletype/index.js +13 -12
  42. package/lib/lib/utils.js +2 -1
  43. package/oclif.manifest.json +1 -1
  44. package/package.json +10 -9
  45. package/lib/lib/surya/vendor/phoenix.d.ts +0 -486
  46. package/lib/lib/surya/vendor/phoenix.js +0 -1299
@@ -0,0 +1,199 @@
1
+ /**
2
+ * Phoenix Channels JavaScript client
3
+ *
4
+ * ## Socket Connection
5
+ *
6
+ * A single connection is established to the server and
7
+ * channels are multiplexed over the connection.
8
+ * Connect to the server using the `Socket` class:
9
+ *
10
+ * ```javascript
11
+ * let socket = new Socket("/socket", {params: {userToken: "123"}})
12
+ * socket.connect()
13
+ * ```
14
+ *
15
+ * The `Socket` constructor takes the mount point of the socket,
16
+ * the authentication params, as well as options that can be found in
17
+ * the Socket docs, such as configuring the `LongPoll` transport, and
18
+ * heartbeat.
19
+ *
20
+ * ## Channels
21
+ *
22
+ * Channels are isolated, concurrent processes on the server that
23
+ * subscribe to topics and broker events between the client and server.
24
+ * To join a channel, you must provide the topic, and channel params for
25
+ * authorization. Here's an example chat room example where `"new_msg"`
26
+ * events are listened for, messages are pushed to the server, and
27
+ * the channel is joined with ok/error/timeout matches:
28
+ *
29
+ * ```javascript
30
+ * let channel = socket.channel("room:123", {token: roomToken})
31
+ * channel.on("new_msg", msg => console.log("Got message", msg) )
32
+ * $input.onEnter( e => {
33
+ * channel.push("new_msg", {body: e.target.val}, 10000)
34
+ * .receive("ok", (msg) => console.log("created message", msg) )
35
+ * .receive("error", (reasons) => console.log("create failed", reasons) )
36
+ * .receive("timeout", () => console.log("Networking issue...") )
37
+ * })
38
+ *
39
+ * channel.join()
40
+ * .receive("ok", ({messages}) => console.log("catching up", messages) )
41
+ * .receive("error", ({reason}) => console.log("failed join", reason) )
42
+ * .receive("timeout", () => console.log("Networking issue. Still waiting..."))
43
+ *```
44
+ *
45
+ * ## Joining
46
+ *
47
+ * Creating a channel with `socket.channel(topic, params)`, binds the params to
48
+ * `channel.params`, which are sent up on `channel.join()`.
49
+ * Subsequent rejoins will send up the modified params for
50
+ * updating authorization params, or passing up last_message_id information.
51
+ * Successful joins receive an "ok" status, while unsuccessful joins
52
+ * receive "error".
53
+ *
54
+ * With the default serializers and WebSocket transport, JSON text frames are
55
+ * used for pushing a JSON object literal. If an `ArrayBuffer` instance is provided,
56
+ * binary encoding will be used and the message will be sent with the binary
57
+ * opcode.
58
+ *
59
+ * *Note*: binary messages are only supported on the WebSocket transport.
60
+ *
61
+ * ## Duplicate Join Subscriptions
62
+ *
63
+ * While the client may join any number of topics on any number of channels,
64
+ * the client may only hold a single subscription for each unique topic at any
65
+ * given time. When attempting to create a duplicate subscription,
66
+ * the server will close the existing channel, log a warning, and
67
+ * spawn a new channel for the topic. The client will have their
68
+ * `channel.onClose` callbacks fired for the existing channel, and the new
69
+ * channel join will have its receive hooks processed as normal.
70
+ *
71
+ * ## Pushing Messages
72
+ *
73
+ * From the previous example, we can see that pushing messages to the server
74
+ * can be done with `channel.push(eventName, payload)` and we can optionally
75
+ * receive responses from the push. Additionally, we can use
76
+ * `receive("timeout", callback)` to abort waiting for our other `receive` hooks
77
+ * and take action after some period of waiting. The default timeout is 10000ms.
78
+ *
79
+ *
80
+ * ## Socket Hooks
81
+ *
82
+ * Lifecycle events of the multiplexed connection can be hooked into via
83
+ * `socket.onError()` and `socket.onClose()` events, ie:
84
+ *
85
+ * ```javascript
86
+ * socket.onError( () => console.log("there was an error with the connection!") )
87
+ * socket.onClose( () => console.log("the connection dropped") )
88
+ * ```
89
+ *
90
+ *
91
+ * ## Channel Hooks
92
+ *
93
+ * For each joined channel, you can bind to `onError` and `onClose` events
94
+ * to monitor the channel lifecycle, ie:
95
+ *
96
+ * ```javascript
97
+ * channel.onError( () => console.log("there was an error!") )
98
+ * channel.onClose( () => console.log("the channel has gone away gracefully") )
99
+ * ```
100
+ *
101
+ * ### onError hooks
102
+ *
103
+ * `onError` hooks are invoked if the socket connection drops, or the channel
104
+ * crashes on the server. In either case, a channel rejoin is attempted
105
+ * automatically in an exponential backoff manner.
106
+ *
107
+ * ### onClose hooks
108
+ *
109
+ * `onClose` hooks are invoked only in two cases. 1) the channel explicitly
110
+ * closed on the server, or 2). The client explicitly closed, by calling
111
+ * `channel.leave()`
112
+ *
113
+ *
114
+ * ## Presence
115
+ *
116
+ * The `Presence` object provides features for syncing presence information
117
+ * from the server with the client and handling presences joining and leaving.
118
+ *
119
+ * ### Syncing state from the server
120
+ *
121
+ * To sync presence state from the server, first instantiate an object and
122
+ * pass your channel in to track lifecycle events:
123
+ *
124
+ * ```javascript
125
+ * let channel = socket.channel("some:topic")
126
+ * let presence = new Presence(channel)
127
+ * ```
128
+ *
129
+ * Next, use the `presence.onSync` callback to react to state changes
130
+ * from the server. For example, to render the list of users every time
131
+ * the list changes, you could write:
132
+ *
133
+ * ```javascript
134
+ * presence.onSync(() => {
135
+ * myRenderUsersFunction(presence.list())
136
+ * })
137
+ * ```
138
+ *
139
+ * ### Listing Presences
140
+ *
141
+ * `presence.list` is used to return a list of presence information
142
+ * based on the local state of metadata. By default, all presence
143
+ * metadata is returned, but a `listBy` function can be supplied to
144
+ * allow the client to select which metadata to use for a given presence.
145
+ * For example, you may have a user online from different devices with
146
+ * a metadata status of "online", but they have set themselves to "away"
147
+ * on another device. In this case, the app may choose to use the "away"
148
+ * status for what appears on the UI. The example below defines a `listBy`
149
+ * function which prioritizes the first metadata which was registered for
150
+ * each user. This could be the first tab they opened, or the first device
151
+ * they came online from:
152
+ *
153
+ * ```javascript
154
+ * let listBy = (id, {metas: [first, ...rest]}) => {
155
+ * first.count = rest.length + 1 // count of this user's presences
156
+ * first.id = id
157
+ * return first
158
+ * }
159
+ * let onlineUsers = presence.list(listBy)
160
+ * ```
161
+ *
162
+ * ### Handling individual presence join and leave events
163
+ *
164
+ * The `presence.onJoin` and `presence.onLeave` callbacks can be used to
165
+ * react to individual presences joining and leaving the app. For example:
166
+ *
167
+ * ```javascript
168
+ * let presence = new Presence(channel)
169
+ *
170
+ * // detect if user has joined for the 1st time or from another tab/device
171
+ * presence.onJoin((id, current, newPres) => {
172
+ * if(!current){
173
+ * console.log("user has entered for the first time", newPres)
174
+ * } else {
175
+ * console.log("user additional presence", newPres)
176
+ * }
177
+ * })
178
+ *
179
+ * // detect if user has left from all tabs/devices, or is still present
180
+ * presence.onLeave((id, current, leftPres) => {
181
+ * if(current.metas.length === 0){
182
+ * console.log("user has left from all devices", leftPres)
183
+ * } else {
184
+ * console.log("user left from a device", leftPres)
185
+ * }
186
+ * })
187
+ * // receive presence data from server
188
+ * presence.onSync(() => {
189
+ * displayUsers(presence.list())
190
+ * })
191
+ * ```
192
+ * @module phoenix
193
+ */
194
+ import Channel from "./channel";
195
+ import LongPoll from "./longpoll";
196
+ import Presence from "./presence";
197
+ import Serializer from "./serializer";
198
+ import Socket from "./socket";
199
+ export { Channel, LongPoll, Presence, Serializer, Socket };
@@ -0,0 +1,207 @@
1
+ "use strict";
2
+ // @ts-nocheck
3
+ /**
4
+ * Phoenix Channels JavaScript client
5
+ *
6
+ * ## Socket Connection
7
+ *
8
+ * A single connection is established to the server and
9
+ * channels are multiplexed over the connection.
10
+ * Connect to the server using the `Socket` class:
11
+ *
12
+ * ```javascript
13
+ * let socket = new Socket("/socket", {params: {userToken: "123"}})
14
+ * socket.connect()
15
+ * ```
16
+ *
17
+ * The `Socket` constructor takes the mount point of the socket,
18
+ * the authentication params, as well as options that can be found in
19
+ * the Socket docs, such as configuring the `LongPoll` transport, and
20
+ * heartbeat.
21
+ *
22
+ * ## Channels
23
+ *
24
+ * Channels are isolated, concurrent processes on the server that
25
+ * subscribe to topics and broker events between the client and server.
26
+ * To join a channel, you must provide the topic, and channel params for
27
+ * authorization. Here's an example chat room example where `"new_msg"`
28
+ * events are listened for, messages are pushed to the server, and
29
+ * the channel is joined with ok/error/timeout matches:
30
+ *
31
+ * ```javascript
32
+ * let channel = socket.channel("room:123", {token: roomToken})
33
+ * channel.on("new_msg", msg => console.log("Got message", msg) )
34
+ * $input.onEnter( e => {
35
+ * channel.push("new_msg", {body: e.target.val}, 10000)
36
+ * .receive("ok", (msg) => console.log("created message", msg) )
37
+ * .receive("error", (reasons) => console.log("create failed", reasons) )
38
+ * .receive("timeout", () => console.log("Networking issue...") )
39
+ * })
40
+ *
41
+ * channel.join()
42
+ * .receive("ok", ({messages}) => console.log("catching up", messages) )
43
+ * .receive("error", ({reason}) => console.log("failed join", reason) )
44
+ * .receive("timeout", () => console.log("Networking issue. Still waiting..."))
45
+ *```
46
+ *
47
+ * ## Joining
48
+ *
49
+ * Creating a channel with `socket.channel(topic, params)`, binds the params to
50
+ * `channel.params`, which are sent up on `channel.join()`.
51
+ * Subsequent rejoins will send up the modified params for
52
+ * updating authorization params, or passing up last_message_id information.
53
+ * Successful joins receive an "ok" status, while unsuccessful joins
54
+ * receive "error".
55
+ *
56
+ * With the default serializers and WebSocket transport, JSON text frames are
57
+ * used for pushing a JSON object literal. If an `ArrayBuffer` instance is provided,
58
+ * binary encoding will be used and the message will be sent with the binary
59
+ * opcode.
60
+ *
61
+ * *Note*: binary messages are only supported on the WebSocket transport.
62
+ *
63
+ * ## Duplicate Join Subscriptions
64
+ *
65
+ * While the client may join any number of topics on any number of channels,
66
+ * the client may only hold a single subscription for each unique topic at any
67
+ * given time. When attempting to create a duplicate subscription,
68
+ * the server will close the existing channel, log a warning, and
69
+ * spawn a new channel for the topic. The client will have their
70
+ * `channel.onClose` callbacks fired for the existing channel, and the new
71
+ * channel join will have its receive hooks processed as normal.
72
+ *
73
+ * ## Pushing Messages
74
+ *
75
+ * From the previous example, we can see that pushing messages to the server
76
+ * can be done with `channel.push(eventName, payload)` and we can optionally
77
+ * receive responses from the push. Additionally, we can use
78
+ * `receive("timeout", callback)` to abort waiting for our other `receive` hooks
79
+ * and take action after some period of waiting. The default timeout is 10000ms.
80
+ *
81
+ *
82
+ * ## Socket Hooks
83
+ *
84
+ * Lifecycle events of the multiplexed connection can be hooked into via
85
+ * `socket.onError()` and `socket.onClose()` events, ie:
86
+ *
87
+ * ```javascript
88
+ * socket.onError( () => console.log("there was an error with the connection!") )
89
+ * socket.onClose( () => console.log("the connection dropped") )
90
+ * ```
91
+ *
92
+ *
93
+ * ## Channel Hooks
94
+ *
95
+ * For each joined channel, you can bind to `onError` and `onClose` events
96
+ * to monitor the channel lifecycle, ie:
97
+ *
98
+ * ```javascript
99
+ * channel.onError( () => console.log("there was an error!") )
100
+ * channel.onClose( () => console.log("the channel has gone away gracefully") )
101
+ * ```
102
+ *
103
+ * ### onError hooks
104
+ *
105
+ * `onError` hooks are invoked if the socket connection drops, or the channel
106
+ * crashes on the server. In either case, a channel rejoin is attempted
107
+ * automatically in an exponential backoff manner.
108
+ *
109
+ * ### onClose hooks
110
+ *
111
+ * `onClose` hooks are invoked only in two cases. 1) the channel explicitly
112
+ * closed on the server, or 2). The client explicitly closed, by calling
113
+ * `channel.leave()`
114
+ *
115
+ *
116
+ * ## Presence
117
+ *
118
+ * The `Presence` object provides features for syncing presence information
119
+ * from the server with the client and handling presences joining and leaving.
120
+ *
121
+ * ### Syncing state from the server
122
+ *
123
+ * To sync presence state from the server, first instantiate an object and
124
+ * pass your channel in to track lifecycle events:
125
+ *
126
+ * ```javascript
127
+ * let channel = socket.channel("some:topic")
128
+ * let presence = new Presence(channel)
129
+ * ```
130
+ *
131
+ * Next, use the `presence.onSync` callback to react to state changes
132
+ * from the server. For example, to render the list of users every time
133
+ * the list changes, you could write:
134
+ *
135
+ * ```javascript
136
+ * presence.onSync(() => {
137
+ * myRenderUsersFunction(presence.list())
138
+ * })
139
+ * ```
140
+ *
141
+ * ### Listing Presences
142
+ *
143
+ * `presence.list` is used to return a list of presence information
144
+ * based on the local state of metadata. By default, all presence
145
+ * metadata is returned, but a `listBy` function can be supplied to
146
+ * allow the client to select which metadata to use for a given presence.
147
+ * For example, you may have a user online from different devices with
148
+ * a metadata status of "online", but they have set themselves to "away"
149
+ * on another device. In this case, the app may choose to use the "away"
150
+ * status for what appears on the UI. The example below defines a `listBy`
151
+ * function which prioritizes the first metadata which was registered for
152
+ * each user. This could be the first tab they opened, or the first device
153
+ * they came online from:
154
+ *
155
+ * ```javascript
156
+ * let listBy = (id, {metas: [first, ...rest]}) => {
157
+ * first.count = rest.length + 1 // count of this user's presences
158
+ * first.id = id
159
+ * return first
160
+ * }
161
+ * let onlineUsers = presence.list(listBy)
162
+ * ```
163
+ *
164
+ * ### Handling individual presence join and leave events
165
+ *
166
+ * The `presence.onJoin` and `presence.onLeave` callbacks can be used to
167
+ * react to individual presences joining and leaving the app. For example:
168
+ *
169
+ * ```javascript
170
+ * let presence = new Presence(channel)
171
+ *
172
+ * // detect if user has joined for the 1st time or from another tab/device
173
+ * presence.onJoin((id, current, newPres) => {
174
+ * if(!current){
175
+ * console.log("user has entered for the first time", newPres)
176
+ * } else {
177
+ * console.log("user additional presence", newPres)
178
+ * }
179
+ * })
180
+ *
181
+ * // detect if user has left from all tabs/devices, or is still present
182
+ * presence.onLeave((id, current, leftPres) => {
183
+ * if(current.metas.length === 0){
184
+ * console.log("user has left from all devices", leftPres)
185
+ * } else {
186
+ * console.log("user left from a device", leftPres)
187
+ * }
188
+ * })
189
+ * // receive presence data from server
190
+ * presence.onSync(() => {
191
+ * displayUsers(presence.list())
192
+ * })
193
+ * ```
194
+ * @module phoenix
195
+ */
196
+ Object.defineProperty(exports, "__esModule", { value: true });
197
+ exports.Socket = exports.Serializer = exports.Presence = exports.LongPoll = exports.Channel = void 0;
198
+ const channel_1 = require("./channel");
199
+ exports.Channel = channel_1.default;
200
+ const longpoll_1 = require("./longpoll");
201
+ exports.LongPoll = longpoll_1.default;
202
+ const presence_1 = require("./presence");
203
+ exports.Presence = presence_1.default;
204
+ const serializer_1 = require("./serializer");
205
+ exports.Serializer = serializer_1.default;
206
+ const socket_1 = require("./socket");
207
+ exports.Socket = socket_1.default;
@@ -0,0 +1,12 @@
1
+ export default class LongPoll {
2
+ constructor(endPoint: any);
3
+ normalizeEndpoint(endPoint: any): any;
4
+ endpointURL(): any;
5
+ closeAndRetry(code: any, reason: any, wasClean: any): void;
6
+ ontimeout(): void;
7
+ isActive(): boolean;
8
+ poll(): void;
9
+ send(body: any): void;
10
+ close(code: any, reason: any, wasClean: any): void;
11
+ ajax(method: any, body: any, onCallerTimeout: any, callback: any): void;
12
+ }
@@ -0,0 +1,129 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ // @ts-nocheck
4
+ const constants_1 = require("./constants");
5
+ const ajax_1 = require("./ajax");
6
+ class LongPoll {
7
+ constructor(endPoint) {
8
+ this.endPoint = null;
9
+ this.token = null;
10
+ this.skipHeartbeat = true;
11
+ this.reqs = new Set();
12
+ this.onopen = function () { }; // noop
13
+ this.onerror = function () { }; // noop
14
+ this.onmessage = function () { }; // noop
15
+ this.onclose = function () { }; // noop
16
+ this.pollEndpoint = this.normalizeEndpoint(endPoint);
17
+ this.readyState = constants_1.SOCKET_STATES.connecting;
18
+ this.poll();
19
+ }
20
+ normalizeEndpoint(endPoint) {
21
+ return (endPoint
22
+ .replace("ws://", "http://")
23
+ .replace("wss://", "https://")
24
+ .replace(new RegExp("(.*)\/" + constants_1.TRANSPORTS.websocket), "$1/" + constants_1.TRANSPORTS.longpoll));
25
+ }
26
+ endpointURL() {
27
+ return ajax_1.default.appendParams(this.pollEndpoint, { token: this.token });
28
+ }
29
+ closeAndRetry(code, reason, wasClean) {
30
+ this.close(code, reason, wasClean);
31
+ this.readyState = constants_1.SOCKET_STATES.connecting;
32
+ }
33
+ ontimeout() {
34
+ this.onerror("timeout");
35
+ this.closeAndRetry(1005, "timeout", false);
36
+ }
37
+ isActive() { return this.readyState === constants_1.SOCKET_STATES.open || this.readyState === constants_1.SOCKET_STATES.connecting; }
38
+ poll() {
39
+ this.ajax("GET", null, () => this.ontimeout(), resp => {
40
+ if (resp) {
41
+ var { status, token, messages } = resp;
42
+ this.token = token;
43
+ }
44
+ else {
45
+ status = 0;
46
+ }
47
+ switch (status) {
48
+ case 200:
49
+ messages.forEach(msg => {
50
+ // Tasks are what things like event handlers, setTimeout callbacks,
51
+ // promise resolves and more are run within.
52
+ // In modern browsers, there are two different kinds of tasks,
53
+ // microtasks and macrotasks.
54
+ // Microtasks are mainly used for Promises, while macrotasks are
55
+ // used for everything else.
56
+ // Microtasks always have priority over macrotasks. If the JS engine
57
+ // is looking for a task to run, it will always try to empty the
58
+ // microtask queue before attempting to run anything from the
59
+ // macrotask queue.
60
+ //
61
+ // For the WebSocket transport, messages always arrive in their own
62
+ // event. This means that if any promises are resolved from within,
63
+ // their callbacks will always finish execution by the time the
64
+ // next message event handler is run.
65
+ //
66
+ // In order to emulate this behaviour, we need to make sure each
67
+ // onmessage handler is run within its own macrotask.
68
+ setTimeout(() => this.onmessage({ data: msg }), 0);
69
+ });
70
+ this.poll();
71
+ break;
72
+ case 204:
73
+ this.poll();
74
+ break;
75
+ case 410:
76
+ this.readyState = constants_1.SOCKET_STATES.open;
77
+ this.onopen({});
78
+ this.poll();
79
+ break;
80
+ case 403:
81
+ this.onerror(403);
82
+ this.close(1008, "forbidden", false);
83
+ break;
84
+ case 0:
85
+ case 500:
86
+ this.onerror(500);
87
+ this.closeAndRetry(1011, "internal server error", 500);
88
+ break;
89
+ default: throw new Error(`unhandled poll status ${status}`);
90
+ }
91
+ });
92
+ }
93
+ send(body) {
94
+ this.ajax("POST", body, () => this.onerror("timeout"), resp => {
95
+ if (!resp || resp.status !== 200) {
96
+ this.onerror(resp && resp.status);
97
+ this.closeAndRetry(1011, "internal server error", false);
98
+ }
99
+ });
100
+ }
101
+ close(code, reason, wasClean) {
102
+ for (let req of this.reqs) {
103
+ req.abort();
104
+ }
105
+ this.readyState = constants_1.SOCKET_STATES.closed;
106
+ let opts = Object.assign({ code: 1000, reason: undefined, wasClean: true }, { code, reason, wasClean });
107
+ if (typeof (CloseEvent) !== "undefined") {
108
+ this.onclose(new CloseEvent("close", opts));
109
+ }
110
+ else {
111
+ this.onclose(opts);
112
+ }
113
+ }
114
+ ajax(method, body, onCallerTimeout, callback) {
115
+ let req;
116
+ let ontimeout = () => {
117
+ this.reqs.delete(req);
118
+ onCallerTimeout();
119
+ };
120
+ req = ajax_1.default.request(method, this.endpointURL(), "application/json", body, this.timeout, ontimeout, resp => {
121
+ this.reqs.delete(req);
122
+ if (this.isActive()) {
123
+ callback(resp);
124
+ }
125
+ });
126
+ this.reqs.add(req);
127
+ }
128
+ }
129
+ exports.default = LongPoll;
@@ -0,0 +1,44 @@
1
+ /**
2
+ * Initializes the Presence
3
+ * @param {Channel} channel - The Channel
4
+ * @param {Object} opts - The options,
5
+ * for example `{events: {state: "state", diff: "diff"}}`
6
+ */
7
+ export default class Presence {
8
+ constructor(channel: any, opts?: {});
9
+ onJoin(callback: any): void;
10
+ onLeave(callback: any): void;
11
+ onSync(callback: any): void;
12
+ list(by: any): any[];
13
+ inPendingSyncState(): boolean;
14
+ /**
15
+ * Used to sync the list of presences on the server
16
+ * with the client's state. An optional `onJoin` and `onLeave` callback can
17
+ * be provided to react to changes in the client's local presences across
18
+ * disconnects and reconnects with the server.
19
+ *
20
+ * @returns {Presence}
21
+ */
22
+ static syncState(currentState: any, newState: any, onJoin: any, onLeave: any): any;
23
+ /**
24
+ *
25
+ * Used to sync a diff of presence join and leave
26
+ * events from the server, as they happen. Like `syncState`, `syncDiff`
27
+ * accepts optional `onJoin` and `onLeave` callbacks to react to a user
28
+ * joining or leaving from a device.
29
+ *
30
+ * @returns {Presence}
31
+ */
32
+ static syncDiff(state: any, diff: any, onJoin: any, onLeave: any): any;
33
+ /**
34
+ * Returns the array of presences, with selected metadata.
35
+ *
36
+ * @param {Object} presences
37
+ * @param {Function} chooser
38
+ *
39
+ * @returns {Presence}
40
+ */
41
+ static list(presences: any, chooser: any): any[];
42
+ static map(obj: any, func: any): any[];
43
+ static clone(obj: any): any;
44
+ }