@aws-amplify/pubsub 4.3.1 → 4.3.2-unstable.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -47,24 +47,27 @@ export type MqttProvidertOptions = MqttProviderOptions;
47
47
  class ClientsQueue {
48
48
  private promises: Map<string, Promise<any>> = new Map();
49
49
 
50
- async get(clientId: string, clientFactory: (string) => Promise<any>) {
51
- let promise = this.promises.get(clientId);
52
- if (promise) {
53
- return promise;
50
+ async get(clientId: string, clientFactory?: (input: string) => Promise<any>) {
51
+ const cachedPromise = this.promises.get(clientId);
52
+ if (cachedPromise) {
53
+ return cachedPromise;
54
54
  }
55
55
 
56
- promise = clientFactory(clientId);
56
+ if (clientFactory) {
57
+ const newPromise = clientFactory(clientId);
57
58
 
58
- this.promises.set(clientId, promise);
59
+ this.promises.set(clientId, newPromise);
59
60
 
60
- return promise;
61
+ return newPromise;
62
+ }
63
+ return undefined;
61
64
  }
62
65
 
63
66
  get allClients() {
64
67
  return Array.from(this.promises.keys());
65
68
  }
66
69
 
67
- remove(clientId) {
70
+ remove(clientId: string) {
68
71
  this.promises.delete(clientId);
69
72
  }
70
73
  }
@@ -95,7 +98,7 @@ export class MqttOverWSProvider extends AbstractPubSubProvider {
95
98
  .aws_appsync_dangerously_connect_to_http_endpoint_for_testing;
96
99
  }
97
100
 
98
- protected getTopicForValue(value) {
101
+ protected getTopicForValue(value: any) {
99
102
  return typeof value === 'object' && value[topicSymbol];
100
103
  }
101
104
 
@@ -103,11 +106,21 @@ export class MqttOverWSProvider extends AbstractPubSubProvider {
103
106
  return 'MqttOverWSProvider';
104
107
  }
105
108
 
106
- public onDisconnect({ clientId, errorCode, ...args }) {
109
+ public onDisconnect({
110
+ clientId,
111
+ errorCode,
112
+ ...args
113
+ }: {
114
+ clientId?: string;
115
+ errorCode?: number;
116
+ }) {
107
117
  if (errorCode !== 0) {
108
118
  logger.warn(clientId, JSON.stringify({ errorCode, ...args }, null, 2));
109
119
 
110
- const topicsToDelete = [];
120
+ const topicsToDelete: string[] = [];
121
+ if (!clientId) {
122
+ return;
123
+ }
111
124
  const clientIdObservers = this._clientIdObservers.get(clientId);
112
125
  if (!clientIdObservers) {
113
126
  return;
@@ -142,10 +155,18 @@ export class MqttOverWSProvider extends AbstractPubSubProvider {
142
155
  client.onMessageArrived = ({
143
156
  destinationName: topic,
144
157
  payloadString: msg,
158
+ }: {
159
+ destinationName: string;
160
+ payloadString: string;
145
161
  }) => {
146
162
  this._onMessage(topic, msg);
147
163
  };
148
- client.onConnectionLost = ({ errorCode, ...args }) => {
164
+ client.onConnectionLost = ({
165
+ errorCode,
166
+ ...args
167
+ }: {
168
+ errorCode: number;
169
+ }) => {
149
170
  this.onDisconnect({ clientId, errorCode, ...args });
150
171
  };
151
172
 
@@ -171,7 +192,7 @@ export class MqttOverWSProvider extends AbstractPubSubProvider {
171
192
  }
172
193
 
173
194
  protected async disconnect(clientId: string): Promise<void> {
174
- const client = await this.clientsQueue.get(clientId, () => null);
195
+ const client = await this.clientsQueue.get(clientId);
175
196
 
176
197
  if (client && client.isConnected()) {
177
198
  client.disconnect();
@@ -199,7 +220,7 @@ export class MqttOverWSProvider extends AbstractPubSubProvider {
199
220
 
200
221
  private _onMessage(topic: string, msg: any) {
201
222
  try {
202
- const matchedTopicObservers = [];
223
+ const matchedTopicObservers: Set<SubscriptionObserver<any>>[] = [];
203
224
  this._topicObservers.forEach((observerForTopic, observerTopic) => {
204
225
  if (mqttTopicMatch(observerTopic, topic)) {
205
226
  matchedTopicObservers.push(observerForTopic);
@@ -269,9 +290,9 @@ export class MqttOverWSProvider extends AbstractPubSubProvider {
269
290
  logger.debug('Unsubscribing from topic(s)', targetTopics.join(','));
270
291
 
271
292
  if (client) {
272
- this._clientIdObservers.get(clientId).delete(observer);
293
+ this._clientIdObservers.get(clientId)?.delete(observer);
273
294
  // No more observers per client => client not needed anymore
274
- if (this._clientIdObservers.get(clientId).size === 0) {
295
+ if (this._clientIdObservers.get(clientId)?.size === 0) {
275
296
  this.disconnect(clientId);
276
297
  this._clientIdObservers.delete(clientId);
277
298
  }