@aws-amplify/pubsub 6.0.1-api-v6-models.9351bcf.0 → 6.0.1-api-v6-models.891fe0d.0
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/lib/Providers/AWSIot.js +27 -49
- package/lib/Providers/MqttOverWS.d.ts +5 -3
- package/lib/Providers/MqttOverWS.js +196 -320
- package/lib/Providers/PubSub.d.ts +3 -4
- package/lib/Providers/PubSub.js +12 -18
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/lib/types/PubSub.d.ts +11 -2
- package/lib/utils/ConnectionStateMonitor.js +51 -64
- package/lib/utils/ReachabilityMonitor/index.js +2 -2
- package/lib/utils/ReachabilityMonitor/index.native.js +4 -6
- package/lib/utils/ReconnectionMonitor.js +20 -24
- package/lib-esm/Providers/AWSIot.js +24 -47
- package/lib-esm/Providers/MqttOverWS.d.ts +5 -3
- package/lib-esm/Providers/MqttOverWS.js +186 -312
- package/lib-esm/Providers/PubSub.d.ts +3 -4
- package/lib-esm/Providers/PubSub.js +11 -18
- package/lib-esm/Providers/constants.js +10 -10
- package/lib-esm/tsconfig.tsbuildinfo +1 -1
- package/lib-esm/types/PubSub.d.ts +11 -2
- package/lib-esm/utils/ConnectionStateMonitor.js +49 -63
- package/lib-esm/utils/ReachabilityMonitor/index.js +1 -1
- package/lib-esm/utils/ReachabilityMonitor/index.native.js +1 -3
- package/lib-esm/utils/ReconnectionMonitor.js +19 -24
- package/package.json +6 -10
- package/src/Providers/MqttOverWS.ts +9 -7
- package/src/Providers/PubSub.ts +10 -13
- package/src/types/PubSub.ts +13 -9
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
-
import { __assign, __awaiter, __extends, __generator, __rest } from "tslib";
|
|
4
3
|
// @ts-ignore
|
|
5
4
|
import * as Paho from '../vendor/paho-mqtt';
|
|
6
5
|
import { Observable } from 'rxjs';
|
|
@@ -11,14 +10,14 @@ import { amplifyUuid } from '@aws-amplify/core/internals/utils';
|
|
|
11
10
|
import { ConnectionStateMonitor, CONNECTION_CHANGE, } from '../utils/ConnectionStateMonitor';
|
|
12
11
|
import { ReconnectEvent, ReconnectionMonitor, } from '../utils/ReconnectionMonitor';
|
|
13
12
|
import { AMPLIFY_SYMBOL, CONNECTION_STATE_CHANGE } from './constants';
|
|
14
|
-
|
|
13
|
+
const logger = new Logger('MqttOverWS');
|
|
15
14
|
export function mqttTopicMatch(filter, topic) {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
for (
|
|
20
|
-
|
|
21
|
-
|
|
15
|
+
const filterArray = filter.split('/');
|
|
16
|
+
const length = filterArray.length;
|
|
17
|
+
const topicArray = topic.split('/');
|
|
18
|
+
for (let i = 0; i < length; ++i) {
|
|
19
|
+
const left = filterArray[i];
|
|
20
|
+
const right = topicArray[i];
|
|
22
21
|
if (left === '#')
|
|
23
22
|
return topicArray.length >= length;
|
|
24
23
|
if (left !== '+' && left !== right)
|
|
@@ -26,369 +25,244 @@ export function mqttTopicMatch(filter, topic) {
|
|
|
26
25
|
}
|
|
27
26
|
return length === topicArray.length;
|
|
28
27
|
}
|
|
29
|
-
|
|
30
|
-
|
|
28
|
+
class ClientsQueue {
|
|
29
|
+
constructor() {
|
|
31
30
|
this.promises = new Map();
|
|
32
31
|
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
});
|
|
50
|
-
};
|
|
51
|
-
Object.defineProperty(ClientsQueue.prototype, "allClients", {
|
|
52
|
-
get: function () {
|
|
53
|
-
return Array.from(this.promises.keys());
|
|
54
|
-
},
|
|
55
|
-
enumerable: false,
|
|
56
|
-
configurable: true
|
|
57
|
-
});
|
|
58
|
-
ClientsQueue.prototype.remove = function (clientId) {
|
|
32
|
+
async get(clientId, clientFactory) {
|
|
33
|
+
const cachedPromise = this.promises.get(clientId);
|
|
34
|
+
if (cachedPromise)
|
|
35
|
+
return cachedPromise;
|
|
36
|
+
if (clientFactory) {
|
|
37
|
+
const newPromise = clientFactory(clientId);
|
|
38
|
+
this.promises.set(clientId, newPromise);
|
|
39
|
+
newPromise.catch(() => this.promises.delete(clientId));
|
|
40
|
+
return newPromise;
|
|
41
|
+
}
|
|
42
|
+
return undefined;
|
|
43
|
+
}
|
|
44
|
+
get allClients() {
|
|
45
|
+
return Array.from(this.promises.keys());
|
|
46
|
+
}
|
|
47
|
+
remove(clientId) {
|
|
59
48
|
this.promises.delete(clientId);
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
var dispatchPubSubEvent = function (payload) {
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
const dispatchPubSubEvent = (payload) => {
|
|
64
52
|
Hub.dispatch('pubsub', payload, 'PubSub', AMPLIFY_SYMBOL);
|
|
65
53
|
};
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
_this._topicObservers = new Map();
|
|
76
|
-
_this._clientIdObservers = new Map();
|
|
54
|
+
const topicSymbol = typeof Symbol !== 'undefined' ? Symbol('topic') : '@@topic';
|
|
55
|
+
export class MqttOverWS extends AbstractPubSub {
|
|
56
|
+
constructor(options = {}) {
|
|
57
|
+
super({ ...options, clientId: options.clientId || amplifyUuid() });
|
|
58
|
+
this._clientsQueue = new ClientsQueue();
|
|
59
|
+
this.connectionStateMonitor = new ConnectionStateMonitor();
|
|
60
|
+
this.reconnectionMonitor = new ReconnectionMonitor();
|
|
61
|
+
this._topicObservers = new Map();
|
|
62
|
+
this._clientIdObservers = new Map();
|
|
77
63
|
// Monitor the connection health state and pass changes along to Hub
|
|
78
|
-
|
|
64
|
+
this.connectionStateMonitor.connectionStateObservable.subscribe(connectionStateChange => {
|
|
79
65
|
dispatchPubSubEvent({
|
|
80
66
|
event: CONNECTION_STATE_CHANGE,
|
|
81
67
|
data: {
|
|
82
|
-
provider:
|
|
68
|
+
provider: this,
|
|
83
69
|
connectionState: connectionStateChange,
|
|
84
70
|
},
|
|
85
|
-
message:
|
|
71
|
+
message: `Connection state is ${connectionStateChange}`,
|
|
86
72
|
});
|
|
87
|
-
|
|
73
|
+
this.connectionState = connectionStateChange;
|
|
88
74
|
// Trigger reconnection when the connection is disrupted
|
|
89
75
|
if (connectionStateChange === ConnectionState.ConnectionDisrupted) {
|
|
90
|
-
|
|
76
|
+
this.reconnectionMonitor.record(ReconnectEvent.START_RECONNECT);
|
|
91
77
|
}
|
|
92
78
|
else if (connectionStateChange !== ConnectionState.Connecting) {
|
|
93
79
|
// Trigger connected to halt reconnection attempts
|
|
94
|
-
|
|
80
|
+
this.reconnectionMonitor.record(ReconnectEvent.HALT_RECONNECT);
|
|
95
81
|
}
|
|
96
82
|
});
|
|
97
|
-
return _this;
|
|
98
83
|
}
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
});
|
|
113
|
-
Object.defineProperty(MqttOverWS.prototype, "clientsQueue", {
|
|
114
|
-
get: function () {
|
|
115
|
-
return this._clientsQueue;
|
|
116
|
-
},
|
|
117
|
-
enumerable: false,
|
|
118
|
-
configurable: true
|
|
119
|
-
});
|
|
120
|
-
Object.defineProperty(MqttOverWS.prototype, "isSSLEnabled", {
|
|
121
|
-
get: function () {
|
|
122
|
-
return !this.options['aws_appsync_dangerously_connect_to_http_endpoint_for_testing'];
|
|
123
|
-
},
|
|
124
|
-
enumerable: false,
|
|
125
|
-
configurable: true
|
|
126
|
-
});
|
|
127
|
-
MqttOverWS.prototype.onDisconnect = function (_a) {
|
|
128
|
-
var clientId = _a.clientId, errorCode = _a.errorCode, args = __rest(_a, ["clientId", "errorCode"]);
|
|
84
|
+
get clientId() {
|
|
85
|
+
return this.options.clientId;
|
|
86
|
+
}
|
|
87
|
+
get endpoint() {
|
|
88
|
+
return Promise.resolve(this.options.endpoint);
|
|
89
|
+
}
|
|
90
|
+
get clientsQueue() {
|
|
91
|
+
return this._clientsQueue;
|
|
92
|
+
}
|
|
93
|
+
get isSSLEnabled() {
|
|
94
|
+
return !this.options['aws_appsync_dangerously_connect_to_http_endpoint_for_testing'];
|
|
95
|
+
}
|
|
96
|
+
onDisconnect({ clientId, errorCode, ...args }) {
|
|
129
97
|
if (errorCode !== 0) {
|
|
130
|
-
logger.warn(clientId, JSON.stringify(
|
|
98
|
+
logger.warn(clientId, JSON.stringify({ errorCode, ...args }, null, 2));
|
|
131
99
|
if (!clientId) {
|
|
132
100
|
return;
|
|
133
101
|
}
|
|
134
|
-
|
|
102
|
+
const clientIdObservers = this._clientIdObservers.get(clientId);
|
|
135
103
|
if (!clientIdObservers) {
|
|
136
104
|
return;
|
|
137
105
|
}
|
|
138
106
|
this.disconnect(clientId);
|
|
139
107
|
}
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
mqttVersion: 3,
|
|
165
|
-
onSuccess: function () { return resolve(true); },
|
|
166
|
-
onFailure: function () {
|
|
167
|
-
if (clientId)
|
|
168
|
-
_this._clientsQueue.remove(clientId);
|
|
169
|
-
_this.connectionStateMonitor.record(CONNECTION_CHANGE.CLOSED);
|
|
170
|
-
resolve(false);
|
|
171
|
-
},
|
|
172
|
-
});
|
|
173
|
-
})];
|
|
174
|
-
case 1:
|
|
175
|
-
connected = _b.sent();
|
|
176
|
-
if (connected) {
|
|
177
|
-
this.connectionStateMonitor.record(CONNECTION_CHANGE.CONNECTION_ESTABLISHED);
|
|
178
|
-
}
|
|
179
|
-
return [2 /*return*/, client];
|
|
180
|
-
}
|
|
181
|
-
});
|
|
182
|
-
});
|
|
183
|
-
};
|
|
184
|
-
MqttOverWS.prototype.connect = function (clientId, options) {
|
|
185
|
-
if (options === void 0) { options = {}; }
|
|
186
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
187
|
-
var _this = this;
|
|
188
|
-
return __generator(this, function (_a) {
|
|
189
|
-
switch (_a.label) {
|
|
190
|
-
case 0: return [4 /*yield*/, this.clientsQueue.get(clientId, function (clientId) { return __awaiter(_this, void 0, void 0, function () {
|
|
191
|
-
var client;
|
|
192
|
-
return __generator(this, function (_a) {
|
|
193
|
-
switch (_a.label) {
|
|
194
|
-
case 0: return [4 /*yield*/, this.newClient(__assign(__assign({}, options), { clientId: clientId }))];
|
|
195
|
-
case 1:
|
|
196
|
-
client = _a.sent();
|
|
197
|
-
if (client) {
|
|
198
|
-
// Once connected, subscribe to all topics registered observers
|
|
199
|
-
this._topicObservers.forEach(function (_value, key) {
|
|
200
|
-
client.subscribe(key);
|
|
201
|
-
});
|
|
202
|
-
}
|
|
203
|
-
return [2 /*return*/, client];
|
|
204
|
-
}
|
|
205
|
-
});
|
|
206
|
-
}); })];
|
|
207
|
-
case 1: return [2 /*return*/, _a.sent()];
|
|
208
|
-
}
|
|
209
|
-
});
|
|
210
|
-
});
|
|
211
|
-
};
|
|
212
|
-
MqttOverWS.prototype.disconnect = function (clientId) {
|
|
213
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
214
|
-
var client;
|
|
215
|
-
return __generator(this, function (_a) {
|
|
216
|
-
switch (_a.label) {
|
|
217
|
-
case 0: return [4 /*yield*/, this.clientsQueue.get(clientId)];
|
|
218
|
-
case 1:
|
|
219
|
-
client = _a.sent();
|
|
220
|
-
if (client && client.isConnected()) {
|
|
221
|
-
client.disconnect();
|
|
222
|
-
}
|
|
223
|
-
this.clientsQueue.remove(clientId);
|
|
224
|
-
this.connectionStateMonitor.record(CONNECTION_CHANGE.CLOSED);
|
|
225
|
-
return [2 /*return*/];
|
|
226
|
-
}
|
|
108
|
+
}
|
|
109
|
+
async newClient({ url, clientId }) {
|
|
110
|
+
logger.debug('Creating new MQTT client', clientId);
|
|
111
|
+
this.connectionStateMonitor.record(CONNECTION_CHANGE.OPENING_CONNECTION);
|
|
112
|
+
// @ts-ignore
|
|
113
|
+
const client = new Paho.Client(url, clientId);
|
|
114
|
+
client.onMessageArrived = ({ destinationName: topic, payloadString: msg, }) => {
|
|
115
|
+
this._onMessage(topic, msg);
|
|
116
|
+
};
|
|
117
|
+
client.onConnectionLost = ({ errorCode, ...args }) => {
|
|
118
|
+
this.onDisconnect({ clientId, errorCode, ...args });
|
|
119
|
+
this.connectionStateMonitor.record(CONNECTION_CHANGE.CLOSED);
|
|
120
|
+
};
|
|
121
|
+
const connected = await new Promise((resolve, reject) => {
|
|
122
|
+
client.connect({
|
|
123
|
+
useSSL: this.isSSLEnabled,
|
|
124
|
+
mqttVersion: 3,
|
|
125
|
+
onSuccess: () => resolve(true),
|
|
126
|
+
onFailure: () => {
|
|
127
|
+
if (clientId)
|
|
128
|
+
this._clientsQueue.remove(clientId);
|
|
129
|
+
this.connectionStateMonitor.record(CONNECTION_CHANGE.CLOSED);
|
|
130
|
+
resolve(false);
|
|
131
|
+
},
|
|
227
132
|
});
|
|
228
133
|
});
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
}
|
|
245
|
-
else {
|
|
246
|
-
logger.debug('Publishing to topic(s) failed', targetTopics.join(','), message);
|
|
247
|
-
}
|
|
248
|
-
return [2 /*return*/];
|
|
249
|
-
}
|
|
250
|
-
});
|
|
134
|
+
if (connected) {
|
|
135
|
+
this.connectionStateMonitor.record(CONNECTION_CHANGE.CONNECTION_ESTABLISHED);
|
|
136
|
+
}
|
|
137
|
+
return client;
|
|
138
|
+
}
|
|
139
|
+
async connect(clientId, options = {}) {
|
|
140
|
+
return await this.clientsQueue.get(clientId, async (clientId) => {
|
|
141
|
+
const client = await this.newClient({ ...options, clientId });
|
|
142
|
+
if (client) {
|
|
143
|
+
// Once connected, subscribe to all topics registered observers
|
|
144
|
+
this._topicObservers.forEach((_value, key) => {
|
|
145
|
+
client.subscribe(key);
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
return client;
|
|
251
149
|
});
|
|
252
|
-
}
|
|
253
|
-
|
|
150
|
+
}
|
|
151
|
+
async disconnect(clientId) {
|
|
152
|
+
const client = await this.clientsQueue.get(clientId);
|
|
153
|
+
if (client && client.isConnected()) {
|
|
154
|
+
client.disconnect();
|
|
155
|
+
}
|
|
156
|
+
this.clientsQueue.remove(clientId);
|
|
157
|
+
this.connectionStateMonitor.record(CONNECTION_CHANGE.CLOSED);
|
|
158
|
+
}
|
|
159
|
+
async publish({ topics, message }) {
|
|
160
|
+
const targetTopics = [].concat(topics);
|
|
161
|
+
const msg = JSON.stringify(message);
|
|
162
|
+
const client = await this.clientsQueue.get(this.clientId);
|
|
163
|
+
if (client) {
|
|
164
|
+
logger.debug('Publishing to topic(s)', targetTopics.join(','), message);
|
|
165
|
+
targetTopics.forEach(topic => client.send(topic, msg));
|
|
166
|
+
}
|
|
167
|
+
else {
|
|
168
|
+
logger.debug('Publishing to topic(s) failed', targetTopics.join(','), message);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
_onMessage(topic, msg) {
|
|
254
172
|
try {
|
|
255
|
-
|
|
256
|
-
this._topicObservers.forEach(
|
|
173
|
+
const matchedTopicObservers = [];
|
|
174
|
+
this._topicObservers.forEach((observerForTopic, observerTopic) => {
|
|
257
175
|
if (mqttTopicMatch(observerTopic, topic)) {
|
|
258
|
-
|
|
176
|
+
matchedTopicObservers.push(observerForTopic);
|
|
259
177
|
}
|
|
260
178
|
});
|
|
261
|
-
|
|
262
|
-
if (typeof
|
|
179
|
+
const parsedMessage = JSON.parse(msg);
|
|
180
|
+
if (typeof parsedMessage === 'object') {
|
|
263
181
|
// @ts-ignore
|
|
264
|
-
|
|
182
|
+
parsedMessage[topicSymbol] = topic;
|
|
265
183
|
}
|
|
266
|
-
|
|
267
|
-
observersForTopic.forEach(
|
|
184
|
+
matchedTopicObservers.forEach(observersForTopic => {
|
|
185
|
+
observersForTopic.forEach(observer => observer.next(parsedMessage));
|
|
268
186
|
});
|
|
269
187
|
}
|
|
270
188
|
catch (error) {
|
|
271
189
|
logger.warn('Error handling message', error, msg);
|
|
272
190
|
}
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
if (options === void 0) { options = {}; }
|
|
277
|
-
var targetTopics = [].concat(topics);
|
|
191
|
+
}
|
|
192
|
+
subscribe({ topics, options = {}, }) {
|
|
193
|
+
const targetTopics = [].concat(topics);
|
|
278
194
|
logger.debug('Subscribing to topic(s)', targetTopics.join(','));
|
|
279
|
-
|
|
280
|
-
return new Observable(
|
|
281
|
-
targetTopics.forEach(
|
|
195
|
+
let reconnectSubscription;
|
|
196
|
+
return new Observable(observer => {
|
|
197
|
+
targetTopics.forEach(topic => {
|
|
282
198
|
// this._topicObservers is used to notify the observers according to the topic received on the message
|
|
283
|
-
|
|
199
|
+
let observersForTopic = this._topicObservers.get(topic);
|
|
284
200
|
if (!observersForTopic) {
|
|
285
201
|
observersForTopic = new Set();
|
|
286
|
-
|
|
202
|
+
this._topicObservers.set(topic, observersForTopic);
|
|
287
203
|
}
|
|
288
204
|
observersForTopic.add(observer);
|
|
289
205
|
});
|
|
290
|
-
|
|
206
|
+
const { clientId = this.clientId } = options;
|
|
291
207
|
// this._clientIdObservers is used to close observers when client gets disconnected
|
|
292
|
-
|
|
208
|
+
let observersForClientId = this._clientIdObservers.get(clientId);
|
|
293
209
|
if (!observersForClientId) {
|
|
294
210
|
observersForClientId = new Set();
|
|
295
211
|
}
|
|
296
212
|
if (observersForClientId) {
|
|
297
213
|
observersForClientId.add(observer);
|
|
298
|
-
|
|
214
|
+
this._clientIdObservers.set(clientId, observersForClientId);
|
|
299
215
|
}
|
|
300
|
-
(
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
return __generator(this, function (_c) {
|
|
309
|
-
switch (_c.label) {
|
|
310
|
-
case 0:
|
|
311
|
-
_c.trys.push([0, 5, , 6]);
|
|
312
|
-
_a = options.url;
|
|
313
|
-
if (!(_a === void 0)) return [3 /*break*/, 2];
|
|
314
|
-
return [4 /*yield*/, this.endpoint];
|
|
315
|
-
case 1:
|
|
316
|
-
_b = _c.sent();
|
|
317
|
-
return [3 /*break*/, 3];
|
|
318
|
-
case 2:
|
|
319
|
-
_b = _a;
|
|
320
|
-
_c.label = 3;
|
|
321
|
-
case 3:
|
|
322
|
-
url = _b;
|
|
323
|
-
return [4 /*yield*/, this.connect(clientId, { url: url })];
|
|
324
|
-
case 4:
|
|
325
|
-
client_1 = _c.sent();
|
|
326
|
-
if (client_1 !== undefined) {
|
|
327
|
-
targetTopics.forEach(function (topic) {
|
|
328
|
-
client_1.subscribe(topic);
|
|
329
|
-
});
|
|
330
|
-
}
|
|
331
|
-
return [3 /*break*/, 6];
|
|
332
|
-
case 5:
|
|
333
|
-
e_1 = _c.sent();
|
|
334
|
-
logger.debug('Error forming connection', e_1);
|
|
335
|
-
return [3 /*break*/, 6];
|
|
336
|
-
case 6: return [2 /*return*/];
|
|
337
|
-
}
|
|
338
|
-
});
|
|
339
|
-
}); };
|
|
340
|
-
// Establish the initial connection
|
|
341
|
-
return [4 /*yield*/, getClient()];
|
|
342
|
-
case 1:
|
|
343
|
-
// Establish the initial connection
|
|
344
|
-
_a.sent();
|
|
345
|
-
// Add an observable to the reconnection list to manage reconnection for this subscription
|
|
346
|
-
reconnectSubscription = new Observable(function (observer) {
|
|
347
|
-
_this.reconnectionMonitor.addObserver(observer);
|
|
348
|
-
}).subscribe(function () {
|
|
349
|
-
getClient();
|
|
216
|
+
(async () => {
|
|
217
|
+
const getClient = async () => {
|
|
218
|
+
try {
|
|
219
|
+
const { url = await this.endpoint } = options;
|
|
220
|
+
const client = await this.connect(clientId, { url });
|
|
221
|
+
if (client !== undefined) {
|
|
222
|
+
targetTopics.forEach(topic => {
|
|
223
|
+
client.subscribe(topic);
|
|
350
224
|
});
|
|
351
|
-
|
|
225
|
+
}
|
|
352
226
|
}
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
return function () { return __awaiter(_this, void 0, void 0, function () {
|
|
356
|
-
var client;
|
|
357
|
-
var _this = this;
|
|
358
|
-
var _a, _b;
|
|
359
|
-
return __generator(this, function (_c) {
|
|
360
|
-
switch (_c.label) {
|
|
361
|
-
case 0: return [4 /*yield*/, this.clientsQueue.get(clientId)];
|
|
362
|
-
case 1:
|
|
363
|
-
client = _c.sent();
|
|
364
|
-
reconnectSubscription === null || reconnectSubscription === void 0 ? void 0 : reconnectSubscription.unsubscribe();
|
|
365
|
-
if (client) {
|
|
366
|
-
(_a = this._clientIdObservers.get(clientId)) === null || _a === void 0 ? void 0 : _a.delete(observer);
|
|
367
|
-
// No more observers per client => client not needed anymore
|
|
368
|
-
if (((_b = this._clientIdObservers.get(clientId)) === null || _b === void 0 ? void 0 : _b.size) === 0) {
|
|
369
|
-
this.disconnect(clientId);
|
|
370
|
-
this.connectionStateMonitor.record(CONNECTION_CHANGE.CLOSING_CONNECTION);
|
|
371
|
-
this._clientIdObservers.delete(clientId);
|
|
372
|
-
}
|
|
373
|
-
targetTopics.forEach(function (topic) {
|
|
374
|
-
var observersForTopic = _this._topicObservers.get(topic) ||
|
|
375
|
-
new Set();
|
|
376
|
-
observersForTopic.delete(observer);
|
|
377
|
-
// if no observers exists for the topic, topic should be removed
|
|
378
|
-
if (observersForTopic.size === 0) {
|
|
379
|
-
_this._topicObservers.delete(topic);
|
|
380
|
-
if (client.isConnected()) {
|
|
381
|
-
client.unsubscribe(topic);
|
|
382
|
-
}
|
|
383
|
-
}
|
|
384
|
-
});
|
|
385
|
-
}
|
|
386
|
-
return [2 /*return*/, null];
|
|
227
|
+
catch (e) {
|
|
228
|
+
logger.debug('Error forming connection', e);
|
|
387
229
|
}
|
|
230
|
+
};
|
|
231
|
+
// Establish the initial connection
|
|
232
|
+
await getClient();
|
|
233
|
+
// Add an observable to the reconnection list to manage reconnection for this subscription
|
|
234
|
+
reconnectSubscription = new Observable(observer => {
|
|
235
|
+
this.reconnectionMonitor.addObserver(observer);
|
|
236
|
+
}).subscribe(() => {
|
|
237
|
+
getClient();
|
|
388
238
|
});
|
|
389
|
-
});
|
|
239
|
+
})();
|
|
240
|
+
return async () => {
|
|
241
|
+
const client = await this.clientsQueue.get(clientId);
|
|
242
|
+
reconnectSubscription?.unsubscribe();
|
|
243
|
+
if (client) {
|
|
244
|
+
this._clientIdObservers.get(clientId)?.delete(observer);
|
|
245
|
+
// No more observers per client => client not needed anymore
|
|
246
|
+
if (this._clientIdObservers.get(clientId)?.size === 0) {
|
|
247
|
+
this.disconnect(clientId);
|
|
248
|
+
this.connectionStateMonitor.record(CONNECTION_CHANGE.CLOSING_CONNECTION);
|
|
249
|
+
this._clientIdObservers.delete(clientId);
|
|
250
|
+
}
|
|
251
|
+
targetTopics.forEach(topic => {
|
|
252
|
+
const observersForTopic = this._topicObservers.get(topic) ||
|
|
253
|
+
new Set();
|
|
254
|
+
observersForTopic.delete(observer);
|
|
255
|
+
// if no observers exists for the topic, topic should be removed
|
|
256
|
+
if (observersForTopic.size === 0) {
|
|
257
|
+
this._topicObservers.delete(topic);
|
|
258
|
+
if (client.isConnected()) {
|
|
259
|
+
client.unsubscribe(topic);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
return null;
|
|
265
|
+
};
|
|
390
266
|
});
|
|
391
|
-
}
|
|
392
|
-
|
|
393
|
-
}(AbstractPubSub));
|
|
394
|
-
export { MqttOverWS };
|
|
267
|
+
}
|
|
268
|
+
}
|
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
import { Observable } from 'rxjs';
|
|
2
|
-
import { PubSubBase, PubSubOptions, PubSubContent } from '../types/PubSub';
|
|
3
|
-
import { CustomUserAgentDetails } from '@aws-amplify/core/internals/utils';
|
|
2
|
+
import { PubSubBase, PubSubOptions, PubSubContent, PublishInput, SubscribeInput } from '../types/PubSub';
|
|
4
3
|
export declare abstract class AbstractPubSub<T extends PubSubOptions> implements PubSubBase {
|
|
5
4
|
private _config;
|
|
6
5
|
constructor(options: T);
|
|
7
6
|
configure(config: T): T;
|
|
8
7
|
protected get options(): T;
|
|
9
8
|
abstract newClient(clientOptions: T): Promise<any>;
|
|
10
|
-
abstract publish(
|
|
11
|
-
abstract subscribe(
|
|
9
|
+
abstract publish(input: PublishInput): void;
|
|
10
|
+
abstract subscribe(input: SubscribeInput): Observable<PubSubContent>;
|
|
12
11
|
}
|
|
@@ -1,22 +1,15 @@
|
|
|
1
|
-
import { __assign } from "tslib";
|
|
2
1
|
import { ConsoleLogger as Logger } from '@aws-amplify/core';
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
2
|
+
const logger = new Logger('AbstractPubSubProvider');
|
|
3
|
+
export class AbstractPubSub {
|
|
4
|
+
constructor(options) {
|
|
6
5
|
this._config = options;
|
|
7
6
|
}
|
|
8
|
-
|
|
9
|
-
this._config =
|
|
10
|
-
logger.debug(
|
|
7
|
+
configure(config) {
|
|
8
|
+
this._config = { ...config, ...this._config };
|
|
9
|
+
logger.debug(`configure`, this._config);
|
|
11
10
|
return this.options;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
enumerable: false,
|
|
18
|
-
configurable: true
|
|
19
|
-
});
|
|
20
|
-
return AbstractPubSub;
|
|
21
|
-
}());
|
|
22
|
-
export { AbstractPubSub };
|
|
11
|
+
}
|
|
12
|
+
get options() {
|
|
13
|
+
return { ...this._config };
|
|
14
|
+
}
|
|
15
|
+
}
|