1 | import { __awaiter, __generator } from "tslib";
|
2 |
|
3 |
|
4 |
|
5 | import Observable from 'zen-observable-ts';
|
6 | import { Amplify, browserOrNode, ConsoleLogger as Logger, INTERNAL_AWS_APPSYNC_REALTIME_PUBSUB_PROVIDER, } from '@aws-amplify/core';
|
7 | import { AWSAppSyncRealTimeProvider } from './Providers';
|
8 | var isNode = browserOrNode().isNode;
|
9 | var logger = new Logger('PubSub');
|
10 | var PubSubClass = (function () {
|
11 | |
12 |
|
13 |
|
14 |
|
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 |
|
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 |
|
40 |
|
41 |
|
42 |
|
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 |
|
56 |
|
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 , config];
|
66 | }
|
67 | return [2 ];
|
68 | });
|
69 | });
|
70 | };
|
71 | |
72 |
|
73 |
|
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 , 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 | }());
|
131 | export { PubSubClass };
|
132 | export var PubSub = new PubSubClass();
|
133 | Amplify.register(PubSub);
|
134 |
|
\ | No newline at end of file |