@aws-amplify/pubsub 5.2.2-unstable.c1fa9c7.5 → 5.3.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/internals/package.json +8 -0
- package/lib/Providers/AWSAppSyncRealTimeProvider/index.d.ts +2 -1
- package/lib/Providers/AWSAppSyncRealTimeProvider/index.js +4 -3
- package/lib/Providers/AWSAppSyncRealTimeProvider/index.js.map +1 -1
- package/lib/Providers/PubSubProvider.d.ts +2 -1
- package/lib/Providers/PubSubProvider.js.map +1 -1
- package/lib/PubSub.d.ts +3 -39
- package/lib/PubSub.js +7 -121
- package/lib/PubSub.js.map +1 -1
- package/lib/internals/InternalPubSub.d.ts +50 -0
- package/lib/internals/InternalPubSub.js +136 -0
- package/lib/internals/InternalPubSub.js.map +1 -0
- package/lib/internals/index.d.ts +1 -0
- package/lib/internals/index.js +8 -0
- package/lib/internals/index.js.map +1 -0
- package/lib-esm/Providers/AWSAppSyncRealTimeProvider/index.d.ts +2 -1
- package/lib-esm/Providers/AWSAppSyncRealTimeProvider/index.js +5 -4
- package/lib-esm/Providers/AWSAppSyncRealTimeProvider/index.js.map +1 -1
- package/lib-esm/Providers/PubSubProvider.d.ts +2 -1
- package/lib-esm/Providers/PubSubProvider.js +1 -1
- package/lib-esm/Providers/PubSubProvider.js.map +1 -1
- package/lib-esm/PubSub.d.ts +3 -39
- package/lib-esm/PubSub.js +9 -123
- package/lib-esm/PubSub.js.map +1 -1
- package/lib-esm/internals/InternalPubSub.d.ts +50 -0
- package/lib-esm/internals/InternalPubSub.js +134 -0
- package/lib-esm/internals/InternalPubSub.js.map +1 -0
- package/lib-esm/internals/index.d.ts +1 -0
- package/lib-esm/internals/index.js +4 -0
- package/lib-esm/internals/index.js.map +1 -0
- package/package.json +9 -8
- package/src/Providers/AWSAppSyncRealTimeProvider/index.ts +9 -2
- package/src/Providers/PubSubProvider.ts +6 -2
- package/src/PubSub.ts +4 -148
- package/src/internals/InternalPubSub.ts +185 -0
- package/src/internals/index.ts +3 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aws-amplify/pubsub",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.3.0",
|
|
4
4
|
"description": "Pubsub category of aws-amplify",
|
|
5
5
|
"main": "./lib/index.js",
|
|
6
6
|
"module": "./lib-esm/index.js",
|
|
@@ -47,12 +47,13 @@
|
|
|
47
47
|
"files": [
|
|
48
48
|
"lib",
|
|
49
49
|
"lib-esm",
|
|
50
|
-
"src"
|
|
50
|
+
"src",
|
|
51
|
+
"internals"
|
|
51
52
|
],
|
|
52
53
|
"dependencies": {
|
|
53
|
-
"@aws-amplify/auth": "5.
|
|
54
|
-
"@aws-amplify/cache": "5.1.1
|
|
55
|
-
"@aws-amplify/core": "5.
|
|
54
|
+
"@aws-amplify/auth": "5.5.0",
|
|
55
|
+
"@aws-amplify/cache": "5.1.1",
|
|
56
|
+
"@aws-amplify/core": "5.5.0",
|
|
56
57
|
"graphql": "15.8.0",
|
|
57
58
|
"tslib": "^1.8.0",
|
|
58
59
|
"url": "0.11.0",
|
|
@@ -64,13 +65,13 @@
|
|
|
64
65
|
"name": "PubSub (IoT provider)",
|
|
65
66
|
"path": "./lib-esm/index.js",
|
|
66
67
|
"import": "{ Amplify, PubSub, AWSIoTProvider }",
|
|
67
|
-
"limit": "
|
|
68
|
+
"limit": "80 kB"
|
|
68
69
|
},
|
|
69
70
|
{
|
|
70
71
|
"name": "PubSub (Mqtt provider)",
|
|
71
72
|
"path": "./lib-esm/index.js",
|
|
72
73
|
"import": "{ Amplify, PubSub, MqttOverWSProvider }",
|
|
73
|
-
"limit": "
|
|
74
|
+
"limit": "80 kB"
|
|
74
75
|
}
|
|
75
76
|
],
|
|
76
77
|
"jest": {
|
|
@@ -123,5 +124,5 @@
|
|
|
123
124
|
"lib-esm"
|
|
124
125
|
]
|
|
125
126
|
},
|
|
126
|
-
"gitHead": "
|
|
127
|
+
"gitHead": "95ab2ccdd0fd62afd90c02e3e8273d4acc400ddc"
|
|
127
128
|
}
|
|
@@ -17,6 +17,8 @@ import {
|
|
|
17
17
|
NonRetryableError,
|
|
18
18
|
ICredentials,
|
|
19
19
|
isNonRetryableError,
|
|
20
|
+
CustomUserAgentDetails,
|
|
21
|
+
getAmplifyUserAgentString,
|
|
20
22
|
} from '@aws-amplify/core';
|
|
21
23
|
import { Cache } from '@aws-amplify/cache';
|
|
22
24
|
import { Auth, GRAPHQL_AUTH_MODE } from '@aws-amplify/auth';
|
|
@@ -208,7 +210,8 @@ export class AWSAppSyncRealTimeProvider extends AbstractPubSubProvider<AWSAppSyn
|
|
|
208
210
|
|
|
209
211
|
subscribe(
|
|
210
212
|
_topics: string[] | string,
|
|
211
|
-
options?: AWSAppSyncRealTimeProviderOptions
|
|
213
|
+
options?: AWSAppSyncRealTimeProviderOptions,
|
|
214
|
+
customUserAgentDetails?: CustomUserAgentDetails
|
|
212
215
|
): Observable<Record<string, unknown>> {
|
|
213
216
|
const appSyncGraphqlEndpoint = options?.appSyncGraphqlEndpoint;
|
|
214
217
|
|
|
@@ -230,11 +233,13 @@ export class AWSAppSyncRealTimeProvider extends AbstractPubSubProvider<AWSAppSyn
|
|
|
230
233
|
const startSubscription = () => {
|
|
231
234
|
if (!subscriptionStartActive) {
|
|
232
235
|
subscriptionStartActive = true;
|
|
236
|
+
|
|
233
237
|
const startSubscriptionPromise =
|
|
234
238
|
this._startSubscriptionWithAWSAppSyncRealTime({
|
|
235
239
|
options,
|
|
236
240
|
observer,
|
|
237
241
|
subscriptionId,
|
|
242
|
+
customUserAgentDetails,
|
|
238
243
|
}).catch<any>(err => {
|
|
239
244
|
logger.debug(
|
|
240
245
|
`${CONTROL_MSG.REALTIME_SUBSCRIPTION_INIT_ERROR}: ${err}`
|
|
@@ -301,10 +306,12 @@ export class AWSAppSyncRealTimeProvider extends AbstractPubSubProvider<AWSAppSyn
|
|
|
301
306
|
options,
|
|
302
307
|
observer,
|
|
303
308
|
subscriptionId,
|
|
309
|
+
customUserAgentDetails,
|
|
304
310
|
}: {
|
|
305
311
|
options: AWSAppSyncRealTimeProviderOptions;
|
|
306
312
|
observer: PubSubContentObserver;
|
|
307
313
|
subscriptionId: string;
|
|
314
|
+
customUserAgentDetails: CustomUserAgentDetails;
|
|
308
315
|
}) {
|
|
309
316
|
const {
|
|
310
317
|
appSyncGraphqlEndpoint,
|
|
@@ -346,7 +353,7 @@ export class AWSAppSyncRealTimeProvider extends AbstractPubSubProvider<AWSAppSyn
|
|
|
346
353
|
})),
|
|
347
354
|
...(await graphql_headers()),
|
|
348
355
|
...additionalHeaders,
|
|
349
|
-
[USER_AGENT_HEADER]:
|
|
356
|
+
[USER_AGENT_HEADER]: getAmplifyUserAgentString(customUserAgentDetails),
|
|
350
357
|
};
|
|
351
358
|
|
|
352
359
|
const subscriptionMessage = {
|
|
@@ -2,7 +2,10 @@
|
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
3
|
import Observable from 'zen-observable-ts';
|
|
4
4
|
import { PubSubProvider, ProviderOptions } from '../types/Provider';
|
|
5
|
-
import {
|
|
5
|
+
import {
|
|
6
|
+
CustomUserAgentDetails,
|
|
7
|
+
ConsoleLogger as Logger,
|
|
8
|
+
} from '@aws-amplify/core';
|
|
6
9
|
import { PubSubContent } from '../types/PubSub';
|
|
7
10
|
|
|
8
11
|
const logger = new Logger('AbstractPubSubProvider');
|
|
@@ -44,6 +47,7 @@ export abstract class AbstractPubSubProvider<T extends ProviderOptions>
|
|
|
44
47
|
|
|
45
48
|
public abstract subscribe(
|
|
46
49
|
topics: string[] | string,
|
|
47
|
-
options?: T
|
|
50
|
+
options?: T,
|
|
51
|
+
customUserAgentDetails?: CustomUserAgentDetails
|
|
48
52
|
): Observable<PubSubContent>;
|
|
49
53
|
}
|
package/src/PubSub.ts
CHANGED
|
@@ -7,169 +7,25 @@ import {
|
|
|
7
7
|
Amplify,
|
|
8
8
|
browserOrNode,
|
|
9
9
|
ConsoleLogger as Logger,
|
|
10
|
-
INTERNAL_AWS_APPSYNC_REALTIME_PUBSUB_PROVIDER,
|
|
11
10
|
} from '@aws-amplify/core';
|
|
12
|
-
import { PubSubProvider,
|
|
13
|
-
import {
|
|
14
|
-
import { PubSubContent } from './types/PubSub';
|
|
15
|
-
|
|
16
|
-
const { isNode } = browserOrNode();
|
|
17
|
-
const logger = new Logger('PubSub');
|
|
11
|
+
import { PubSubProvider, ProviderOptions } from './types';
|
|
12
|
+
import { InternalPubSubClass } from './internals';
|
|
18
13
|
|
|
19
14
|
type PubSubObservable = {
|
|
20
15
|
provider: PubSubProvider;
|
|
21
16
|
value: string | Record<string, unknown>;
|
|
22
17
|
};
|
|
23
18
|
|
|
24
|
-
export class PubSubClass {
|
|
25
|
-
private _options: PubSubOptions;
|
|
26
|
-
|
|
27
|
-
private _pluggables: PubSubProvider[];
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Internal instance of AWSAppSyncRealTimeProvider used by the API category to subscribe to AppSync
|
|
31
|
-
*/
|
|
32
|
-
private _awsAppSyncRealTimeProvider?: AWSAppSyncRealTimeProvider;
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Lazy instantiate AWSAppSyncRealTimeProvider when it is required by the API category
|
|
36
|
-
*/
|
|
37
|
-
private get awsAppSyncRealTimeProvider() {
|
|
38
|
-
if (!this._awsAppSyncRealTimeProvider) {
|
|
39
|
-
this._awsAppSyncRealTimeProvider = new AWSAppSyncRealTimeProvider(
|
|
40
|
-
this._options
|
|
41
|
-
);
|
|
42
|
-
}
|
|
43
|
-
return this._awsAppSyncRealTimeProvider;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* Initialize PubSub with AWS configurations
|
|
48
|
-
*
|
|
49
|
-
* @param {PubSubOptions} options - Configuration object for PubSub
|
|
50
|
-
*/
|
|
51
|
-
constructor(options?: PubSubOptions) {
|
|
52
|
-
this._options = options ?? {};
|
|
53
|
-
logger.debug('PubSub Options', this._options);
|
|
54
|
-
this._pluggables = [];
|
|
55
|
-
this.subscribe = this.subscribe.bind(this);
|
|
56
|
-
}
|
|
57
|
-
|
|
19
|
+
export class PubSubClass extends InternalPubSubClass {
|
|
58
20
|
public getModuleName() {
|
|
59
21
|
return 'PubSub';
|
|
60
22
|
}
|
|
61
23
|
|
|
62
|
-
/**
|
|
63
|
-
* Configure PubSub part with configurations
|
|
64
|
-
*
|
|
65
|
-
* @param {PubSubOptions} config - Configuration for PubSub
|
|
66
|
-
* @return {Object} - The current configuration
|
|
67
|
-
*/
|
|
68
|
-
configure(options: PubSubOptions) {
|
|
69
|
-
const opt: Record<string, unknown> = options
|
|
70
|
-
? options.PubSub || options
|
|
71
|
-
: {};
|
|
72
|
-
logger.debug('configure PubSub', { opt });
|
|
73
|
-
|
|
74
|
-
this._options = Object.assign({}, this._options, opt);
|
|
75
|
-
|
|
76
|
-
this._pluggables.map(pluggable => pluggable.configure(this._options));
|
|
77
|
-
|
|
78
|
-
return this._options;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
/**
|
|
82
|
-
* add plugin into Analytics category
|
|
83
|
-
* @param {Object} pluggable - an instance of the plugin
|
|
84
|
-
*/
|
|
85
|
-
public async addPluggable(pluggable: PubSubProvider) {
|
|
86
|
-
if (pluggable && pluggable.getCategory() === 'PubSub') {
|
|
87
|
-
this._pluggables.push(pluggable);
|
|
88
|
-
|
|
89
|
-
const config = pluggable.configure(this._options);
|
|
90
|
-
|
|
91
|
-
return config;
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* remove plugin from PubSub category
|
|
97
|
-
* @param providerName - the name of the plugin
|
|
98
|
-
*/
|
|
99
|
-
removePluggable(providerName: string): void {
|
|
100
|
-
this._pluggables = this._pluggables.filter(
|
|
101
|
-
pluggable => pluggable.getProviderName() !== providerName
|
|
102
|
-
);
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
private getProviderByName(providerName: string | symbol) {
|
|
106
|
-
if (providerName === INTERNAL_AWS_APPSYNC_REALTIME_PUBSUB_PROVIDER) {
|
|
107
|
-
return this.awsAppSyncRealTimeProvider;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
return this._pluggables.find(
|
|
111
|
-
pluggable => pluggable.getProviderName() === providerName
|
|
112
|
-
);
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
private getProviders(options: ProviderOptions = {}) {
|
|
116
|
-
const providerName = options.provider;
|
|
117
|
-
if (!providerName) {
|
|
118
|
-
return this._pluggables;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
const provider = this.getProviderByName(providerName);
|
|
122
|
-
if (!provider) {
|
|
123
|
-
throw new Error(`Could not find provider named ${String(providerName)}`);
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
return [provider];
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
async publish(
|
|
130
|
-
topics: string[] | string,
|
|
131
|
-
msg: PubSubContent,
|
|
132
|
-
options?: ProviderOptions
|
|
133
|
-
) {
|
|
134
|
-
return Promise.all(
|
|
135
|
-
this.getProviders(options).map(provider =>
|
|
136
|
-
provider.publish(topics, msg, options)
|
|
137
|
-
)
|
|
138
|
-
);
|
|
139
|
-
}
|
|
140
|
-
|
|
141
24
|
subscribe(
|
|
142
25
|
topics: string[] | string,
|
|
143
26
|
options?: ProviderOptions
|
|
144
27
|
): Observable<PubSubObservable> {
|
|
145
|
-
|
|
146
|
-
throw new Error(
|
|
147
|
-
'Subscriptions are not supported for Server-Side Rendering (SSR)'
|
|
148
|
-
);
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
logger.debug('subscribe options', options);
|
|
152
|
-
|
|
153
|
-
const providers = this.getProviders(options);
|
|
154
|
-
|
|
155
|
-
return new Observable<PubSubObservable>(observer => {
|
|
156
|
-
const observables = providers.map(provider => ({
|
|
157
|
-
provider,
|
|
158
|
-
observable: provider.subscribe(topics, options),
|
|
159
|
-
}));
|
|
160
|
-
|
|
161
|
-
const subscriptions = observables.map(({ provider, observable }) =>
|
|
162
|
-
observable.subscribe({
|
|
163
|
-
start: console.error,
|
|
164
|
-
next: (value: PubSubContent) => observer.next({ provider, value }),
|
|
165
|
-
error: (error: unknown) => observer.error({ provider, error }),
|
|
166
|
-
// complete: observer.complete, // TODO: when all completed, complete the outer one
|
|
167
|
-
})
|
|
168
|
-
);
|
|
169
|
-
|
|
170
|
-
return () =>
|
|
171
|
-
subscriptions.forEach(subscription => subscription.unsubscribe());
|
|
172
|
-
});
|
|
28
|
+
return super.subscribe(topics, options);
|
|
173
29
|
}
|
|
174
30
|
}
|
|
175
31
|
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
import {
|
|
4
|
+
Amplify,
|
|
5
|
+
browserOrNode,
|
|
6
|
+
Category,
|
|
7
|
+
ConsoleLogger as Logger,
|
|
8
|
+
CustomUserAgentDetails,
|
|
9
|
+
INTERNAL_AWS_APPSYNC_REALTIME_PUBSUB_PROVIDER,
|
|
10
|
+
PubSubAction,
|
|
11
|
+
} from '@aws-amplify/core';
|
|
12
|
+
import { PubSubProvider, PubSubOptions, ProviderOptions } from '../types';
|
|
13
|
+
import { AWSAppSyncRealTimeProvider } from '../Providers';
|
|
14
|
+
import { PubSubContent } from '../types/PubSub';
|
|
15
|
+
import Observable from 'zen-observable-ts';
|
|
16
|
+
|
|
17
|
+
const { isNode } = browserOrNode();
|
|
18
|
+
const logger = new Logger('PubSub');
|
|
19
|
+
|
|
20
|
+
type PubSubObservable = {
|
|
21
|
+
provider: PubSubProvider;
|
|
22
|
+
value: string | Record<string, unknown>;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export class InternalPubSubClass {
|
|
26
|
+
private _options: PubSubOptions;
|
|
27
|
+
|
|
28
|
+
private _pluggables: PubSubProvider[];
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Internal instance of AWSAppSyncRealTimeProvider used by the API category to subscribe to AppSync
|
|
32
|
+
*/
|
|
33
|
+
private _awsAppSyncRealTimeProvider?: AWSAppSyncRealTimeProvider;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Lazy instantiate AWSAppSyncRealTimeProvider when it is required by the API category
|
|
37
|
+
*/
|
|
38
|
+
private get awsAppSyncRealTimeProvider() {
|
|
39
|
+
if (!this._awsAppSyncRealTimeProvider) {
|
|
40
|
+
this._awsAppSyncRealTimeProvider = new AWSAppSyncRealTimeProvider(
|
|
41
|
+
this._options
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
return this._awsAppSyncRealTimeProvider;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Initialize PubSub with AWS configurations
|
|
49
|
+
*
|
|
50
|
+
* @param {PubSubOptions} options - Configuration object for PubSub
|
|
51
|
+
*/
|
|
52
|
+
constructor(options?: PubSubOptions) {
|
|
53
|
+
this._options = options ?? {};
|
|
54
|
+
logger.debug('PubSub Options', this._options);
|
|
55
|
+
this._pluggables = [];
|
|
56
|
+
this.subscribe = this.subscribe.bind(this);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
public getModuleName() {
|
|
60
|
+
return 'InternalPubSub';
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Configure PubSub part with configurations
|
|
65
|
+
*
|
|
66
|
+
* @param {PubSubOptions} config - Configuration for PubSub
|
|
67
|
+
* @return {Object} - The current configuration
|
|
68
|
+
*/
|
|
69
|
+
configure(options: PubSubOptions) {
|
|
70
|
+
const opt: Record<string, unknown> = options
|
|
71
|
+
? options.PubSub || options
|
|
72
|
+
: {};
|
|
73
|
+
logger.debug('configure PubSub', { opt });
|
|
74
|
+
|
|
75
|
+
this._options = Object.assign({}, this._options, opt);
|
|
76
|
+
|
|
77
|
+
this._pluggables.map(pluggable => pluggable.configure(this._options));
|
|
78
|
+
|
|
79
|
+
return this._options;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* add plugin into Analytics category
|
|
84
|
+
* @param {Object} pluggable - an instance of the plugin
|
|
85
|
+
*/
|
|
86
|
+
public async addPluggable(pluggable: PubSubProvider) {
|
|
87
|
+
if (pluggable && pluggable.getCategory() === 'PubSub') {
|
|
88
|
+
this._pluggables.push(pluggable);
|
|
89
|
+
|
|
90
|
+
const config = pluggable.configure(this._options);
|
|
91
|
+
|
|
92
|
+
return config;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* remove plugin from PubSub category
|
|
98
|
+
* @param providerName - the name of the plugin
|
|
99
|
+
*/
|
|
100
|
+
removePluggable(providerName: string): void {
|
|
101
|
+
this._pluggables = this._pluggables.filter(
|
|
102
|
+
pluggable => pluggable.getProviderName() !== providerName
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
private getProviderByName(providerName: string | symbol) {
|
|
107
|
+
if (providerName === INTERNAL_AWS_APPSYNC_REALTIME_PUBSUB_PROVIDER) {
|
|
108
|
+
return this.awsAppSyncRealTimeProvider;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return this._pluggables.find(
|
|
112
|
+
pluggable => pluggable.getProviderName() === providerName
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
private getProviders(options: ProviderOptions = {}) {
|
|
117
|
+
const providerName = options.provider;
|
|
118
|
+
if (!providerName) {
|
|
119
|
+
return this._pluggables;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const provider = this.getProviderByName(providerName);
|
|
123
|
+
if (!provider) {
|
|
124
|
+
throw new Error(`Could not find provider named ${String(providerName)}`);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
return [provider];
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
async publish(
|
|
131
|
+
topics: string[] | string,
|
|
132
|
+
msg: PubSubContent,
|
|
133
|
+
options?: ProviderOptions
|
|
134
|
+
) {
|
|
135
|
+
return Promise.all(
|
|
136
|
+
this.getProviders(options).map(provider =>
|
|
137
|
+
provider.publish(topics, msg, options)
|
|
138
|
+
)
|
|
139
|
+
);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
subscribe(
|
|
143
|
+
topics: string[] | string,
|
|
144
|
+
options?: ProviderOptions,
|
|
145
|
+
customUserAgentDetails?: CustomUserAgentDetails
|
|
146
|
+
): Observable<PubSubObservable> {
|
|
147
|
+
if (isNode && this._options && this._options.ssr) {
|
|
148
|
+
throw new Error(
|
|
149
|
+
'Subscriptions are not supported for Server-Side Rendering (SSR)'
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
logger.debug('subscribe options', options);
|
|
154
|
+
|
|
155
|
+
const providers = this.getProviders(options);
|
|
156
|
+
|
|
157
|
+
const pubSubUserAgentDetails: CustomUserAgentDetails = {
|
|
158
|
+
category: Category.PubSub,
|
|
159
|
+
action: PubSubAction.Subscribe,
|
|
160
|
+
...customUserAgentDetails,
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
return new Observable<PubSubObservable>(observer => {
|
|
164
|
+
const observables = providers.map(provider => ({
|
|
165
|
+
provider,
|
|
166
|
+
observable: provider.subscribe(topics, options, pubSubUserAgentDetails),
|
|
167
|
+
}));
|
|
168
|
+
|
|
169
|
+
const subscriptions = observables.map(({ provider, observable }) =>
|
|
170
|
+
observable.subscribe({
|
|
171
|
+
start: console.error,
|
|
172
|
+
next: (value: PubSubContent) => observer.next({ provider, value }),
|
|
173
|
+
error: (error: unknown) => observer.error({ provider, error }),
|
|
174
|
+
// complete: observer.complete, // TODO: when all completed, complete the outer one
|
|
175
|
+
})
|
|
176
|
+
);
|
|
177
|
+
|
|
178
|
+
return () =>
|
|
179
|
+
subscriptions.forEach(subscription => subscription.unsubscribe());
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
export const InternalPubSub = new InternalPubSubClass();
|
|
185
|
+
Amplify.register(InternalPubSub);
|