@monterosa/sdk-connect-kit 0.18.9 → 0.18.10-local-only-storage.1

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/dist/connect.d.ts CHANGED
@@ -19,7 +19,8 @@ declare const getConnectMemoized: (...args: any[]) => Promise<Connect>;
19
19
  * host. If host is not provided, it returns Connect instance for
20
20
  * the host associated with the default sdk.
21
21
  *
22
- * @param sdk - Monterosa SDK.
22
+ * @param host - The host URL. If not provided, uses the host
23
+ * from the default SDK instance.
23
24
  */
24
25
  declare function getConnect(host?: string): Promise<Connect>;
25
26
  /**
@@ -65,7 +66,10 @@ declare function onMessage(conn: Connect, callback: (message: Message) => void):
65
66
  */
66
67
  declare const connectMemoized: (...args: any[]) => Promise<void>;
67
68
  /**
68
- * Starts a connection process. If connect is successful a promise resolved by
69
+ * Starts a connection process.
70
+ *
71
+ * @remarks
72
+ * If connect is successful a promise resolved by
69
73
  * and {@link onConnected} is triggered, otherwise a next attempt will be
70
74
  * automatically invoked and {@link onConnecting} is triggered.
71
75
  *
@@ -114,6 +118,7 @@ declare const subscribeMemoized: (...args: any[]) => Promise<void>;
114
118
  /**
115
119
  * Subscribe to the server channel.
116
120
  *
121
+ * @remarks
117
122
  * Any message published to a channel is {@link onMessage | received}
118
123
  * only by subscribers to that channel. The sdk out of the box handles
119
124
  * all required subscriptions internally and {@link onMessage | handles messages}
@@ -128,6 +133,7 @@ declare function subscribe(conn: Connect, channel: string): Promise<void>;
128
133
  /**
129
134
  * Unsubscribe from the server channel.
130
135
  *
136
+ * @remarks
131
137
  * Removes subscription to the channel so that recipient no longer receive
132
138
  * messages from that channel.
133
139
  *
@@ -138,6 +144,7 @@ declare function unsubscribe(conn: Connect, channel: string): void;
138
144
  /**
139
145
  * Send message to the server.
140
146
  *
147
+ * @remarks
141
148
  * The sdk out of the box provides more high level functions that uses
142
149
  * this function to send data to the server. Though a developer still can
143
150
  * use it if he knows a message protocol (klass and body) and a channel
@@ -150,5 +157,19 @@ declare function unsubscribe(conn: Connect, channel: string): void;
150
157
  * @param body - Message payload
151
158
  */
152
159
  declare function send(conn: Connect, channel: string, klass: Klass | string, body: (string | number)[]): void;
160
+ /**
161
+ * Authenticates the connection with user credentials.
162
+ *
163
+ * @remarks
164
+ * The session signature can be obtained from Identify Kit using
165
+ * {@link @monterosa/sdk-identify-kit#getSessionSignature}.
166
+ *
167
+ * @param conn - The Connect instance
168
+ * @param userId - The user identifier
169
+ * @param timestamp - The signature timestamp in seconds
170
+ * @param signature - The session signature string
171
+ *
172
+ * @public
173
+ */
153
174
  declare function login(conn: Connect, userId: string, timestamp: number, signature: string): Promise<void>;
154
175
  export { onConnected, onConnecting, onDisconnected, onMessage, disconnect, unsubscribe, send, getConnect, getConnectMemoized, connect, connectMemoized, subscribe, subscribeMemoized, login, };
package/dist/index.cjs.js CHANGED
@@ -109,8 +109,10 @@ exports.InitState = void 0;
109
109
  InitState["Initialising"] = "initialising";
110
110
  })(exports.InitState || (exports.InitState = {}));
111
111
  /**
112
- * A list of possible connection states. Can be used with
113
- * {@link Connect.state} to determine the current state.
112
+ * A list of possible connection states.
113
+ *
114
+ * @remarks
115
+ * Can be used with {@link Connect | Connect.state} to determine the current state.
114
116
  *
115
117
  * By default {@link Connect} is in the disconnected state.
116
118
  * When {@link connect} invoked `Connect` state switches to
@@ -412,7 +414,8 @@ var getConnectMemoized = sdkUtil.memoizePromise(function (host) { return __await
412
414
  * host. If host is not provided, it returns Connect instance for
413
415
  * the host associated with the default sdk.
414
416
  *
415
- * @param sdk - Monterosa SDK.
417
+ * @param host - The host URL. If not provided, uses the host
418
+ * from the default SDK instance.
416
419
  */
