UNPKG

5.74 kBJavaScriptView Raw
1import { __awaiter, __generator } from "tslib";
2// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3// SPDX-License-Identifier: Apache-2.0
4// import '../Common/Polyfills';
5import Observable from 'zen-observable-ts';
6import { Amplify, browserOrNode, ConsoleLogger as Logger, INTERNAL_AWS_APPSYNC_REALTIME_PUBSUB_PROVIDER, } from '@aws-amplify/core';
7import { AWSAppSyncRealTimeProvider } from './Providers';
8var isNode = browserOrNode().isNode;
9var logger = new Logger('PubSub');
10var PubSubClass = /** @class */ (function () {
11 /**
12 * Initialize PubSub with AWS configurations
13 *
14 * @param {PubSubOptions} options - Configuration object for PubSub
15 */
16 function PubSubClass(options) {
17 this._options = options !== null && options !== void 0 ? options : {};
18 logger.debug('PubSub Options', this._options);
19 this._pluggables = [];
20 this.subscribe = this.subscribe.bind(this);
21 }
22 Object.defineProperty(PubSubClass.prototype, "awsAppSyncRealTimeProvider", {
23 /**
24 * Lazy instantiate AWSAppSyncRealTimeProvider when it is required by the API category
25 */
26 get: function () {
27 if (!this._awsAppSyncRealTimeProvider) {
28 this._awsAppSyncRealTimeProvider = new AWSAppSyncRealTimeProvider(this._options);
29 }
30 return this._awsAppSyncRealTimeProvider;
31 },
32 enumerable: true,
33 configurable: true
34 });
35 PubSubClass.prototype.getModuleName = function () {
36 return 'PubSub';
37 };
38 /**
39 * Configure PubSub part with configurations
40 *
41 * @param {PubSubOptions} config - Configuration for PubSub
42 * @return {Object} - The current configuration
43 */
44 PubSubClass.prototype.configure = function (options) {
45 var _this = this;
46 var opt = options
47 ? options.PubSub || options
48 : {};
49 logger.debug('configure PubSub', { opt: opt });
50 this._options = Object.assign({}, this._options, opt);
51 this._pluggables.map(function (pluggable) { return pluggable.configure(_this._options); });
52 return this._options;
53 };
54 /**
55 * add plugin into Analytics category
56 * @param {Object} pluggable - an instance of the plugin
57 */
58 PubSubClass.prototype.addPluggable = function (pluggable) {
59 return __awaiter(this, void 0, void 0, function () {
60 var config;
61 return __generator(this, function (_a) {
62 if (pluggable && pluggable.getCategory() === 'PubSub') {
63 this._pluggables.push(pluggable);
64 config = pluggable.configure(this._options);
65 return [2 /*return*/, config];
66 }
67 return [2 /*return*/];
68 });
69 });
70 };
71 /**
72 * remove plugin from PubSub category
73 * @param providerName - the name of the plugin
74 */
75 PubSubClass.prototype.removePluggable = function (providerName) {
76 this._pluggables = this._pluggables.filter(function (pluggable) { return pluggable.getProviderName() !== providerName; });
77 };
78 PubSubClass.prototype.getProviderByName = function (providerName) {
79 if (providerName === INTERNAL_AWS_APPSYNC_REALTIME_PUBSUB_PROVIDER) {
80 return this.awsAppSyncRealTimeProvider;
81 }
82 return this._pluggables.find(function (pluggable) { return pluggable.getProviderName() === providerName; });
83 };
84 PubSubClass.prototype.getProviders = function (options) {
85 if (options === void 0) { options = {}; }
86 var providerName = options.provider;
87 if (!providerName) {
88 return this._pluggables;
89 }
90 var provider = this.getProviderByName(providerName);
91 if (!provider) {
92 throw new Error("Could not find provider named " + String(providerName));
93 }
94 return [provider];
95 };
96 PubSubClass.prototype.publish = function (topics, msg, options) {
97 return __awaiter(this, void 0, void 0, function () {
98 return __generator(this, function (_a) {
99 return [2 /*return*/, Promise.all(this.getProviders(options).map(function (provider) {
100 return provider.publish(topics, msg, options);
101 }))];
102 });
103 });
104 };
105 PubSubClass.prototype.subscribe = function (topics, options) {
106 if (isNode && this._options && this._options.ssr) {
107 throw new Error('Subscriptions are not supported for Server-Side Rendering (SSR)');
108 }
109 logger.debug('subscribe options', options);
110 var providers = this.getProviders(options);
111 return new Observable(function (observer) {
112 var observables = providers.map(function (provider) { return ({
113 provider: provider,
114 observable: provider.subscribe(topics, options),
115 }); });
116 var subscriptions = observables.map(function (_a) {
117 var provider = _a.provider, observable = _a.observable;
118 return observable.subscribe({
119 start: console.error,
120 next: function (value) { return observer.next({ provider: provider, value: value }); },
121 error: function (error) { return observer.error({ provider: provider, error: error }); },
122 });
123 });
124 return function () {
125 return subscriptions.forEach(function (subscription) { return subscription.unsubscribe(); });
126 };
127 });
128 };
129 return PubSubClass;
130}());
131export { PubSubClass };
132export var PubSub = new PubSubClass();
133Amplify.register(PubSub);
134//# sourceMappingURL=PubSub.js.map
\No newline at end of file