UNPKG

hub-postmate

Version:

A powerful, simple, promise-based postMessage library

9 lines (8 loc) 4.74 kB
/** hub-postmate - A powerful, simple, promise-based postMessage library @version v1.6.7 @link https://github.com/Antoninum/postmate @author Jacob Kelley <jakie8@gmail.com> @license MIT **/ !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).Postmate=t()}(this,function(){"use strict";var h="application/x-postmate-v1+json",r=0,s=0;function l(e,t){return("string"!=typeof t||e.origin===t)&&(!!e.data&&(("object"!=typeof e.data||"postmate"in e.data)&&(e.data.type===h&&!!n[e.data.postmate])))}var n={handshake:1,"handshake-reply":1,call:1,emit:1,reply:1,request:1},c=function(){function e(e){var i=this;this.parent=e.parent,this.frame=e.frame,this.child=e.child,this.childOrigin=e.childOrigin,this.childId=e.childId,this.events={},this.listener=function(e){if(!l(e,i.childOrigin))return!1;var t=((e||{}).data||{}).value||{},n=t.data,t=t.name;"emit"===e.data.postmate&&e.data.childId===i.childId&&t in i.events&&i.events[t].forEach(function(e){e.call(i,n)})},this.parent.addEventListener("message",this.listener,!1)}var t=e.prototype;return t.get=function(e){var a=this;return new d.Promise(function(t){function n(e){e.data.uid===i&&"reply"===e.data.postmate&&e.data.childId===a.childId&&(a.parent.removeEventListener("message",n,!1),t(e.data.value))}var i=++r;a.parent.addEventListener("message",n,!1),a.child.postMessage({postmate:"request",type:h,property:e,uid:i},a.childOrigin)})},t.call=function(e,t){this.child.postMessage({postmate:"call",type:h,property:e,data:t},this.childOrigin)},t.on=function(e,t){this.events[e]||(this.events[e]=[]),this.events[e].push(t)},t.destroy=function(){window.removeEventListener("message",this.listener,!1),this.frame.parentNode.removeChild(this.frame)},e}(),o=function(){function e(e){var a=this;this.model=e.model,this.parent=e.parent,this.parentOrigin=e.parentOrigin,this.child=e.child,this.childId=e.childId,this.child.addEventListener("message",function(t){var n,i,e;l(t,a.parentOrigin)&&(e=t.data,n=e.property,i=e.uid,e=e.data,"call"!==t.data.postmate?function(e,t){t="function"==typeof e[t]?e[t]():e[t];return d.Promise.resolve(t)}(a.model,n).then(function(e){return t.source.postMessage({property:n,postmate:"reply",type:h,childId:a.childId,uid:i,value:e},t.origin)}):n in a.model&&"function"==typeof a.model[n]&&a.model[n](e))})}return e.prototype.emit=function(e,t){this.parent.postMessage({postmate:"emit",type:h,childId:this.childId,value:{name:e,data:t}},this.parentOrigin)},e}(),d=function(){function e(e){var t=e.container,n=void 0===t?void 0!==n?n:document.body:t,i=e.model,a=e.url,t=e.name,e=e.classListArray,e=void 0===e?[]:e;return this.parent=window,this.frame=document.createElement("iframe"),this.frame.allow="geolocation",this.frame.name=t||"",0<e.length&&this.frame.classList.add.apply(this.frame.classList,e),n.appendChild(this.frame),this.child=this.frame.contentWindow||this.frame.contentDocument.parentWindow,this.model=i||{},this.childId=++s,this.sendHandshake(a)}return e.prototype.sendHandshake=function(a){var r,s=this,o=function(e){var t=document.createElement("a");t.href=e;var n=(4<t.protocol.length?t:window.location).protocol,e=t.host.length?"80"===t.port||"443"===t.port?t.hostname:t.host:window.location.host;return t.origin||n+"//"+e}(a),d=0;return new e.Promise(function(n,i){var e=function e(t){return!!l(t,o)&&(t.data.childId===s.childId&&("handshake-reply"===t.data.postmate?(clearInterval(r),s.parent.removeEventListener("message",e,!1),s.childOrigin=t.origin,n(new c(s))):i("Failed handshake")))};s.parent.addEventListener("message",e,!1);function t(){if(5<++d)return clearInterval(r),i("Handshake Timeout Reached");s.child.postMessage({postmate:"handshake",type:h,model:s.model,childId:s.childId},o)}e=function(){t(),r=setInterval(t,500)};s.frame.attachEvent?s.frame.attachEvent("onload",e):s.frame.addEventListener("load",e),s.frame.src=a})},e}();return d.debug=!1,d.Promise=function(){try{return window?window.Promise:Promise}catch(e){return null}}(),d.Model=function(){function e(e){return this.child=window,this.model=e,this.parent=this.child.parent,this.sendHandshakeReply()}return e.prototype.sendHandshakeReply=function(){var r=this;return new d.Promise(function(n,i){function a(e){if(e.data.postmate){if("handshake"!==e.data.postmate)return i("Handshake Reply Failed");r.child.removeEventListener("message",a,!1),e.source.postMessage({postmate:"handshake-reply",type:h,childId:e.data.childId},e.origin),r.childId=e.data.childId,r.parentOrigin=e.origin;var t=e.data.model;return t&&Object.keys(t).forEach(function(e){r.model[e]=t[e]}),n(new o(r))}}r.child.addEventListener("message",a,!1)})},e}(),d});