chronosjs
Version:
JS Channels Mechanism
1 lines • 22.1 kB
JavaScript
!function(a,b,c){"use strict";if("function"==typeof define&&define.amd)define("Chronos.PostMessageUtilities",[],function(){return c(a,b,!0)});else if("object"!=typeof exports){b.Chronos=b.Chronos||{};c(a,b.Chronos)}}(this,"undefined"==typeof ChronosRoot?this:ChronosRoot,function(a,b,c){"use strict";function d(){var a,b;if("function"==typeof Array.prototype.toJSON){b=Array.prototype.toJSON;Array.prototype.toJSON=void 0;try{a=JSON.stringify.apply(null,arguments)}catch(a){Array.prototype.toJSON=b;throw a}Array.prototype.toJSON=b}else a=JSON.stringify.apply(null,arguments);return a}function e(){var b=!0;try{a.postMessage({toString:function(){b=!1}},"*")}catch(a){a&&"DataCloneError"!==a.name&&(b=!1)}return b}function f(a){return a&&a.replace(/[xy]/g,function(a){var b=16*Math.random()|0;return("x"===a?b:3&b|8).toString(16)})}function g(a,b){return!isNaN(a)&&0<a?parseInt(a,10):b}function h(a,b){return"function"==typeof a?a:!0===b?function(){}:b}function i(a,b,c){var d,e,f,g=new RegExp(/(http{1}s{0,1}?:\/\/)([^\/\?]+)(\/?)/gi);if(!a||0!==a.indexOf("http")){f=c?b.top||b.contentWindow&&b.contentWindow.parent||window:b;return f.location.protocol+"//"+f.location.host}d=g.exec(a);d&&3<=d.length&&""!==d[2]&&(e=d[1].toLowerCase()+d[2].toLowerCase());return e}function j(a,b){var c,d=l("lpHost",b);if(!d){c=l("hostParam",b)||a;c&&(d=l(c,b))}return d}function k(a,b,c){var d,e,f;try{e=a&&a.contentWindow&&"undefined"!=typeof Window&&!(a instanceof Window)&&a.getAttribute&&a.getAttribute("src")}catch(a){}try{e||(e=j(c));if(!e){e=document.referrer;f=!0}if(e){e=decodeURIComponent(e);f&&(e=j(c,e))}d=i(e,a,b)}catch(a){q("Cannot parse origin","ERROR","PostMessageUtilities")}return d||"*"}function l(a,b){return decodeURIComponent((new RegExp("[?|&]"+a+"=([^&;]+?)(&|#|;|$)").exec(b||document.location.search)||[void 0,""])[1].replace(/\+/g,"%20"))||null}function m(a,b){var c;"undefined"!=typeof setImmediate&&(isNaN(b)||0>=b)?c=setImmediate(a):!1===b?a():c=setTimeout(a,isNaN(b)||0>=b?0:parseInt(b,10));return function(){n(c)}}function n(a){var b=g(a);b&&("undefined"!=typeof clearImmediate?clearImmediate(b):clearTimeout(b))}function o(a,b,c){a.addEventListener?a.addEventListener(b,c,!1):a.attachEvent("on"+b,c);return function(){p(a,b,c)}}function p(a,b,c){a.removeEventListener?a.removeEventListener(b,c,!1):a.detachEvent("on"+b,c)}function q(b,c,d){a&&"function"==typeof a.log&&a.log(b,c,d)}function r(a){function b(){}function c(){return e.apply(this instanceof b&&a?this:a,d.concat(Array.prototype.slice.call(arguments)))}var d,e;if("function"!=typeof this)throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable");d=Array.prototype.slice.call(arguments,1);e=this;b.prototype=this.prototype;c.prototype=new b;return c}Function.prototype.bind||(Function.prototype.bind=r);var s={SEQUENCE_FORMAT:"_xxxxxx-4xxx-yxxx",stringify:d,hasPostMessageObjectsSupport:e,createUniqueSequence:f,parseNumber:g,parseFunction:h,getHost:i,resolveOrigin:k,getURLParameter:l,delay:m,addEventListener:o,removeEventListener:p,log:q,bind:r};c||(b.PostMessageUtilities=b.PostMessageUtilities||s);return s});!function(a,b,c){"use strict";if("function"==typeof define&&define.amd)define("Chronos.PostMessagePromise",["exports"],function(){return c(a,b,!0)});else if("object"!=typeof exports){b.Chronos=b.Chronos||{};c(a,b.Chronos)}}(this,"undefined"==typeof ChronosRoot?this:ChronosRoot,function(a,b,c){"use strict";function d(a){if(!1==this instanceof d)return new d(a);this.initialize(a)}var e={RESOLVE:"resolve",REJECT:"reject",PROGRESS:"progress"};d.prototype=function(){function a(a){if(!this.initialized){this.queue=[];this.actions={resolve:c.bind(this),reject:d.bind(this),progress:f.bind(this)};"function"==typeof a&&a.call(this,this.actions.resolve,this.actions.reject);this.initialized=!0}}function b(a,b,c){this.queue.push({resolve:a,reject:b,progress:c})}function c(a){h.call(this,e.RESOLVE,a)}function d(a){h.call(this,e.REJECT,a)}function f(a){g.call(this,e.PROGRESS,a)}function g(a,b,c){var d,e;if(this.queue&&this.queue.length){d=0;e=this.queue[d++];for(;e;){e[a]&&e[a].call(this,b);e=this.queue[d++]}c&&(this.queue.length=0)}}function h(a,b){var c=this.actions[a];this.then=function(a,d){c&&c.call(this,b)}.bind(this);this.resolve=this.reject=function(){throw new Error("This Promise instance had already been completed.")};this.progress=function(){return!1};g.call(this,a,b,!0);if(this.queue){this.queue.length=0;delete this.queue}}return{initialize:a,then:b,resolve:c,reject:d,progress:f}}();d.polyfill=function(){a.Promise||(a.Promise=d)};c||(b.PostMessagePromise=b.PostMessagePromise||d);return d});!function(a,b){"use strict";if("function"==typeof define&&define.amd)define("Chronos.PostMessageMapper",["Chronos.PostMessageUtilities"],function(c){return b(a,a,c,!0)});else if("object"!=typeof exports){a.Chronos=a.Chronos||{};b(a,a.Chronos,a.Chronos.PostMessageUtilities)}}("undefined"==typeof ChronosRoot?this:ChronosRoot,function(a,b,c,d){"use strict";function e(a){if(!1==this instanceof e)return new e(a);this.initialize(a)}e.prototype=function(){function a(a){if(!this.initialized){this.eventChannel=a;this.initialized=!0}}function b(a){if(a){if(a.error){c.log("Error on message: "+a.error,"ERROR","PostMessageMapper");return function(){return a}}return e.call(this,a)}}function d(a,b){return{method:{id:a,name:b,args:Array.prototype.slice.call(arguments,2)}}}function e(a){var b=a&&a.method,d=b&&b.name,e=b&&b.args,f=this.eventChannel;return function(){if(f&&f[d])return f[d].apply(f,e);c.log("No channel exists","ERROR","PostMessageMapper")}}return{initialize:a,toEvent:b,toMessage:d}}();d||(b.PostMessageMapper=b.PostMessageMapper||e);return e});!function(a,b,c){"use strict";if("function"==typeof define&&define.amd)define("Chronos.PostMessageChannelPolyfill",["Chronos.PostMessageUtilities"],function(d){return c(a,b,d,!0)});else if("object"!=typeof exports){b.Chronos=b.Chronos||{};c(a,b.Chronos,b.Chronos.PostMessageUtilities)}}(this,"undefined"==typeof ChronosRoot?this:ChronosRoot,function(a,b,c,d){"use strict";function e(a,b){if(!1==this instanceof e)return new e(a,b);this.initialize(a,b)}var f="LPPort_";e.prototype=function(){function b(b,d){if(!this.initialized){d=d||{};this.target=b||a.top;this.hosted=this.target===a||this.target===a.top;this.portId=c.createUniqueSequence(f+c.SEQUENCE_FORMAT);this.serialize=c.parseFunction(d.serialize,c.stringify);this.deserialize=c.parseFunction(d.deserialize,JSON.parse);this.initialized=!0}}function d(a){var b,d,e=g.call(this),f=this.target;if(a)try{this.hosted||(f=this.target.contentWindow);b=h.call(this,a);d=this.serialize(b);f.postMessage(d,e)}catch(a){c.log("Error while trying to post the message","ERROR","PostMessageChannelPolyfill");return!1}}function e(a){var b;if("function"==typeof this.onmessage){b=i.call(this,a);return this.onmessage(b)}}function g(){this.origin||(this.origin=c.resolveOrigin(this.target));return this.origin}function h(a){return{port:this.portId,message:a}}function i(a){var b;if(a&&a.data)try{b=this.deserialize(a.data);if(b.port&&0===b.port.indexOf(f))return{origin:a.origin,data:b.message}}catch(a){c.log("Error while trying to deserialize the message","ERROR","PostMessageChannelPolyfill")}return b||a}return{initialize:b,postMessage:d,receive:e}}();d||(b.PostMessageChannelPolyfill=b.PostMessageChannelPolyfill||e);return e});!function(a,b,c){"use strict";if("function"==typeof define&&define.amd)define("Chronos.PostMessageChannel",["Chronos.PostMessageUtilities","Chronos.PostMessageChannelPolyfill"],function(d,e){return c(a,b,d,e,!0)});else if("object"!=typeof exports){b.Chronos=b.Chronos||{};c(a,b.Chronos,b.Chronos.PostMessageUtilities,b.Chronos.PostMessageChannelPolyfill)}}(this,"undefined"==typeof ChronosRoot?this:ChronosRoot,function(a,b,c,d,e){"use strict";function f(a,b){if(!1==this instanceof f)return new f(a,b);this.initialize(a,b)}var g="LPTKN",h="HNDSK";f.prototype=function(){function b(b,e){var f,g;if(!this.initialized){this.hosted=!1;this.messageQueue=[];b=b||{};g=m.call(this,b,e);if(!x.call(this)){this.receiver=new d(this.target,{serialize:this.serialize,deserialize:this.deserialize});this.receiver.onmessage=g}if(this.hosted||!x.call(this)){f=o(g).bind(this);this.removeListener=c.addEventListener(a,"message",f)}else x.call(this)&&this.channelFactory();!this.target||this.loading||this.ready||l.call(this,g,f);this.initialized=!0}}function e(a,b){var d=c.parseFunction(this[a]);if(d){b||d.call(this);this[a]=void 0;delete this[a]}}function f(a){e.call(this,"rmtimer",a)}function i(a){e.call(this,"rmload",a)}function j(){if(!this.disposed){if(this.removeListener){this.removeListener.call(this);this.removeListener=void 0}if(this.targetUrl&&this.target||this.removeDispose)try{this.targetContainer?this.targetContainer.removeChild(this.target):document.body.removeChild(this.target)}catch(a){c.log("Error while trying to remove the iframe from the container","ERROR","PostMessageChannel")}f.call(this);i.call(this);this.messageQueue.length=0;this.messageQueue=void 0;this.channel=void 0;this.onready=void 0;this.disposed=!0}}function k(a,b,d){var e,f;if(!this.disposed)try{if(a){if(this.ready||d){e=b||this.receiver;f=p.call(this,a);e.postMessage(f);return!0}if(this.maxConcurrency>=this.messageQueue.length){this.messageQueue.push(a);return!0}return!1}}catch(a){c.log("Error while trying to post the message","ERROR","PostMessageChannel");return!1}}function l(b,e){var f;try{f=y.call(this)}catch(a){f=!1}if(!f){this.channel=!1;this.receiver=new d(this.target,{serialize:this.serialize,deserialize:this.deserialize});this.receiver.onmessage=b;if(!this.hosted){e=o(b).bind(this);this.removeListener=c.addEventListener(a,"message",e)}y.call(this)}this.handshakeAttempts--;c.delay(function(){if(!this.disposed&&!this.hosted&&!this.ready){this.rmload=C.call(this,this.target);this.rmtimer=c.delay(y.bind(this,this.handshakeInterval),this.handshakeInterval)}}.bind(this))}function m(b,d){var e;n.call(this,b);e=w(d).bind(this);this.channelFactory=r.call(this,e);if(!b.target||(b.target!==a||b.target===a.top)&&"undefined"!=typeof Window&&b.target instanceof Window){this.hosted=!0;this.target=b.target||a.top}else if(b.target.contentWindow)this.target=b.target;else if(b.target.url){this.targetUrl=b.target.url;this.targetOrigin=this.targetOrigin||c.getHost(b.target.url)}this.hosted||(this.token=c.createUniqueSequence(g+c.SEQUENCE_FORMAT));if(this.targetUrl){this.loading=!0;this.targetContainer=b.target.container||document.body;this.target=B.call(this,b.target,this.targetContainer)}return e}function n(a){this.serialize=c.parseFunction(a.serialize,c.stringify);this.deserialize=c.parseFunction(a.deserialize,JSON.parse);this.targetOrigin=a.targetOrigin;this.maxConcurrency=c.parseNumber(a.maxConcurrency,100);this.handshakeInterval=c.parseNumber(a.handshakeInterval,5e3);this.handshakeAttemptsOrig=c.parseNumber(a.handshakeAttempts,3);this.handshakeAttempts=this.handshakeAttemptsOrig;this.hostParam=a.hostParam;this.channel=void 0!==a.channel?a.channel:q();this.useObjects=a.useObjects;this.onready=v(a.onready,a.target).bind(this);this.removeDispose=a.removeDispose}function o(a){return function(b){var d,e;if(b.ports&&0<b.ports.length){this.receiver=b.ports[0];u.call(this,b)&&(this.token||(this.token=b.data));this.receiver.start();e=this.removeListener.bind(this);this.removeListener=c.addEventListener(this.receiver,"message",a);e();this.disposed||!this.hosted||this.ready||(d=!0)}else if(u.call(this,b)){this.token||(this.token=b.data);this.disposed||!this.hosted||this.ready||(d=!0)}else this.token&&this.receiver.receive.call(this.receiver,b);if(d){this.receiver.postMessage(h+this.token);z.call(this)}}}function p(a){s.call(this,a);return this.serialize(a)}function q(){if("true"===c.getURLParameter("lpPMCPolyfill"))return!1}function r(a){return function(){this.channel=new MessageChannel;this.receiver=this.channel.port1;this.dispatcher=this.channel.port2;this.receiver.onmessage=a;this.neutered=!1}.bind(this)}function s(a){this.token&&(a.token=this.token)}function t(a){return a&&a.token===this.token}function u(a){return a&&a.data&&"string"==typeof a.data&&(0===a.data.indexOf(g)||h+this.token===a.data)}function v(a,b){return function(c){b&&"function"==typeof b.callback&&b.callback.call(b.context,c,this.target);a&&("function"==typeof a?a(c,this.target):"function"==typeof a.callback&&a.callback.call(a.context,c,this.target))}}function w(a){return function(b){var d;if(!b.origin||"*"===b.origin||this.targetOrigin===b.origin){if(!u.call(this,b)||this.disposed||this.hosted||this.ready){try{d=this.deserialize(b.data);if(t.call(this,d))return a&&a(d)}catch(a){d=b.data||b;c.log("Error while trying to handle the message","ERROR","PostMessageChannel")}return d||b}z.call(this)}}}function x(){return!1!==this.channel&&"undefined"!=typeof MessageChannel&&"undefined"!=typeof MessagePort}function y(a){f.call(this,!0);if(!this.disposed&&!this.ready){x.call(this)||(this.targetOrigin=this.targetOrigin||c.resolveOrigin(this.target)||"*");if(!this.hosted)if(x.call(this))try{this.neutered&&this.channelFactory();this.target.contentWindow.postMessage(this.token,this.targetOrigin,[this.dispatcher]);this.neutered=!0}catch(a){return!1}else this.target.contentWindow.postMessage(this.token,this.targetOrigin)}if(!this.disposed&&!this.ready&&a)if(0<this.handshakeAttempts){this.handshakeAttempts--;this.rmtimer=c.delay(y.bind(this,a),a)}else this.onready(new Error("Loading: Operation Timeout!"));return!0}function z(){if(!this.disposed&&!this.ready){this.ready=!0;this.handshakeAttempts=this.handshakeAttemptsOrig;this.messageQueue&&this.messageQueue.length?c.delay(function(){var a,b;if(!this.disposed&&this.ready){for(;this.messageQueue&&this.messageQueue.length;){a=this.messageQueue.shift();try{b=p.call(this,a);this.receiver.postMessage(b)}catch(a){c.log("Error while trying to post the message from queue","ERROR","PostMessageChannel")}}this.onready()}}.bind(this)):this.onready()}}function A(b){function d(){f.body?e():c.delay(d,g||100)}b=b||{};var e=b.onready,f=b.doc||a.document,g=b.delay;c.delay(d,g||!1)}function B(a,b){var d=document.createElement("IFRAME"),e=c.createUniqueSequence("LPFRM"+c.SEQUENCE_FORMAT),f=a.delayLoad,g={id:e,name:e,tabindex:"-1","aria-hidden":"true",title:"",role:"presentation",allowTransparency:"true"},h={width:"0px",height:"0px",position:"absolute",top:"-1000px",left:"-1000px"};a.attributes=a.attributes||g;for(var i in a.attributes)a.attributes.hasOwnProperty(i)&&d.setAttribute(i,a.attributes[i]);a.style=a.style||h;if(a.style)for(var j in a.style)a.style.hasOwnProperty(j)&&(d.style[j]=a.style[j]);A({delay:f,onready:function(){(b||document.body).appendChild(d);this.rmload=C.call(this,d);E.call(this,d,a.url,!1!==a.bust)}.bind(this)});return d}function C(a){var b=function(){this.loading=!1;this.handshakeAttempts===this.handshakeAttemptsOrig&&(this.ready=!1);y.call(this,this.handshakeInterval)}.bind(this);c.addEventListener(a,"load",b);return function(){D(a,b)}}function D(a,b){c.removeEventListener(a,"load",b)}function E(a,b,d){b+=0<b.indexOf("?")?"&":"?";if(d){b+="bust=";b+=(new Date).getTime()+"&"}b+=(this.hostParam?"hostParam="+this.hostParam+"&"+this.hostParam+"=":"lpHost=")+encodeURIComponent(c.getHost(void 0,a,!0));x.call(this)||(b+="&lpPMCPolyfill=true");!1===this.useObjects&&(b+="&lpPMDeSerialize=true");a.setAttribute("src",b)}return{initialize:b,postMessage:k,dispose:j}}();e||(b.PostMessageChannel=f);return f});!function(a,b,c,d){"use strict";if("function"==typeof define&&define.amd)define("Chronos.PostMessageCourier",["Chronos.PostMessageUtilities","Chronos.Channels","cacher","CircuitBreaker","Chronos.PostMessageChannel","Chronos.PostMessagePromise","Chronos.PostMessageMapper"],function(b,c,e,f,g,h,i){return d(a,a,b,c,e,f,g,h,i,!0)});else if("object"!=typeof exports){a.Chronos=a.Chronos||{};d(a,a.Chronos,a.Chronos.PostMessageUtilities,a.Chronos.Channels,b.Cacher,c.CircuitBreaker,a.Chronos.PostMessageChannel,a.Chronos.PostMessagePromise,a.Chronos.PostMessageMapper)}}("undefined"==typeof ChronosRoot?this:ChronosRoot,"undefined"==typeof CacherRoot?this:CacherRoot,"undefined"==typeof CircuitRoot?this:CircuitRoot,function(a,b,c,d,e,f,g,h,i,j){"use strict";function k(a){if(!1==this instanceof k)return new k(a);this.initialize(a)}var l={TRIGGER:"trigger",COMMAND:"command",REQUEST:"request",RETURN:"return"};k.prototype=function(){function a(a){if(!this.initialized){a=a||{};q.call(this,a);r.call(this,a);s.call(this,a);t.call(this,a);b.call(this,this.eventChannel);this.once=this.eventChannel.once;this.hasFiredEvents=this.eventChannel.hasFiredEvents;this.bind=this.eventChannel.bind;this.register=this.eventChannel.register;this.unbind=this.eventChannel.unbind;this.unregister=this.eventChannel.unregister;this.hasFiredCommands=this.eventChannel.hasFiredCommands;this.comply=this.eventChannel.comply;this.stopComplying=this.eventChannel.stopComplying;this.hasFiredReqres=this.eventChannel.hasFiredReqres;this.reply=this.eventChannel.reply;this.stopReplying=this.eventChannel.stopReplying;this.initialized=!0}}function b(a){a&&"function"==typeof a.registerProxy&&a.registerProxy({trigger:function(){w.call(this,Array.prototype.slice.apply(arguments),l.TRIGGER)},context:this})}function j(){return this.messageChannel}function k(){return this.eventChannel}function m(){if(!this.disposed){var a=Array.prototype.slice.apply(arguments);(2!==arguments.length&&4!==arguments.length||!0!==arguments[arguments.length-1])&&this.eventChannel.trigger.apply(this.eventChannel,a);return w.call(this,a,l.TRIGGER)}}function n(){if(!this.disposed){var a=Array.prototype.slice.apply(arguments);return w.call(this,a,l.COMMAND)}}function o(){if(!this.disposed){var a=Array.prototype.slice.apply(arguments);return w.call(this,a,l.REQUEST)}}function p(){if(!this.disposed){this.messageChannel.dispose();this.messageChannel=void 0;this.eventChannel=void 0;this.mapper=void 0;this.callbackCache=void 0;this.circuit=void 0;this.disposed=!0}}function q(a){this.useObjects=!1===a.useObjects?a.useObjects:u();void 0===this.useObjects&&(this.useObjects=!0);a.useObjects=this.useObjects;if("function"!=typeof a.serialize||"function"!=typeof a.deserialize){if(this.useObjects&&c.hasPostMessageObjectsSupport()){this.serialize=v;this.deserialize=v}else{this.serialize=c.stringify;this.deserialize=JSON.parse}a.serialize=this.serialize;a.deserialize=this.deserialize}else{this.serialize=a.serialize;this.deserialize=a.deserialize}}function r(a){var b,c;this.eventChannel=a.eventChannel||new d({events:a.events,commands:a.commands,reqres:a.reqres});this.mapper=new i(this.eventChannel);b=this.mapper.toEvent.bind(this.mapper);c=E(b).bind(this);this.messageChannel=new g(a,c)}function s(a){this.callbackCache=new e({max:c.parseNumber(a.maxConcurrency,100),ttl:c.parseNumber(a.timeout,3e4),interval:1e3})}function t(a){var b=c.parseNumber(a.messureTime,3e4);this.circuit=new f({timeWindow:b,slidesNumber:Math.ceil(b/100),tolerance:c.parseNumber(a.messureTolerance,30),calibration:c.parseNumber(a.messureCalibration,10),onopen:c.parseFunction(a.ondisconnect,!0),onclose:c.parseFunction(a.onreconnect,!0)})}function u(){if("true"===c.getURLParameter("lpPMDeSerialize"))return!1}function v(a){return a}function w(a,b){return this.circuit.run(function(c,d,e){var f=y.call(this,a,b,e);if(f)try{!1===this.messageChannel.postMessage.call(this.messageChannel,f)?d():c()}catch(a){d()}else d()}.bind(this))}function x(a,b){return this.circuit.run(function(c,d){try{!1===this.messageChannel.postMessage.call(this.messageChannel,a,b)?d():c()}catch(a){d()}}.bind(this))}function y(a,b,d){var e,f,g=c.createUniqueSequence("LPMSG_"+b+c.SEQUENCE_FORMAT);a.unshift(g,b);if(z(b)){if(1<a.length&&"function"==typeof a[a.length-1])e=a.pop();else if(2<a.length&&!isNaN(a[a.length-1])&&"function"==typeof a[a.length-2]){f=parseInt(a.pop(),10);e=a.pop()}if(e&&!this.callbackCache.set(g,e,f,function(a,b){d();A.call(this,a,b)}.bind(this)))return}return this.mapper.toMessage.apply(this.mapper,a)}function z(a){return l.REQUEST===a||l.COMMAND===a}function A(a,b){if(a&&"function"==typeof b)try{b.call(null,new Error("Callback: Operation Timeout!"))}catch(a){c.log("Error while trying to handle the timeout using the callback","ERROR","PostMessageCourier")}}function B(a,b){var d=this.callbackCache.get(a,!0),e=b&&b.args;if("function"==typeof d){e&&e.length&&e[0]&&"Error"===e[0].type&&"string"==typeof e[0].message&&(e[0]=new Error(e[0].message));try{d.apply(null,e)}catch(a){c.log("Error while trying to handle the returned message from request/command","ERROR","PostMessageCourier")}}}function C(a,b,c){return function(d,e){var f,g,h=d;d instanceof Error&&(h={type:"Error",message:d.message});g=[a,l.RETURN,h];l.REQUEST===b&&g.push(e);f=this.mapper.toMessage.apply(this.mapper,g);x.call(this,f,c.source)}.bind(this)}function D(a,b,c,d){var e,f;if("undefined"!=typeof Promise&&c instanceof Promise||c instanceof h)c.then(function(c){f=[a,l.RETURN,null];l.REQUEST===b&&f.push(c);e=this.mapper.toMessage.apply(this.mapper,f);x.call(this,e,d.source)}.bind(this),function(b){f=[a,l.RETURN,b];e=this.mapper.toMessage.apply(this.mapper,f);x.call(this,e,d.source)}.bind(this));else if(c&&c.error){f=[a,l.RETURN,c];e=this.mapper.toMessage.apply(this.mapper,f);x.call(this,e,d.source)}else if(void 0!==c){f=[a,l.RETURN,null];l.REQUEST===b&&f.push(c);e=this.mapper.toMessage.apply(this.mapper,f);x.call(this,e,d.source)}}function E(a){return function(b){var d,e,f,g,h,i,j;if(b){h=b.method&&b.method.id;i=b.method&&b.method.name;j=b.method&&b.method.args;if(l.RETURN===i)B.call(this,h,b.method);else{try{z(i)&&j.length&&j.push(C.call(this,h,i,b));d=a(b);e=d&&d()}catch(a){c.log("Error while trying to invoke the handler on the events channel","ERROR","PostMessageCourier");if(z(i)){f=[h,l.RETURN,{error:a.toString()}];g=this.mapper.toMessage.apply(this.mapper,f);x.call(this,g,b.source)}}z(i)&&void 0!==e&&D.call(this,h,i,e,b)}}}}return{initialize:a,getMessageChannel:j,getEventChannel:k,trigger:m,publish:m,command:n,request:o,dispose:p}}();j||(b.PostMessageCourier=b.PostMessageCourier||k);return k});