@monterosa/sdk-connect-kit 2.0.0-rc.3 → 2.0.0-rc.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.
@@ -0,0 +1,395 @@
1
+ /**
2
+ * Establishes real-time connections to events and projects.
3
+ *
4
+ * @packageDocumentation
5
+ */
6
+
7
+ import { Emitter } from '@monterosa/sdk-util';
8
+ import Enmasse from '@monterosa/sdk-enmasse-kit';
9
+ import { MonterosaKit } from '@monterosa/sdk-core';
10
+ import { Unsubscribe } from '@monterosa/sdk-util';
11
+
12
+ /**
13
+ * Represents a Connect Kit instance. Obtain one via {@link getConnect}.
14
+ */
15
+ export declare interface Connect extends MonterosaKit, Emitter {
16
+ /**
17
+ * Connection state
18
+ */
19
+ state: ConnState;
20
+ /**
21
+ * Session ID
22
+ */
23
+ sessionId: string | null;
24
+ /**
25
+ * @internal
26
+ */
27
+ initState: InitState;
28
+ /**
29
+ * @internal
30
+ */
31
+ init(host: string): Promise<void>;
32
+ /**
33
+ * @internal
34
+ */
35
+ connect(): Promise<void>;
36
+ /**
37
+ * @internal
38
+ */
39
+ disconnect(): void;
40
+ /**
41
+ * @internal
42
+ */
43
+ subscribe(channel: string): Promise<void>;
44
+ /**
45
+ * @internal
46
+ */
47
+ unsubscribe(channel: string): void;
48
+ /**
49
+ * @internal
50
+ */
51
+ send(channel: string, klass: string, body: (string | number)[]): void;
52
+ /**
53
+ * @internal
54
+ */
55
+ login(userId: string, timestamp: number, signature: string): Promise<void>;
56
+ logout(): void;
57
+ }
58
+
59
+ /**
60
+ * Starts a connection process.
61
+ *
62
+ * @remarks
63
+ * If connect is successful a promise resolved by
64
+ * and {@link onConnected} is triggered, otherwise a next attempt will be
65
+ * automatically invoked and {@link onConnecting} is triggered.
66
+ *
67
+ * SDK connects automatically when its created. Connection procedure can be
68
+ * aborted by calling {@link disconnect} function and the promise is rejected.
69
+ *
70
+ * @example
71
+ * ```javascript
72
+ * try {
73
+ * const conn = await getConnect();
74
+ *
75
+ * await connect(conn);
76
+ *
77
+ * console.log('connected!');
78
+ * } catch (err) {
79
+ * console.log(err)
80
+ * }
81
+ * ```
82
+ *
83
+ * State of the connection can be handled separately
84
+ *
85
+ * @example
86
+ * ```javascript
87
+ * onConnected(() => console.log('connected'));
88
+ *
89
+ * onConnecting((attempt, delay) => console.log('connecting'));
90
+ *
91
+ * onDisconnected(() => console.log('disconnected'));
92
+ * ```
93
+ *
94
+ * @param conn - The {@link Connect} instance
95
+ * @param host - Interaction Cloud Studio host.
96
+ * Can be found in Project Settings in API tab
97
+ */
98
+ export declare function connect(conn: Connect): Promise<void>;
99
+
100
+ /**
101
+ * @internal
102
+ */
103
+ export declare class ConnectImpl extends Emitter implements Connect {
104
+ private static instance;
105
+ initState: InitState;
106
+ private initPromise;
107
+ private connectPromise;
108
+ private constructor();
109
+ get state(): ConnState;
110
+ get sessionId(): string | null;
111
+ static getInstance(): ConnectImpl;
112
+ static toConnState(state: Enmasse.States): ConnState;
113
+ init(host: string): Promise<void>;
114
+ connect(): Promise<void>;
115
+ disconnect(): void;
116
+ subscribe(channel: string): Promise<void>;
117
+ unsubscribe(channel: string): void;
118
+ send(channel: string, klass: string, body: (string | number)[]): void;
119
+ login(userId: string, timestamp: number, signature: string): Promise<void>;
120
+ logout(): Promise<void>;
121
+ delete(): void;
122
+ }
123
+
124
+ /**
125
+ * A list of possible connection states.
126
+ *
127
+ * @remarks
128
+ * Can be used with {@link Connect | Connect.state} to determine the current state.
129
+ *
130
+ * By default {@link Connect} is in the disconnected state.
131
+ * When {@link connect} invoked `Connect` state switches to
132
+ * `ConnState.Connecting` and connection is maintained permanently until
133
+ * {@link disconnect} method is invoked. If connection was unsucessful,
134
+ * a new attempt will start after a short delay and `ConnState.connecting`
135
+ * is triggered. Delays between attempts increased based on this sequence:
136
+ * 0, 3, 5, 10, 20, 30, 45, 60 where 60 seconds is the max.
137
+ *
138
+ * If `Connect` was previously connected and for any reason connection is lost,
139
+ * the state changed to `ConnState.Connecting` again and the connection process
140
+ * starts again automatically.
141
+ *
142
+ * @example
143
+ * ```javascript
144
+ * if (conn.state === ConnState.Connecting) {
145
+ * // display "connecting" message
146
+ * }
147
+ * ```
148
+ */
149
+ export declare enum ConnState {
150
+ /**
151
+ * Disconnected state
152
+ */
153
+ Disconnected = "disconnected",
154
+ /**
155
+ * Connecting state
156
+ */
157
+ Connecting = "connecting",
158
+ /**
159
+ * Connected state
160
+ */
161
+ Connected = "connected"
162
+ }
163
+
164
+ /**
165
+ * Disconnect from the server and triggers {@link onDisconnected}
166
+ *
167
+ * @param conn - The {@link Connect} instance
168
+ */
169
+ export declare function disconnect(conn: Connect): void;
170
+
171
+ /**
172
+ * Returns the {@link Connect} instance associated with the provided
173
+ * host. If host is not provided, it returns Connect instance for
174
+ * the host associated with the default sdk.
175
+ *
176
+ * @param host - The host URL. If not provided, uses the host
177
+ * from the default SDK instance.
178
+ */
179
+ export declare function getConnect(host?: string): Promise<Connect>;
180
+
181
+ /**
182
+ * @internal
183
+ */
184
+ export declare const getConnectMemoized: (...args: any[]) => Promise<Connect>;
185
+
186
+ /**
187
+ * @internal
188
+ */
189
+ export declare enum InitState {
190
+ NotInitialised = "not_initialised",
191
+ Initialised = "initialised",
192
+ Initialising = "initialising"
193
+ }
194
+
195
+ /**
196
+ * @internal
197
+ */
198
+ export declare const KIT_NAME = "connect";
199
+
200
+ /**
201
+ * Class of the {@link Message}
202
+ */
203
+ export declare enum Klass {
204
+ /**
205
+ * Authentication request. Sent by server on client connection.
206
+ */
207
+ Authenticate = "auth",
208
+ /**
209
+ * Authentication response. Sent by client as response
210
+ * to authentication request.
211
+ */
212
+ AuthenticateResponse = "authr",
213
+ /**
214
+ * Authentication acknowledgement. Sent by the server as confirmation
215
+ * of successful authentication.
216
+ */
217
+ AuthenticateSuccess = "authok",
218
+ /**
219
+ * Ping message is sent by server periodically as a keep-alive.
220
+ * Ping message is only sent if no messages are sent for specific time
221
+ * period, meaning if there are other messages sent frequently enough
222
+ * then ping messages are not sent at all.
223
+ */
224
+ Ping = "ping",
225
+ /**
226
+ * Subscribe to a channel. Sent by the client to subscribe to a channel
227
+ */
228
+ Subscribe = "sub",
229
+ /**
230
+ * Unsubscribe from a channel. Sent by the client to unsubscribe from a channel
231
+ */
232
+ Unsububscribe = "unsub",
233
+ /**
234
+ * End of cache. Sent by the server to notify that all messages are sent from
235
+ * a channel cache
236
+ */
237
+ EndOfCache = "eoc",
238
+ /**
239
+ * User login. Sent by client to supply verified user identity to the server.
240
+ */
241
+ Login = "login",
242
+ /**
243
+ * User login success
244
+ */
245
+ LoginSuccess = "login_ok",
246
+ /**
247
+ * User login failure
248
+ */
249
+ LoginFail = "login_fail",
250
+ /**
251
+ * Averages counters. Client can submit a counter which is processed together
252
+ * with other counters to determine the average of all counters within specific
253
+ * demographics segment.
254
+ */
255
+ Counter = "counter",
256
+ /**
257
+ * Client votes. Sents by a user to vote for a poll option
258
+ */
259
+ Vote = "v"
260
+ }
261
+
262
+ /**
263
+ * Authenticates the connection with user credentials.
264
+ *
265
+ * @param conn - The Connect instance
266
+ * @param userId - The user identifier
267
+ * @param timestamp - The signature timestamp in seconds
268
+ * @param signature - The session signature string
269
+ *
270
+ * @public
271
+ */
272
+ export declare function login(conn: Connect, userId: string, timestamp: number, signature: string): Promise<void>;
273
+
274
+ export declare function logout(conn: Connect): Promise<void>;
275
+
276
+ /**
277
+ * Represents a message received from or sent to the server.
278
+ * Subscribe via {@link onMessage} to receive messages.
279
+ */
280
+ export declare interface Message {
281
+ /**
282
+ * The channel on which this message was received. You must
283
+ * {@link subscribe} to a channel to start receiving messages.
284
+ */
285
+ channel: string;
286
+ /**
287
+ * The class indicates the desired action to be performed or the result of
288
+ * the action. E.g. in order to authenticate a developer should {@link send}
289
+ * {@link Klass | Klass.AuthenticateResponse} klass message as a response on
290
+ * a received {@link Klass | Klass.Authenticate} message
291
+ */
292
+ klass: Klass | string;
293
+ /**
294
+ * The timestamp (in seconds) when this message was sent
295
+ */
296
+ sent_at: number;
297
+ /**
298
+ * The message payload. Each piece of data is a `string`
299
+ */
300
+ body: string[];
301
+ }
302
+
303
+ /**
304
+ * Adds an observer for when connection state changed to connected.
305
+ *
306
+ * @param conn - The {@link Connect} instance
307
+ * @param callback - The callback triggered when {@link ConnState | state}
308
+ * changed to connected
309
+ */
310
+ export declare function onConnected(conn: Connect, callback: (timestamp: number) => void): Unsubscribe;
311
+
312
+ /**
313
+ * Adds an observer for when connection state changed to connecting.
314
+ *
315
+ * @param conn - The {@link Connect} instance
316
+ * @param callback - callback triggered on connecting state. Two parameters
317
+ * are provided: `attempt`, the sequential number of connect attempt, and
318
+ * `delay` - delay in seconds before the next attempt
319
+ */
320
+ export declare function onConnecting(conn: Connect, callback: (attempt: number, delay: number) => void): Unsubscribe;
321
+
322
+ /**
323
+ * Adds an observer for when connection state changed to disconnected.
324
+ *
325
+ * @param conn - The {@link Connect} instance
326
+ * @param callback - callback triggered on disconnected state
327
+ */
328
+ export declare function onDisconnected(conn: Connect, callback: () => void): Unsubscribe;
329
+
330
+ /**
331
+ * Adds an observer for when a new message received
332
+ *
333
+ * @example
334
+ * ```javascript
335
+ * const unsubscribe = onMessage(conn, (message) => {
336
+ * console.log(message.body);
337
+ * });
338
+ * ```
339
+ *
340
+ * @param conn - The {@link Connect} instance
341
+ * @param callback - The callback function triggered on message receive
342
+ */
343
+ export declare function onMessage(conn: Connect, callback: (message: Message) => void): Unsubscribe;
344
+
345
+ /**
346
+ * Send message to the server.
347
+ *
348
+ * @remarks
349
+ * The sdk out of the box provides more high level functions that uses
350
+ * this function to send data to the server. Though a developer still can
351
+ * use it if he knows a message protocol (klass and body) and a channel
352
+ * that expect the message.
353
+ *
354
+ * @param conn - The Connect instance
355
+ * @param channel - Channel name. Only {@link subscribe | subscribers}
356
+ * to that channel can send messages.
357
+ * @param klass - Message class name.
358
+ * @param body - Message payload
359
+ */
360
+ export declare function send(conn: Connect, channel: string, klass: Klass | string, body: (string | number)[]): void;
361
+
362
+ /**
363
+ * Subscribe to the server channel.
364
+ *
365
+ * @remarks
366
+ * Any message published to a channel is {@link onMessage | received}
367
+ * only by subscribers to that channel. The sdk out of the box handles
368
+ * all required subscriptions internally and {@link onMessage | handles messages}
369
+ * so there is no need in to subscribe by developers.
370
+ *
371
+ * To stop receiving messages on a channel {@link unsubscribe} should be called.
372
+ *
373
+ * @param conn - The {@link Connect} instance
374
+ * @param channel - Channel name
375
+ */
376
+ export declare function subscribe(conn: Connect, channel: string): Promise<void>;
377
+
378
+ /**
379
+ * @internal
380
+ */
381
+ export declare const subscribeMemoized: (...args: any[]) => Promise<void>;
382
+
383
+ /**
384
+ * Unsubscribe from the server channel.
385
+ *
386
+ * @remarks
387
+ * Removes subscription to the channel so that recipient no longer receive
388
+ * messages from that channel.
389
+ *
390
+ * @param conn - The {@link Connect} instance
391
+ * @param channel - Channel name
392
+ */
393
+ export declare function unsubscribe(conn: Connect, channel: string): void;
394
+
395
+ export { }
@@ -0,0 +1,11 @@
1
+ // This file is read by tools that parse documentation comments conforming to the TSDoc standard.
2
+ // It should be published with your NPM package. It should not be tracked by Git.
3
+ {
4
+ "tsdocVersion": "0.12",
5
+ "toolPackages": [
6
+ {
7
+ "packageName": "@microsoft/api-extractor",
8
+ "packageVersion": "7.23.0"
9
+ }
10
+ ]
11
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@monterosa/sdk-connect-kit",
3
- "version": "2.0.0-rc.3",
3
+ "version": "2.0.0-rc.5",
4
4
  "description": "Monterosa JS SDK / Connect",
5
5
  "author": "Monterosa Productions Limited <hello@monterosa.co.uk> (https://www.monterosa.co/)",
6
6
  "main": "./dist/index.cjs",
@@ -32,9 +32,9 @@
32
32
  ],
33
33
  "license": "MIT",
34
34
  "dependencies": {
35
- "@monterosa/sdk-core": "2.0.0-rc.3",
36
- "@monterosa/sdk-enmasse-kit": "2.0.0-rc.3",
37
- "@monterosa/sdk-util": "2.0.0-rc.3"
35
+ "@monterosa/sdk-core": "2.0.0-rc.5",
36
+ "@monterosa/sdk-enmasse-kit": "2.0.0-rc.5",
37
+ "@monterosa/sdk-util": "2.0.0-rc.5"
38
38
  },
39
39
  "devDependencies": {
40
40
  "@rollup/plugin-json": "^4.1.0",
@@ -54,5 +54,5 @@
54
54
  "publishConfig": {
55
55
  "access": "public"
56
56
  },
57
- "gitHead": "d692ad26af0b5ea7cd7b664168bffa069f9ab911"
57
+ "gitHead": "4f697a9733d36fb689a35b8e1dd6d589a9ae284c"
58
58
  }