1 | "use strict";
|
2 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3 | function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
4 | return new (P || (P = Promise))(function (resolve, reject) {
|
5 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
6 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
7 | function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
8 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
9 | });
|
10 | };
|
11 | var __generator = (this && this.__generator) || function (thisArg, body) {
|
12 | var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
13 | return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
14 | function verb(n) { return function (v) { return step([n, v]); }; }
|
15 | function step(op) {
|
16 | if (f) throw new TypeError("Generator is already executing.");
|
17 | while (_) try {
|
18 | 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;
|
19 | if (y = 0, t) op = [op[0] & 2, t.value];
|
20 | switch (op[0]) {
|
21 | case 0: case 1: t = op; break;
|
22 | case 4: _.label++; return { value: op[1], done: false };
|
23 | case 5: _.label++; y = op[1]; op = [0]; continue;
|
24 | case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
25 | default:
|
26 | if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
27 | if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
28 | if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
29 | if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
30 | if (t[2]) _.ops.pop();
|
31 | _.trys.pop(); continue;
|
32 | }
|
33 | op = body.call(thisArg, _);
|
34 | } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
35 | if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
36 | }
|
37 | };
|
38 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
39 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
40 | };
|
41 | Object.defineProperty(exports, "__esModule", { value: true });
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 | var zen_observable_ts_1 = __importDefault(require("zen-observable-ts"));
|
56 | var core_1 = require("@aws-amplify/core");
|
57 | var Providers_1 = require("./Providers");
|
58 | var isNode = core_1.browserOrNode().isNode;
|
59 | var logger = new core_1.ConsoleLogger('PubSub');
|
60 | var PubSubClass = (function () {
|
61 | |
62 |
|
63 |
|
64 |
|
65 |
|
66 | function PubSubClass(options) {
|
67 | this._options = options !== null && options !== void 0 ? options : {};
|
68 | logger.debug('PubSub Options', this._options);
|
69 | this._pluggables = [];
|
70 | this.subscribe = this.subscribe.bind(this);
|
71 | }
|
72 | Object.defineProperty(PubSubClass.prototype, "awsAppSyncProvider", {
|
73 | |
74 |
|
75 |
|
76 | get: function () {
|
77 | if (!this._awsAppSyncProvider) {
|
78 | this._awsAppSyncProvider = new Providers_1.AWSAppSyncProvider(this._options);
|
79 | }
|
80 | return this._awsAppSyncProvider;
|
81 | },
|
82 | enumerable: true,
|
83 | configurable: true
|
84 | });
|
85 | Object.defineProperty(PubSubClass.prototype, "awsAppSyncRealTimeProvider", {
|
86 | |
87 |
|
88 |
|
89 | get: function () {
|
90 | if (!this._awsAppSyncRealTimeProvider) {
|
91 | this._awsAppSyncRealTimeProvider = new Providers_1.AWSAppSyncRealTimeProvider(this._options);
|
92 | }
|
93 | return this._awsAppSyncRealTimeProvider;
|
94 | },
|
95 | enumerable: true,
|
96 | configurable: true
|
97 | });
|
98 | PubSubClass.prototype.getModuleName = function () {
|
99 | return 'PubSub';
|
100 | };
|
101 | |
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 | PubSubClass.prototype.configure = function (options) {
|
108 | var _this = this;
|
109 | var opt = options ? options.PubSub || options : {};
|
110 | logger.debug('configure PubSub', { opt: opt });
|
111 | this._options = Object.assign({}, this._options, opt);
|
112 | this._pluggables.map(function (pluggable) { return pluggable.configure(_this._options); });
|
113 | return this._options;
|
114 | };
|
115 | |
116 |
|
117 |
|
118 |
|
119 | PubSubClass.prototype.addPluggable = function (pluggable) {
|
120 | return __awaiter(this, void 0, void 0, function () {
|
121 | var config;
|
122 | return __generator(this, function (_a) {
|
123 | if (pluggable && pluggable.getCategory() === 'PubSub') {
|
124 | this._pluggables.push(pluggable);
|
125 | config = pluggable.configure(this._options);
|
126 | return [2 , config];
|
127 | }
|
128 | return [2 ];
|
129 | });
|
130 | });
|
131 | };
|
132 | |
133 |
|
134 |
|
135 |
|
136 | PubSubClass.prototype.removePluggable = function (providerName) {
|
137 | this._pluggables = this._pluggables.filter(function (pluggable) { return pluggable.getProviderName() !== providerName; });
|
138 | };
|
139 | PubSubClass.prototype.getProviderByName = function (providerName) {
|
140 | if (providerName === core_1.INTERNAL_AWS_APPSYNC_PUBSUB_PROVIDER) {
|
141 | return this.awsAppSyncProvider;
|
142 | }
|
143 | if (providerName === core_1.INTERNAL_AWS_APPSYNC_REALTIME_PUBSUB_PROVIDER) {
|
144 | return this.awsAppSyncRealTimeProvider;
|
145 | }
|
146 | return this._pluggables.find(function (pluggable) { return pluggable.getProviderName() === providerName; });
|
147 | };
|
148 | PubSubClass.prototype.getProviders = function (options) {
|
149 | if (options === void 0) { options = {}; }
|
150 | var providerName = options.provider;
|
151 | if (!providerName) {
|
152 | return this._pluggables;
|
153 | }
|
154 | var provider = this.getProviderByName(providerName);
|
155 | if (!provider) {
|
156 | throw new Error("Could not find provider named " + providerName);
|
157 | }
|
158 | return [provider];
|
159 | };
|
160 | PubSubClass.prototype.publish = function (topics, msg, options) {
|
161 | return __awaiter(this, void 0, void 0, function () {
|
162 | return __generator(this, function (_a) {
|
163 | return [2 , Promise.all(this.getProviders(options).map(function (provider) {
|
164 | return provider.publish(topics, msg, options);
|
165 | }))];
|
166 | });
|
167 | });
|
168 | };
|
169 | PubSubClass.prototype.subscribe = function (topics, options) {
|
170 | if (isNode && this._options && this._options.ssr) {
|
171 | throw new Error('Subscriptions are not supported for Server-Side Rendering (SSR)');
|
172 | }
|
173 | logger.debug('subscribe options', options);
|
174 | var providers = this.getProviders(options);
|
175 | return new zen_observable_ts_1.default(function (observer) {
|
176 | var observables = providers.map(function (provider) { return ({
|
177 | provider: provider,
|
178 | observable: provider.subscribe(topics, options),
|
179 | }); });
|
180 | var subscriptions = observables.map(function (_a) {
|
181 | var provider = _a.provider, observable = _a.observable;
|
182 | return observable.subscribe({
|
183 | start: console.error,
|
184 | next: function (value) { return observer.next({ provider: provider, value: value }); },
|
185 | error: function (error) { return observer.error({ provider: provider, error: error }); },
|
186 | });
|
187 | });
|
188 | return function () {
|
189 | return subscriptions.forEach(function (subscription) { return subscription.unsubscribe(); });
|
190 | };
|
191 | });
|
192 | };
|
193 | return PubSubClass;
|
194 | }());
|
195 | exports.PubSubClass = PubSubClass;
|
196 | exports.PubSub = new PubSubClass();
|
197 | core_1.Amplify.register(exports.PubSub);
|
198 |
|
\ | No newline at end of file |