forked from dollarshaveclub/postmate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
postmate.min.js
8 lines (8 loc) · 3.95 KB
/
postmate.min.js
1
2
3
4
5
6
7
8
/**
postmate - A powerful, simple, promise-based postMessage library
@version v1.4.1
@link https://github.com/dollarshaveclub/postmate
@author Jacob Kelley <[email protected]>
@license MIT
**/
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):e.Postmate=t()}(this,function(){"use strict";var e="application/x-postmate-v1+json",t=Object.prototype.hasOwnProperty,n=0,i=function(t,n){return t.origin===n&&("object"==typeof t.data&&("postmate"in t.data&&(t.data.type===e&&!!{"handshake-reply":1,call:1,emit:1,reply:1,request:1}[t.data.postmate])))},a=function(e,t){var n="function"==typeof e[t]?e[t]():e[t];return s.Promise.resolve(n)},r=function(){function t(e){var t=this;this.parent=e.parent,this.frame=e.frame,this.child=e.child,this.childOrigin=e.childOrigin,this.events={},this.listener=function(e){var n=((e||{}).data||{}).value||{},i=n.data,a=n.name;"emit"===e.data.postmate&&a in t.events&&t.events[a].call(t,i)},this.parent.addEventListener("message",this.listener,!1)}var i=t.prototype;return i.get=function(t){var i=this;return new s.Promise(function(a){var r=++n;i.parent.addEventListener("message",function e(t){t.data.uid===r&&"reply"===t.data.postmate&&(i.parent.removeEventListener("message",e,!1),a(t.data.value))},!1),i.child.postMessage({postmate:"request",type:e,property:t,uid:r},i.childOrigin)})},i.call=function(t,n){this.child.postMessage({postmate:"call",type:e,property:t,data:n},this.childOrigin)},i.on=function(e,t){this.events[e]=t},i.destroy=function(){window.removeEventListener("message",this.listener,!1),this.frame.parentNode.removeChild(this.frame)},t}(),o=function(){function t(t){var n=this;this.model=t.model,this.parent=t.parent,this.parentOrigin=t.parentOrigin,this.child=t.child,this.child.addEventListener("message",function(t){if(i(t,n.parentOrigin)){var r=t.data,o=r.property,s=r.uid,d=r.data;"call"!==t.data.postmate?a(n.model,o).then(function(n){return t.source.postMessage({property:o,postmate:"reply",type:e,uid:s,value:n},t.origin)}):o in n.model&&"function"==typeof n.model[o]&&n.model[o].call(n,d)}})}return t.prototype.emit=function(t,n){this.parent.postMessage({postmate:"emit",type:e,value:{name:t,data:n}},this.parentOrigin)},t}(),s=function(){function t(e){var t=void 0===e?userOptions:e,n=t.container,i=void 0===n?void 0!==i?i:document.body:n,a=t.model,r=t.url;return this.parent=window,this.frame=document.createElement("iframe"),i.appendChild(this.frame),this.child=this.frame.contentWindow||this.frame.contentDocument.parentWindow,this.model=a||{},this.sendHandshake(r)}return t.prototype.sendHandshake=function(n){var a,o=this,s=function(e){var t=document.createElement("a");return t.href=e,t.origin||t.protocol+"//"+t.hostname}(n),d=0;return new t.Promise(function(t,p){o.parent.addEventListener("message",function e(n){return!!i(n,s)&&("handshake-reply"===n.data.postmate?(clearInterval(a),o.parent.removeEventListener("message",e,!1),o.childOrigin=n.origin,t(new r(o))):p("Failed handshake"))},!1);var c=function(){d++,o.child.postMessage({postmate:"handshake",type:e,model:o.model},s),5===d&&clearInterval(a)},h=function(){c(),a=setInterval(c,500)};o.frame.attachEvent?o.frame.attachEvent("onload",h):o.frame.onload=h,o.frame.src=n})},t}();return s.debug=!1,s.Promise=function(){try{return window?window.Promise:Promise}catch(e){return null}}(),s.Model=function(){function n(e){return this.child=window,this.model=e,this.parent=this.child.parent,this.sendHandshakeReply()}return n.prototype.sendHandshakeReply=function(){var n=this;return new s.Promise(function(i,a){n.child.addEventListener("message",function r(s){if(s.data.postmate){if("handshake"===s.data.postmate){n.child.removeEventListener("message",r,!1),s.source.postMessage({postmate:"handshake-reply",type:e},s.origin),n.parentOrigin=s.origin;var d=s.data.model;if(d)for(var p=Object.keys(d),c=0;c<p.length;c++)t.call(d,p[c])&&(n.model[p[c]]=d[p[c]]);return i(new o(n))}return a("Handshake Reply Failed")}},!1)})},n}(),s});