@aws-amplify/pubsub 4.3.1-unstable.9 → 4.3.2-unstable.3
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/CHANGELOG.md +11 -0
- package/dist/aws-amplify-pubsub.js +229 -200
- package/dist/aws-amplify-pubsub.js.map +1 -1
- package/dist/aws-amplify-pubsub.min.js +2 -2
- package/dist/aws-amplify-pubsub.min.js.map +1 -1
- package/lib/Providers/AWSAppSyncRealTimeProvider.d.ts +2 -2
- package/lib/Providers/AWSAppSyncRealTimeProvider.js +174 -154
- package/lib/Providers/AWSAppSyncRealTimeProvider.js.map +1 -1
- package/lib/Providers/MqttOverWSProvider.d.ts +4 -5
- package/lib/Providers/MqttOverWSProvider.js +17 -10
- package/lib/Providers/MqttOverWSProvider.js.map +1 -1
- package/lib/PubSub.d.ts +3 -3
- package/lib/PubSub.js +2 -2
- package/lib/PubSub.js.map +1 -1
- package/lib-esm/Providers/AWSAppSyncRealTimeProvider.d.ts +2 -2
- package/lib-esm/Providers/AWSAppSyncRealTimeProvider.js +174 -154
- package/lib-esm/Providers/AWSAppSyncRealTimeProvider.js.map +1 -1
- package/lib-esm/Providers/MqttOverWSProvider.d.ts +4 -5
- package/lib-esm/Providers/MqttOverWSProvider.js +17 -10
- package/lib-esm/Providers/MqttOverWSProvider.js.map +1 -1
- package/lib-esm/PubSub.d.ts +3 -3
- package/lib-esm/PubSub.js +2 -2
- package/lib-esm/PubSub.js.map +1 -1
- package/package.json +5 -5
- package/src/Providers/AWSAppSyncRealTimeProvider.ts +231 -188
- package/src/Providers/MqttOverWSProvider.ts +37 -16
- package/src/PubSub.ts +6 -6
|
@@ -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:
|
|
51
|
-
|
|
52
|
-
if (
|
|
53
|
-
return
|
|
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
|
-
|
|
56
|
+
if (clientFactory) {
|
|
57
|
+
const newPromise = clientFactory(clientId);
|
|
57
58
|
|
|
58
|
-
|
|
59
|
+
this.promises.set(clientId, newPromise);
|
|
59
60
|
|
|
60
|
-
|
|
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({
|
|
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 = ({
|
|
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
|
|
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)
|
|
293
|
+
this._clientIdObservers.get(clientId)?.delete(observer);
|
|
273
294
|
// No more observers per client => client not needed anymore
|
|
274
|
-
if (this._clientIdObservers.get(clientId)
|
|
295
|
+
if (this._clientIdObservers.get(clientId)?.size === 0) {
|
|
275
296
|
this.disconnect(clientId);
|
|
276
297
|
this._clientIdObservers.delete(clientId);
|
|
277
298
|
}
|
package/src/PubSub.ts
CHANGED
|
@@ -34,12 +34,12 @@ export class PubSubClass {
|
|
|
34
34
|
/**
|
|
35
35
|
* Internal instance of AWSAppSyncProvider used by the API category to subscribe to AppSync
|
|
36
36
|
*/
|
|
37
|
-
private _awsAppSyncProvider
|
|
37
|
+
private _awsAppSyncProvider?: AWSAppSyncProvider;
|
|
38
38
|
|
|
39
39
|
/**
|
|
40
40
|
* Internal instance of AWSAppSyncRealTimeProvider used by the API category to subscribe to AppSync
|
|
41
41
|
*/
|
|
42
|
-
private _awsAppSyncRealTimeProvider
|
|
42
|
+
private _awsAppSyncRealTimeProvider?: AWSAppSyncRealTimeProvider;
|
|
43
43
|
|
|
44
44
|
/**
|
|
45
45
|
* Lazy instantiate AWSAppSyncProvider when it is required by the API category
|
|
@@ -68,8 +68,8 @@ export class PubSubClass {
|
|
|
68
68
|
*
|
|
69
69
|
* @param {PubSubOptions} options - Configuration object for PubSub
|
|
70
70
|
*/
|
|
71
|
-
constructor(options
|
|
72
|
-
this._options = options;
|
|
71
|
+
constructor(options?: PubSubOptions) {
|
|
72
|
+
this._options = options ?? {};
|
|
73
73
|
logger.debug('PubSub Options', this._options);
|
|
74
74
|
this._pluggables = [];
|
|
75
75
|
this.subscribe = this.subscribe.bind(this);
|
|
@@ -120,7 +120,7 @@ export class PubSubClass {
|
|
|
120
120
|
);
|
|
121
121
|
}
|
|
122
122
|
|
|
123
|
-
private getProviderByName(providerName) {
|
|
123
|
+
private getProviderByName(providerName: string | symbol) {
|
|
124
124
|
if (providerName === INTERNAL_AWS_APPSYNC_PUBSUB_PROVIDER) {
|
|
125
125
|
return this.awsAppSyncProvider;
|
|
126
126
|
}
|
|
@@ -194,5 +194,5 @@ export class PubSubClass {
|
|
|
194
194
|
}
|
|
195
195
|
}
|
|
196
196
|
|
|
197
|
-
export const PubSub = new PubSubClass(
|
|
197
|
+
export const PubSub = new PubSubClass();
|
|
198
198
|
Amplify.register(PubSub);
|