From 78135c4b5ab6f59c328d23bd575421a98c72173e Mon Sep 17 00:00:00 2001 From: John Carmichael Date: Wed, 16 Sep 2020 18:24:46 +0200 Subject: [PATCH 1/2] Remove the process.env check --- src/postmate.js | 84 +++++++++++++++++++------------------------------ 1 file changed, 32 insertions(+), 52 deletions(-) diff --git a/src/postmate.js b/src/postmate.js index 0196f0ae..3406d839 100644 --- a/src/postmate.js +++ b/src/postmate.js @@ -1,4 +1,3 @@ - /** * The type of messages our frames our sending * @type {String} @@ -99,10 +98,8 @@ export class ParentAPI { this.events = {} - if (process.env.NODE_ENV !== 'production') { - log('Parent: Registering API') - log('Parent: Awaiting messages...') - } + log('Parent: Registering API') + log('Parent: Awaiting messages...') this.listener = (e) => { if (!sanitize(e, this.childOrigin)) return false @@ -113,9 +110,8 @@ export class ParentAPI { const { data, name } = (((e || {}).data || {}).value || {}) if (e.data.postmate === 'emit') { - if (process.env.NODE_ENV !== 'production') { - log(`Parent: Received event emission: ${name}`) - } + log(`Parent: Received event emission: ${name}`) + if (name in this.events) { this.events[name].forEach(callback => { callback.call(this, data) @@ -125,9 +121,7 @@ export class ParentAPI { } this.parent.addEventListener('message', this.listener, false) - if (process.env.NODE_ENV !== 'production') { - log('Parent: Awaiting event emissions from Child') - } + log('Parent: Awaiting event emissions from Child') } get (property) { @@ -172,9 +166,7 @@ export class ParentAPI { } destroy () { - if (process.env.NODE_ENV !== 'production') { - log('Parent: Destroying Postmate instance') - } + log('Parent: Destroying Postmate instance') window.removeEventListener('message', this.listener, false) this.frame.parentNode.removeChild(this.frame) } @@ -191,17 +183,13 @@ export class ChildAPI { this.parentOrigin = info.parentOrigin this.child = info.child - if (process.env.NODE_ENV !== 'production') { - log('Child: Registering API') - log('Child: Awaiting messages...') - } + log('Child: Registering API') + log('Child: Awaiting messages...') this.child.addEventListener('message', (e) => { if (!sanitize(e, this.parentOrigin)) return - if (process.env.NODE_ENV !== 'production') { - log('Child: Received request', e.data) - } + log('Child: Received request', e.data) const { property, uid, data } = e.data @@ -225,9 +213,9 @@ export class ChildAPI { } emit (name, data) { - if (process.env.NODE_ENV !== 'production') { - log(`Child: Emitting Event "${name}"`, data) - } + + log(`Child: Emitting Event "${name}"`, data) + this.parent.postMessage({ postmate: 'emit', type: messageType, @@ -240,7 +228,7 @@ export class ChildAPI { } /** - * The entry point of the Parent. + * The entry point of the Parent. * @type {Class} */ class Postmate { @@ -292,22 +280,19 @@ class Postmate { if (!sanitize(e, childOrigin)) return false if (e.data.postmate === 'handshake-reply') { clearInterval(responseInterval) - if (process.env.NODE_ENV !== 'production') { - log('Parent: Received handshake reply from Child') - } + + log('Parent: Received handshake reply from Child') this.parent.removeEventListener('message', reply, false) this.childOrigin = e.origin - if (process.env.NODE_ENV !== 'production') { - log('Parent: Saving Child origin', this.childOrigin) - } + log('Parent: Saving Child origin', this.childOrigin) + return resolve(new ParentAPI(this)) } // Might need to remove since parent might be receiving different messages // from different hosts - if (process.env.NODE_ENV !== 'production') { - log('Parent: Invalid handshake reply') - } + log('Parent: Invalid handshake reply') + return reject('Failed handshake') } @@ -315,9 +300,8 @@ class Postmate { const doSend = () => { attempt++ - if (process.env.NODE_ENV !== 'production') { - log(`Parent: Sending handshake attempt ${attempt}`, { childOrigin }) - } + log(`Parent: Sending handshake attempt ${attempt}`, { childOrigin }) + this.child.postMessage({ postmate: 'handshake', type: messageType, @@ -340,9 +324,8 @@ class Postmate { this.frame.addEventListener('load', loaded) } - if (process.env.NODE_ENV !== 'production') { - log('Parent: Loading frame', { url }) - } + log('Parent: Loading frame', { url }) + this.frame.src = url }) } @@ -376,13 +359,12 @@ Postmate.Model = class Model { return } if (e.data.postmate === 'handshake') { - if (process.env.NODE_ENV !== 'production') { - log('Child: Received handshake from Parent') - } + log('Child: Received handshake from Parent') + this.child.removeEventListener('message', shake, false) - if (process.env.NODE_ENV !== 'production') { - log('Child: Sending handshake reply to Parent') - } + + log('Child: Sending handshake reply to Parent') + e.source.postMessage({ postmate: 'handshake-reply', type: messageType, @@ -395,14 +377,12 @@ Postmate.Model = class Model { Object.keys(defaults).forEach(key => { this.model[key] = defaults[key] }) - if (process.env.NODE_ENV !== 'production') { - log('Child: Inherited and extended model from Parent') - } - } - if (process.env.NODE_ENV !== 'production') { - log('Child: Saving Parent origin', this.parentOrigin) + log('Child: Inherited and extended model from Parent') } + + log('Child: Saving Parent origin', this.parentOrigin) + return resolve(new ChildAPI(this)) } return reject('Handshake Reply Failed') From bde83fe92b139afeca87a437c0b03455d2211db4 Mon Sep 17 00:00:00 2001 From: John Carmichael Date: Wed, 16 Sep 2020 18:27:40 +0200 Subject: [PATCH 2/2] Ran the build scripts --- README.md | 2 +- build/postmate.dev.js | 125 ++++++++++++------------------------------ build/postmate.es.js | 125 ++++++++++++------------------------------ build/postmate.js | 125 ++++++++++++------------------------------ build/postmate.min.js | 2 +- 5 files changed, 104 insertions(+), 275 deletions(-) diff --git a/README.md b/README.md index bf0d6ede..a8d5bf66 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,7 @@ You can download the compiled javascript directly [here](/build/postmate.min.js) * Child emits events that the parent can listen to. * Parent can `call` functions within a `child` * *Zero* dependencies. Provide your own polyfill or abstraction for the `Promise` API if needed. -* Lightweight, weighing in at ~ `1.6kb` (minified & gzipped). +* Lightweight, weighing in at ~ `1.9kb` (minified & gzipped). NOTE: While the underlying mechanism is [window.postMessage()](https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage), only iFrame is supported. diff --git a/build/postmate.dev.js b/build/postmate.dev.js index 936454ab..19b68e45 100644 --- a/build/postmate.dev.js +++ b/build/postmate.dev.js @@ -67,14 +67,14 @@ emit: 1, reply: 1, request: 1 - /** - * Ensures that a message is safe to interpret - * @param {Object} message The postmate message being sent - * @param {String|Boolean} allowedOrigin The whitelisted origin or false to skip origin check - * @return {Boolean} - */ - }; + /** + * Ensures that a message is safe to interpret + * @param {Object} message The postmate message being sent + * @param {String|Boolean} allowedOrigin The whitelisted origin or false to skip origin check + * @return {Boolean} + */ + var sanitize = function sanitize(message, allowedOrigin) { if (typeof allowedOrigin === 'string' && message.origin !== allowedOrigin) return false; if (!message.data) return false; @@ -101,9 +101,7 @@ * @param {Object} info Information on the consumer */ - var ParentAPI = - /*#__PURE__*/ - function () { + var ParentAPI = /*#__PURE__*/function () { function ParentAPI(info) { var _this = this; @@ -112,11 +110,8 @@ this.child = info.child; this.childOrigin = info.childOrigin; this.events = {}; - - { - log('Parent: Registering API'); - log('Parent: Awaiting messages...'); - } + log('Parent: Registering API'); + log('Parent: Awaiting messages...'); this.listener = function (e) { if (!sanitize(e, _this.childOrigin)) return false; @@ -129,9 +124,7 @@ name = _ref.name; if (e.data.postmate === 'emit') { - { - log("Parent: Received event emission: " + name); - } + log("Parent: Received event emission: " + name); if (name in _this.events) { _this.events[name].forEach(function (callback) { @@ -142,10 +135,7 @@ }; this.parent.addEventListener('message', this.listener, false); - - { - log('Parent: Awaiting event emissions from Child'); - } + log('Parent: Awaiting event emissions from Child'); } var _proto = ParentAPI.prototype; @@ -197,10 +187,7 @@ }; _proto.destroy = function destroy() { - { - log('Parent: Destroying Postmate instance'); - } - + log('Parent: Destroying Postmate instance'); window.removeEventListener('message', this.listener, false); this.frame.parentNode.removeChild(this.frame); }; @@ -212,9 +199,7 @@ * @param {Object} info Information on the consumer */ - var ChildAPI = - /*#__PURE__*/ - function () { + var ChildAPI = /*#__PURE__*/function () { function ChildAPI(info) { var _this3 = this; @@ -222,19 +207,11 @@ this.parent = info.parent; this.parentOrigin = info.parentOrigin; this.child = info.child; - - { - log('Child: Registering API'); - log('Child: Awaiting messages...'); - } - + log('Child: Registering API'); + log('Child: Awaiting messages...'); this.child.addEventListener('message', function (e) { if (!sanitize(e, _this3.parentOrigin)) return; - - { - log('Child: Received request', e.data); - } - + log('Child: Received request', e.data); var _e$data = e.data, property = _e$data.property, uid = _e$data.uid, @@ -264,10 +241,7 @@ var _proto2 = ChildAPI.prototype; _proto2.emit = function emit(name, data) { - { - log("Child: Emitting Event \"" + name + "\"", data); - } - + log("Child: Emitting Event \"" + name + "\"", data); this.parent.postMessage({ postmate: 'emit', type: messageType, @@ -281,13 +255,11 @@ return ChildAPI; }(); /** - * The entry point of the Parent. + * The entry point of the Parent. * @type {Class} */ - var Postmate = - /*#__PURE__*/ - function () { + var Postmate = /*#__PURE__*/function () { // eslint-disable-line no-undef // Internet Explorer craps itself @@ -335,28 +307,18 @@ if (e.data.postmate === 'handshake-reply') { clearInterval(responseInterval); - - { - log('Parent: Received handshake reply from Child'); - } + log('Parent: Received handshake reply from Child'); _this4.parent.removeEventListener('message', reply, false); _this4.childOrigin = e.origin; - - { - log('Parent: Saving Child origin', _this4.childOrigin); - } - + log('Parent: Saving Child origin', _this4.childOrigin); return resolve(new ParentAPI(_this4)); } // Might need to remove since parent might be receiving different messages // from different hosts - { - log('Parent: Invalid handshake reply'); - } - + log('Parent: Invalid handshake reply'); return reject('Failed handshake'); }; @@ -364,12 +326,9 @@ var doSend = function doSend() { attempt++; - - { - log("Parent: Sending handshake attempt " + attempt, { - childOrigin: childOrigin - }); - } + log("Parent: Sending handshake attempt " + attempt, { + childOrigin: childOrigin + }); _this4.child.postMessage({ postmate: 'handshake', @@ -393,12 +352,9 @@ _this4.frame.addEventListener('load', loaded); } - { - log('Parent: Loading frame', { - url: url - }); - } - + log('Parent: Loading frame', { + url: url + }); _this4.frame.src = url; }); }; @@ -421,9 +377,7 @@ } }(); - Postmate.Model = - /*#__PURE__*/ - function () { + Postmate.Model = /*#__PURE__*/function () { /** * Initializes the child, model, parent, and responds to the Parents handshake * @param {Object} model Hash of values, functions, or promises @@ -453,16 +407,11 @@ } if (e.data.postmate === 'handshake') { - { - log('Child: Received handshake from Parent'); - } + log('Child: Received handshake from Parent'); _this5.child.removeEventListener('message', shake, false); - { - log('Child: Sending handshake reply to Parent'); - } - + log('Child: Sending handshake reply to Parent'); e.source.postMessage({ postmate: 'handshake-reply', type: messageType @@ -475,16 +424,10 @@ Object.keys(defaults).forEach(function (key) { _this5.model[key] = defaults[key]; }); - - { - log('Child: Inherited and extended model from Parent'); - } - } - - { - log('Child: Saving Parent origin', _this5.parentOrigin); + log('Child: Inherited and extended model from Parent'); } + log('Child: Saving Parent origin', _this5.parentOrigin); return resolve(new ChildAPI(_this5)); } diff --git a/build/postmate.es.js b/build/postmate.es.js index e6ce5057..865db4d6 100644 --- a/build/postmate.es.js +++ b/build/postmate.es.js @@ -61,14 +61,14 @@ var messageTypes = { emit: 1, reply: 1, request: 1 - /** - * Ensures that a message is safe to interpret - * @param {Object} message The postmate message being sent - * @param {String|Boolean} allowedOrigin The whitelisted origin or false to skip origin check - * @return {Boolean} - */ - }; +/** + * Ensures that a message is safe to interpret + * @param {Object} message The postmate message being sent + * @param {String|Boolean} allowedOrigin The whitelisted origin or false to skip origin check + * @return {Boolean} + */ + var sanitize = function sanitize(message, allowedOrigin) { if (typeof allowedOrigin === 'string' && message.origin !== allowedOrigin) return false; if (!message.data) return false; @@ -95,9 +95,7 @@ var resolveValue = function resolveValue(model, property) { * @param {Object} info Information on the consumer */ -var ParentAPI = -/*#__PURE__*/ -function () { +var ParentAPI = /*#__PURE__*/function () { function ParentAPI(info) { var _this = this; @@ -106,11 +104,8 @@ function () { this.child = info.child; this.childOrigin = info.childOrigin; this.events = {}; - - if (process.env.NODE_ENV !== 'production') { - log('Parent: Registering API'); - log('Parent: Awaiting messages...'); - } + log('Parent: Registering API'); + log('Parent: Awaiting messages...'); this.listener = function (e) { if (!sanitize(e, _this.childOrigin)) return false; @@ -123,9 +118,7 @@ function () { name = _ref.name; if (e.data.postmate === 'emit') { - if (process.env.NODE_ENV !== 'production') { - log("Parent: Received event emission: " + name); - } + log("Parent: Received event emission: " + name); if (name in _this.events) { _this.events[name].forEach(function (callback) { @@ -136,10 +129,7 @@ function () { }; this.parent.addEventListener('message', this.listener, false); - - if (process.env.NODE_ENV !== 'production') { - log('Parent: Awaiting event emissions from Child'); - } + log('Parent: Awaiting event emissions from Child'); } var _proto = ParentAPI.prototype; @@ -191,10 +181,7 @@ function () { }; _proto.destroy = function destroy() { - if (process.env.NODE_ENV !== 'production') { - log('Parent: Destroying Postmate instance'); - } - + log('Parent: Destroying Postmate instance'); window.removeEventListener('message', this.listener, false); this.frame.parentNode.removeChild(this.frame); }; @@ -206,9 +193,7 @@ function () { * @param {Object} info Information on the consumer */ -var ChildAPI = -/*#__PURE__*/ -function () { +var ChildAPI = /*#__PURE__*/function () { function ChildAPI(info) { var _this3 = this; @@ -216,19 +201,11 @@ function () { this.parent = info.parent; this.parentOrigin = info.parentOrigin; this.child = info.child; - - if (process.env.NODE_ENV !== 'production') { - log('Child: Registering API'); - log('Child: Awaiting messages...'); - } - + log('Child: Registering API'); + log('Child: Awaiting messages...'); this.child.addEventListener('message', function (e) { if (!sanitize(e, _this3.parentOrigin)) return; - - if (process.env.NODE_ENV !== 'production') { - log('Child: Received request', e.data); - } - + log('Child: Received request', e.data); var _e$data = e.data, property = _e$data.property, uid = _e$data.uid, @@ -258,10 +235,7 @@ function () { var _proto2 = ChildAPI.prototype; _proto2.emit = function emit(name, data) { - if (process.env.NODE_ENV !== 'production') { - log("Child: Emitting Event \"" + name + "\"", data); - } - + log("Child: Emitting Event \"" + name + "\"", data); this.parent.postMessage({ postmate: 'emit', type: messageType, @@ -275,13 +249,11 @@ function () { return ChildAPI; }(); /** - * The entry point of the Parent. + * The entry point of the Parent. * @type {Class} */ -var Postmate = -/*#__PURE__*/ -function () { +var Postmate = /*#__PURE__*/function () { // eslint-disable-line no-undef // Internet Explorer craps itself @@ -329,28 +301,18 @@ function () { if (e.data.postmate === 'handshake-reply') { clearInterval(responseInterval); - - if (process.env.NODE_ENV !== 'production') { - log('Parent: Received handshake reply from Child'); - } + log('Parent: Received handshake reply from Child'); _this4.parent.removeEventListener('message', reply, false); _this4.childOrigin = e.origin; - - if (process.env.NODE_ENV !== 'production') { - log('Parent: Saving Child origin', _this4.childOrigin); - } - + log('Parent: Saving Child origin', _this4.childOrigin); return resolve(new ParentAPI(_this4)); } // Might need to remove since parent might be receiving different messages // from different hosts - if (process.env.NODE_ENV !== 'production') { - log('Parent: Invalid handshake reply'); - } - + log('Parent: Invalid handshake reply'); return reject('Failed handshake'); }; @@ -358,12 +320,9 @@ function () { var doSend = function doSend() { attempt++; - - if (process.env.NODE_ENV !== 'production') { - log("Parent: Sending handshake attempt " + attempt, { - childOrigin: childOrigin - }); - } + log("Parent: Sending handshake attempt " + attempt, { + childOrigin: childOrigin + }); _this4.child.postMessage({ postmate: 'handshake', @@ -387,12 +346,9 @@ function () { _this4.frame.addEventListener('load', loaded); } - if (process.env.NODE_ENV !== 'production') { - log('Parent: Loading frame', { - url: url - }); - } - + log('Parent: Loading frame', { + url: url + }); _this4.frame.src = url; }); }; @@ -415,9 +371,7 @@ Postmate.Promise = function () { } }(); -Postmate.Model = -/*#__PURE__*/ -function () { +Postmate.Model = /*#__PURE__*/function () { /** * Initializes the child, model, parent, and responds to the Parents handshake * @param {Object} model Hash of values, functions, or promises @@ -447,16 +401,11 @@ function () { } if (e.data.postmate === 'handshake') { - if (process.env.NODE_ENV !== 'production') { - log('Child: Received handshake from Parent'); - } + log('Child: Received handshake from Parent'); _this5.child.removeEventListener('message', shake, false); - if (process.env.NODE_ENV !== 'production') { - log('Child: Sending handshake reply to Parent'); - } - + log('Child: Sending handshake reply to Parent'); e.source.postMessage({ postmate: 'handshake-reply', type: messageType @@ -469,16 +418,10 @@ function () { Object.keys(defaults).forEach(function (key) { _this5.model[key] = defaults[key]; }); - - if (process.env.NODE_ENV !== 'production') { - log('Child: Inherited and extended model from Parent'); - } - } - - if (process.env.NODE_ENV !== 'production') { - log('Child: Saving Parent origin', _this5.parentOrigin); + log('Child: Inherited and extended model from Parent'); } + log('Child: Saving Parent origin', _this5.parentOrigin); return resolve(new ChildAPI(_this5)); } diff --git a/build/postmate.js b/build/postmate.js index 8f83a10d..c3194159 100644 --- a/build/postmate.js +++ b/build/postmate.js @@ -63,14 +63,14 @@ var messageTypes = { emit: 1, reply: 1, request: 1 - /** - * Ensures that a message is safe to interpret - * @param {Object} message The postmate message being sent - * @param {String|Boolean} allowedOrigin The whitelisted origin or false to skip origin check - * @return {Boolean} - */ - }; +/** + * Ensures that a message is safe to interpret + * @param {Object} message The postmate message being sent + * @param {String|Boolean} allowedOrigin The whitelisted origin or false to skip origin check + * @return {Boolean} + */ + var sanitize = function sanitize(message, allowedOrigin) { if (typeof allowedOrigin === 'string' && message.origin !== allowedOrigin) return false; if (!message.data) return false; @@ -97,9 +97,7 @@ var resolveValue = function resolveValue(model, property) { * @param {Object} info Information on the consumer */ -var ParentAPI = -/*#__PURE__*/ -function () { +var ParentAPI = /*#__PURE__*/function () { function ParentAPI(info) { var _this = this; @@ -108,11 +106,8 @@ function () { this.child = info.child; this.childOrigin = info.childOrigin; this.events = {}; - - if (process.env.NODE_ENV !== 'production') { - log('Parent: Registering API'); - log('Parent: Awaiting messages...'); - } + log('Parent: Registering API'); + log('Parent: Awaiting messages...'); this.listener = function (e) { if (!sanitize(e, _this.childOrigin)) return false; @@ -125,9 +120,7 @@ function () { name = _ref.name; if (e.data.postmate === 'emit') { - if (process.env.NODE_ENV !== 'production') { - log("Parent: Received event emission: " + name); - } + log("Parent: Received event emission: " + name); if (name in _this.events) { _this.events[name].forEach(function (callback) { @@ -138,10 +131,7 @@ function () { }; this.parent.addEventListener('message', this.listener, false); - - if (process.env.NODE_ENV !== 'production') { - log('Parent: Awaiting event emissions from Child'); - } + log('Parent: Awaiting event emissions from Child'); } var _proto = ParentAPI.prototype; @@ -193,10 +183,7 @@ function () { }; _proto.destroy = function destroy() { - if (process.env.NODE_ENV !== 'production') { - log('Parent: Destroying Postmate instance'); - } - + log('Parent: Destroying Postmate instance'); window.removeEventListener('message', this.listener, false); this.frame.parentNode.removeChild(this.frame); }; @@ -208,9 +195,7 @@ function () { * @param {Object} info Information on the consumer */ -var ChildAPI = -/*#__PURE__*/ -function () { +var ChildAPI = /*#__PURE__*/function () { function ChildAPI(info) { var _this3 = this; @@ -218,19 +203,11 @@ function () { this.parent = info.parent; this.parentOrigin = info.parentOrigin; this.child = info.child; - - if (process.env.NODE_ENV !== 'production') { - log('Child: Registering API'); - log('Child: Awaiting messages...'); - } - + log('Child: Registering API'); + log('Child: Awaiting messages...'); this.child.addEventListener('message', function (e) { if (!sanitize(e, _this3.parentOrigin)) return; - - if (process.env.NODE_ENV !== 'production') { - log('Child: Received request', e.data); - } - + log('Child: Received request', e.data); var _e$data = e.data, property = _e$data.property, uid = _e$data.uid, @@ -260,10 +237,7 @@ function () { var _proto2 = ChildAPI.prototype; _proto2.emit = function emit(name, data) { - if (process.env.NODE_ENV !== 'production') { - log("Child: Emitting Event \"" + name + "\"", data); - } - + log("Child: Emitting Event \"" + name + "\"", data); this.parent.postMessage({ postmate: 'emit', type: messageType, @@ -277,13 +251,11 @@ function () { return ChildAPI; }(); /** - * The entry point of the Parent. + * The entry point of the Parent. * @type {Class} */ -var Postmate = -/*#__PURE__*/ -function () { +var Postmate = /*#__PURE__*/function () { // eslint-disable-line no-undef // Internet Explorer craps itself @@ -331,28 +303,18 @@ function () { if (e.data.postmate === 'handshake-reply') { clearInterval(responseInterval); - - if (process.env.NODE_ENV !== 'production') { - log('Parent: Received handshake reply from Child'); - } + log('Parent: Received handshake reply from Child'); _this4.parent.removeEventListener('message', reply, false); _this4.childOrigin = e.origin; - - if (process.env.NODE_ENV !== 'production') { - log('Parent: Saving Child origin', _this4.childOrigin); - } - + log('Parent: Saving Child origin', _this4.childOrigin); return resolve(new ParentAPI(_this4)); } // Might need to remove since parent might be receiving different messages // from different hosts - if (process.env.NODE_ENV !== 'production') { - log('Parent: Invalid handshake reply'); - } - + log('Parent: Invalid handshake reply'); return reject('Failed handshake'); }; @@ -360,12 +322,9 @@ function () { var doSend = function doSend() { attempt++; - - if (process.env.NODE_ENV !== 'production') { - log("Parent: Sending handshake attempt " + attempt, { - childOrigin: childOrigin - }); - } + log("Parent: Sending handshake attempt " + attempt, { + childOrigin: childOrigin + }); _this4.child.postMessage({ postmate: 'handshake', @@ -389,12 +348,9 @@ function () { _this4.frame.addEventListener('load', loaded); } - if (process.env.NODE_ENV !== 'production') { - log('Parent: Loading frame', { - url: url - }); - } - + log('Parent: Loading frame', { + url: url + }); _this4.frame.src = url; }); }; @@ -417,9 +373,7 @@ Postmate.Promise = function () { } }(); -Postmate.Model = -/*#__PURE__*/ -function () { +Postmate.Model = /*#__PURE__*/function () { /** * Initializes the child, model, parent, and responds to the Parents handshake * @param {Object} model Hash of values, functions, or promises @@ -449,16 +403,11 @@ function () { } if (e.data.postmate === 'handshake') { - if (process.env.NODE_ENV !== 'production') { - log('Child: Received handshake from Parent'); - } + log('Child: Received handshake from Parent'); _this5.child.removeEventListener('message', shake, false); - if (process.env.NODE_ENV !== 'production') { - log('Child: Sending handshake reply to Parent'); - } - + log('Child: Sending handshake reply to Parent'); e.source.postMessage({ postmate: 'handshake-reply', type: messageType @@ -471,16 +420,10 @@ function () { Object.keys(defaults).forEach(function (key) { _this5.model[key] = defaults[key]; }); - - if (process.env.NODE_ENV !== 'production') { - log('Child: Inherited and extended model from Parent'); - } - } - - if (process.env.NODE_ENV !== 'production') { - log('Child: Saving Parent origin', _this5.parentOrigin); + log('Child: Inherited and extended model from Parent'); } + log('Child: Saving Parent origin', _this5.parentOrigin); return resolve(new ChildAPI(_this5)); } diff --git a/build/postmate.min.js b/build/postmate.min.js index fe713a4c..6ed35db2 100644 --- a/build/postmate.min.js +++ b/build/postmate.min.js @@ -5,4 +5,4 @@ @author Jacob Kelley @license MIT **/ -!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e=e||self).Postmate=t()}(this,function(){"use strict";var h="application/x-postmate-v1+json",r=0,n={handshake:1,"handshake-reply":1,call:1,emit:1,reply:1,request:1},p=function(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])))},c=function(){function e(e){var a=this;this.parent=e.parent,this.frame=e.frame,this.child=e.child,this.childOrigin=e.childOrigin,this.events={},this.listener=function(e){if(!p(e,a.childOrigin))return!1;var t=((e||{}).data||{}).value||{},n=t.data,i=t.name;"emit"===e.data.postmate&&i in a.events&&a.events[i].forEach(function(e){e.call(a,n)})},this.parent.addEventListener("message",this.listener,!1)}var t=e.prototype;return t.get=function(e){var a=this;return new l.Promise(function(n){var i=++r;a.parent.addEventListener("message",function e(t){t.data.uid===i&&"reply"===t.data.postmate&&(a.parent.removeEventListener("message",e,!1),n(t.data.value))},!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}(),s=function(){function e(e){var d=this;this.model=e.model,this.parent=e.parent,this.parentOrigin=e.parentOrigin,this.child=e.child,this.child.addEventListener("message",function(t){if(p(t,d.parentOrigin)){var e,n,i,a=t.data,r=a.property,s=a.uid,o=a.data;if("call"!==t.data.postmate)(e=d.model,n=r,i="function"==typeof e[n]?e[n]():e[n],l.Promise.resolve(i)).then(function(e){return t.source.postMessage({property:r,postmate:"reply",type:h,uid:s,value:e},t.origin)});else r in d.model&&"function"==typeof d.model[r]&&d.model[r](o)}})}return e.prototype.emit=function(e,t){this.parent.postMessage({postmate:"emit",type:h,value:{name:e,data:t}},this.parentOrigin)},e}(),l=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,r=e.name,s=e.classListArray,o=void 0===s?[]:s;return this.parent=window,this.frame=document.createElement("iframe"),this.frame.name=r||"",this.frame.classList.add.apply(this.frame.classList,o),n.appendChild(this.frame),this.child=this.frame.contentWindow||this.frame.contentDocument.parentWindow,this.model=i||{},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