sdc-pubsub
Version:
Publish Subscribe library using post message for sdc plugins
14 lines • 4.34 kB
JavaScript
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define("sdcPubSub",[],e):"object"==typeof exports?exports.sdcPubSub=e():t.sdcPubSub=e()}(window,function(){return function(t){var e={};function n(i){if(e[i])return e[i].exports;var r=e[i]={i:i,l:!1,exports:{}};return t[i].call(r.exports,r,r.exports,n),r.l=!0,r.exports}return n.m=t,n.c=e,n.d=function(t,e,i){n.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:i})},n.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},n.t=function(t,e){if(1&e&&(t=n(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var i=Object.create(null);if(n.r(i),Object.defineProperty(i,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var r in t)n.d(i,r,function(e){return t[e]}.bind(null,r));return i},n.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return n.d(e,"a",e),e},n.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},n.p="",n(n.s="./index.ts")}({"./index.ts":
/*!******************!*\
!*** ./index.ts ***!
\******************/
/*! no static exports found */function(t,e,n){"use strict";Object.defineProperty(e,"__esModule",{value:!0});var i=n(/*! ./src/base-pubsub */"./src/base-pubsub.ts");e.BasePubSub=i.BasePubSub;var r=n(/*! ./src/plugin-pubsub */"./src/plugin-pubsub.ts");e.PluginPubSub=r.PluginPubSub},"./src/base-pubsub.ts":
/*!****************************!*\
!*** ./src/base-pubsub.ts ***!
\****************************/
/*! no static exports found */function(t,e,n){"use strict";Object.defineProperty(e,"__esModule",{value:!0});var i=function(){function t(t){this.subscribers=new Map,this.eventsCallbacks=[],this.eventsToWait=new Map,this.clientId=t,this.lastEventNotified="",this.onMessage=this.onMessage.bind(this),window.addEventListener("message",this.onMessage)}return t.prototype.register=function(t,e,n){var i={window:e,locationUrl:n||e.location.href};this.subscribers.set(t,i)},t.prototype.unregister=function(t){this.subscribers.delete(t)},t.prototype.on=function(t){this.eventsCallbacks.find(function(e){return t.toString()===e.toString()})||this.eventsCallbacks.push(t)},t.prototype.off=function(t){var e=this.eventsCallbacks.indexOf(t);this.eventsCallbacks.splice(e,1)},t.prototype.notify=function(t,e){var n={type:t,data:e,originId:this.clientId};return this.subscribers.forEach(function(t,e){t.window.postMessage(n,t.locationUrl)}),this.lastEventNotified=t,{subscribe:function(e){var n=this;if(0!==this.subscribers.size){var i=Array.from(this.subscribers.keys()),r=function(t){var n=i.indexOf(t);i.splice(n,1),0===i.length&&e()};this.subscribers.forEach(function(e,i){if(n.eventsToWait.has(i)&&-1!==n.eventsToWait.get(i).indexOf(t)){var s=function(t,e){void 0===e&&(e=i),"ACTION_COMPLETED"===t.type&&r(e),n.off(s)};n.on(s)}else r(i)})}else e()}.bind(this)}},t.prototype.isWaitingForEvent=function(t){return Array.from(this.eventsToWait.values()).some(function(e){return-1!==e.indexOf(t)})},t.prototype.onMessage=function(t){this.subscribers.has(t.data.originId)&&this.eventsCallbacks.forEach(function(e){e(t.data,t)})},t}();e.BasePubSub=i},"./src/plugin-pubsub.ts":
/*!******************************!*\
!*** ./src/plugin-pubsub.ts ***!
\******************************/
/*! no static exports found */function(t,e,n){"use strict";var i=this&&this.__extends||function(){var t=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var n in e)e.hasOwnProperty(n)&&(t[n]=e[n])};return function(e,n){function i(){this.constructor=e}t(e,n),e.prototype=null===n?Object.create(n):(i.prototype=n.prototype,new i)}}();Object.defineProperty(e,"__esModule",{value:!0});var r=function(t){function e(e,n,i){var r=t.call(this,e)||this;return r.register("sdc-hub",window.parent,n),r.subscribe(i),r}return i(e,t),e.prototype.subscribe=function(t){var e={pluginId:this.clientId,eventsToWait:t||[]};this.notify("PLUGIN_REGISTER",e)},e.prototype.unsubscribe=function(){var t={pluginId:this.clientId};this.notify("PLUGIN_UNREGISTER",t)},e}(n(/*! ./base-pubsub */"./src/base-pubsub.ts").BasePubSub);e.PluginPubSub=r}})});
//# sourceMappingURL=sdc-pubsub.min.js.map