1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var tslib_1 = require("tslib");
|
4 |
|
5 |
|
6 | var core_1 = require("@aws-amplify/core");
|
7 | var Providers_1 = require("../Providers");
|
8 | var zen_observable_ts_1 = tslib_1.__importDefault(require("zen-observable-ts"));
|
9 | var isNode = core_1.browserOrNode().isNode;
|
10 | var logger = new core_1.ConsoleLogger('PubSub');
|
11 | var InternalPubSubClass = (function () {
|
12 | |
13 |
|
14 |
|
15 |
|
16 |
|
17 | function InternalPubSubClass(options) {
|
18 | this._options = options !== null && options !== void 0 ? options : {};
|
19 | logger.debug('PubSub Options', this._options);
|
20 | this._pluggables = [];
|
21 | this.subscribe = this.subscribe.bind(this);
|
22 | }
|
23 | Object.defineProperty(InternalPubSubClass.prototype, "awsAppSyncRealTimeProvider", {
|
24 | |
25 |
|
26 |
|
27 | get: function () {
|
28 | if (!this._awsAppSyncRealTimeProvider) {
|
29 | this._awsAppSyncRealTimeProvider = new Providers_1.AWSAppSyncRealTimeProvider(this._options);
|
30 | }
|
31 | return this._awsAppSyncRealTimeProvider;
|
32 | },
|
33 | enumerable: true,
|
34 | configurable: true
|
35 | });
|
36 | InternalPubSubClass.prototype.getModuleName = function () {
|
37 | return 'InternalPubSub';
|
38 | };
|
39 | |
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 | InternalPubSubClass.prototype.configure = function (options) {
|
46 | var _this = this;
|
47 | var opt = options
|
48 | ? options.PubSub || options
|
49 | : {};
|
50 | logger.debug('configure PubSub', { opt: opt });
|
51 | this._options = Object.assign({}, this._options, opt);
|
52 | this._pluggables.map(function (pluggable) { return pluggable.configure(_this._options); });
|
53 | return this._options;
|
54 | };
|
55 | |
56 |
|
57 |
|
58 |
|
59 | InternalPubSubClass.prototype.addPluggable = function (pluggable) {
|
60 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
61 | var config;
|
62 | return tslib_1.__generator(this, function (_a) {
|
63 | if (pluggable && pluggable.getCategory() === 'PubSub') {
|
64 | this._pluggables.push(pluggable);
|
65 | config = pluggable.configure(this._options);
|
66 | return [2 , config];
|
67 | }
|
68 | return [2 ];
|
69 | });
|
70 | });
|
71 | };
|
72 | |
73 |
|
74 |
|
75 |
|
76 | InternalPubSubClass.prototype.removePluggable = function (providerName) {
|
77 | this._pluggables = this._pluggables.filter(function (pluggable) { return pluggable.getProviderName() !== providerName; });
|
78 | };
|
79 | InternalPubSubClass.prototype.getProviderByName = function (providerName) {
|
80 | if (providerName === core_1.INTERNAL_AWS_APPSYNC_REALTIME_PUBSUB_PROVIDER) {
|
81 | return this.awsAppSyncRealTimeProvider;
|
82 | }
|
83 | return this._pluggables.find(function (pluggable) { return pluggable.getProviderName() === providerName; });
|
84 | };
|
85 | InternalPubSubClass.prototype.getProviders = function (options) {
|
86 | if (options === void 0) { options = {}; }
|
87 | var providerName = options.provider;
|
88 | if (!providerName) {
|
89 | return this._pluggables;
|
90 | }
|
91 | var provider = this.getProviderByName(providerName);
|
92 | if (!provider) {
|
93 | throw new Error("Could not find provider named " + String(providerName));
|
94 | }
|
95 | return [provider];
|
96 | };
|
97 | InternalPubSubClass.prototype.publish = function (topics, msg, options) {
|
98 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
99 | return tslib_1.__generator(this, function (_a) {
|
100 | return [2 , Promise.all(this.getProviders(options).map(function (provider) {
|
101 | return provider.publish(topics, msg, options);
|
102 | }))];
|
103 | });
|
104 | });
|
105 | };
|
106 | InternalPubSubClass.prototype.subscribe = function (topics, options, customUserAgentDetails) {
|
107 | if (isNode && this._options && this._options.ssr) {
|
108 | throw new Error('Subscriptions are not supported for Server-Side Rendering (SSR)');
|
109 | }
|
110 | logger.debug('subscribe options', options);
|
111 | var providers = this.getProviders(options);
|
112 | var pubSubUserAgentDetails = tslib_1.__assign({ category: core_1.Category.PubSub, action: core_1.PubSubAction.Subscribe }, customUserAgentDetails);
|
113 | return new zen_observable_ts_1.default(function (observer) {
|
114 | var observables = providers.map(function (provider) { return ({
|
115 | provider: provider,
|
116 | observable: provider.subscribe(topics, options, pubSubUserAgentDetails),
|
117 | }); });
|
118 | var subscriptions = observables.map(function (_a) {
|
119 | var provider = _a.provider, observable = _a.observable;
|
120 | return observable.subscribe({
|
121 | start: console.error,
|
122 | next: function (value) { return observer.next({ provider: provider, value: value }); },
|
123 | error: function (error) { return observer.error({ provider: provider, error: error }); },
|
124 | });
|
125 | });
|
126 | return function () {
|
127 | return subscriptions.forEach(function (subscription) { return subscription.unsubscribe(); });
|
128 | };
|
129 | });
|
130 | };
|
131 | return InternalPubSubClass;
|
132 | }());
|
133 | exports.InternalPubSubClass = InternalPubSubClass;
|
134 | exports.InternalPubSub = new InternalPubSubClass();
|
135 | core_1.Amplify.register(exports.InternalPubSub);
|
136 |
|
\ | No newline at end of file |