UNPKG

9.25 kBJavaScriptView Raw
1var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2 function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3 return new (P || (P = Promise))(function (resolve, reject) {
4 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6 function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7 step((generator = generator.apply(thisArg, _arguments || [])).next());
8 });
9};
10var __generator = (this && this.__generator) || function (thisArg, body) {
11 var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
12 return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
13 function verb(n) { return function (v) { return step([n, v]); }; }
14 function step(op) {
15 if (f) throw new TypeError("Generator is already executing.");
16 while (_) try {
17 if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
18 if (y = 0, t) op = [op[0] & 2, t.value];
19 switch (op[0]) {
20 case 0: case 1: t = op; break;
21 case 4: _.label++; return { value: op[1], done: false };
22 case 5: _.label++; y = op[1]; op = [0]; continue;
23 case 7: op = _.ops.pop(); _.trys.pop(); continue;
24 default:
25 if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
26 if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
27 if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
28 if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
29 if (t[2]) _.ops.pop();
30 _.trys.pop(); continue;
31 }
32 op = body.call(thisArg, _);
33 } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
34 if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
35 }
36};
37/*
38 * Copyright 2017-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
39 *
40 * Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with
41 * the License. A copy of the License is located at
42 *
43 * http://aws.amazon.com/apache2.0/
44 *
45 * or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
46 * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
47 * and limitations under the License.
48 */
49// import '../Common/Polyfills';
50import Observable from 'zen-observable-ts';
51import { Amplify, browserOrNode, ConsoleLogger as Logger, INTERNAL_AWS_APPSYNC_PUBSUB_PROVIDER, INTERNAL_AWS_APPSYNC_REALTIME_PUBSUB_PROVIDER, } from '@aws-amplify/core';
52import { AWSAppSyncProvider, AWSAppSyncRealTimeProvider } from './Providers';
53var isNode = browserOrNode().isNode;
54var logger = new Logger('PubSub');
55var PubSubClass = /** @class */ (function () {
56 /**
57 * Initialize PubSub with AWS configurations
58 *
59 * @param {PubSubOptions} options - Configuration object for PubSub
60 */
61 function PubSubClass(options) {
62 this._options = options !== null && options !== void 0 ? options : {};
63 logger.debug('PubSub Options', this._options);
64 this._pluggables = [];
65 this.subscribe = this.subscribe.bind(this);
66 }
67 Object.defineProperty(PubSubClass.prototype, "awsAppSyncProvider", {
68 /**
69 * Lazy instantiate AWSAppSyncProvider when it is required by the API category
70 */
71 get: function () {
72 if (!this._awsAppSyncProvider) {
73 this._awsAppSyncProvider = new AWSAppSyncProvider(this._options);
74 }
75 return this._awsAppSyncProvider;
76 },
77 enumerable: true,
78 configurable: true
79 });
80 Object.defineProperty(PubSubClass.prototype, "awsAppSyncRealTimeProvider", {
81 /**
82 * Lazy instantiate AWSAppSyncRealTimeProvider when it is required by the API category
83 */
84 get: function () {
85 if (!this._awsAppSyncRealTimeProvider) {
86 this._awsAppSyncRealTimeProvider = new AWSAppSyncRealTimeProvider(this._options);
87 }
88 return this._awsAppSyncRealTimeProvider;
89 },
90 enumerable: true,
91 configurable: true
92 });
93 PubSubClass.prototype.getModuleName = function () {
94 return 'PubSub';
95 };
96 /**
97 * Configure PubSub part with configurations
98 *
99 * @param {PubSubOptions} config - Configuration for PubSub
100 * @return {Object} - The current configuration
101 */
102 PubSubClass.prototype.configure = function (options) {
103 var _this = this;
104 var opt = options ? options.PubSub || options : {};
105 logger.debug('configure PubSub', { opt: opt });
106 this._options = Object.assign({}, this._options, opt);
107 this._pluggables.map(function (pluggable) { return pluggable.configure(_this._options); });
108 return this._options;
109 };
110 /**
111 * add plugin into Analytics category
112 * @param {Object} pluggable - an instance of the plugin
113 */
114 PubSubClass.prototype.addPluggable = function (pluggable) {
115 return __awaiter(this, void 0, void 0, function () {
116 var config;
117 return __generator(this, function (_a) {
118 if (pluggable && pluggable.getCategory() === 'PubSub') {
119 this._pluggables.push(pluggable);
120 config = pluggable.configure(this._options);
121 return [2 /*return*/, config];
122 }
123 return [2 /*return*/];
124 });
125 });
126 };
127 /**
128 * remove plugin from PubSub category
129 * @param providerName - the name of the plugin
130 */
131 PubSubClass.prototype.removePluggable = function (providerName) {
132 this._pluggables = this._pluggables.filter(function (pluggable) { return pluggable.getProviderName() !== providerName; });
133 };
134 PubSubClass.prototype.getProviderByName = function (providerName) {
135 if (providerName === INTERNAL_AWS_APPSYNC_PUBSUB_PROVIDER) {
136 return this.awsAppSyncProvider;
137 }
138 if (providerName === INTERNAL_AWS_APPSYNC_REALTIME_PUBSUB_PROVIDER) {
139 return this.awsAppSyncRealTimeProvider;
140 }
141 return this._pluggables.find(function (pluggable) { return pluggable.getProviderName() === providerName; });
142 };
143 PubSubClass.prototype.getProviders = function (options) {
144 if (options === void 0) { options = {}; }
145 var providerName = options.provider;
146 if (!providerName) {
147 return this._pluggables;
148 }
149 var provider = this.getProviderByName(providerName);
150 if (!provider) {
151 throw new Error("Could not find provider named " + providerName);
152 }
153 return [provider];
154 };
155 PubSubClass.prototype.publish = function (topics, msg, options) {
156 return __awaiter(this, void 0, void 0, function () {
157 return __generator(this, function (_a) {
158 return [2 /*return*/, Promise.all(this.getProviders(options).map(function (provider) {
159 return provider.publish(topics, msg, options);
160 }))];
161 });
162 });
163 };
164 PubSubClass.prototype.subscribe = function (topics, options) {
165 if (isNode && this._options && this._options.ssr) {
166 throw new Error('Subscriptions are not supported for Server-Side Rendering (SSR)');
167 }
168 logger.debug('subscribe options', options);
169 var providers = this.getProviders(options);
170 return new Observable(function (observer) {
171 var observables = providers.map(function (provider) { return ({
172 provider: provider,
173 observable: provider.subscribe(topics, options),
174 }); });
175 var subscriptions = observables.map(function (_a) {
176 var provider = _a.provider, observable = _a.observable;
177 return observable.subscribe({
178 start: console.error,
179 next: function (value) { return observer.next({ provider: provider, value: value }); },
180 error: function (error) { return observer.error({ provider: provider, error: error }); },
181 });
182 });
183 return function () {
184 return subscriptions.forEach(function (subscription) { return subscription.unsubscribe(); });
185 };
186 });
187 };
188 return PubSubClass;
189}());
190export { PubSubClass };
191export var PubSub = new PubSubClass();
192Amplify.register(PubSub);
193//# sourceMappingURL=PubSub.js.map
\No newline at end of file