417
420
  function getConnect(host) {
418
421
  if (host === undefined) {
@@ -488,7 +491,10 @@ var connectMemoized = sdkUtil.memoizePromise(function (conn) { return __awaiter(
488
491
  clearOnReject: true,
489
492
  });
490
493
  /**
491
- * Starts a connection process. If connect is successful a promise resolved by
494
+ * Starts a connection process.
495
+ *
496
+ * @remarks
497
+ * If connect is successful a promise resolved by
492
498
  * and {@link onConnected} is triggered, otherwise a next attempt will be
493
499
  * automatically invoked and {@link onConnecting} is triggered.
494
500
  *
@@ -543,6 +549,7 @@ var subscribeMemoized = sdkUtil.memoizePromise(function (conn, channel) { return
543
549
  /**
544
550
  * Subscribe to the server channel.
545
551
  *
552
+ * @remarks
546
553
  * Any message published to a channel is {@link onMessage | received}
547
554
  * only by subscribers to that channel. The sdk out of the box handles
548
555
  * all required subscriptions internally and {@link onMessage | handles messages}
@@ -559,6 +566,7 @@ function subscribe(conn, channel) {
559
566
  /**
560
567
  * Unsubscribe from the server channel.
561
568
  *
569
+ * @remarks
562
570
  * Removes subscription to the channel so that recipient no longer receive
563
571
  * messages from that channel.
564
572
  *
@@ -571,6 +579,7 @@ function unsubscribe(conn, channel) {
571
579
  /**
572
580
  * Send message to the server.
573
581
  *
582
+ * @remarks
574
583
  * The sdk out of the box provides more high level functions that uses
575
584
  * this function to send data to the server. Though a developer still can
576
585
  * use it if he knows a message protocol (klass and body) and a channel
@@ -585,6 +594,20 @@ function unsubscribe(conn, channel) {
585
594
  function send(conn, channel, klass, body) {
586
595
  conn.send(channel, klass, body);
587
596
  }
597
+ /**
598
+ * Authenticates the connection with user credentials.
599
+ *
600
+ * @remarks
601
+ * The session signature can be obtained from Identify Kit using
602
+ * {@link @monterosa/sdk-identify-kit#getSessionSignature}.
603
+ *
604
+ * @param conn - The Connect instance
605
+ * @param userId - The user identifier
606
+ * @param timestamp - The signature timestamp in seconds
607
+ * @param signature - The session signature string
608
+ *
609
+ * @public
610
+ */
588
611
  function login(conn, userId, timestamp, signature) {
589
612
  return __awaiter(this, void 0, void 0, function () {
590
613
  var result;
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs.js","sources":["../src/types.ts","../src/helpers.ts","../src/connect_impl.ts","../src/connect.ts"],"sourcesContent":["/**\n * @license\n * types.ts\n * connect-kit\n *\n * Created by Rygor Kharytanovich <rygor@monterosa.co.uk> on 2022-01-19\n * Copyright © 2022 Monterosa. All rights reserved.\n *\n * More details on the license can be found at https://www.monterosa.co/sdk/license\n */\n\nimport { MonterosaKit } from '@monterosa/sdk-core';\nimport { Emitter } from '@monterosa/sdk-util';\n\n/**\n * @internal\n */\nexport const KIT_NAME = 'connect';\n\n/**\n * Interface representing Connect kit.\n */\nexport interface Connect extends MonterosaKit, Emitter {\n /**\n * Connection state\n */\n state: ConnState;\n\n /**\n * @internal\n */\n initState: InitState;\n\n /**\n * @internal\n */\n init(host: string): Promise<void>;\n\n /**\n * @internal\n */\n connect(): Promise<void>;\n\n /**\n * @internal\n */\n disconnect(): void;\n\n /**\n * @internal\n */\n subscribe(channel: string): Promise<void>;\n\n /**\n * @internal\n */\n unsubscribe(channel: string): void;\n\n /**\n * @internal\n */\n send(channel: string, klass: string, body: (string | number)[]): void;\n\n login(userId: string, timestamp: number, signature: string): Promise<void>;\n}\n\n/**\n * @internal\n */\nexport enum InitState {\n NotInitialised = 'not_initialised',\n Initialised = 'initialised',\n Initialising = 'initialising',\n}\n\n/**\n * A list of possible connection states. Can be used with\n * {@link Connect.state} to determine the current state.\n *\n * By default {@link Connect} is in the disconnected state.\n * When {@link connect} invoked `Connect` state switches to\n * `ConnState.Connecting` and connection is maintained permanently until\n * {@link disconnect} method is invoked. If connection was unsucessful,\n * a new attempt will start after a short delay and `ConnState.connecting`\n * is triggered. Delays between attempts increased based on this sequence:\n * 0, 3, 5, 10, 20, 30, 45, 60 where 60 seconds is the max.\n *\n * If `Connect` was previously connected and for any reason connection is lost,\n * the state changed to `ConnState.Connecting` again and the connection process\n * starts again automatically.\n *\n * @example\n * ```javascript\n * if (conn.state === ConnState.Connecting) {\n * // display \"connecting\" message\n * }\n * ```\n */\nexport enum ConnState {\n /**\n * Disconnected state\n */\n Disconnected = 'disconnected',\n /**\n * Connecting state\n */\n Connecting = 'connecting',\n /**\n * Connected state\n */\n Connected = 'connected',\n}\n\n/**\n * Class of the {@link Message}\n */\nexport enum Klass {\n /**\n * Authentication request. Sent by server on client connection.\n */\n Authenticate = 'auth',\n /**\n * Authentication response. Sent by client as response\n * to authentication request.\n */\n AuthenticateResponse = 'authr',\n /**\n * Authentication acknowledgement. Sent by the server as confirmation\n * of successful authentication.\n */\n AuthenticateSuccess = 'authok',\n /**\n * Ping message is sent by server periodically as a keep-alive.\n * Ping message is only sent if no messages are sent for specific time\n * period, meaning if there are other messages sent frequently enough\n * then ping messages are not sent at all.\n */\n Ping = 'ping',\n /**\n * Subscribe to a channel. Sent by the client to subscribe to a channel\n */\n Subscribe = 'sub',\n /**\n * Unsubscribe from a channel. Sent by the client to unsubscribe from a channel\n */\n Unsububscribe = 'unsub',\n /**\n * End of cache. Sent by the server to notify that all messages are sent from\n * a channel cache\n */\n EndOfCache = 'eoc',\n /**\n * User login. Sent by client to supply verified user identity to the server.\n */\n Login = 'login',\n /**\n * User login success\n */\n LoginSuccess = 'login_ok',\n /**\n * User login failure\n */\n LoginFail = 'login_fail',\n /**\n * Averages counters. Client can submit a counter which is processed together\n * with other counters to determine the average of all counters within specific\n * demographics segment.\n */\n Counter = 'counter',\n /**\n * Client votes. Sents by a user to vote for a poll option\n */\n Vote = 'v',\n}\n\n/**\n * A Message to be sent to the Monterosa / Interaction Cloud\n */\nexport interface Message {\n /**\n * The channel id on which this message was received. It s required to be\n * {@link subscribe} to that channel in order to start receiving\n * messages\n */\n channel: string;\n /**\n * The class indicates the desired action to be performed or the result of\n * the action. E.g. in order to authenticate a developer should {@link send}\n * {@link Klass.AuthenticateResponse} klass message as a response on\n * a received {@link Klass.Authenticate} message\n */\n klass: Klass | string;\n /**\n * The timestamp (in seconds) when this message was sent\n */\n sent_at: number;\n /**\n * The message payload. Each peace of data is a `string`\n */\n body: string[];\n}\n","/**\n * @license\n * helpers.ts\n * connect-kit\n *\n * Created by Rygor Kharytanovich <rygor@monterosa.co.uk> on 2022-09-14\n * Copyright © 2022 Monterosa. All rights reserved.\n *\n * More details on the license can be found at https://www.monterosa.co/sdk/license\n */\n\nimport { Unsubscribe } from '@monterosa/sdk-util';\n\nimport { Connect, ConnState } from './types';\n\nexport function onState(\n conn: Connect,\n needle: ConnState,\n callback: Function,\n): Unsubscribe {\n const handler = (state: ConnState, ...rest: unknown[]) => {\n if (state === needle) {\n callback(...rest);\n }\n };\n\n conn.on('state', handler);\n\n return () => conn.off('state', handler);\n}\n","/* eslint class-methods-use-this: \"off\" */\n\n/**\n * @license\n * connect_impl.ts\n * connect-kit\n *\n * Created by Rygor Kharytanovich <rygor@monterosa.co.uk> on 2022-02-15\n * Copyright © 2022 Monterosa. All rights reserved.\n *\n * More details on the license can be found at https://www.monterosa.co/sdk/license\n */\n\nimport Enmasse from '@monterosa/sdk-enmasse-kit';\nimport { Emitter, setTimestamp } from '@monterosa/sdk-util';\n\nimport { Connect, ConnState, InitState, Message } from './types';\n\n/**\n * @internal\n */\nexport class ConnectImpl extends Emitter implements Connect {\n private static instance: ConnectImpl;\n public initState: InitState = InitState.NotInitialised;\n private initPromise: Promise<void> = Promise.resolve();\n\n private constructor() {\n super();\n\n Enmasse.bind(\n Enmasse.ON_STATE,\n (state: Enmasse.States, ...rest: unknown[]) => {\n if (state === Enmasse.STATE_CONNECTED) {\n const [timestamp] = rest as [number];\n\n setTimestamp(timestamp);\n }\n\n this.emit('state', ConnectImpl.toConnState(state), ...rest);\n },\n );\n\n Enmasse.bind(Enmasse.ON_MESSAGE, (message: Enmasse.Message) => {\n this.emit('message', <Message>message);\n });\n }\n\n get state() {\n return ConnectImpl.toConnState(Enmasse.getState());\n }\n\n public static getInstance(): ConnectImpl {\n if (!ConnectImpl.instance) {\n ConnectImpl.instance = new ConnectImpl();\n }\n\n return ConnectImpl.instance;\n }\n\n static toConnState(state: Enmasse.States): ConnState {\n switch (state) {\n default:\n case Enmasse.STATE_DISCONNECTED:\n return ConnState.Disconnected;\n case Enmasse.STATE_CONNECTING:\n return ConnState.Connecting;\n case Enmasse.STATE_CONNECTED:\n return ConnState.Connected;\n }\n }\n\n async init(host: string): Promise<void> {\n if (\n this.initState === InitState.Initialising ||\n this.initState === InitState.Initialised\n ) {\n return this.initPromise;\n }\n\n this.initPromise = new Promise((resolve, reject) => {\n this.initState = InitState.Initialising;\n\n Enmasse.bind(Enmasse.ON_READY, () => {\n this.initState = InitState.Initialised;\n\n Enmasse.unbind(Enmasse.ON_READY);\n Enmasse.unbind(Enmasse.ON_ERROR);\n\n resolve();\n });\n\n Enmasse.bind(Enmasse.ON_ERROR, (err: string) => {\n this.initState = InitState.NotInitialised;\n\n Enmasse.unbind(Enmasse.ON_READY);\n Enmasse.unbind(Enmasse.ON_ERROR);\n\n reject(new Error(`Unable to initialise connect kit: ${err}`));\n });\n\n Enmasse.init({\n config: `https://${host}/config/enmasse.json`,\n });\n });\n\n return this.initPromise;\n }\n\n async connect(): Promise<void> {\n if (this.initState !== InitState.Initialised) {\n throw new Error('must be initialised before connecting');\n }\n\n if (this.state === ConnState.Connected) {\n throw new Error('already connected');\n }\n\n if (this.state === ConnState.Connecting) {\n throw new Error('already connecting');\n }\n\n return new Promise((resolve, reject) => {\n const handler = (state: ConnState) => {\n if (state === ConnState.Connected) {\n Enmasse.unbind(Enmasse.ON_STATE, handler);\n\n resolve();\n }\n\n if (state === ConnState.Disconnected) {\n Enmasse.unbind(Enmasse.ON_STATE, handler);\n\n reject(new Error('disconnected'));\n }\n };\n\n Enmasse.bind(Enmasse.ON_STATE, handler);\n\n Enmasse.connect();\n });\n }\n\n disconnect() {\n if (this.state !== ConnState.Connected) {\n throw new Error('must be connected before disconnecting');\n }\n\n Enmasse.disconnect();\n }\n\n subscribe(channel: string): Promise<void> {\n return new Promise((resolve) => {\n const handler = (ch: string) => {\n if (ch !== channel) return;\n\n Enmasse.unbind(Enmasse.ON_EOC, handler);\n\n resolve();\n };\n\n Enmasse.bind(Enmasse.ON_EOC, handler);\n\n Enmasse.subscribe(channel);\n });\n }\n\n unsubscribe(channel: string): void {\n Enmasse.unsubscribe(channel);\n }\n\n send(channel: string, klass: string, body: (string | number)[]): void {\n Enmasse.send(channel, klass, body);\n }\n\n async login(\n userId: string,\n timestamp: number,\n signature: string,\n ): Promise<void> {\n return new Promise((resolve, reject) => {\n Enmasse.login(userId, timestamp, signature, resolve, () => {\n reject(new Error('Login failure'));\n });\n });\n }\n\n delete() {}\n}\n","/**\n * @license\n * connect.ts\n * connect-kit\n *\n * Created by Rygor Kharytanovich <rygor@monterosa.co.uk> on 2022-01-19\n * Copyright © 2022 Monterosa. All rights reserved.\n *\n * More details on the license can be found at https://www.monterosa.co/sdk/license\n */\n\nimport { getSdk } from '@monterosa/sdk-core';\nimport { memoizePromise, Unsubscribe } from '@monterosa/sdk-util';\n\nimport { Connect, ConnState, Message, Klass } from './types';\nimport { onState } from './helpers';\nimport { ConnectImpl } from './connect_impl';\n\n/**\n * @internal\n */\nconst getConnectMemoized = memoizePromise(\n async (host: string): Promise<Connect> => {\n const conn = ConnectImpl.getInstance();\n\n await conn.init(host);\n\n return conn;\n },\n (host: string): string => host,\n);\n\n/**\n * Returns the {@link Connect} instance associated with the provided\n * host. If host is not provided, it returns Connect instance for\n * the host associated with the default sdk.\n *\n * @param sdk - Monterosa SDK.\n */\nfunction getConnect(host?: string): Promise<Connect> {\n if (host === undefined) {\n const sdk = getSdk();\n\n host = sdk.options.host;\n }\n\n return getConnectMemoized(host);\n}\n\n/**\n * Adds an observer for when connection state changed to connected.\n *\n * @param conn - The {@link Connect} instance\n * @param callback - The callback triggered when {@link ConnState | state}\n * changed to connected\n */\nfunction onConnected(\n conn: Connect,\n callback: (timestamp: number) => void,\n): Unsubscribe {\n return onState(conn, ConnState.Connected, callback);\n}\n\n/**\n * Adds an observer for when connection state changed to connecting.\n *\n * @param conn - The {@link Connect} instance\n * @param callback - callback triggered on connecting state. Two parameters\n * are provided: `attempt` — sequential number of connect attempt and\n * `delay` - delay in seconds before the next attempt\n */\nfunction onConnecting(\n conn: Connect,\n callback: (attempt: number, delay: number) => void,\n): Unsubscribe {\n return onState(conn, ConnState.Connecting, callback);\n}\n\n/**\n * Adds an observer for when connection state changed to disconnected.\n *\n * @param conn - The {@link Connect} instance\n * @param callback - callback triggered on disconnected state\n */\nfunction onDisconnected(conn: Connect, callback: () => void): Unsubscribe {\n return onState(conn, ConnState.Disconnected, callback);\n}\n\n/**\n * Adds an observer for when a new message received\n *\n * @example\n * ```javascript\n * const unsubscribe = onMessage(conn, (message) => {\n * console.log(message.body);\n * });\n * ```\n *\n * @param conn - The {@link Connect} instance\n * @param callback - The callback function triggered on message receive\n */\nfunction onMessage(\n conn: Connect,\n callback: (message: Message) => void,\n): Unsubscribe {\n const handler = (message: Message) => {\n callback(message);\n };\n\n conn.on('message', handler);\n\n return () => conn.off('message', handler);\n}\n\n/**\n * @internal\n */\nconst connectMemoized = memoizePromise(\n async (conn: Connect): Promise<void> => {\n await conn.connect();\n },\n (conn: Connect): Connect => conn,\n {\n clearOnResolve: true,\n clearOnReject: true,\n },\n);\n\n/**\n * Starts a connection process. If connect is successful a promise resolved by\n * and {@link onConnected} is triggered, otherwise a next attempt will be\n * automatically invoked and {@link onConnecting} is triggered.\n *\n * SDK connects automatically when its created. Connection procedure can be\n * aborted by calling {@link disconnect} function and the promise is rejected.\n *\n * @example\n * ```javascript\n * try {\n * const conn = await getConnect();\n *\n * await connect(conn);\n *\n * console.log('connected!');\n * } catch (err) {\n * console.log(err)\n * }\n * ```\n *\n * State of the connection can be handled separately\n *\n * @example\n * ```javascript\n * onConnected(() => console.log('connected'));\n *\n * onConnecting((attempt, delay) => console.log('connecting'));\n *\n * onDisconnected(() => console.log('disconnected'));\n * ```\n *\n * @param conn - The {@link Connect} instance\n * @param host - Interaction Cloud Studio host.\n * Can be found in Project Settings in API tab\n */\nfunction connect(conn: Connect): Promise<void> {\n return connectMemoized(conn);\n}\n\n/**\n * Disconnect from the server and triggers {@link onDisconnected}\n *\n * @param conn - The {@link Connect} instance\n */\nfunction disconnect(conn: Connect): void {\n conn.disconnect();\n}\n\n/**\n * @internal\n */\nconst subscribeMemoized = memoizePromise(\n async (conn: Connect, channel: string): Promise<void> =>\n conn.subscribe(channel),\n (conn: Connect, channel: string): string => channel,\n);\n\n/**\n * Subscribe to the server channel.\n *\n * Any message published to a channel is {@link onMessage | received}\n * only by subscribers to that channel. The sdk out of the box handles\n * all required subscriptions internally and {@link onMessage | handles messages}\n * so there is no need in to subscribe by developers.\n *\n * To stop receiving messages on a channel {@link unsubscribe} should be called.\n *\n * @param conn - The {@link Connect} instance\n * @param channel - Channel name\n */\nfunction subscribe(conn: Connect, channel: string): Promise<void> {\n return subscribeMemoized(conn, channel);\n}\n\n/**\n * Unsubscribe from the server channel.\n *\n * Removes subscription to the channel so that recipient no longer receive\n * messages from that channel.\n *\n * @param conn - The {@link Connect} instance\n * @param channel - Channel name\n */\nfunction unsubscribe(conn: Connect, channel: string): void {\n conn.unsubscribe(channel);\n}\n\n/**\n * Send message to the server.\n *\n * The sdk out of the box provides more high level functions that uses\n * this function to send data to the server. Though a developer still can\n * use it if he knows a message protocol (klass and body) and a channel\n * that expect the message.\n *\n * @param conn - The Connect instance\n * @param channel - Channel name. Only {@link subscribe | subscribers}\n * to that channel can send messages.\n * @param klass - Message class name.\n * @param body - Message payload\n */\nfunction send(\n conn: Connect,\n channel: string,\n klass: Klass | string,\n body: (string | number)[],\n): void {\n conn.send(channel, klass, body);\n}\n\nasync function login(\n conn: Connect,\n userId: string,\n timestamp: number,\n signature: string,\n): Promise<void> {\n const result = await conn.login(userId, timestamp, signature);\n\n return result;\n}\n\nexport {\n onConnected,\n onConnecting,\n onDisconnected,\n onMessage,\n disconnect,\n unsubscribe,\n send,\n getConnect,\n getConnectMemoized,\n connect,\n connectMemoized,\n subscribe,\n subscribeMemoized,\n login,\n};\n"],"names":["InitState","ConnState","Klass","Enmasse","setTimestamp","Emitter","memoizePromise","getSdk"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;;;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,SAAS,OAAO,CAAC,IAAa;IAC5B,OAAO,eAAe,CAAC,IAAI,CAAC,CAAC;AAC/B,CAAC;AAED;;;;;AAKA,SAAS,UAAU,CAAC,IAAa;IAC/B,IAAI,CAAC,UAAU,EAAE,CAAC;AACpB,CAAC;AAED;;;IAGM,iBAAiB,GAAGA,sBAAc,CACtC,UAAO,IAAa,EAAE,OAAe;IACnC,sBAAA,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,EAAA;SAAA,EACzB,UAAC,IAAa,EAAE,OAAe,IAAa,OAAA,OAAO,GAAA,EACnD;AAEF;;;;;;;;;;;;;AAaA,SAAS,SAAS,CAAC,IAAa,EAAE,OAAe;IAC/C,OAAO,iBAAiB,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;AAC1C,CAAC;AAED;;;;;;;;;AASA,SAAS,WAAW,CAAC,IAAa,EAAE,OAAe;IACjD,IAAI,CAAC,WAAW,CAAC,OAAO,CAAC,CAAC;AAC5B,CAAC;AAED;;;;;;;;;;;;;;AAcA,SAAS,IAAI,CACX,IAAa,EACb,OAAe,EACf,KAAqB,EACrB,IAAyB;IAEzB,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,KAAK,EAAE,IAAI,CAAC,CAAC;AAClC,CAAC;AAED,SAAe,KAAK,CAClB,IAAa,EACb,MAAc,EACd,SAAiB,EACjB,SAAiB;;;;;wBAEF,qBAAM,IAAI,CAAC,KAAK,CAAC,MAAM,EAAE,SAAS,EAAE,SAAS,CAAC,EAAA;;oBAAvD,MAAM,GAAG,SAA8C;oBAE7D,sBAAO,MAAM,EAAC;;;;;;;;;;;;;;;;;;;;;;;"}
1
+ {"version":3,"file":"index.cjs.js","sources":["../src/types.ts","../src/helpers.ts","../src/connect_impl.ts","../src/connect.ts"],"sourcesContent":["/**\n * @license\n * types.ts\n * connect-kit\n *\n * Created by Rygor Kharytanovich <rygor@monterosa.co.uk> on 2022-01-19\n * Copyright © 2022 Monterosa. All rights reserved.\n *\n * More details on the license can be found at https://www.monterosa.co/sdk/license\n */\n\nimport { MonterosaKit } from '@monterosa/sdk-core';\nimport { Emitter } from '@monterosa/sdk-util';\n\n/**\n * @internal\n */\nexport const KIT_NAME = 'connect';\n\n/**\n * Represents a Connect Kit instance. Obtain one via {@link getConnect}.\n */\nexport interface Connect extends MonterosaKit, Emitter {\n /**\n * Connection state\n */\n state: ConnState;\n\n /**\n * @internal\n */\n initState: InitState;\n\n /**\n * @internal\n */\n init(host: string): Promise<void>;\n\n /**\n * @internal\n */\n connect(): Promise<void>;\n\n /**\n * @internal\n */\n disconnect(): void;\n\n /**\n * @internal\n */\n subscribe(channel: string): Promise<void>;\n\n /**\n * @internal\n */\n unsubscribe(channel: string): void;\n\n /**\n * @internal\n */\n send(channel: string, klass: string, body: (string | number)[]): void;\n\n /**\n * @internal\n */\n login(userId: string, timestamp: number, signature: string): Promise<void>;\n}\n\n/**\n * @internal\n */\nexport enum InitState {\n NotInitialised = 'not_initialised',\n Initialised = 'initialised',\n Initialising = 'initialising',\n}\n\n/**\n * A list of possible connection states.\n *\n * @remarks\n * Can be used with {@link Connect | Connect.state} to determine the current state.\n *\n * By default {@link Connect} is in the disconnected state.\n * When {@link connect} invoked `Connect` state switches to\n * `ConnState.Connecting` and connection is maintained permanently until\n * {@link disconnect} method is invoked. If connection was unsucessful,\n * a new attempt will start after a short delay and `ConnState.connecting`\n * is triggered. Delays between attempts increased based on this sequence:\n * 0, 3, 5, 10, 20, 30, 45, 60 where 60 seconds is the max.\n *\n * If `Connect` was previously connected and for any reason connection is lost,\n * the state changed to `ConnState.Connecting` again and the connection process\n * starts again automatically.\n *\n * @example\n * ```javascript\n * if (conn.state === ConnState.Connecting) {\n * // display \"connecting\" message\n * }\n * ```\n */\nexport enum ConnState {\n /**\n * Disconnected state\n */\n Disconnected = 'disconnected',\n /**\n * Connecting state\n */\n Connecting = 'connecting',\n /**\n * Connected state\n */\n Connected = 'connected',\n}\n\n/**\n * Class of the {@link Message}\n */\nexport enum Klass {\n /**\n * Authentication request. Sent by server on client connection.\n */\n Authenticate = 'auth',\n /**\n * Authentication response. Sent by client as response\n * to authentication request.\n */\n AuthenticateResponse = 'authr',\n /**\n * Authentication acknowledgement. Sent by the server as confirmation\n * of successful authentication.\n */\n AuthenticateSuccess = 'authok',\n /**\n * Ping message is sent by server periodically as a keep-alive.\n * Ping message is only sent if no messages are sent for specific time\n * period, meaning if there are other messages sent frequently enough\n * then ping messages are not sent at all.\n */\n Ping = 'ping',\n /**\n * Subscribe to a channel. Sent by the client to subscribe to a channel\n */\n Subscribe = 'sub',\n /**\n * Unsubscribe from a channel. Sent by the client to unsubscribe from a channel\n */\n Unsububscribe = 'unsub',\n /**\n * End of cache. Sent by the server to notify that all messages are sent from\n * a channel cache\n */\n EndOfCache = 'eoc',\n /**\n * User login. Sent by client to supply verified user identity to the server.\n */\n Login = 'login',\n /**\n * User login success\n */\n LoginSuccess = 'login_ok',\n /**\n * User login failure\n */\n LoginFail = 'login_fail',\n /**\n * Averages counters. Client can submit a counter which is processed together\n * with other counters to determine the average of all counters within specific\n * demographics segment.\n */\n Counter = 'counter',\n /**\n * Client votes. Sents by a user to vote for a poll option\n */\n Vote = 'v',\n}\n\n/**\n * Represents a message received from or sent to the server.\n * Subscribe via {@link onMessage} to receive messages.\n */\nexport interface Message {\n /**\n * The channel on which this message was received. You must\n * {@link subscribe} to a channel to start receiving messages.\n */\n channel: string;\n /**\n * The class indicates the desired action to be performed or the result of\n * the action. E.g. in order to authenticate a developer should {@link send}\n * {@link Klass | Klass.AuthenticateResponse} klass message as a response on\n * a received {@link Klass | Klass.Authenticate} message\n */\n klass: Klass | string;\n /**\n * The timestamp (in seconds) when this message was sent\n */\n sent_at: number;\n /**\n * The message payload. Each piece of data is a `string`\n */\n body: string[];\n}\n","/**\n * @license\n * helpers.ts\n * connect-kit\n *\n * Created by Rygor Kharytanovich <rygor@monterosa.co.uk> on 2022-09-14\n * Copyright © 2022 Monterosa. All rights reserved.\n *\n * More details on the license can be found at https://www.monterosa.co/sdk/license\n */\n\nimport { Unsubscribe } from '@monterosa/sdk-util';\n\nimport { Connect, ConnState } from './types';\n\nexport function onState(\n conn: Connect,\n needle: ConnState,\n callback: Function,\n): Unsubscribe {\n const handler = (state: ConnState, ...rest: unknown[]) => {\n if (state === needle) {\n callback(...rest);\n }\n };\n\n conn.on('state', handler);\n\n return () => conn.off('state', handler);\n}\n","/* eslint class-methods-use-this: \"off\" */\n\n/**\n * @license\n * connect_impl.ts\n * connect-kit\n *\n * Created by Rygor Kharytanovich <rygor@monterosa.co.uk> on 2022-02-15\n * Copyright © 2022 Monterosa. All rights reserved.\n *\n * More details on the license can be found at https://www.monterosa.co/sdk/license\n */\n\nimport Enmasse from '@monterosa/sdk-enmasse-kit';\nimport { Emitter, setTimestamp } from '@monterosa/sdk-util';\n\nimport { Connect, ConnState, InitState, Message } from './types';\n\n/**\n * @internal\n */\nexport class ConnectImpl extends Emitter implements Connect {\n private static instance: ConnectImpl;\n public initState: InitState = InitState.NotInitialised;\n private initPromise: Promise<void> = Promise.resolve();\n\n private constructor() {\n super();\n\n Enmasse.bind(\n Enmasse.ON_STATE,\n (state: Enmasse.States, ...rest: unknown[]) => {\n if (state === Enmasse.STATE_CONNECTED) {\n const [timestamp] = rest as [number];\n\n setTimestamp(timestamp);\n }\n\n this.emit('state', ConnectImpl.toConnState(state), ...rest);\n },\n );\n\n Enmasse.bind(Enmasse.ON_MESSAGE, (message: Enmasse.Message) => {\n this.emit('message', <Message>message);\n });\n }\n\n get state() {\n return ConnectImpl.toConnState(Enmasse.getState());\n }\n\n public static getInstance(): ConnectImpl {\n if (!ConnectImpl.instance) {\n ConnectImpl.instance = new ConnectImpl();\n }\n\n return ConnectImpl.instance;\n }\n\n static toConnState(state: Enmasse.States): ConnState {\n switch (state) {\n default:\n case Enmasse.STATE_DISCONNECTED:\n return ConnState.Disconnected;\n case Enmasse.STATE_CONNECTING:\n return ConnState.Connecting;\n case Enmasse.STATE_CONNECTED:\n return ConnState.Connected;\n }\n }\n\n async init(host: string): Promise<void> {\n if (\n this.initState === InitState.Initialising ||\n this.initState === InitState.Initialised\n ) {\n return this.initPromise;\n }\n\n this.initPromise = new Promise((resolve, reject) => {\n this.initState = InitState.Initialising;\n\n Enmasse.bind(Enmasse.ON_READY, () => {\n this.initState = InitState.Initialised;\n\n Enmasse.unbind(Enmasse.ON_READY);\n Enmasse.unbind(Enmasse.ON_ERROR);\n\n resolve();\n });\n\n Enmasse.bind(Enmasse.ON_ERROR, (err: string) => {\n this.initState = InitState.NotInitialised;\n\n Enmasse.unbind(Enmasse.ON_READY);\n Enmasse.unbind(Enmasse.ON_ERROR);\n\n reject(new Error(`Unable to initialise connect kit: ${err}`));\n });\n\n Enmasse.init({\n config: `https://${host}/config/enmasse.json`,\n });\n });\n\n return this.initPromise;\n }\n\n async connect(): Promise<void> {\n if (this.initState !== InitState.Initialised) {\n throw new Error('must be initialised before connecting');\n }\n\n if (this.state === ConnState.Connected) {\n throw new Error('already connected');\n }\n\n if (this.state === ConnState.Connecting) {\n throw new Error('already connecting');\n }\n\n return new Promise((resolve, reject) => {\n const handler = (state: ConnState) => {\n if (state === ConnState.Connected) {\n Enmasse.unbind(Enmasse.ON_STATE, handler);\n\n resolve();\n }\n\n if (state === ConnState.Disconnected) {\n Enmasse.unbind(Enmasse.ON_STATE, handler);\n\n reject(new Error('disconnected'));\n }\n };\n\n Enmasse.bind(Enmasse.ON_STATE, handler);\n\n Enmasse.connect();\n });\n }\n\n disconnect() {\n if (this.state !== ConnState.Connected) {\n throw new Error('must be connected before disconnecting');\n }\n\n Enmasse.disconnect();\n }\n\n subscribe(channel: string): Promise<void> {\n return new Promise((resolve) => {\n const handler = (ch: string) => {\n if (ch !== channel) return;\n\n Enmasse.unbind(Enmasse.ON_EOC, handler);\n\n resolve();\n };\n\n Enmasse.bind(Enmasse.ON_EOC, handler);\n\n Enmasse.subscribe(channel);\n });\n }\n\n unsubscribe(channel: string): void {\n Enmasse.unsubscribe(channel);\n }\n\n send(channel: string, klass: string, body: (string | number)[]): void {\n Enmasse.send(channel, klass, body);\n }\n\n async login(\n userId: string,\n timestamp: number,\n signature: string,\n ): Promise<void> {\n return new Promise((resolve, reject) => {\n Enmasse.login(userId, timestamp, signature, resolve, () => {\n reject(new Error('Login failure'));\n });\n });\n }\n\n delete() {}\n}\n","/**\n * @license\n * connect.ts\n * connect-kit\n *\n * Created by Rygor Kharytanovich <rygor@monterosa.co.uk> on 2022-01-19\n * Copyright © 2022 Monterosa. All rights reserved.\n *\n * More details on the license can be found at https://www.monterosa.co/sdk/license\n */\n\nimport { getSdk } from '@monterosa/sdk-core';\nimport { memoizePromise, Unsubscribe } from '@monterosa/sdk-util';\n\nimport { Connect, ConnState, Message, Klass } from './types';\nimport { onState } from './helpers';\nimport { ConnectImpl } from './connect_impl';\n\n/**\n * @internal\n */\nconst getConnectMemoized = memoizePromise(\n async (host: string): Promise<Connect> => {\n const conn = ConnectImpl.getInstance();\n\n await conn.init(host);\n\n return conn;\n },\n (host: string): string => host,\n);\n\n/**\n * Returns the {@link Connect} instance associated with the provided\n * host. If host is not provided, it returns Connect instance for\n * the host associated with the default sdk.\n *\n * @param host - The host URL. If not provided, uses the host\n * from the default SDK instance.\n */\nfunction getConnect(host?: string): Promise<Connect> {\n if (host === undefined) {\n const sdk = getSdk();\n\n host = sdk.options.host;\n }\n\n return getConnectMemoized(host);\n}\n\n/**\n * Adds an observer for when connection state changed to connected.\n *\n * @param conn - The {@link Connect} instance\n * @param callback - The callback triggered when {@link ConnState | state}\n * changed to connected\n */\nfunction onConnected(\n conn: Connect,\n callback: (timestamp: number) => void,\n): Unsubscribe {\n return onState(conn, ConnState.Connected, callback);\n}\n\n/**\n * Adds an observer for when connection state changed to connecting.\n *\n * @param conn - The {@link Connect} instance\n * @param callback - callback triggered on connecting state. Two parameters\n * are provided: `attempt` — sequential number of connect attempt and\n * `delay` - delay in seconds before the next attempt\n */\nfunction onConnecting(\n conn: Connect,\n callback: (attempt: number, delay: number) => void,\n): Unsubscribe {\n return onState(conn, ConnState.Connecting, callback);\n}\n\n/**\n * Adds an observer for when connection state changed to disconnected.\n *\n * @param conn - The {@link Connect} instance\n * @param callback - callback triggered on disconnected state\n */\nfunction onDisconnected(conn: Connect, callback: () => void): Unsubscribe {\n return onState(conn, ConnState.Disconnected, callback);\n}\n\n/**\n * Adds an observer for when a new message received\n *\n * @example\n * ```javascript\n * const unsubscribe = onMessage(conn, (message) => {\n * console.log(message.body);\n * });\n * ```\n *\n * @param conn - The {@link Connect} instance\n * @param callback - The callback function triggered on message receive\n */\nfunction onMessage(\n conn: Connect,\n callback: (message: Message) => void,\n): Unsubscribe {\n const handler = (message: Message) => {\n callback(message);\n };\n\n conn.on('message', handler);\n\n return () => conn.off('message', handler);\n}\n\n/**\n * @internal\n */\nconst connectMemoized = memoizePromise(\n async (conn: Connect): Promise<void> => {\n await conn.connect();\n },\n (conn: Connect): Connect => conn,\n {\n clearOnResolve: true,\n clearOnReject: true,\n },\n);\n\n/**\n * Starts a connection process.\n *\n * @remarks\n * If connect is successful a promise resolved by\n * and {@link onConnected} is triggered, otherwise a next attempt will be\n * automatically invoked and {@link onConnecting} is triggered.\n *\n * SDK connects automatically when its created. Connection procedure can be\n * aborted by calling {@link disconnect} function and the promise is rejected.\n *\n * @example\n * ```javascript\n * try {\n * const conn = await getConnect();\n *\n * await connect(conn);\n *\n * console.log('connected!');\n * } catch (err) {\n * console.log(err)\n * }\n * ```\n *\n * State of the connection can be handled separately\n *\n * @example\n * ```javascript\n * onConnected(() => console.log('connected'));\n *\n * onConnecting((attempt, delay) => console.log('connecting'));\n *\n * onDisconnected(() => console.log('disconnected'));\n * ```\n *\n * @param conn - The {@link Connect} instance\n * @param host - Interaction Cloud Studio host.\n * Can be found in Project Settings in API tab\n */\nfunction connect(conn: Connect): Promise<void> {\n return connectMemoized(conn);\n}\n\n/**\n * Disconnect from the server and triggers {@link onDisconnected}\n *\n * @param conn - The {@link Connect} instance\n */\nfunction disconnect(conn: Connect): void {\n conn.disconnect();\n}\n\n/**\n * @internal\n */\nconst subscribeMemoized = memoizePromise(\n async (conn: Connect, channel: string): Promise<void> =>\n conn.subscribe(channel),\n (conn: Connect, channel: string): string => channel,\n);\n\n/**\n * Subscribe to the server channel.\n *\n * @remarks\n * Any message published to a channel is {@link onMessage | received}\n * only by subscribers to that channel. The sdk out of the box handles\n * all required subscriptions internally and {@link onMessage | handles messages}\n * so there is no need in to subscribe by developers.\n *\n * To stop receiving messages on a channel {@link unsubscribe} should be called.\n *\n * @param conn - The {@link Connect} instance\n * @param channel - Channel name\n */\nfunction subscribe(conn: Connect, channel: string): Promise<void> {\n return subscribeMemoized(conn, channel);\n}\n\n/**\n * Unsubscribe from the server channel.\n *\n * @remarks\n * Removes subscription to the channel so that recipient no longer receive\n * messages from that channel.\n *\n * @param conn - The {@link Connect} instance\n * @param channel - Channel name\n */\nfunction unsubscribe(conn: Connect, channel: string): void {\n conn.unsubscribe(channel);\n}\n\n/**\n * Send message to the server.\n *\n * @remarks\n * The sdk out of the box provides more high level functions that uses\n * this function to send data to the server. Though a developer still can\n * use it if he knows a message protocol (klass and body) and a channel\n * that expect the message.\n *\n * @param conn - The Connect instance\n * @param channel - Channel name. Only {@link subscribe | subscribers}\n * to that channel can send messages.\n * @param klass - Message class name.\n * @param body - Message payload\n */\nfunction send(\n conn: Connect,\n channel: string,\n klass: Klass | string,\n body: (string | number)[],\n): void {\n conn.send(channel, klass, body);\n}\n\n/**\n * Authenticates the connection with user credentials.\n *\n * @remarks\n * The session signature can be obtained from Identify Kit using\n * {@link @monterosa/sdk-identify-kit#getSessionSignature}.\n *\n * @param conn - The Connect instance\n * @param userId - The user identifier\n * @param timestamp - The signature timestamp in seconds\n * @param signature - The session signature string\n *\n * @public\n */\nasync function login(\n conn: Connect,\n userId: string,\n timestamp: number,\n signature: string,\n): Promise<void> {\n const result = await conn.login(userId, timestamp, signature);\n\n return result;\n}\n\nexport {\n onConnected,\n onConnecting,\n onDisconnected,\n onMessage,\n disconnect,\n unsubscribe,\n send,\n getConnect,\n getConnectMemoized,\n connect,\n connectMemoized,\n subscribe,\n subscribeMemoized,\n login,\n};\n"],"names":["InitState","ConnState","Klass","Enmasse","setTimestamp","Emitter","memoizePromise","getSdk"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;;;AASG;AAKH;;AAEG;AACI,IAAM,QAAQ,GAAG,UAAU;AAoDlC;;AAEG;AACSA,2BAIX;AAJD,CAAA,UAAY,SAAS,EAAA;AACnB,IAAA,SAAA,CAAA,gBAAA,CAAA,GAAA,iBAAkC,CAAA;AAClC,IAAA,SAAA,CAAA,aAAA,CAAA,GAAA,aAA2B,CAAA;AAC3B,IAAA,SAAA,CAAA,cAAA,CAAA,GAAA,cAA6B,CAAA;AAC/B,CAAC,EAJWA,iBAAS,KAATA,iBAAS,GAIpB,EAAA,CAAA,CAAA,CAAA;AAED;;;;;;;;;;;;;;;;;;;;;;;;AAwBG;AACSC,2BAaX;AAbD,CAAA,UAAY,SAAS,EAAA;AACnB;;AAEG;AACH,IAAA,SAAA,CAAA,cAAA,CAAA,GAAA,cAA6B,CAAA;AAC7B;;AAEG;AACH,IAAA,SAAA,CAAA,YAAA,CAAA,GAAA,YAAyB,CAAA;AACzB;;AAEG;AACH,IAAA,SAAA,CAAA,WAAA,CAAA,GAAA,WAAuB,CAAA;AACzB,CAAC,EAbWA,iBAAS,KAATA,iBAAS,GAapB,EAAA,CAAA,CAAA,CAAA;AAED;;AAEG;AACSC,uBAyDX;AAzDD,CAAA,UAAY,KAAK,EAAA;AACf;;AAEG;AACH,IAAA,KAAA,CAAA,cAAA,CAAA,GAAA,MAAqB,CAAA;AACrB;;;AAGG;AACH,IAAA,KAAA,CAAA,sBAAA,CAAA,GAAA,OAA8B,CAAA;AAC9B;;;AAGG;AACH,IAAA,KAAA,CAAA,qBAAA,CAAA,GAAA,Q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A,EAAA,UAAA,EAAA,EAAA;AACnC,IAAA,OAAA,CAAA,CAAA,aAAA,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,CAAA,CAAA;SAAA,EACzB,UAAC,IAAa,EAAE,OAAe,EAAA,EAAa,OAAA,OAAO,CAAA,EAAA,EACnD;AAEF;;;;;;;;;;;;;AAaG;AACH,SAAS,SAAS,CAAC,IAAa,EAAE,OAAe,EAAA;AAC/C,IAAA,OAAO,iBAAiB,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;AAC1C,CAAC;AAED;;;;;;;;;AASG;AACH,SAAS,WAAW,CAAC,IAAa,EAAE,OAAe,EAAA;AACjD,IAAA,IAAI,CAAC,WAAW,CAAC,OAAO,CAAC,CAAC;AAC5B,CAAC;AAED;;;;;;;;;;;;;;AAcG;AACH,SAAS,IAAI,CACX,IAAa,EACb,OAAe,EACf,KAAqB,EACrB,IAAyB,EAAA;IAEzB,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,KAAK,EAAE,IAAI,CAAC,CAAC;AAClC,CAAC;AAED;;;;;;;;;;;;;AAaG;AACH,SAAe,KAAK,CAClB,IAAa,EACb,MAAc,EACd,SAAiB,EACjB,SAAiB,EAAA;;;;;wBAEF,OAAM,CAAA,CAAA,YAAA,IAAI,CAAC,KAAK,CAAC,MAAM,EAAE,SAAS,EAAE,SAAS,CAAC,CAAA,CAAA;;AAAvD,oBAAA,MAAM,GAAG,EAA8C,CAAA,IAAA,EAAA,CAAA;AAE7D,oBAAA,OAAA,CAAA,CAAA,aAAO,MAAM,CAAC,CAAA;;;;AACf;;;;;;;;;;;;;;;;;;;"}
package/dist/index.d.ts CHANGED
@@ -1,3 +1,8 @@
1
+ /**
2
+ * Establishes real-time connections to events and projects.
3
+ *
4
+ * @packageDocumentation
5
+ */
1
6
  /**
2
7
  * @license
3
8
  * index.ts
@@ -8,11 +13,6 @@
8
13
  *
9
14
  * More details on the license can be found at https://www.monterosa.co/sdk/license
10
15
  */
11
- /**
12
- * Monterosa SDK / Connect Kit
13
- *
14
- * @packageDocumentation
15
- */
16
16
  export * from './connect';
17
17
  export * from './connect_impl';
18
18
  export * from './types';
@@ -26,8 +26,10 @@ var InitState;
26
26
  InitState["Initialising"] = "initialising";
27
27
  })(InitState || (InitState = {}));
28
28
  /**
29
- * A list of possible connection states. Can be used with
30
- * {@link Connect.state} to determine the current state.
29
+ * A list of possible connection states.
30
+ *
31
+ * @remarks
32
+ * Can be used with {@link Connect | Connect.state} to determine the current state.
31
33
  *
32
34
  * By default {@link Connect} is in the disconnected state.
33
35
  * When {@link connect} invoked `Connect` state switches to
@@ -293,7 +295,8 @@ const getConnectMemoized = memoizePromise(async (host) => {
293
295
  * host. If host is not provided, it returns Connect instance for
294
296
  * the host associated with the default sdk.
295
297
  *
296
- * @param sdk - Monterosa SDK.
298
+ * @param host - The host URL. If not provided, uses the host
299
+ * from the default SDK instance.
297
300
  */
298
301
  function getConnect(host) {
299
302
  if (host === undefined) {
@@ -362,7 +365,10 @@ const connectMemoized = memoizePromise(async (conn) => {
362
365
  clearOnReject: true,
363
366
  });
364
367
  /**
365
- * Starts a connection process. If connect is successful a promise resolved by
368
+ * Starts a connection process.
369
+ *
370
+ * @remarks
371
+ * If connect is successful a promise resolved by
366
372
  * and {@link onConnected} is triggered, otherwise a next attempt will be
367
373
  * automatically invoked and {@link onConnecting} is triggered.
368
374
  *
@@ -415,6 +421,7 @@ const subscribeMemoized = memoizePromise(async (conn, channel) => conn.subscribe
415
421
  /**
416
422
  * Subscribe to the server channel.
417
423
  *
424
+ * @remarks
418
425
  * Any message published to a channel is {@link onMessage | received}
419
426
  * only by subscribers to that channel. The sdk out of the box handles
420
427
  * all required subscriptions internally and {@link onMessage | handles messages}
@@ -431,6 +438,7 @@ function subscribe(conn, channel) {
431
438
  /**
432
439
  * Unsubscribe from the server channel.
433
440
  *
441
+ * @remarks
434
442
  * Removes subscription to the channel so that recipient no longer receive
435
443
  * messages from that channel.
436
444
  *
@@ -443,6 +451,7 @@ function unsubscribe(conn, channel) {
443
451
  /**
444
452
  * Send message to the server.
445
453
  *
454
+ * @remarks
446
455
  * The sdk out of the box provides more high level functions that uses
447
456
  * this function to send data to the server. Though a developer still can
448
457
  * use it if he knows a message protocol (klass and body) and a channel
@@ -457,6 +466,20 @@ function unsubscribe(conn, channel) {
457
466
  function send(conn, channel, klass, body) {
458
467
  conn.send(channel, klass, body);
459
468
  }
469
+ /**
470
+ * Authenticates the connection with user credentials.
471
+ *
472
+ * @remarks
473
+ * The session signature can be obtained from Identify Kit using
474
+ * {@link @monterosa/sdk-identify-kit#getSessionSignature}.
475
+ *
476
+ * @param conn - The Connect instance
477
+ * @param userId - The user identifier
478
+ * @param timestamp - The signature timestamp in seconds
479
+ * @param signature - The session signature string
480
+ *
481
+ * @public
482
+ */
460
483
  async function login(conn, userId, timestamp, signature) {
461
484
  const result = await conn.login(userId, timestamp, signature);
462
485
  return result;
@@ -1 +1 @@
1
- {"version":3,"file":"index.esm2017.js","sources":["../src/types.ts","../src/helpers.ts","../src/connect_impl.ts","../src/connect.ts"],"sourcesContent":["/**\n * @license\n * types.ts\n * connect-kit\n *\n * Created by Rygor Kharytanovich <rygor@monterosa.co.uk> on 2022-01-19\n * Copyright © 2022 Monterosa. All rights reserved.\n *\n * More details on the license can be found at https://www.monterosa.co/sdk/license\n */\n\nimport { MonterosaKit } from '@monterosa/sdk-core';\nimport { Emitter } from '@monterosa/sdk-util';\n\n/**\n * @internal\n */\nexport const KIT_NAME = 'connect';\n\n/**\n * Interface representing Connect kit.\n */\nexport interface Connect extends MonterosaKit, Emitter {\n /**\n * Connection state\n */\n state: ConnState;\n\n /**\n * @internal\n */\n initState: InitState;\n\n /**\n * @internal\n */\n init(host: string): Promise<void>;\n\n /**\n * @internal\n */\n connect(): Promise<void>;\n\n /**\n * @internal\n */\n disconnect(): void;\n\n /**\n * @internal\n */\n subscribe(channel: string): Promise<void>;\n\n /**\n * @internal\n */\n unsubscribe(channel: string): void;\n\n /**\n * @internal\n */\n send(channel: string, klass: string, body: (string | number)[]): void;\n\n login(userId: string, timestamp: number, signature: string): Promise<void>;\n}\n\n/**\n * @internal\n */\nexport enum InitState {\n NotInitialised = 'not_initialised',\n Initialised = 'initialised',\n Initialising = 'initialising',\n}\n\n/**\n * A list of possible connection states. Can be used with\n * {@link Connect.state} to determine the current state.\n *\n * By default {@link Connect} is in the disconnected state.\n * When {@link connect} invoked `Connect` state switches to\n * `ConnState.Connecting` and connection is maintained permanently until\n * {@link disconnect} method is invoked. If connection was unsucessful,\n * a new attempt will start after a short delay and `ConnState.connecting`\n * is triggered. Delays between attempts increased based on this sequence:\n * 0, 3, 5, 10, 20, 30, 45, 60 where 60 seconds is the max.\n *\n * If `Connect` was previously connected and for any reason connection is lost,\n * the state changed to `ConnState.Connecting` again and the connection process\n * starts again automatically.\n *\n * @example\n * ```javascript\n * if (conn.state === ConnState.Connecting) {\n * // display \"connecting\" message\n * }\n * ```\n */\nexport enum ConnState {\n /**\n * Disconnected state\n */\n Disconnected = 'disconnected',\n /**\n * Connecting state\n */\n Connecting = 'connecting',\n /**\n * Connected state\n */\n Connected = 'connected',\n}\n\n/**\n * Class of the {@link Message}\n */\nexport enum Klass {\n /**\n * Authentication request. Sent by server on client connection.\n */\n Authenticate = 'auth',\n /**\n * Authentication response. Sent by client as response\n * to authentication request.\n */\n AuthenticateResponse = 'authr',\n /**\n * Authentication acknowledgement. Sent by the server as confirmation\n * of successful authentication.\n */\n AuthenticateSuccess = 'authok',\n /**\n * Ping message is sent by server periodically as a keep-alive.\n * Ping message is only sent if no messages are sent for specific time\n * period, meaning if there are other messages sent frequently enough\n * then ping messages are not sent at all.\n */\n Ping = 'ping',\n /**\n * Subscribe to a channel. Sent by the client to subscribe to a channel\n */\n Subscribe = 'sub',\n /**\n * Unsubscribe from a channel. Sent by the client to unsubscribe from a channel\n */\n Unsububscribe = 'unsub',\n /**\n * End of cache. Sent by the server to notify that all messages are sent from\n * a channel cache\n */\n EndOfCache = 'eoc',\n /**\n * User login. Sent by client to supply verified user identity to the server.\n */\n Login = 'login',\n /**\n * User login success\n */\n LoginSuccess = 'login_ok',\n /**\n * User login failure\n */\n LoginFail = 'login_fail',\n /**\n * Averages counters. Client can submit a counter which is processed together\n * with other counters to determine the average of all counters within specific\n * demographics segment.\n */\n Counter = 'counter',\n /**\n * Client votes. Sents by a user to vote for a poll option\n */\n Vote = 'v',\n}\n\n/**\n * A Message to be sent to the Monterosa / Interaction Cloud\n */\nexport interface Message {\n /**\n * The channel id on which this message was received. It s required to be\n * {@link subscribe} to that channel in order to start receiving\n * messages\n */\n channel: string;\n /**\n * The class indicates the desired action to be performed or the result of\n * the action. E.g. in order to authenticate a developer should {@link send}\n * {@link Klass.AuthenticateResponse} klass message as a response on\n * a received {@link Klass.Authenticate} message\n */\n klass: Klass | string;\n /**\n * The timestamp (in seconds) when this message was sent\n */\n sent_at: number;\n /**\n * The message payload. Each peace of data is a `string`\n */\n body: string[];\n}\n","/**\n * @license\n * helpers.ts\n * connect-kit\n *\n * Created by Rygor Kharytanovich <rygor@monterosa.co.uk> on 2022-09-14\n * Copyright © 2022 Monterosa. All rights reserved.\n *\n * More details on the license can be found at https://www.monterosa.co/sdk/license\n */\n\nimport { Unsubscribe } from '@monterosa/sdk-util';\n\nimport { Connect, ConnState } from './types';\n\nexport function onState(\n conn: Connect,\n needle: ConnState,\n callback: Function,\n): Unsubscribe {\n const handler = (state: ConnState, ...rest: unknown[]) => {\n if (state === needle) {\n callback(...rest);\n }\n };\n\n conn.on('state', handler);\n\n return () => conn.off('state', handler);\n}\n","/* eslint class-methods-use-this: \"off\" */\n\n/**\n * @license\n * connect_impl.ts\n * connect-kit\n *\n * Created by Rygor Kharytanovich <rygor@monterosa.co.uk> on 2022-02-15\n * Copyright © 2022 Monterosa. All rights reserved.\n *\n * More details on the license can be found at https://www.monterosa.co/sdk/license\n */\n\nimport Enmasse from '@monterosa/sdk-enmasse-kit';\nimport { Emitter, setTimestamp } from '@monterosa/sdk-util';\n\nimport { Connect, ConnState, InitState, Message } from './types';\n\n/**\n * @internal\n */\nexport class ConnectImpl extends Emitter implements Connect {\n private static instance: ConnectImpl;\n public initState: InitState = InitState.NotInitialised;\n private initPromise: Promise<void> = Promise.resolve();\n\n private constructor() {\n super();\n\n Enmasse.bind(\n Enmasse.ON_STATE,\n (state: Enmasse.States, ...rest: unknown[]) => {\n if (state === Enmasse.STATE_CONNECTED) {\n const [timestamp] = rest as [number];\n\n setTimestamp(timestamp);\n }\n\n this.emit('state', ConnectImpl.toConnState(state), ...rest);\n },\n );\n\n Enmasse.bind(Enmasse.ON_MESSAGE, (message: Enmasse.Message) => {\n this.emit('message', <Message>message);\n });\n }\n\n get state() {\n return ConnectImpl.toConnState(Enmasse.getState());\n }\n\n public static getInstance(): ConnectImpl {\n if (!ConnectImpl.instance) {\n ConnectImpl.instance = new ConnectImpl();\n }\n\n return ConnectImpl.instance;\n }\n\n static toConnState(state: Enmasse.States): ConnState {\n switch (state) {\n default:\n case Enmasse.STATE_DISCONNECTED:\n return ConnState.Disconnected;\n case Enmasse.STATE_CONNECTING:\n return ConnState.Connecting;\n case Enmasse.STATE_CONNECTED:\n return ConnState.Connected;\n }\n }\n\n async init(host: string): Promise<void> {\n if (\n this.initState === InitState.Initialising ||\n this.initState === InitState.Initialised\n ) {\n return this.initPromise;\n }\n\n this.initPromise = new Promise((resolve, reject) => {\n this.initState = InitState.Initialising;\n\n Enmasse.bind(Enmasse.ON_READY, () => {\n this.initState = InitState.Initialised;\n\n Enmasse.unbind(Enmasse.ON_READY);\n Enmasse.unbind(Enmasse.ON_ERROR);\n\n resolve();\n });\n\n Enmasse.bind(Enmasse.ON_ERROR, (err: string) => {\n this.initState = InitState.NotInitialised;\n\n Enmasse.unbind(Enmasse.ON_READY);\n Enmasse.unbind(Enmasse.ON_ERROR);\n\n reject(new Error(`Unable to initialise connect kit: ${err}`));\n });\n\n Enmasse.init({\n config: `https://${host}/config/enmasse.json`,\n });\n });\n\n return this.initPromise;\n }\n\n async connect(): Promise<void> {\n if (this.initState !== InitState.Initialised) {\n throw new Error('must be initialised before connecting');\n }\n\n if (this.state === ConnState.Connected) {\n throw new Error('already connected');\n }\n\n if (this.state === ConnState.Connecting) {\n throw new Error('already connecting');\n }\n\n return new Promise((resolve, reject) => {\n const handler = (state: ConnState) => {\n if (state === ConnState.Connected) {\n Enmasse.unbind(Enmasse.ON_STATE, handler);\n\n resolve();\n }\n\n if (state === ConnState.Disconnected) {\n Enmasse.unbind(Enmasse.ON_STATE, handler);\n\n reject(new Error('disconnected'));\n }\n };\n\n Enmasse.bind(Enmasse.ON_STATE, handler);\n\n Enmasse.connect();\n });\n }\n\n disconnect() {\n if (this.state !== ConnState.Connected) {\n throw new Error('must be connected before disconnecting');\n }\n\n Enmasse.disconnect();\n }\n\n subscribe(channel: string): Promise<void> {\n return new Promise((resolve) => {\n const handler = (ch: string) => {\n if (ch !== channel) return;\n\n Enmasse.unbind(Enmasse.ON_EOC, handler);\n\n resolve();\n };\n\n Enmasse.bind(Enmasse.ON_EOC, handler);\n\n Enmasse.subscribe(channel);\n });\n }\n\n unsubscribe(channel: string): void {\n Enmasse.unsubscribe(channel);\n }\n\n send(channel: string, klass: string, body: (string | number)[]): void {\n Enmasse.send(channel, klass, body);\n }\n\n async login(\n userId: string,\n timestamp: number,\n signature: string,\n ): Promise<void> {\n return new Promise((resolve, reject) => {\n Enmasse.login(userId, timestamp, signature, resolve, () => {\n reject(new Error('Login failure'));\n });\n });\n }\n\n delete() {}\n}\n","/**\n * @license\n * connect.ts\n * connect-kit\n *\n * Created by Rygor Kharytanovich <rygor@monterosa.co.uk> on 2022-01-19\n * Copyright © 2022 Monterosa. All rights reserved.\n *\n * More details on the license can be found at https://www.monterosa.co/sdk/license\n */\n\nimport { getSdk } from '@monterosa/sdk-core';\nimport { memoizePromise, Unsubscribe } from '@monterosa/sdk-util';\n\nimport { Connect, ConnState, Message, Klass } from './types';\nimport { onState } from './helpers';\nimport { ConnectImpl } from './connect_impl';\n\n/**\n * @internal\n */\nconst getConnectMemoized = memoizePromise(\n async (host: string): Promise<Connect> => {\n const conn = ConnectImpl.getInstance();\n\n await conn.init(host);\n\n return conn;\n },\n (host: string): string => host,\n);\n\n/**\n * Returns the {@link Connect} instance associated with the provided\n * host. If host is not provided, it returns Connect instance for\n * the host associated with the default sdk.\n *\n * @param sdk - Monterosa SDK.\n */\nfunction getConnect(host?: string): Promise<Connect> {\n if (host === undefined) {\n const sdk = getSdk();\n\n host = sdk.options.host;\n }\n\n return getConnectMemoized(host);\n}\n\n/**\n * Adds an observer for when connection state changed to connected.\n *\n * @param conn - The {@link Connect} instance\n * @param callback - The callback triggered when {@link ConnState | state}\n * changed to connected\n */\nfunction onConnected(\n conn: Connect,\n callback: (timestamp: number) => void,\n): Unsubscribe {\n return onState(conn, ConnState.Connected, callback);\n}\n\n/**\n * Adds an observer for when connection state changed to connecting.\n *\n * @param conn - The {@link Connect} instance\n * @param callback - callback triggered on connecting state. Two parameters\n * are provided: `attempt` — sequential number of connect attempt and\n * `delay` - delay in seconds before the next attempt\n */\nfunction onConnecting(\n conn: Connect,\n callback: (attempt: number, delay: number) => void,\n): Unsubscribe {\n return onState(conn, ConnState.Connecting, callback);\n}\n\n/**\n * Adds an observer for when connection state changed to disconnected.\n *\n * @param conn - The {@link Connect} instance\n * @param callback - callback triggered on disconnected state\n */\nfunction onDisconnected(conn: Connect, callback: () => void): Unsubscribe {\n return onState(conn, ConnState.Disconnected, callback);\n}\n\n/**\n * Adds an observer for when a new message received\n *\n * @example\n * ```javascript\n * const unsubscribe = onMessage(conn, (message) => {\n * console.log(message.body);\n * });\n * ```\n *\n * @param conn - The {@link Connect} instance\n * @param callback - The callback function triggered on message receive\n */\nfunction onMessage(\n conn: Connect,\n callback: (message: Message) => void,\n): Unsubscribe {\n const handler = (message: Message) => {\n callback(message);\n };\n\n conn.on('message', handler);\n\n return () => conn.off('message', handler);\n}\n\n/**\n * @internal\n */\nconst connectMemoized = memoizePromise(\n async (conn: Connect): Promise<void> => {\n await conn.connect();\n },\n (conn: Connect): Connect => conn,\n {\n clearOnResolve: true,\n clearOnReject: true,\n },\n);\n\n/**\n * Starts a connection process. If connect is successful a promise resolved by\n * and {@link onConnected} is triggered, otherwise a next attempt will be\n * automatically invoked and {@link onConnecting} is triggered.\n *\n * SDK connects automatically when its created. Connection procedure can be\n * aborted by calling {@link disconnect} function and the promise is rejected.\n *\n * @example\n * ```javascript\n * try {\n * const conn = await getConnect();\n *\n * await connect(conn);\n *\n * console.log('connected!');\n * } catch (err) {\n * console.log(err)\n * }\n * ```\n *\n * State of the connection can be handled separately\n *\n * @example\n * ```javascript\n * onConnected(() => console.log('connected'));\n *\n * onConnecting((attempt, delay) => console.log('connecting'));\n *\n * onDisconnected(() => console.log('disconnected'));\n * ```\n *\n * @param conn - The {@link Connect} instance\n * @param host - Interaction Cloud Studio host.\n * Can be found in Project Settings in API tab\n */\nfunction connect(conn: Connect): Promise<void> {\n return connectMemoized(conn);\n}\n\n/**\n * Disconnect from the server and triggers {@link onDisconnected}\n *\n * @param conn - The {@link Connect} instance\n */\nfunction disconnect(conn: Connect): void {\n conn.disconnect();\n}\n\n/**\n * @internal\n */\nconst subscribeMemoized = memoizePromise(\n async (conn: Connect, channel: string): Promise<void> =>\n conn.subscribe(channel),\n (conn: Connect, channel: string): string => channel,\n);\n\n/**\n * Subscribe to the server channel.\n *\n * Any message published to a channel is {@link onMessage | received}\n * only by subscribers to that channel. The sdk out of the box handles\n * all required subscriptions internally and {@link onMessage | handles messages}\n * so there is no need in to subscribe by developers.\n *\n * To stop receiving messages on a channel {@link unsubscribe} should be called.\n *\n * @param conn - The {@link Connect} instance\n * @param channel - Channel name\n */\nfunction subscribe(conn: Connect, channel: string): Promise<void> {\n return subscribeMemoized(conn, channel);\n}\n\n/**\n * Unsubscribe from the server channel.\n *\n * Removes subscription to the channel so that recipient no longer receive\n * messages from that channel.\n *\n * @param conn - The {@link Connect} instance\n * @param channel - Channel name\n */\nfunction unsubscribe(conn: Connect, channel: string): void {\n conn.unsubscribe(channel);\n}\n\n/**\n * Send message to the server.\n *\n * The sdk out of the box provides more high level functions that uses\n * this function to send data to the server. Though a developer still can\n * use it if he knows a message protocol (klass and body) and a channel\n * that expect the message.\n *\n * @param conn - The Connect instance\n * @param channel - Channel name. Only {@link subscribe | subscribers}\n * to that channel can send messages.\n * @param klass - Message class name.\n * @param body - Message payload\n */\nfunction send(\n conn: Connect,\n channel: string,\n klass: Klass | string,\n body: (string | number)[],\n): void {\n conn.send(channel, klass, body);\n}\n\nasync function login(\n conn: Connect,\n userId: string,\n timestamp: number,\n signature: string,\n): Promise<void> {\n const result = await conn.login(userId, timestamp, signature);\n\n return result;\n}\n\nexport {\n onConnected,\n onConnecting,\n onDisconnected,\n onMessage,\n disconnect,\n unsubscribe,\n send,\n getConnect,\n getConnectMemoized,\n connect,\n connectMemoized,\n subscribe,\n subscribeMemoized,\n login,\n};\n"],"names":[],"mappings":";;;;AAAA;;;;;;;;;;AAcA;;;MAGa,QAAQ,GAAG,UAAU;AAiDlC;;;IAGY;AAAZ,WAAY,SAAS;IACnB,+CAAkC,CAAA;IAClC,wCAA2B,CAAA;IAC3B,0CAA6B,CAAA;AAC/B,CAAC,EAJW,SAAS,KAAT,SAAS,QAIpB;AAED;;;;;;;;;;;;;;;;;;;;;;;IAuBY;AAAZ,WAAY,SAAS;;;;IAInB,0CAA6B,CAAA;;;;IAI7B,sCAAyB,CAAA;;;;IAIzB,oCAAuB,CAAA;AACzB,CAAC,EAbW,SAAS,KAAT,SAAS,QAapB;AAED;;;IAGY;AAAZ,WAAY,KAAK;;;;IAIf,8BAAqB,CAAA;;;;;IAKrB,uCAA8B,CAAA;;;;;IAK9B,uCAA8B,CAAA;;;;;;;IAO9B,sBAAa,CAAA;;;;IAIb,0BAAiB,CAAA;;;;IAIjB,gCAAuB,CAAA;;;;;IAKvB,2BAAkB,CAAA;;;;IAIlB,wBAAe,CAAA;;;;IAIf,kCAAyB,CAAA;;;;IAIzB,iCAAw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ACE,cAAc,EAAE,IAAI;IACpB,aAAa,EAAE,IAAI;CACpB,EACD;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAoCA,SAAS,OAAO,CAAC,IAAa;IAC5B,OAAO,eAAe,CAAC,IAAI,CAAC,CAAC;AAC/B,CAAC;AAED;;;;;AAKA,SAAS,UAAU,CAAC,IAAa;IAC/B,IAAI,CAAC,UAAU,EAAE,CAAC;AACpB,CAAC;AAED;;;MAGM,iBAAiB,GAAG,cAAc,CACtC,OAAO,IAAa,EAAE,OAAe,KACnC,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,EACzB,CAAC,IAAa,EAAE,OAAe,KAAa,OAAO,EACnD;AAEF;;;;;;;;;;;;;AAaA,SAAS,SAAS,CAAC,IAAa,EAAE,OAAe;IAC/C,OAAO,iBAAiB,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;AAC1C,CAAC;AAED;;;;;;;;;AASA,SAAS,WAAW,CAAC,IAAa,EAAE,OAAe;IACjD,IAAI,CAAC,WAAW,CAAC,OAAO,CAAC,CAAC;AAC5B,CAAC;AAED;;;;;;;;;;;;;;AAcA,SAAS,IAAI,CACX,IAAa,EACb,OAAe,EACf,KAAqB,EACrB,IAAyB;IAEzB,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,KAAK,EAAE,IAAI,CAAC,CAAC;AAClC,CAAC;AAED,eAAe,KAAK,CAClB,IAAa,EACb,MAAc,EACd,SAAiB,EACjB,SAAiB;IAEjB,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,KAAK,CAAC,MAAM,EAAE,SAAS,EAAE,SAAS,CAAC,CAAC;IAE9D,OAAO,MAAM,CAAC;AAChB;;;;"}
1
+ {"version":3,"file":"index.esm2017.js","sources":["../src/types.ts","../src/helpers.ts","../src/connect_impl.ts","../src/connect.ts"],"sourcesContent":["/**\n * @license\n * types.ts\n * connect-kit\n *\n * Created by Rygor Kharytanovich <rygor@monterosa.co.uk> on 2022-01-19\n * Copyright © 2022 Monterosa. All rights reserved.\n *\n * More details on the license can be found at https://www.monterosa.co/sdk/license\n */\n\nimport { MonterosaKit } from '@monterosa/sdk-core';\nimport { Emitter } from '@monterosa/sdk-util';\n\n/**\n * @internal\n */\nexport const KIT_NAME = 'connect';\n\n/**\n * Represents a Connect Kit instance. Obtain one via {@link getConnect}.\n */\nexport interface Connect extends MonterosaKit, Emitter {\n /**\n * Connection state\n */\n state: ConnState;\n\n /**\n * @internal\n */\n initState: InitState;\n\n /**\n * @internal\n */\n init(host: string): Promise<void>;\n\n /**\n * @internal\n */\n connect(): Promise<void>;\n\n /**\n * @internal\n */\n disconnect(): void;\n\n /**\n * @internal\n */\n subscribe(channel: string): Promise<void>;\n\n /**\n * @internal\n */\n unsubscribe(channel: string): void;\n\n /**\n * @internal\n */\n send(channel: string, klass: string, body: (string | number)[]): void;\n\n /**\n * @internal\n */\n login(userId: string, timestamp: number, signature: string): Promise<void>;\n}\n\n/**\n * @internal\n */\nexport enum InitState {\n NotInitialised = 'not_initialised',\n Initialised = 'initialised',\n Initialising = 'initialising',\n}\n\n/**\n * A list of possible connection states.\n *\n * @remarks\n * Can be used with {@link Connect | Connect.state} to determine the current state.\n *\n * By default {@link Connect} is in the disconnected state.\n * When {@link connect} invoked `Connect` state switches to\n * `ConnState.Connecting` and connection is maintained permanently until\n * {@link disconnect} method is invoked. If connection was unsucessful,\n * a new attempt will start after a short delay and `ConnState.connecting`\n * is triggered. Delays between attempts increased based on this sequence:\n * 0, 3, 5, 10, 20, 30, 45, 60 where 60 seconds is the max.\n *\n * If `Connect` was previously connected and for any reason connection is lost,\n * the state changed to `ConnState.Connecting` again and the connection process\n * starts again automatically.\n *\n * @example\n * ```javascript\n * if (conn.state === ConnState.Connecting) {\n * // display \"connecting\" message\n * }\n * ```\n */\nexport enum ConnState {\n /**\n * Disconnected state\n */\n Disconnected = 'disconnected',\n /**\n * Connecting state\n */\n Connecting = 'connecting',\n /**\n * Connected state\n */\n Connected = 'connected',\n}\n\n/**\n * Class of the {@link Message}\n */\nexport enum Klass {\n /**\n * Authentication request. Sent by server on client connection.\n */\n Authenticate = 'auth',\n /**\n * Authentication response. Sent by client as response\n * to authentication request.\n */\n AuthenticateResponse = 'authr',\n /**\n * Authentication acknowledgement. Sent by the server as confirmation\n * of successful authentication.\n */\n AuthenticateSuccess = 'authok',\n /**\n * Ping message is sent by server periodically as a keep-alive.\n * Ping message is only sent if no messages are sent for specific time\n * period, meaning if there are other messages sent frequently enough\n * then ping messages are not sent at all.\n */\n Ping = 'ping',\n /**\n * Subscribe to a channel. Sent by the client to subscribe to a channel\n */\n Subscribe = 'sub',\n /**\n * Unsubscribe from a channel. Sent by the client to unsubscribe from a channel\n */\n Unsububscribe = 'unsub',\n /**\n * End of cache. Sent by the server to notify that all messages are sent from\n * a channel cache\n */\n EndOfCache = 'eoc',\n /**\n * User login. Sent by client to supply verified user identity to the server.\n */\n Login = 'login',\n /**\n * User login success\n */\n LoginSuccess = 'login_ok',\n /**\n * User login failure\n */\n LoginFail = 'login_fail',\n /**\n * Averages counters. Client can submit a counter which is processed together\n * with other counters to determine the average of all counters within specific\n * demographics segment.\n */\n Counter = 'counter',\n /**\n * Client votes. Sents by a user to vote for a poll option\n */\n Vote = 'v',\n}\n\n/**\n * Represents a message received from or sent to the server.\n * Subscribe via {@link onMessage} to receive messages.\n */\nexport interface Message {\n /**\n * The channel on which this message was received. You must\n * {@link subscribe} to a channel to start receiving messages.\n */\n channel: string;\n /**\n * The class indicates the desired action to be performed or the result of\n * the action. E.g. in order to authenticate a developer should {@link send}\n * {@link Klass | Klass.AuthenticateResponse} klass message as a response on\n * a received {@link Klass | Klass.Authenticate} message\n */\n klass: Klass | string;\n /**\n * The timestamp (in seconds) when this message was sent\n */\n sent_at: number;\n /**\n * The message payload. Each piece of data is a `string`\n */\n body: string[];\n}\n","/**\n * @license\n * helpers.ts\n * connect-kit\n *\n * Created by Rygor Kharytanovich <rygor@monterosa.co.uk> on 2022-09-14\n * Copyright © 2022 Monterosa. All rights reserved.\n *\n * More details on the license can be found at https://www.monterosa.co/sdk/license\n */\n\nimport { Unsubscribe } from '@monterosa/sdk-util';\n\nimport { Connect, ConnState } from './types';\n\nexport function onState(\n conn: Connect,\n needle: ConnState,\n callback: Function,\n): Unsubscribe {\n const handler = (state: ConnState, ...rest: unknown[]) => {\n if (state === needle) {\n callback(...rest);\n }\n };\n\n conn.on('state', handler);\n\n return () => conn.off('state', handler);\n}\n","/* eslint class-methods-use-this: \"off\" */\n\n/**\n * @license\n * connect_impl.ts\n * connect-kit\n *\n * Created by Rygor Kharytanovich <rygor@monterosa.co.uk> on 2022-02-15\n * Copyright © 2022 Monterosa. All rights reserved.\n *\n * More details on the license can be found at https://www.monterosa.co/sdk/license\n */\n\nimport Enmasse from '@monterosa/sdk-enmasse-kit';\nimport { Emitter, setTimestamp } from '@monterosa/sdk-util';\n\nimport { Connect, ConnState, InitState, Message } from './types';\n\n/**\n * @internal\n */\nexport class ConnectImpl extends Emitter implements Connect {\n private static instance: ConnectImpl;\n public initState: InitState = InitState.NotInitialised;\n private initPromise: Promise<void> = Promise.resolve();\n\n private constructor() {\n super();\n\n Enmasse.bind(\n Enmasse.ON_STATE,\n (state: Enmasse.States, ...rest: unknown[]) => {\n if (state === Enmasse.STATE_CONNECTED) {\n const [timestamp] = rest as [number];\n\n setTimestamp(timestamp);\n }\n\n this.emit('state', ConnectImpl.toConnState(state), ...rest);\n },\n );\n\n Enmasse.bind(Enmasse.ON_MESSAGE, (message: Enmasse.Message) => {\n this.emit('message', <Message>message);\n });\n }\n\n get state() {\n return ConnectImpl.toConnState(Enmasse.getState());\n }\n\n public static getInstance(): ConnectImpl {\n if (!ConnectImpl.instance) {\n ConnectImpl.instance = new ConnectImpl();\n }\n\n return ConnectImpl.instance;\n }\n\n static toConnState(state: Enmasse.States): ConnState {\n switch (state) {\n default:\n case Enmasse.STATE_DISCONNECTED:\n return ConnState.Disconnected;\n case Enmasse.STATE_CONNECTING:\n return ConnState.Connecting;\n case Enmasse.STATE_CONNECTED:\n return ConnState.Connected;\n }\n }\n\n async init(host: string): Promise<void> {\n if (\n this.initState === InitState.Initialising ||\n this.initState === InitState.Initialised\n ) {\n return this.initPromise;\n }\n\n this.initPromise = new Promise((resolve, reject) => {\n this.initState = InitState.Initialising;\n\n Enmasse.bind(Enmasse.ON_READY, () => {\n this.initState = InitState.Initialised;\n\n Enmasse.unbind(Enmasse.ON_READY);\n Enmasse.unbind(Enmasse.ON_ERROR);\n\n resolve();\n });\n\n Enmasse.bind(Enmasse.ON_ERROR, (err: string) => {\n this.initState = InitState.NotInitialised;\n\n Enmasse.unbind(Enmasse.ON_READY);\n Enmasse.unbind(Enmasse.ON_ERROR);\n\n reject(new Error(`Unable to initialise connect kit: ${err}`));\n });\n\n Enmasse.init({\n config: `https://${host}/config/enmasse.json`,\n });\n });\n\n return this.initPromise;\n }\n\n async connect(): Promise<void> {\n if (this.initState !== InitState.Initialised) {\n throw new Error('must be initialised before connecting');\n }\n\n if (this.state === ConnState.Connected) {\n throw new Error('already connected');\n }\n\n if (this.state === ConnState.Connecting) {\n throw new Error('already connecting');\n }\n\n return new Promise((resolve, reject) => {\n const handler = (state: ConnState) => {\n if (state === ConnState.Connected) {\n Enmasse.unbind(Enmasse.ON_STATE, handler);\n\n resolve();\n }\n\n if (state === ConnState.Disconnected) {\n Enmasse.unbind(Enmasse.ON_STATE, handler);\n\n reject(new Error('disconnected'));\n }\n };\n\n Enmasse.bind(Enmasse.ON_STATE, handler);\n\n Enmasse.connect();\n });\n }\n\n disconnect() {\n if (this.state !== ConnState.Connected) {\n throw new Error('must be connected before disconnecting');\n }\n\n Enmasse.disconnect();\n }\n\n subscribe(channel: string): Promise<void> {\n return new Promise((resolve) => {\n const handler = (ch: string) => {\n if (ch !== channel) return;\n\n Enmasse.unbind(Enmasse.ON_EOC, handler);\n\n resolve();\n };\n\n Enmasse.bind(Enmasse.ON_EOC, handler);\n\n Enmasse.subscribe(channel);\n });\n }\n\n unsubscribe(channel: string): void {\n Enmasse.unsubscribe(channel);\n }\n\n send(channel: string, klass: string, body: (string | number)[]): void {\n Enmasse.send(channel, klass, body);\n }\n\n async login(\n userId: string,\n timestamp: number,\n signature: string,\n ): Promise<void> {\n return new Promise((resolve, reject) => {\n Enmasse.login(userId, timestamp, signature, resolve, () => {\n reject(new Error('Login failure'));\n });\n });\n }\n\n delete() {}\n}\n","/**\n * @license\n * connect.ts\n * connect-kit\n *\n * Created by Rygor Kharytanovich <rygor@monterosa.co.uk> on 2022-01-19\n * Copyright © 2022 Monterosa. All rights reserved.\n *\n * More details on the license can be found at https://www.monterosa.co/sdk/license\n */\n\nimport { getSdk } from '@monterosa/sdk-core';\nimport { memoizePromise, Unsubscribe } from '@monterosa/sdk-util';\n\nimport { Connect, ConnState, Message, Klass } from './types';\nimport { onState } from './helpers';\nimport { ConnectImpl } from './connect_impl';\n\n/**\n * @internal\n */\nconst getConnectMemoized = memoizePromise(\n async (host: string): Promise<Connect> => {\n const conn = ConnectImpl.getInstance();\n\n await conn.init(host);\n\n return conn;\n },\n (host: string): string => host,\n);\n\n/**\n * Returns the {@link Connect} instance associated with the provided\n * host. If host is not provided, it returns Connect instance for\n * the host associated with the default sdk.\n *\n * @param host - The host URL. If not provided, uses the host\n * from the default SDK instance.\n */\nfunction getConnect(host?: string): Promise<Connect> {\n if (host === undefined) {\n const sdk = getSdk();\n\n host = sdk.options.host;\n }\n\n return getConnectMemoized(host);\n}\n\n/**\n * Adds an observer for when connection state changed to connected.\n *\n * @param conn - The {@link Connect} instance\n * @param callback - The callback triggered when {@link ConnState | state}\n * changed to connected\n */\nfunction onConnected(\n conn: Connect,\n callback: (timestamp: number) => void,\n): Unsubscribe {\n return onState(conn, ConnState.Connected, callback);\n}\n\n/**\n * Adds an observer for when connection state changed to connecting.\n *\n * @param conn - The {@link Connect} instance\n * @param callback - callback triggered on connecting state. Two parameters\n * are provided: `attempt` — sequential number of connect attempt and\n * `delay` - delay in seconds before the next attempt\n */\nfunction onConnecting(\n conn: Connect,\n callback: (attempt: number, delay: number) => void,\n): Unsubscribe {\n return onState(conn, ConnState.Connecting, callback);\n}\n\n/**\n * Adds an observer for when connection state changed to disconnected.\n *\n * @param conn - The {@link Connect} instance\n * @param callback - callback triggered on disconnected state\n */\nfunction onDisconnected(conn: Connect, callback: () => void): Unsubscribe {\n return onState(conn, ConnState.Disconnected, callback);\n}\n\n/**\n * Adds an observer for when a new message received\n *\n * @example\n * ```javascript\n * const unsubscribe = onMessage(conn, (message) => {\n * console.log(message.body);\n * });\n * ```\n *\n * @param conn - The {@link Connect} instance\n * @param callback - The callback function triggered on message receive\n */\nfunction onMessage(\n conn: Connect,\n callback: (message: Message) => void,\n): Unsubscribe {\n const handler = (message: Message) => {\n callback(message);\n };\n\n conn.on('message', handler);\n\n return () => conn.off('message', handler);\n}\n\n/**\n * @internal\n */\nconst connectMemoized = memoizePromise(\n async (conn: Connect): Promise<void> => {\n await conn.connect();\n },\n (conn: Connect): Connect => conn,\n {\n clearOnResolve: true,\n clearOnReject: true,\n },\n);\n\n/**\n * Starts a connection process.\n *\n * @remarks\n * If connect is successful a promise resolved by\n * and {@link onConnected} is triggered, otherwise a next attempt will be\n * automatically invoked and {@link onConnecting} is triggered.\n *\n * SDK connects automatically when its created. Connection procedure can be\n * aborted by calling {@link disconnect} function and the promise is rejected.\n *\n * @example\n * ```javascript\n * try {\n * const conn = await getConnect();\n *\n * await connect(conn);\n *\n * console.log('connected!');\n * } catch (err) {\n * console.log(err)\n * }\n * ```\n *\n * State of the connection can be handled separately\n *\n * @example\n * ```javascript\n * onConnected(() => console.log('connected'));\n *\n * onConnecting((attempt, delay) => console.log('connecting'));\n *\n * onDisconnected(() => console.log('disconnected'));\n * ```\n *\n * @param conn - The {@link Connect} instance\n * @param host - Interaction Cloud Studio host.\n * Can be found in Project Settings in API tab\n */\nfunction connect(conn: Connect): Promise<void> {\n return connectMemoized(conn);\n}\n\n/**\n * Disconnect from the server and triggers {@link onDisconnected}\n *\n * @param conn - The {@link Connect} instance\n */\nfunction disconnect(conn: Connect): void {\n conn.disconnect();\n}\n\n/**\n * @internal\n */\nconst subscribeMemoized = memoizePromise(\n async (conn: Connect, channel: string): Promise<void> =>\n conn.subscribe(channel),\n (conn: Connect, channel: string): string => channel,\n);\n\n/**\n * Subscribe to the server channel.\n *\n * @remarks\n * Any message published to a channel is {@link onMessage | received}\n * only by subscribers to that channel. The sdk out of the box handles\n * all required subscriptions internally and {@link onMessage | handles messages}\n * so there is no need in to subscribe by developers.\n *\n * To stop receiving messages on a channel {@link unsubscribe} should be called.\n *\n * @param conn - The {@link Connect} instance\n * @param channel - Channel name\n */\nfunction subscribe(conn: Connect, channel: string): Promise<void> {\n return subscribeMemoized(conn, channel);\n}\n\n/**\n * Unsubscribe from the server channel.\n *\n * @remarks\n * Removes subscription to the channel so that recipient no longer receive\n * messages from that channel.\n *\n * @param conn - The {@link Connect} instance\n * @param channel - Channel name\n */\nfunction unsubscribe(conn: Connect, channel: string): void {\n conn.unsubscribe(channel);\n}\n\n/**\n * Send message to the server.\n *\n * @remarks\n * The sdk out of the box provides more high level functions that uses\n * this function to send data to the server. Though a developer still can\n * use it if he knows a message protocol (klass and body) and a channel\n * that expect the message.\n *\n * @param conn - The Connect instance\n * @param channel - Channel name. Only {@link subscribe | subscribers}\n * to that channel can send messages.\n * @param klass - Message class name.\n * @param body - Message payload\n */\nfunction send(\n conn: Connect,\n channel: string,\n klass: Klass | string,\n body: (string | number)[],\n): void {\n conn.send(channel, klass, body);\n}\n\n/**\n * Authenticates the connection with user credentials.\n *\n * @remarks\n * The session signature can be obtained from Identify Kit using\n * {@link @monterosa/sdk-identify-kit#getSessionSignature}.\n *\n * @param conn - The Connect instance\n * @param userId - The user identifier\n * @param timestamp - The signature timestamp in seconds\n * @param signature - The session signature string\n *\n * @public\n */\nasync function login(\n conn: Connect,\n userId: string,\n timestamp: number,\n signature: string,\n): Promise<void> {\n const result = await conn.login(userId, timestamp, signature);\n\n return result;\n}\n\nexport {\n onConnected,\n onConnecting,\n onDisconnected,\n onMessage,\n disconnect,\n unsubscribe,\n send,\n getConnect,\n getConnectMemoized,\n connect,\n connectMemoized,\n subscribe,\n subscribeMemoized,\n login,\n};\n"],"names":[],"mappings":";;;;AAAA;;;;;;;;;AASG;AAKH;;AAEG;AACI,MAAM,QAAQ,GAAG,UAAU;AAoDlC;;AAEG;IACS,UAIX;AAJD,CAAA,UAAY,SAAS,EAAA;AACnB,IAAA,SAAA,CAAA,gBAAA,CAAA,GAAA,iBAAkC,CAAA;AAClC,IAAA,SAAA,CAAA,aAAA,CAAA,GAAA,aAA2B,CAAA;AAC3B,IAAA,SAAA,CAAA,cAAA,CAAA,GAAA,cAA6B,CAAA;AAC/B,CAAC,EAJW,SAAS,KAAT,SAAS,GAIpB,EAAA,CAAA,CAAA,CAAA;AAED;;;;;;;;;;;;;;;;;;;;;;;;AAwBG;IACS,UAaX;AAbD,CAAA,UAAY,SAAS,EAAA;AACnB;;AAEG;AACH,IAAA,SAAA,CAAA,cAAA,CAAA,GAAA,cAA6B,CAAA;AAC7B;;AAEG;AACH,IAAA,SAAA,CAAA,YAAA,CAAA,GAAA,YAAyB,CAAA;AACzB;;AAEG;AACH,IAAA,SAAA,CAAA,WAAA,CAAA,GAAA,WAAuB,CAAA;AACzB,CAAC,EAbW,SAAS,KAAT,SAAS,GAapB,EAAA,CAAA,CAAA,CAAA;AAED;;AAEG;IACS,MAyDX;AAzDD,CAAA,UAAY,KAAK,EAAA;AACf;;AAEG;AACH,IAAA,KAAA,CAAA,cAAA,CAAA,GAAA,MAAqB,CAAA;AACrB;;;AAGG;AACH,IAAA,KAAA,CAAA,sBAAA,CAAA,GAAA,OAA8B,CAAA;AAC9B;;;AAGG;AACH,IAAA,KAAA,CAAA,qBAAA,CAAA,GAAA,QAA8B,CAAA;AAC9B;;;;;AAKG;AACH,IAAA,KAAA,CAAA,MAAA,CAAA,GAAA,MAAa,CAAA;AACb;;AAEG;AACH,IAAA,KAAA,CAAA,WAAA,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D,IAAA,OAAO,MAAM,CAAC;AAChB;;;;"}
@@ -101,8 +101,10 @@ var InitState;
101
101
  InitState["Initialising"] = "initialising";
102
102
  })(InitState || (InitState = {}));
103
103
  /**
104
- * A list of possible connection states. Can be used with
105
- * {@link Connect.state} to determine the current state.
104
+ * A list of possible connection states.
105
+ *
106
+ * @remarks
107
+ * Can be used with {@link Connect | Connect.state} to determine the current state.
106
108
  *
107
109
  * By default {@link Connect} is in the disconnected state.
108
110
  * When {@link connect} invoked `Connect` state switches to
@@ -404,7 +406,8 @@ var getConnectMemoized = memoizePromise(function (host) { return __awaiter(void
404
406
  * host. If host is not provided, it returns Connect instance for
405
407
  * the host associated with the default sdk.
406
408
  *
407
- * @param sdk - Monterosa SDK.
409
+ * @param host - The host URL. If not provided, uses the host
410
+ * from the default SDK instance.
408
411
  */
409
412
  function getConnect(host) {
410
413
  if (host === undefined) {
@@ -480,7 +483,10 @@ var connectMemoized = memoizePromise(function (conn) { return __awaiter(void 0,
480
483
  clearOnReject: true,
481
484
  });
482
485
  /**
483
- * Starts a connection process. If connect is successful a promise resolved by
486
+ * Starts a connection process.
487
+ *
488
+ * @remarks
489
+ * If connect is successful a promise resolved by
484
490
  * and {@link onConnected} is triggered, otherwise a next attempt will be
485
491
  * automatically invoked and {@link onConnecting} is triggered.
486
492
  *
@@ -535,6 +541,7 @@ var subscribeMemoized = memoizePromise(function (conn, channel) { return __await
535
541
  /**
536
542
  * Subscribe to the server channel.
537
543
  *
544
+ * @remarks
538
545
  * Any message published to a channel is {@link onMessage | received}
539
546
  * only by subscribers to that channel. The sdk out of the box handles
540
547
  * all required subscriptions internally and {@link onMessage | handles messages}
@@ -551,6 +558,7 @@ function subscribe(conn, channel) {
551
558
  /**
552
559
  * Unsubscribe from the server channel.
553
560
  *
561
+ * @remarks
554
562
  * Removes subscription to the channel so that recipient no longer receive
555
563
  * messages from that channel.
556
564
  *
@@ -563,6 +571,7 @@ function unsubscribe(conn, channel) {
563
571
  /**
564
572
  * Send message to the server.
565
573
  *
574
+ * @remarks
566
575
  * The sdk out of the box provides more high level functions that uses
567
576
  * this function to send data to the server. Though a developer still can
568
577
  * use it if he knows a message protocol (klass and body) and a channel
@@ -577,6 +586,20 @@ function unsubscribe(conn, channel) {
577
586
  function send(conn, channel, klass, body) {
578
587
  conn.send(channel, klass, body);
579
588
  }
589
+ /**
590
+ * Authenticates the connection with user credentials.
591
+ *
592
+ * @remarks
593
+ * The session signature can be obtained from Identify Kit using
594
+ * {@link @monterosa/sdk-identify-kit#getSessionSignature}.
595
+ *
596
+ * @param conn - The Connect instance
597
+ * @param userId - The user identifier
598
+ * @param timestamp - The signature timestamp in seconds
599
+ * @param signature - The session signature string
600
+ *
601
+ * @public
602
+ */
580
603
  function login(conn, userId, timestamp, signature) {
581
604
  return __awaiter(this, void 0, void 0, function () {
582
605
  var result;
@@ -1 +1 @@
1
- {"version":3,"file":"index.esm5.js","sources":["../src/types.ts","../src/helpers.ts","../src/connect_impl.ts","../src/connect.ts"],"sourcesContent":["/**\n * @license\n * types.ts\n * connect-kit\n *\n * Created by Rygor Kharytanovich <rygor@monterosa.co.uk> on 2022-01-19\n * Copyright © 2022 Monterosa. All rights reserved.\n *\n * More details on the license can be found at https://www.monterosa.co/sdk/license\n */\n\nimport { MonterosaKit } from '@monterosa/sdk-core';\nimport { Emitter } from '@monterosa/sdk-util';\n\n/**\n * @internal\n */\nexport const KIT_NAME = 'connect';\n\n/**\n * Interface representing Connect kit.\n */\nexport interface Connect extends MonterosaKit, Emitter {\n /**\n * Connection state\n */\n state: ConnState;\n\n /**\n * @internal\n */\n initState: InitState;\n\n /**\n * @internal\n */\n init(host: string): Promise<void>;\n\n /**\n * @internal\n */\n connect(): Promise<void>;\n\n /**\n * @internal\n */\n disconnect(): void;\n\n /**\n * @internal\n */\n subscribe(channel: string): Promise<void>;\n\n /**\n * @internal\n */\n unsubscribe(channel: string): void;\n\n /**\n * @internal\n */\n send(channel: string, klass: string, body: (string | number)[]): void;\n\n login(userId: string, timestamp: number, signature: string): Promise<void>;\n}\n\n/**\n * @internal\n */\nexport enum InitState {\n NotInitialised = 'not_initialised',\n Initialised = 'initialised',\n Initialising = 'initialising',\n}\n\n/**\n * A list of possible connection states. Can be used with\n * {@link Connect.state} to determine the current state.\n *\n * By default {@link Connect} is in the disconnected state.\n * When {@link connect} invoked `Connect` state switches to\n * `ConnState.Connecting` and connection is maintained permanently until\n * {@link disconnect} method is invoked. If connection was unsucessful,\n * a new attempt will start after a short delay and `ConnState.connecting`\n * is triggered. Delays between attempts increased based on this sequence:\n * 0, 3, 5, 10, 20, 30, 45, 60 where 60 seconds is the max.\n *\n * If `Connect` was previously connected and for any reason connection is lost,\n * the state changed to `ConnState.Connecting` again and the connection process\n * starts again automatically.\n *\n * @example\n * ```javascript\n * if (conn.state === ConnState.Connecting) {\n * // display \"connecting\" message\n * }\n * ```\n */\nexport enum ConnState {\n /**\n * Disconnected state\n */\n Disconnected = 'disconnected',\n /**\n * Connecting state\n */\n Connecting = 'connecting',\n /**\n * Connected state\n */\n Connected = 'connected',\n}\n\n/**\n * Class of the {@link Message}\n */\nexport enum Klass {\n /**\n * Authentication request. Sent by server on client connection.\n */\n Authenticate = 'auth',\n /**\n * Authentication response. Sent by client as response\n * to authentication request.\n */\n AuthenticateResponse = 'authr',\n /**\n * Authentication acknowledgement. Sent by the server as confirmation\n * of successful authentication.\n */\n AuthenticateSuccess = 'authok',\n /**\n * Ping message is sent by server periodically as a keep-alive.\n * Ping message is only sent if no messages are sent for specific time\n * period, meaning if there are other messages sent frequently enough\n * then ping messages are not sent at all.\n */\n Ping = 'ping',\n /**\n * Subscribe to a channel. Sent by the client to subscribe to a channel\n */\n Subscribe = 'sub',\n /**\n * Unsubscribe from a channel. Sent by the client to unsubscribe from a channel\n */\n Unsububscribe = 'unsub',\n /**\n * End of cache. Sent by the server to notify that all messages are sent from\n * a channel cache\n */\n EndOfCache = 'eoc',\n /**\n * User login. Sent by client to supply verified user identity to the server.\n */\n Login = 'login',\n /**\n * User login success\n */\n LoginSuccess = 'login_ok',\n /**\n * User login failure\n */\n LoginFail = 'login_fail',\n /**\n * Averages counters. Client can submit a counter which is processed together\n * with other counters to determine the average of all counters within specific\n * demographics segment.\n */\n Counter = 'counter',\n /**\n * Client votes. Sents by a user to vote for a poll option\n */\n Vote = 'v',\n}\n\n/**\n * A Message to be sent to the Monterosa / Interaction Cloud\n */\nexport interface Message {\n /**\n * The channel id on which this message was received. It s required to be\n * {@link subscribe} to that channel in order to start receiving\n * messages\n */\n channel: string;\n /**\n * The class indicates the desired action to be performed or the result of\n * the action. E.g. in order to authenticate a developer should {@link send}\n * {@link Klass.AuthenticateResponse} klass message as a response on\n * a received {@link Klass.Authenticate} message\n */\n klass: Klass | string;\n /**\n * The timestamp (in seconds) when this message was sent\n */\n sent_at: number;\n /**\n * The message payload. Each peace of data is a `string`\n */\n body: string[];\n}\n","/**\n * @license\n * helpers.ts\n * connect-kit\n *\n * Created by Rygor Kharytanovich <rygor@monterosa.co.uk> on 2022-09-14\n * Copyright © 2022 Monterosa. All rights reserved.\n *\n * More details on the license can be found at https://www.monterosa.co/sdk/license\n */\n\nimport { Unsubscribe } from '@monterosa/sdk-util';\n\nimport { Connect, ConnState } from './types';\n\nexport function onState(\n conn: Connect,\n needle: ConnState,\n callback: Function,\n): Unsubscribe {\n const handler = (state: ConnState, ...rest: unknown[]) => {\n if (state === needle) {\n callback(...rest);\n }\n };\n\n conn.on('state', handler);\n\n return () => conn.off('state', handler);\n}\n","/* eslint class-methods-use-this: \"off\" */\n\n/**\n * @license\n * connect_impl.ts\n * connect-kit\n *\n * Created by Rygor Kharytanovich <rygor@monterosa.co.uk> on 2022-02-15\n * Copyright © 2022 Monterosa. All rights reserved.\n *\n * More details on the license can be found at https://www.monterosa.co/sdk/license\n */\n\nimport Enmasse from '@monterosa/sdk-enmasse-kit';\nimport { Emitter, setTimestamp } from '@monterosa/sdk-util';\n\nimport { Connect, ConnState, InitState, Message } from './types';\n\n/**\n * @internal\n */\nexport class ConnectImpl extends Emitter implements Connect {\n private static instance: ConnectImpl;\n public initState: InitState = InitState.NotInitialised;\n private initPromise: Promise<void> = Promise.resolve();\n\n private constructor() {\n super();\n\n Enmasse.bind(\n Enmasse.ON_STATE,\n (state: Enmasse.States, ...rest: unknown[]) => {\n if (state === Enmasse.STATE_CONNECTED) {\n const [timestamp] = rest as [number];\n\n setTimestamp(timestamp);\n }\n\n this.emit('state', ConnectImpl.toConnState(state), ...rest);\n },\n );\n\n Enmasse.bind(Enmasse.ON_MESSAGE, (message: Enmasse.Message) => {\n this.emit('message', <Message>message);\n });\n }\n\n get state() {\n return ConnectImpl.toConnState(Enmasse.getState());\n }\n\n public static getInstance(): ConnectImpl {\n if (!ConnectImpl.instance) {\n ConnectImpl.instance = new ConnectImpl();\n }\n\n return ConnectImpl.instance;\n }\n\n static toConnState(state: Enmasse.States): ConnState {\n switch (state) {\n default:\n case Enmasse.STATE_DISCONNECTED:\n return ConnState.Disconnected;\n case Enmasse.STATE_CONNECTING:\n return ConnState.Connecting;\n case Enmasse.STATE_CONNECTED:\n return ConnState.Connected;\n }\n }\n\n async init(host: string): Promise<void> {\n if (\n this.initState === InitState.Initialising ||\n this.initState === InitState.Initialised\n ) {\n return this.initPromise;\n }\n\n this.initPromise = new Promise((resolve, reject) => {\n this.initState = InitState.Initialising;\n\n Enmasse.bind(Enmasse.ON_READY, () => {\n this.initState = InitState.Initialised;\n\n Enmasse.unbind(Enmasse.ON_READY);\n Enmasse.unbind(Enmasse.ON_ERROR);\n\n resolve();\n });\n\n Enmasse.bind(Enmasse.ON_ERROR, (err: string) => {\n this.initState = InitState.NotInitialised;\n\n Enmasse.unbind(Enmasse.ON_READY);\n Enmasse.unbind(Enmasse.ON_ERROR);\n\n reject(new Error(`Unable to initialise connect kit: ${err}`));\n });\n\n Enmasse.init({\n config: `https://${host}/config/enmasse.json`,\n });\n });\n\n return this.initPromise;\n }\n\n async connect(): Promise<void> {\n if (this.initState !== InitState.Initialised) {\n throw new Error('must be initialised before connecting');\n }\n\n if (this.state === ConnState.Connected) {\n throw new Error('already connected');\n }\n\n if (this.state === ConnState.Connecting) {\n throw new Error('already connecting');\n }\n\n return new Promise((resolve, reject) => {\n const handler = (state: ConnState) => {\n if (state === ConnState.Connected) {\n Enmasse.unbind(Enmasse.ON_STATE, handler);\n\n resolve();\n }\n\n if (state === ConnState.Disconnected) {\n Enmasse.unbind(Enmasse.ON_STATE, handler);\n\n reject(new Error('disconnected'));\n }\n };\n\n Enmasse.bind(Enmasse.ON_STATE, handler);\n\n Enmasse.connect();\n });\n }\n\n disconnect() {\n if (this.state !== ConnState.Connected) {\n throw new Error('must be connected before disconnecting');\n }\n\n Enmasse.disconnect();\n }\n\n subscribe(channel: string): Promise<void> {\n return new Promise((resolve) => {\n const handler = (ch: string) => {\n if (ch !== channel) return;\n\n Enmasse.unbind(Enmasse.ON_EOC, handler);\n\n resolve();\n };\n\n Enmasse.bind(Enmasse.ON_EOC, handler);\n\n Enmasse.subscribe(channel);\n });\n }\n\n unsubscribe(channel: string): void {\n Enmasse.unsubscribe(channel);\n }\n\n send(channel: string, klass: string, body: (string | number)[]): void {\n Enmasse.send(channel, klass, body);\n }\n\n async login(\n userId: string,\n timestamp: number,\n signature: string,\n ): Promise<void> {\n return new Promise((resolve, reject) => {\n Enmasse.login(userId, timestamp, signature, resolve, () => {\n reject(new Error('Login failure'));\n });\n });\n }\n\n delete() {}\n}\n","/**\n * @license\n * connect.ts\n * connect-kit\n *\n * Created by Rygor Kharytanovich <rygor@monterosa.co.uk> on 2022-01-19\n * Copyright © 2022 Monterosa. All rights reserved.\n *\n * More details on the license can be found at https://www.monterosa.co/sdk/license\n */\n\nimport { getSdk } from '@monterosa/sdk-core';\nimport { memoizePromise, Unsubscribe } from '@monterosa/sdk-util';\n\nimport { Connect, ConnState, Message, Klass } from './types';\nimport { onState } from './helpers';\nimport { ConnectImpl } from './connect_impl';\n\n/**\n * @internal\n */\nconst getConnectMemoized = memoizePromise(\n async (host: string): Promise<Connect> => {\n const conn = ConnectImpl.getInstance();\n\n await conn.init(host);\n\n return conn;\n },\n (host: string): string => host,\n);\n\n/**\n * Returns the {@link Connect} instance associated with the provided\n * host. If host is not provided, it returns Connect instance for\n * the host associated with the default sdk.\n *\n * @param sdk - Monterosa SDK.\n */\nfunction getConnect(host?: string): Promise<Connect> {\n if (host === undefined) {\n const sdk = getSdk();\n\n host = sdk.options.host;\n }\n\n return getConnectMemoized(host);\n}\n\n/**\n * Adds an observer for when connection state changed to connected.\n *\n * @param conn - The {@link Connect} instance\n * @param callback - The callback triggered when {@link ConnState | state}\n * changed to connected\n */\nfunction onConnected(\n conn: Connect,\n callback: (timestamp: number) => void,\n): Unsubscribe {\n return onState(conn, ConnState.Connected, callback);\n}\n\n/**\n * Adds an observer for when connection state changed to connecting.\n *\n * @param conn - The {@link Connect} instance\n * @param callback - callback triggered on connecting state. Two parameters\n * are provided: `attempt` — sequential number of connect attempt and\n * `delay` - delay in seconds before the next attempt\n */\nfunction onConnecting(\n conn: Connect,\n callback: (attempt: number, delay: number) => void,\n): Unsubscribe {\n return onState(conn, ConnState.Connecting, callback);\n}\n\n/**\n * Adds an observer for when connection state changed to disconnected.\n *\n * @param conn - The {@link Connect} instance\n * @param callback - callback triggered on disconnected state\n */\nfunction onDisconnected(conn: Connect, callback: () => void): Unsubscribe {\n return onState(conn, ConnState.Disconnected, callback);\n}\n\n/**\n * Adds an observer for when a new message received\n *\n * @example\n * ```javascript\n * const unsubscribe = onMessage(conn, (message) => {\n * console.log(message.body);\n * });\n * ```\n *\n * @param conn - The {@link Connect} instance\n * @param callback - The callback function triggered on message receive\n */\nfunction onMessage(\n conn: Connect,\n callback: (message: Message) => void,\n): Unsubscribe {\n const handler = (message: Message) => {\n callback(message);\n };\n\n conn.on('message', handler);\n\n return () => conn.off('message', handler);\n}\n\n/**\n * @internal\n */\nconst connectMemoized = memoizePromise(\n async (conn: Connect): Promise<void> => {\n await conn.connect();\n },\n (conn: Connect): Connect => conn,\n {\n clearOnResolve: true,\n clearOnReject: true,\n },\n);\n\n/**\n * Starts a connection process. If connect is successful a promise resolved by\n * and {@link onConnected} is triggered, otherwise a next attempt will be\n * automatically invoked and {@link onConnecting} is triggered.\n *\n * SDK connects automatically when its created. Connection procedure can be\n * aborted by calling {@link disconnect} function and the promise is rejected.\n *\n * @example\n * ```javascript\n * try {\n * const conn = await getConnect();\n *\n * await connect(conn);\n *\n * console.log('connected!');\n * } catch (err) {\n * console.log(err)\n * }\n * ```\n *\n * State of the connection can be handled separately\n *\n * @example\n * ```javascript\n * onConnected(() => console.log('connected'));\n *\n * onConnecting((attempt, delay) => console.log('connecting'));\n *\n * onDisconnected(() => console.log('disconnected'));\n * ```\n *\n * @param conn - The {@link Connect} instance\n * @param host - Interaction Cloud Studio host.\n * Can be found in Project Settings in API tab\n */\nfunction connect(conn: Connect): Promise<void> {\n return connectMemoized(conn);\n}\n\n/**\n * Disconnect from the server and triggers {@link onDisconnected}\n *\n * @param conn - The {@link Connect} instance\n */\nfunction disconnect(conn: Connect): void {\n conn.disconnect();\n}\n\n/**\n * @internal\n */\nconst subscribeMemoized = memoizePromise(\n async (conn: Connect, channel: string): Promise<void> =>\n conn.subscribe(channel),\n (conn: Connect, channel: string): string => channel,\n);\n\n/**\n * Subscribe to the server channel.\n *\n * Any message published to a channel is {@link onMessage | received}\n * only by subscribers to that channel. The sdk out of the box handles\n * all required subscriptions internally and {@link onMessage | handles messages}\n * so there is no need in to subscribe by developers.\n *\n * To stop receiving messages on a channel {@link unsubscribe} should be called.\n *\n * @param conn - The {@link Connect} instance\n * @param channel - Channel name\n */\nfunction subscribe(conn: Connect, channel: string): Promise<void> {\n return subscribeMemoized(conn, channel);\n}\n\n/**\n * Unsubscribe from the server channel.\n *\n * Removes subscription to the channel so that recipient no longer receive\n * messages from that channel.\n *\n * @param conn - The {@link Connect} instance\n * @param channel - Channel name\n */\nfunction unsubscribe(conn: Connect, channel: string): void {\n conn.unsubscribe(channel);\n}\n\n/**\n * Send message to the server.\n *\n * The sdk out of the box provides more high level functions that uses\n * this function to send data to the server. Though a developer still can\n * use it if he knows a message protocol (klass and body) and a channel\n * that expect the message.\n *\n * @param conn - The Connect instance\n * @param channel - Channel name. Only {@link subscribe | subscribers}\n * to that channel can send messages.\n * @param klass - Message class name.\n * @param body - Message payload\n */\nfunction send(\n conn: Connect,\n channel: string,\n klass: Klass | string,\n body: (string | number)[],\n): void {\n conn.send(channel, klass, body);\n}\n\nasync function login(\n conn: Connect,\n userId: string,\n timestamp: number,\n signature: string,\n): Promise<void> {\n const result = await conn.login(userId, timestamp, signature);\n\n return result;\n}\n\nexport {\n onConnected,\n onConnecting,\n onDisconnected,\n onMessage,\n disconnect,\n unsubscribe,\n send,\n getConnect,\n getConnectMemoized,\n connect,\n connectMemoized,\n subscribe,\n subscribeMemoized,\n login,\n};\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;;;;AAcA;;;IAGa,QAAQ,GAAG,UAAU;AAiDlC;;;IAGY;AAAZ,WAAY,SAAS;IACnB,+CAAkC,CAAA;IAClC,wCAA2B,CAAA;IAC3B,0CAA6B,CAAA;AAC/B,CAAC,EAJW,SAAS,KAAT,SAAS,QAIpB;AAED;;;;;;;;;;;;;;;;;;;;;;;IAuBY;AAAZ,WAAY,SAAS;;;;IAInB,0CAA6B,CAAA;;;;IAI7B,sCAAyB,CAAA;;;;IAIzB,oCAAuB,CAAA;AACzB,CAAC,EAbW,SAAS,KAAT,SAAS,QAapB;AAED;;;IAGY;AAAZ,WAAY,KAAK;;;;IAIf,8BAAqB,CAAA;;;;;IAKrB,uCAA8B,CAAA;;;;;IAK9B,uCAA8B,CAAA;;;;;;;IAO9B,sBAAa,CAAA;;;;IAIb,0BAAiB,CAAA;;;;IAIjB,gCAAuB,CAAA;;;;;IAKvB,2BAAkB,CAAA;;;;IAIlB,wBAAe,C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E,KAAK,EAAE,IAAI,CAAC,CAAC;AAClC,CAAC;AAED,SAAe,KAAK,CAClB,IAAa,EACb,MAAc,EACd,SAAiB,EACjB,SAAiB;;;;;wBAEF,qBAAM,IAAI,CAAC,KAAK,CAAC,MAAM,EAAE,SAAS,EAAE,SAAS,CAAC,EAAA;;oBAAvD,MAAM,GAAG,SAA8C;oBAE7D,sBAAO,MAAM,EAAC;;;;;;;;"}
1
+ {"version":3,"file":"index.esm5.js","sources":["../src/types.ts","../src/helpers.ts","../src/connect_impl.ts","../src/connect.ts"],"sourcesContent":["/**\n * @license\n * types.ts\n * connect-kit\n *\n * Created by Rygor Kharytanovich <rygor@monterosa.co.uk> on 2022-01-19\n * Copyright © 2022 Monterosa. All rights reserved.\n *\n * More details on the license can be found at https://www.monterosa.co/sdk/license\n */\n\nimport { MonterosaKit } from '@monterosa/sdk-core';\nimport { Emitter } from '@monterosa/sdk-util';\n\n/**\n * @internal\n */\nexport const KIT_NAME = 'connect';\n\n/**\n * Represents a Connect Kit instance. Obtain one via {@link getConnect}.\n */\nexport interface Connect extends MonterosaKit, Emitter {\n /**\n * Connection state\n */\n state: ConnState;\n\n /**\n * @internal\n */\n initState: InitState;\n\n /**\n * @internal\n */\n init(host: string): Promise<void>;\n\n /**\n * @internal\n */\n connect(): Promise<void>;\n\n /**\n * @internal\n */\n disconnect(): void;\n\n /**\n * @internal\n */\n subscribe(channel: string): Promise<void>;\n\n /**\n * @internal\n */\n unsubscribe(channel: string): void;\n\n /**\n * @internal\n */\n send(channel: string, klass: string, body: (string | number)[]): void;\n\n /**\n * @internal\n */\n login(userId: string, timestamp: number, signature: string): Promise<void>;\n}\n\n/**\n * @internal\n */\nexport enum InitState {\n NotInitialised = 'not_initialised',\n Initialised = 'initialised',\n Initialising = 'initialising',\n}\n\n/**\n * A list of possible connection states.\n *\n * @remarks\n * Can be used with {@link Connect | Connect.state} to determine the current state.\n *\n * By default {@link Connect} is in the disconnected state.\n * When {@link connect} invoked `Connect` state switches to\n * `ConnState.Connecting` and connection is maintained permanently until\n * {@link disconnect} method is invoked. If connection was unsucessful,\n * a new attempt will start after a short delay and `ConnState.connecting`\n * is triggered. Delays between attempts increased based on this sequence:\n * 0, 3, 5, 10, 20, 30, 45, 60 where 60 seconds is the max.\n *\n * If `Connect` was previously connected and for any reason connection is lost,\n * the state changed to `ConnState.Connecting` again and the connection process\n * starts again automatically.\n *\n * @example\n * ```javascript\n * if (conn.state === ConnState.Connecting) {\n * // display \"connecting\" message\n * }\n * ```\n */\nexport enum ConnState {\n /**\n * Disconnected state\n */\n Disconnected = 'disconnected',\n /**\n * Connecting state\n */\n Connecting = 'connecting',\n /**\n * Connected state\n */\n Connected = 'connected',\n}\n\n/**\n * Class of the {@link Message}\n */\nexport enum Klass {\n /**\n * Authentication request. Sent by server on client connection.\n */\n Authenticate = 'auth',\n /**\n * Authentication response. Sent by client as response\n * to authentication request.\n */\n AuthenticateResponse = 'authr',\n /**\n * Authentication acknowledgement. Sent by the server as confirmation\n * of successful authentication.\n */\n AuthenticateSuccess = 'authok',\n /**\n * Ping message is sent by server periodically as a keep-alive.\n * Ping message is only sent if no messages are sent for specific time\n * period, meaning if there are other messages sent frequently enough\n * then ping messages are not sent at all.\n */\n Ping = 'ping',\n /**\n * Subscribe to a channel. Sent by the client to subscribe to a channel\n */\n Subscribe = 'sub',\n /**\n * Unsubscribe from a channel. Sent by the client to unsubscribe from a channel\n */\n Unsububscribe = 'unsub',\n /**\n * End of cache. Sent by the server to notify that all messages are sent from\n * a channel cache\n */\n EndOfCache = 'eoc',\n /**\n * User login. Sent by client to supply verified user identity to the server.\n */\n Login = 'login',\n /**\n * User login success\n */\n LoginSuccess = 'login_ok',\n /**\n * User login failure\n */\n LoginFail = 'login_fail',\n /**\n * Averages counters. Client can submit a counter which is processed together\n * with other counters to determine the average of all counters within specific\n * demographics segment.\n */\n Counter = 'counter',\n /**\n * Client votes. Sents by a user to vote for a poll option\n */\n Vote = 'v',\n}\n\n/**\n * Represents a message received from or sent to the server.\n * Subscribe via {@link onMessage} to receive messages.\n */\nexport interface Message {\n /**\n * The channel on which this message was received. You must\n * {@link subscribe} to a channel to start receiving messages.\n */\n channel: string;\n /**\n * The class indicates the desired action to be performed or the result of\n * the action. E.g. in order to authenticate a developer should {@link send}\n * {@link Klass | Klass.AuthenticateResponse} klass message as a response on\n * a received {@link Klass | Klass.Authenticate} message\n */\n klass: Klass | string;\n /**\n * The timestamp (in seconds) when this message was sent\n */\n sent_at: number;\n /**\n * The message payload. Each piece of data is a `string`\n */\n body: string[];\n}\n","/**\n * @license\n * helpers.ts\n * connect-kit\n *\n * Created by Rygor Kharytanovich <rygor@monterosa.co.uk> on 2022-09-14\n * Copyright © 2022 Monterosa. All rights reserved.\n *\n * More details on the license can be found at https://www.monterosa.co/sdk/license\n */\n\nimport { Unsubscribe } from '@monterosa/sdk-util';\n\nimport { Connect, ConnState } from './types';\n\nexport function onState(\n conn: Connect,\n needle: ConnState,\n callback: Function,\n): Unsubscribe {\n const handler = (state: ConnState, ...rest: unknown[]) => {\n if (state === needle) {\n callback(...rest);\n }\n };\n\n conn.on('state', handler);\n\n return () => conn.off('state', handler);\n}\n","/* eslint class-methods-use-this: \"off\" */\n\n/**\n * @license\n * connect_impl.ts\n * connect-kit\n *\n * Created by Rygor Kharytanovich <rygor@monterosa.co.uk> on 2022-02-15\n * Copyright © 2022 Monterosa. All rights reserved.\n *\n * More details on the license can be found at https://www.monterosa.co/sdk/license\n */\n\nimport Enmasse from '@monterosa/sdk-enmasse-kit';\nimport { Emitter, setTimestamp } from '@monterosa/sdk-util';\n\nimport { Connect, ConnState, InitState, Message } from './types';\n\n/**\n * @internal\n */\nexport class ConnectImpl extends Emitter implements Connect {\n private static instance: ConnectImpl;\n public initState: InitState = InitState.NotInitialised;\n private initPromise: Promise<void> = Promise.resolve();\n\n private constructor() {\n super();\n\n Enmasse.bind(\n Enmasse.ON_STATE,\n (state: Enmasse.States, ...rest: unknown[]) => {\n if (state === Enmasse.STATE_CONNECTED) {\n const [timestamp] = rest as [number];\n\n setTimestamp(timestamp);\n }\n\n this.emit('state', ConnectImpl.toConnState(state), ...rest);\n },\n );\n\n Enmasse.bind(Enmasse.ON_MESSAGE, (message: Enmasse.Message) => {\n this.emit('message', <Message>message);\n });\n }\n\n get state() {\n return ConnectImpl.toConnState(Enmasse.getState());\n }\n\n public static getInstance(): ConnectImpl {\n if (!ConnectImpl.instance) {\n ConnectImpl.instance = new ConnectImpl();\n }\n\n return ConnectImpl.instance;\n }\n\n static toConnState(state: Enmasse.States): ConnState {\n switch (state) {\n default:\n case Enmasse.STATE_DISCONNECTED:\n return ConnState.Disconnected;\n case Enmasse.STATE_CONNECTING:\n return ConnState.Connecting;\n case Enmasse.STATE_CONNECTED:\n return ConnState.Connected;\n }\n }\n\n async init(host: string): Promise<void> {\n if (\n this.initState === InitState.Initialising ||\n this.initState === InitState.Initialised\n ) {\n return this.initPromise;\n }\n\n this.initPromise = new Promise((resolve, reject) => {\n this.initState = InitState.Initialising;\n\n Enmasse.bind(Enmasse.ON_READY, () => {\n this.initState = InitState.Initialised;\n\n Enmasse.unbind(Enmasse.ON_READY);\n Enmasse.unbind(Enmasse.ON_ERROR);\n\n resolve();\n });\n\n Enmasse.bind(Enmasse.ON_ERROR, (err: string) => {\n this.initState = InitState.NotInitialised;\n\n Enmasse.unbind(Enmasse.ON_READY);\n Enmasse.unbind(Enmasse.ON_ERROR);\n\n reject(new Error(`Unable to initialise connect kit: ${err}`));\n });\n\n Enmasse.init({\n config: `https://${host}/config/enmasse.json`,\n });\n });\n\n return this.initPromise;\n }\n\n async connect(): Promise<void> {\n if (this.initState !== InitState.Initialised) {\n throw new Error('must be initialised before connecting');\n }\n\n if (this.state === ConnState.Connected) {\n throw new Error('already connected');\n }\n\n if (this.state === ConnState.Connecting) {\n throw new Error('already connecting');\n }\n\n return new Promise((resolve, reject) => {\n const handler = (state: ConnState) => {\n if (state === ConnState.Connected) {\n Enmasse.unbind(Enmasse.ON_STATE, handler);\n\n resolve();\n }\n\n if (state === ConnState.Disconnected) {\n Enmasse.unbind(Enmasse.ON_STATE, handler);\n\n reject(new Error('disconnected'));\n }\n };\n\n Enmasse.bind(Enmasse.ON_STATE, handler);\n\n Enmasse.connect();\n });\n }\n\n disconnect() {\n if (this.state !== ConnState.Connected) {\n throw new Error('must be connected before disconnecting');\n }\n\n Enmasse.disconnect();\n }\n\n subscribe(channel: string): Promise<void> {\n return new Promise((resolve) => {\n const handler = (ch: string) => {\n if (ch !== channel) return;\n\n Enmasse.unbind(Enmasse.ON_EOC, handler);\n\n resolve();\n };\n\n Enmasse.bind(Enmasse.ON_EOC, handler);\n\n Enmasse.subscribe(channel);\n });\n }\n\n unsubscribe(channel: string): void {\n Enmasse.unsubscribe(channel);\n }\n\n send(channel: string, klass: string, body: (string | number)[]): void {\n Enmasse.send(channel, klass, body);\n }\n\n async login(\n userId: string,\n timestamp: number,\n signature: string,\n ): Promise<void> {\n return new Promise((resolve, reject) => {\n Enmasse.login(userId, timestamp, signature, resolve, () => {\n reject(new Error('Login failure'));\n });\n });\n }\n\n delete() {}\n}\n","/**\n * @license\n * connect.ts\n * connect-kit\n *\n * Created by Rygor Kharytanovich <rygor@monterosa.co.uk> on 2022-01-19\n * Copyright © 2022 Monterosa. All rights reserved.\n *\n * More details on the license can be found at https://www.monterosa.co/sdk/license\n */\n\nimport { getSdk } from '@monterosa/sdk-core';\nimport { memoizePromise, Unsubscribe } from '@monterosa/sdk-util';\n\nimport { Connect, ConnState, Message, Klass } from './types';\nimport { onState } from './helpers';\nimport { ConnectImpl } from './connect_impl';\n\n/**\n * @internal\n */\nconst getConnectMemoized = memoizePromise(\n async (host: string): Promise<Connect> => {\n const conn = ConnectImpl.getInstance();\n\n await conn.init(host);\n\n return conn;\n },\n (host: string): string => host,\n);\n\n/**\n * Returns the {@link Connect} instance associated with the provided\n * host. If host is not provided, it returns Connect instance for\n * the host associated with the default sdk.\n *\n * @param host - The host URL. If not provided, uses the host\n * from the default SDK instance.\n */\nfunction getConnect(host?: string): Promise<Connect> {\n if (host === undefined) {\n const sdk = getSdk();\n\n host = sdk.options.host;\n }\n\n return getConnectMemoized(host);\n}\n\n/**\n * Adds an observer for when connection state changed to connected.\n *\n * @param conn - The {@link Connect} instance\n * @param callback - The callback triggered when {@link ConnState | state}\n * changed to connected\n */\nfunction onConnected(\n conn: Connect,\n callback: (timestamp: number) => void,\n): Unsubscribe {\n return onState(conn, ConnState.Connected, callback);\n}\n\n/**\n * Adds an observer for when connection state changed to connecting.\n *\n * @param conn - The {@link Connect} instance\n * @param callback - callback triggered on connecting state. Two parameters\n * are provided: `attempt` — sequential number of connect attempt and\n * `delay` - delay in seconds before the next attempt\n */\nfunction onConnecting(\n conn: Connect,\n callback: (attempt: number, delay: number) => void,\n): Unsubscribe {\n return onState(conn, ConnState.Connecting, callback);\n}\n\n/**\n * Adds an observer for when connection state changed to disconnected.\n *\n * @param conn - The {@link Connect} instance\n * @param callback - callback triggered on disconnected state\n */\nfunction onDisconnected(conn: Connect, callback: () => void): Unsubscribe {\n return onState(conn, ConnState.Disconnected, callback);\n}\n\n/**\n * Adds an observer for when a new message received\n *\n * @example\n * ```javascript\n * const unsubscribe = onMessage(conn, (message) => {\n * console.log(message.body);\n * });\n * ```\n *\n * @param conn - The {@link Connect} instance\n * @param callback - The callback function triggered on message receive\n */\nfunction onMessage(\n conn: Connect,\n callback: (message: Message) => void,\n): Unsubscribe {\n const handler = (message: Message) => {\n callback(message);\n };\n\n conn.on('message', handler);\n\n return () => conn.off('message', handler);\n}\n\n/**\n * @internal\n */\nconst connectMemoized = memoizePromise(\n async (conn: Connect): Promise<void> => {\n await conn.connect();\n },\n (conn: Connect): Connect => conn,\n {\n clearOnResolve: true,\n clearOnReject: true,\n },\n);\n\n/**\n * Starts a connection process.\n *\n * @remarks\n * If connect is successful a promise resolved by\n * and {@link onConnected} is triggered, otherwise a next attempt will be\n * automatically invoked and {@link onConnecting} is triggered.\n *\n * SDK connects automatically when its created. Connection procedure can be\n * aborted by calling {@link disconnect} function and the promise is rejected.\n *\n * @example\n * ```javascript\n * try {\n * const conn = await getConnect();\n *\n * await connect(conn);\n *\n * console.log('connected!');\n * } catch (err) {\n * console.log(err)\n * }\n * ```\n *\n * State of the connection can be handled separately\n *\n * @example\n * ```javascript\n * onConnected(() => console.log('connected'));\n *\n * onConnecting((attempt, delay) => console.log('connecting'));\n *\n * onDisconnected(() => console.log('disconnected'));\n * ```\n *\n * @param conn - The {@link Connect} instance\n * @param host - Interaction Cloud Studio host.\n * Can be found in Project Settings in API tab\n */\nfunction connect(conn: Connect): Promise<void> {\n return connectMemoized(conn);\n}\n\n/**\n * Disconnect from the server and triggers {@link onDisconnected}\n *\n * @param conn - The {@link Connect} instance\n */\nfunction disconnect(conn: Connect): void {\n conn.disconnect();\n}\n\n/**\n * @internal\n */\nconst subscribeMemoized = memoizePromise(\n async (conn: Connect, channel: string): Promise<void> =>\n conn.subscribe(channel),\n (conn: Connect, channel: string): string => channel,\n);\n\n/**\n * Subscribe to the server channel.\n *\n * @remarks\n * Any message published to a channel is {@link onMessage | received}\n * only by subscribers to that channel. The sdk out of the box handles\n * all required subscriptions internally and {@link onMessage | handles messages}\n * so there is no need in to subscribe by developers.\n *\n * To stop receiving messages on a channel {@link unsubscribe} should be called.\n *\n * @param conn - The {@link Connect} instance\n * @param channel - Channel name\n */\nfunction subscribe(conn: Connect, channel: string): Promise<void> {\n return subscribeMemoized(conn, channel);\n}\n\n/**\n * Unsubscribe from the server channel.\n *\n * @remarks\n * Removes subscription to the channel so that recipient no longer receive\n * messages from that channel.\n *\n * @param conn - The {@link Connect} instance\n * @param channel - Channel name\n */\nfunction unsubscribe(conn: Connect, channel: string): void {\n conn.unsubscribe(channel);\n}\n\n/**\n * Send message to the server.\n *\n * @remarks\n * The sdk out of the box provides more high level functions that uses\n * this function to send data to the server. Though a developer still can\n * use it if he knows a message protocol (klass and body) and a channel\n * that expect the message.\n *\n * @param conn - The Connect instance\n * @param channel - Channel name. Only {@link subscribe | subscribers}\n * to that channel can send messages.\n * @param klass - Message class name.\n * @param body - Message payload\n */\nfunction send(\n conn: Connect,\n channel: string,\n klass: Klass | string,\n body: (string | number)[],\n): void {\n conn.send(channel, klass, body);\n}\n\n/**\n * Authenticates the connection with user credentials.\n *\n * @remarks\n * The session signature can be obtained from Identify Kit using\n * {@link @monterosa/sdk-identify-kit#getSessionSignature}.\n *\n * @param conn - The Connect instance\n * @param userId - The user identifier\n * @param timestamp - The signature timestamp in seconds\n * @param signature - The session signature string\n *\n * @public\n */\nasync function login(\n conn: Connect,\n userId: string,\n timestamp: number,\n signature: string,\n): Promise<void> {\n const result = await conn.login(userId, timestamp, signature);\n\n return result;\n}\n\nexport {\n onConnected,\n onConnecting,\n onDisconnected,\n onMessage,\n disconnect,\n unsubscribe,\n send,\n getConnect,\n getConnectMemoized,\n connect,\n connectMemoized,\n subscribe,\n subscribeMemoized,\n login,\n};\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;;;AASG;AAKH;;AAEG;AACI,IAAM,QAAQ,GAAG,UAAU;AAoDlC;;AAEG;IACS,UAIX;AAJD,CAAA,UAAY,SAAS,EAAA;AACnB,IAAA,SAAA,CAAA,gBAAA,CAAA,GAAA,iBAAkC,CAAA;AAClC,IAAA,SAAA,CAAA,aAAA,CAAA,GAAA,aAA2B,CAAA;AAC3B,IAAA,SAAA,CAAA,cAAA,CAAA,GAAA,cAA6B,CAAA;AAC/B,CAAC,EAJW,SAAS,KAAT,SAAS,GAIpB,EAAA,CAAA,CAAA,CAAA;AAED;;;;;;;;;;;;;;;;;;;;;;;;AAwBG;IACS,UAaX;AAbD,CAAA,UAAY,SAAS,EAAA;AACnB;;AAEG;AACH,IAAA,SAAA,CAAA,cAAA,CAAA,GAAA,cAA6B,CAAA;AAC7B;;AAEG;AACH,IAAA,SAAA,CAAA,YAAA,CAAA,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CAAC,CAAA;;;;AACf;;;;"}
@@ -0,0 +1,394 @@
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
+ * @internal
22
+ */
23
+ initState: InitState;
24
+ /**
25
+ * @internal
26
+ */
27
+ init(host: string): Promise<void>;
28
+ /**
29
+ * @internal
30
+ */
31
+ connect(): Promise<void>;
32
+ /**
33
+ * @internal
34
+ */
35
+ disconnect(): void;
36
+ /**
37
+ * @internal
38
+ */
39
+ subscribe(channel: string): Promise<void>;
40
+ /**
41
+ * @internal
42
+ */
43
+ unsubscribe(channel: string): void;
44
+ /**
45
+ * @internal
46
+ */
47
+ send(channel: string, klass: string, body: (string | number)[]): void;
48
+ /**
49
+ * @internal
50
+ */
51
+ login(userId: string, timestamp: number, signature: string): Promise<void>;
52
+ }
53
+
54
+ /**
55
+ * Starts a connection process.
56
+ *
57
+ * @remarks
58
+ * If connect is successful a promise resolved by
59
+ * and {@link onConnected} is triggered, otherwise a next attempt will be
60
+ * automatically invoked and {@link onConnecting} is triggered.
61
+ *
62
+ * SDK connects automatically when its created. Connection procedure can be
63
+ * aborted by calling {@link disconnect} function and the promise is rejected.
64
+ *
65
+ * @example
66
+ * ```javascript
67
+ * try {
68
+ * const conn = await getConnect();
69
+ *
70
+ * await connect(conn);
71
+ *
72
+ * console.log('connected!');
73
+ * } catch (err) {
74
+ * console.log(err)
75
+ * }
76
+ * ```
77
+ *
78
+ * State of the connection can be handled separately
79
+ *
80
+ * @example
81
+ * ```javascript
82
+ * onConnected(() => console.log('connected'));
83
+ *
84
+ * onConnecting((attempt, delay) => console.log('connecting'));
85
+ *
86
+ * onDisconnected(() => console.log('disconnected'));
87
+ * ```
88
+ *
89
+ * @param conn - The {@link Connect} instance
90
+ * @param host - Interaction Cloud Studio host.
91
+ * Can be found in Project Settings in API tab
92
+ */
93
+ export declare function connect(conn: Connect): Promise<void>;
94
+
95
+ /**
96
+ * @internal
97
+ */
98
+ export declare class ConnectImpl extends Emitter implements Connect {
99
+ private static instance;
100
+ initState: InitState;
101
+ private initPromise;
102
+ private constructor();
103
+ get state(): ConnState;
104
+ static getInstance(): ConnectImpl;
105
+ static toConnState(state: Enmasse.States): ConnState;
106
+ init(host: string): Promise<void>;
107
+ connect(): Promise<void>;
108
+ disconnect(): void;
109
+ subscribe(channel: string): Promise<void>;
110
+ unsubscribe(channel: string): void;
111
+ send(channel: string, klass: string, body: (string | number)[]): void;
112
+ login(userId: string, timestamp: number, signature: string): Promise<void>;
113
+ delete(): void;
114
+ }
115
+
116
+ /**
117
+ * @internal
118
+ */
119
+ export declare const connectMemoized: (...args: any[]) => Promise<void>;
120
+
121
+ /**
122
+ * A list of possible connection states.
123
+ *
124
+ * @remarks
125
+ * Can be used with {@link Connect | Connect.state} to determine the current state.
126
+ *
127
+ * By default {@link Connect} is in the disconnected state.
128
+ * When {@link connect} invoked `Connect` state switches to
129
+ * `ConnState.Connecting` and connection is maintained permanently until
130
+ * {@link disconnect} method is invoked. If connection was unsucessful,
131
+ * a new attempt will start after a short delay and `ConnState.connecting`
132
+ * is triggered. Delays between attempts increased based on this sequence:
133
+ * 0, 3, 5, 10, 20, 30, 45, 60 where 60 seconds is the max.
134
+ *
135
+ * If `Connect` was previously connected and for any reason connection is lost,
136
+ * the state changed to `ConnState.Connecting` again and the connection process
137
+ * starts again automatically.
138
+ *
139
+ * @example
140
+ * ```javascript
141
+ * if (conn.state === ConnState.Connecting) {
142
+ * // display "connecting" message
143
+ * }
144
+ * ```
145
+ */
146
+ export declare enum ConnState {
147
+ /**
148
+ * Disconnected state
149
+ */
150
+ Disconnected = "disconnected",
151
+ /**
152
+ * Connecting state
153
+ */
154
+ Connecting = "connecting",
155
+ /**
156
+ * Connected state
157
+ */
158
+ Connected = "connected"
159
+ }
160
+
161
+ /**
162
+ * Disconnect from the server and triggers {@link onDisconnected}
163
+ *
164
+ * @param conn - The {@link Connect} instance
165
+ */
166
+ export declare function disconnect(conn: Connect): void;
167
+
168
+ /**
169
+ * Returns the {@link Connect} instance associated with the provided
170
+ * host. If host is not provided, it returns Connect instance for
171
+ * the host associated with the default sdk.
172
+ *
173
+ * @param host - The host URL. If not provided, uses the host
174
+ * from the default SDK instance.
175
+ */
176
+ export declare function getConnect(host?: string): Promise<Connect>;
177
+
178
+ /**
179
+ * @internal
180
+ */
181
+ export declare const getConnectMemoized: (...args: any[]) => Promise<Connect>;
182
+
183
+ /**
184
+ * @internal
185
+ */
186
+ export declare enum InitState {
187
+ NotInitialised = "not_initialised",
188
+ Initialised = "initialised",
189
+ Initialising = "initialising"
190
+ }
191
+
192
+ /**
193
+ * @internal
194
+ */
195
+ export declare const KIT_NAME = "connect";
196
+
197
+ /**
198
+ * Class of the {@link Message}
199
+ */
200
+ export declare enum Klass {
201
+ /**
202
+ * Authentication request. Sent by server on client connection.
203
+ */
204
+ Authenticate = "auth",
205
+ /**
206
+ * Authentication response. Sent by client as response
207
+ * to authentication request.
208
+ */
209
+ AuthenticateResponse = "authr",
210
+ /**
211
+ * Authentication acknowledgement. Sent by the server as confirmation
212
+ * of successful authentication.
213
+ */
214
+ AuthenticateSuccess = "authok",
215
+ /**
216
+ * Ping message is sent by server periodically as a keep-alive.
217
+ * Ping message is only sent if no messages are sent for specific time
218
+ * period, meaning if there are other messages sent frequently enough
219
+ * then ping messages are not sent at all.
220
+ */
221
+ Ping = "ping",
222
+ /**
223
+ * Subscribe to a channel. Sent by the client to subscribe to a channel
224
+ */
225
+ Subscribe = "sub",
226
+ /**
227
+ * Unsubscribe from a channel. Sent by the client to unsubscribe from a channel
228
+ */
229
+ Unsububscribe = "unsub",
230
+ /**
231
+ * End of cache. Sent by the server to notify that all messages are sent from
232
+ * a channel cache
233
+ */
234
+ EndOfCache = "eoc",
235
+ /**
236
+ * User login. Sent by client to supply verified user identity to the server.
237
+ */
238
+ Login = "login",
239
+ /**
240
+ * User login success
241
+ */
242
+ LoginSuccess = "login_ok",
243
+ /**
244
+ * User login failure
245
+ */
246
+ LoginFail = "login_fail",
247
+ /**
248
+ * Averages counters. Client can submit a counter which is processed together
249
+ * with other counters to determine the average of all counters within specific
250
+ * demographics segment.
251
+ */
252
+ Counter = "counter",
253
+ /**
254
+ * Client votes. Sents by a user to vote for a poll option
255
+ */
256
+ Vote = "v"
257
+ }
258
+
259
+ /**
260
+ * Authenticates the connection with user credentials.
261
+ *
262
+ * @remarks
263
+ * The session signature can be obtained from Identify Kit using
264
+ * {@link @monterosa/sdk-identify-kit#getSessionSignature}.
265
+ *
266
+ * @param conn - The Connect instance
267
+ * @param userId - The user identifier
268
+ * @param timestamp - The signature timestamp in seconds
269
+ * @param signature - The session signature string
270
+ *
271
+ * @public
272
+ */
273
+ export declare function login(conn: Connect, userId: string, timestamp: number, signature: string): Promise<void>;
274
+
275
+ /**
276
+ * Represents a message received from or sent to the server.
277
+ * Subscribe via {@link onMessage} to receive messages.
278
+ */
279
+ export declare interface Message {
280
+ /**
281
+ * The channel on which this message was received. You must
282
+ * {@link subscribe} to a channel to start receiving messages.
283
+ */
284
+ channel: string;
285
+ /**
286
+ * The class indicates the desired action to be performed or the result of
287
+ * the action. E.g. in order to authenticate a developer should {@link send}
288
+ * {@link Klass | Klass.AuthenticateResponse} klass message as a response on
289
+ * a received {@link Klass | Klass.Authenticate} message
290
+ */
291
+ klass: Klass | string;
292
+ /**
293
+ * The timestamp (in seconds) when this message was sent
294
+ */
295
+ sent_at: number;
296
+ /**
297
+ * The message payload. Each piece of data is a `string`
298
+ */
299
+ body: string[];
300
+ }
301
+
302
+ /**
303
+ * Adds an observer for when connection state changed to connected.
304
+ *
305
+ * @param conn - The {@link Connect} instance
306
+ * @param callback - The callback triggered when {@link ConnState | state}
307
+ * changed to connected
308
+ */
309
+ export declare function onConnected(conn: Connect, callback: (timestamp: number) => void): Unsubscribe;
310
+
311
+ /**
312
+ * Adds an observer for when connection state changed to connecting.
313
+ *
314
+ * @param conn - The {@link Connect} instance
315
+ * @param callback - callback triggered on connecting state. Two parameters
316
+ * are provided: `attempt` — sequential number of connect attempt and
317
+ * `delay` - delay in seconds before the next attempt
318
+ */
319
+ export declare function onConnecting(conn: Connect, callback: (attempt: number, delay: number) => void): Unsubscribe;
320
+
321
+ /**
322
+ * Adds an observer for when connection state changed to disconnected.
323
+ *
324
+ * @param conn - The {@link Connect} instance
325
+ * @param callback - callback triggered on disconnected state
326
+ */
327
+ export declare function onDisconnected(conn: Connect, callback: () => void): Unsubscribe;
328
+
329
+ /**
330
+ * Adds an observer for when a new message received
331
+ *
332
+ * @example
333
+ * ```javascript
334
+ * const unsubscribe = onMessage(conn, (message) => {
335
+ * console.log(message.body);
336
+ * });
337
+ * ```
338
+ *
339
+ * @param conn - The {@link Connect} instance
340
+ * @param callback - The callback function triggered on message receive
341
+ */
342
+ export declare function onMessage(conn: Connect, callback: (message: Message) => void): Unsubscribe;
343
+
344
+ /**
345
+ * Send message to the server.
346
+ *
347
+ * @remarks
348
+ * The sdk out of the box provides more high level functions that uses
349
+ * this function to send data to the server. Though a developer still can
350
+ * use it if he knows a message protocol (klass and body) and a channel
351
+ * that expect the message.
352
+ *
353
+ * @param conn - The Connect instance
354
+ * @param channel - Channel name. Only {@link subscribe | subscribers}
355
+ * to that channel can send messages.
356
+ * @param klass - Message class name.
357
+ * @param body - Message payload
358
+ */
359
+ export declare function send(conn: Connect, channel: string, klass: Klass | string, body: (string | number)[]): void;
360
+
361
+ /**
362
+ * Subscribe to the server channel.
363
+ *
364
+ * @remarks
365
+ * Any message published to a channel is {@link onMessage | received}
366
+ * only by subscribers to that channel. The sdk out of the box handles
367
+ * all required subscriptions internally and {@link onMessage | handles messages}
368
+ * so there is no need in to subscribe by developers.
369
+ *
370
+ * To stop receiving messages on a channel {@link unsubscribe} should be called.
371
+ *
372
+ * @param conn - The {@link Connect} instance
373
+ * @param channel - Channel name
374
+ */
375
+ export declare function subscribe(conn: Connect, channel: string): Promise<void>;
376
+
377
+ /**
378
+ * @internal
379
+ */
380
+ export declare const subscribeMemoized: (...args: any[]) => Promise<void>;
381
+
382
+ /**
383
+ * Unsubscribe from the server channel.
384
+ *
385
+ * @remarks
386
+ * Removes subscription to the channel so that recipient no longer receive
387
+ * messages from that channel.
388
+ *
389
+ * @param conn - The {@link Connect} instance
390
+ * @param channel - Channel name
391
+ */
392
+ export declare function unsubscribe(conn: Connect, channel: string): void;
393
+
394
+ 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/dist/types.d.ts CHANGED
@@ -15,7 +15,7 @@ import { Emitter } from '@monterosa/sdk-util';
15
15
  */
16
16
  export declare const KIT_NAME = "connect";
17
17
  /**
18
- * Interface representing Connect kit.
18
+ * Represents a Connect Kit instance. Obtain one via {@link getConnect}.
19
19
  */
20
20
  export interface Connect extends MonterosaKit, Emitter {
21
21
  /**
@@ -50,6 +50,9 @@ export interface Connect extends MonterosaKit, Emitter {
50
50
  * @internal
51
51
  */
52
52
  send(channel: string, klass: string, body: (string | number)[]): void;
53
+ /**
54
+ * @internal
55
+ */
53
56
  login(userId: string, timestamp: number, signature: string): Promise<void>;
54
57
  }
55
58
  /**
@@ -61,8 +64,10 @@ export declare enum InitState {
61
64
  Initialising = "initialising"
62
65
  }
63
66
  /**
64
- * A list of possible connection states. Can be used with
65
- * {@link Connect.state} to determine the current state.
67
+ * A list of possible connection states.
68
+ *
69
+ * @remarks
70
+ * Can be used with {@link Connect | Connect.state} to determine the current state.
66
71
  *
67
72
  * By default {@link Connect} is in the disconnected state.
68
73
  * When {@link connect} invoked `Connect` state switches to
@@ -159,20 +164,20 @@ export declare enum Klass {
159
164
  Vote = "v"
160
165
  }
161
166
  /**
162
- * A Message to be sent to the Monterosa / Interaction Cloud
167
+ * Represents a message received from or sent to the server.
168
+ * Subscribe via {@link onMessage} to receive messages.
163
169
  */
164
170
  export interface Message {
165
171
  /**
166
- * The channel id on which this message was received. It s required to be
167
- * {@link subscribe} to that channel in order to start receiving
168
- * messages
172
+ * The channel on which this message was received. You must
173
+ * {@link subscribe} to a channel to start receiving messages.
169
174
  */
170
175
  channel: string;
171
176
  /**
172
177
  * The class indicates the desired action to be performed or the result of
173
178
  * the action. E.g. in order to authenticate a developer should {@link send}
174
- * {@link Klass.AuthenticateResponse} klass message as a response on
175
- * a received {@link Klass.Authenticate} message
179
+ * {@link Klass | Klass.AuthenticateResponse} klass message as a response on
180
+ * a received {@link Klass | Klass.Authenticate} message
176
181
  */
177
182
  klass: Klass | string;
178
183
  /**
@@ -180,7 +185,7 @@ export interface Message {
180
185
  */
181
186
  sent_at: number;
182
187
  /**
183
- * The message payload. Each peace of data is a `string`
188
+ * The message payload. Each piece of data is a `string`
184
189
  */
185
190
  body: string[];
186
191
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@monterosa/sdk-connect-kit",
3
- "version": "0.18.9",
3
+ "version": "0.18.10-local-only-storage.1",
4
4
  "description": "Monterosa JS SDK / Connect",
5
5
  "author": "Monterosa <hello@monterosa.co.uk> (https://www.monterosa.co/)",
6
6
  "main": "dist/index.cjs.js",
@@ -28,9 +28,9 @@
28
28
  },
29
29
  "dependencies": {
30
30
  "@babel/runtime": "^7.15.4",
31
- "@monterosa/sdk-core": "^0.18.9",
32
- "@monterosa/sdk-enmasse-kit": "^0.18.9",
33
- "@monterosa/sdk-util": "^0.18.9"
31
+ "@monterosa/sdk-core": "^0.18.10-local-only-storage.1",
32
+ "@monterosa/sdk-enmasse-kit": "^0.18.10-local-only-storage.1",
33
+ "@monterosa/sdk-util": "^0.18.10-local-only-storage.1"
34
34
  },
35
35
  "devDependencies": {
36
36
  "@rollup/plugin-json": "^4.1.0",
@@ -46,5 +46,5 @@
46
46
  "publishConfig": {
47
47
  "access": "public"
48
48
  },
49
- "gitHead": "cf8b4ea9ed5e7b9ab5b64456a07c2a78c2b568c0"
49
+ "gitHead": "e378a0c6c562bc4dae20dec0e3e94e300d8fb9fa"
50
50
  }