UNPKG

@aws-amplify/core

Version:
179 lines • 6.59 kB
"use strict"; /* * Copyright 2017-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with * the License. A copy of the License is located at * * http://aws.amazon.com/apache2.0/ * * or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions * and limitations under the License. */ var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __spreadArrays = (this && this.__spreadArrays) || function () { for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length; for (var r = Array(s), k = 0, i = 0; i < il; i++) for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) r[k] = a[j]; return r; }; Object.defineProperty(exports, "__esModule", { value: true }); var Logger_1 = require("./Logger"); var logger = new Logger_1.ConsoleLogger('Hub'); var AMPLIFY_SYMBOL = (typeof Symbol !== 'undefined' && typeof Symbol.for === 'function' ? Symbol.for('amplify_default') : '@@amplify_default'); function isLegacyCallback(callback) { return callback.onHubCapsule !== undefined; } var HubClass = /** @class */ (function () { function HubClass(name) { this.listeners = []; this.patterns = []; this.protectedChannels = [ 'core', 'auth', 'api', 'analytics', 'interactions', 'pubsub', 'storage', 'xr', ]; this.name = name; } // Note - Need to pass channel as a reference for removal to work and not anonymous function HubClass.prototype.remove = function (channel, listener) { if (channel instanceof RegExp) { var pattern_1 = this.patterns.find(function (_a) { var pattern = _a.pattern; return pattern.source === channel.source; }); if (!pattern_1) { logger.warn("No listeners for " + channel); return; } this.patterns = __spreadArrays(this.patterns.filter(function (x) { return x !== pattern_1; })); } else { var holder = this.listeners[channel]; if (!holder) { logger.warn("No listeners for " + channel); return; } this.listeners[channel] = __spreadArrays(holder.filter(function (_a) { var callback = _a.callback; return callback !== listener; })); } }; HubClass.prototype.dispatch = function (channel, payload, source, ampSymbol) { if (source === void 0) { source = ''; } if (this.protectedChannels.indexOf(channel) > -1) { var hasAccess = ampSymbol === AMPLIFY_SYMBOL; if (!hasAccess) { logger.warn("WARNING: " + channel + " is protected and dispatching on it can have unintended consequences"); } } var capsule = { channel: channel, payload: __assign({}, payload), source: source, patternInfo: [], }; try { this._toListeners(capsule); } catch (e) { logger.error(e); } }; HubClass.prototype.listen = function (channel, callback, listenerName) { if (listenerName === void 0) { listenerName = 'noname'; } var cb; // Check for legacy onHubCapsule callback for backwards compatability if (isLegacyCallback(callback)) { logger.warn("WARNING onHubCapsule is Deprecated. Please pass in a callback."); cb = callback.onHubCapsule.bind(callback); } else if (typeof callback !== 'function') { throw new Error('No callback supplied to Hub'); } else { cb = callback; } if (channel instanceof RegExp) { this.patterns.push({ pattern: channel, callback: cb, }); } else { var holder = this.listeners[channel]; if (!holder) { holder = []; this.listeners[channel] = holder; } holder.push({ name: listenerName, callback: cb, }); } }; HubClass.prototype._toListeners = function (capsule) { var channel = capsule.channel, payload = capsule.payload; var holder = this.listeners[channel]; if (holder) { holder.forEach(function (listener) { logger.debug("Dispatching to " + channel + " with ", payload); try { listener.callback(capsule); } catch (e) { logger.error(e); } }); } if (this.patterns.length > 0) { if (!payload.message) { logger.warn("Cannot perform pattern matching without a message key"); return; } var payloadStr_1 = payload.message; this.patterns.forEach(function (pattern) { var match = payloadStr_1.match(pattern.pattern); if (match) { var groups = match.slice(1); var dispatchingCapsule = __assign(__assign({}, capsule), { patternInfo: groups }); try { pattern.callback(dispatchingCapsule); } catch (e) { logger.error(e); } } }); } }; return HubClass; }()); exports.HubClass = HubClass; /*We export a __default__ instance of HubClass to use it as a psuedo Singleton for the main messaging bus, however you can still create your own instance of HubClass() for a separate "private bus" of events.*/ var Hub = new HubClass('__default__'); exports.default = Hub; //# sourceMappingURL=Hub.js.map