Comparing version 1.0.0 to 1.0.1
// Derived from https://github.com/umdjs/umd/blob/master/templates/amdWebGlobal.js | ||
(function (root, factory) { | ||
var exports = {}; | ||
factory(exports); | ||
if (typeof define === 'function' && define.amd) { | ||
// AMD | ||
define('penpal', ['exports'], factory); | ||
define('PenPal', exports.default); | ||
} else { | ||
// Browser global | ||
factory((root.LoveNotes = {})); | ||
root.PenPal = exports.default; | ||
} | ||
@@ -20,21 +23,22 @@ }(this, function (exports) { | ||
/** | ||
* Whether debug messages should be logged. | ||
* @type {boolean} | ||
*/ | ||
var debug = false; | ||
var PenPal = { | ||
/** | ||
* Promise implementation. | ||
* @type {Constructor} | ||
*/ | ||
Promise: function () { | ||
try { | ||
return window ? window.Promise : null; | ||
} catch (e) { | ||
return null; | ||
} | ||
}(), | ||
/** | ||
* Whether debug messages should be logged. | ||
* @type {boolean} | ||
*/ | ||
debug: false | ||
}; | ||
/** | ||
* Promise implementation. | ||
* @type {Constructor} | ||
*/ | ||
var Promise = function () { | ||
try { | ||
return window ? window.Promise : Promise; | ||
} catch (e) { | ||
return null; | ||
} | ||
}(); | ||
/** | ||
* @return {number} A unique ID (not universally unique) | ||
@@ -54,3 +58,3 @@ */ | ||
function log() { | ||
if (debug) { | ||
if (PenPal.debug) { | ||
var _console; | ||
@@ -97,3 +101,3 @@ | ||
log(localName + ': Sending ' + methodName + '() call'); | ||
return new Promise(function (resolve) { | ||
return new PenPal.Promise(function (resolve) { | ||
var id = generateId(); | ||
@@ -154,3 +158,3 @@ var handleMessage = function handleMessage(message) { | ||
if (methodName in methods) { | ||
Promise.resolve(methods[methodName].apply(methods, _toConsumableArray(args))).then(function (returnValue) { | ||
PenPal.Promise.resolve(methods[methodName].apply(methods, _toConsumableArray(args))).then(function (returnValue) { | ||
log(localName + ': Sending ' + methodName + '() reply'); | ||
@@ -187,3 +191,3 @@ | ||
*/ | ||
var connectToChild = exports.connectToChild = function connectToChild(_ref) { | ||
PenPal.connectToChild = function (_ref) { | ||
var url = _ref.url, | ||
@@ -200,3 +204,3 @@ appendTo = _ref.appendTo, | ||
return new Promise(function (resolve) { | ||
return new PenPal.Promise(function (resolve) { | ||
var handleMessage = function handleMessage(message) { | ||
@@ -257,3 +261,3 @@ if (message.origin === childOrigin && message.data && message.data.penpal === 'handshake-reply') { | ||
*/ | ||
var connectToParent = exports.connectToParent = function connectToParent(_ref2) { | ||
PenPal.connectToParent = function (_ref2) { | ||
var parentOrigin = _ref2.parentOrigin, | ||
@@ -266,3 +270,3 @@ _ref2$methods = _ref2.methods, | ||
return new Promise(function (resolve) { | ||
return new PenPal.Promise(function (resolve) { | ||
var handleMessage = function handleMessage(message) { | ||
@@ -298,19 +302,4 @@ if ((!parentOrigin || message.origin === parentOrigin) && message.data && message.data.penpal === 'handshake') { | ||
/** | ||
* Enabled or disables debugging. Debugging is disabled by default. | ||
* @param {boolean} value Whether debugging should be enabled. | ||
*/ | ||
var setDebug = exports.setDebug = function setDebug(value) { | ||
debug = value; | ||
}; | ||
exports.default = PenPal; | ||
/** | ||
* Sets the promise implementation that should be used. If not called, the native implementation | ||
* will be used. | ||
* @param {Constructor} value The Promise implementation that should be used. | ||
*/ | ||
var setPromise = exports.setPromise = function setPromise(value) { | ||
Promise = value; | ||
}; | ||
})); |
@@ -1,1 +0,1 @@ | ||
!function(e,n){"function"==typeof define&&define.amd?define("penpal",["exports"],n):n(e.LoveNotes={})}(this,function(e){"use strict";function n(e){if(Array.isArray(e)){for(var n=0,t=Array(e.length);n<e.length;n++)t[n]=e[n];return t}return Array.from(e)}function t(){if(i){var e;(e=console).log.apply(e,arguments)}}function a(e){var n=document.createElement("a");return n.href=e,n.origin||n.protocol+"//"+n.hostname}function r(e,n){var a=e.localName,r=e.local,o=e.remote,i=e.remoteOrigin;t(a+": Creating call sender");var s=function(e){return function(){for(var n=arguments.length,s=Array(n),l=0;l<n;l++)s[l]=arguments[l];return t(a+": Sending "+e+"() call"),new d(function(n){var d=c(),l=function o(c){c.origin===i&&c.data&&"reply"===c.data.penpal&&c.data.id===d&&(t(a+": Received "+e+"() reply"),r.removeEventListener("message",o),n(c.data.returnValue))};r.addEventListener("message",l),o.postMessage({penpal:"call",id:d,methodName:e,args:s},i)})}};return n.reduce(function(e,n){return e[n]=s(n),e},{})}function o(e,a){var r=e.localName,o=e.local,i=e.remote,c=e.remoteOrigin;t(r+": Connecting call receiver");var s=function(e){e.origin===c&&e.data&&"call"===e.data.penpal&&!function(){var o=e.data,s=o.methodName,l=o.args,u=o.id;t(r+": Received "+s+"() call"),s in a&&d.resolve(a[s].apply(a,n(l))).then(function(e){t(r+": Sending "+s+"() reply"),i.postMessage({penpal:"reply",id:u,returnValue:e},c)})}()};return o.addEventListener("message",s),t(r+": Awaiting calls..."),function(){o.removeEventListener("message",s)}}Object.defineProperty(e,"__esModule",{value:!0});var i=!1,d=function(){try{return window?window.Promise:d}catch(e){return null}}(),c=function(){var e=0;return function(){return++e}}();e.connectToChild=function(e){var n=e.url,i=e.appendTo,c=e.methods,s=void 0===c?{}:c,l=window,u=document.createElement("iframe");(i||document.body).appendChild(u);var m=u.contentWindow||u.contentDocument.parentWindow,f=a(n);return new d(function(e){var a=function n(a){a.origin===f&&a.data&&"handshake-reply"===a.data.penpal&&!function(){t("Parent: Received handshake reply from Child"),l.removeEventListener("message",n);var i={localName:"Parent",local:l,remote:m,remoteOrigin:a.origin},d=o(i,s),c=r(i,a.data.methodNames);c.frame=u,c.destroy=function(){d(),u.parentNode.removeChild(u)},e(c)}()};l.addEventListener("message",a),u.addEventListener("load",function(){t("Parent: Sending handshake"),setTimeout(function(){m.postMessage({penpal:"handshake",methodNames:Object.keys(s)},f)})}),t("Parent: Loading frame"),u.src=n})},e.connectToParent=function(e){var n=e.parentOrigin,a=e.methods,i=void 0===a?{}:a,c=window,s=c.parent;return new d(function(e){var a=function a(d){if((!n||d.origin===n)&&d.data&&"handshake"===d.data.penpal){t("Child: Received handshake from Parent"),c.removeEventListener("message",a),t("Child: Sending handshake reply to Parent"),d.source.postMessage({penpal:"handshake-reply",methodNames:Object.keys(i)},d.origin);var l={localName:"Child",local:c,remote:s,remoteOrigin:d.origin};o(l,i),e(r(l,d.data.methodNames))}};c.addEventListener("message",a)})},e.setDebug=function(e){i=e},e.setPromise=function(e){d=e}}); | ||
!function(e,n){var a={};n(a),"function"==typeof define&&define.amd?define("PenPal",a.default):e.PenPal=a.default}(this,function(e){"use strict";function n(e){if(Array.isArray(e)){for(var n=0,a=Array(e.length);n<e.length;n++)a[n]=e[n];return a}return Array.from(e)}function a(){if(i.debug){var e;(e=console).log.apply(e,arguments)}}function r(e){var n=document.createElement("a");return n.href=e,n.origin||n.protocol+"//"+n.hostname}function t(e,n){var r=e.localName,t=e.local,o=e.remote,l=e.remoteOrigin;a(r+": Creating call sender");var s=function(e){return function(){for(var n=arguments.length,s=Array(n),c=0;c<n;c++)s[c]=arguments[c];return a(r+": Sending "+e+"() call"),new i.Promise(function(n){var i=d(),c=function o(d){d.origin===l&&d.data&&"reply"===d.data.penpal&&d.data.id===i&&(a(r+": Received "+e+"() reply"),t.removeEventListener("message",o),n(d.data.returnValue))};t.addEventListener("message",c),o.postMessage({penpal:"call",id:i,methodName:e,args:s},l)})}};return n.reduce(function(e,n){return e[n]=s(n),e},{})}function o(e,r){var t=e.localName,o=e.local,d=e.remote,l=e.remoteOrigin;a(t+": Connecting call receiver");var s=function(e){e.origin===l&&e.data&&"call"===e.data.penpal&&!function(){var o=e.data,s=o.methodName,c=o.args,u=o.id;a(t+": Received "+s+"() call"),s in r&&i.Promise.resolve(r[s].apply(r,n(c))).then(function(e){a(t+": Sending "+s+"() reply"),d.postMessage({penpal:"reply",id:u,returnValue:e},l)})}()};return o.addEventListener("message",s),a(t+": Awaiting calls..."),function(){o.removeEventListener("message",s)}}Object.defineProperty(e,"__esModule",{value:!0});var i={Promise:function(){try{return window?window.Promise:null}catch(e){return null}}(),debug:!1},d=function(){var e=0;return function(){return++e}}();i.connectToChild=function(e){var n=e.url,d=e.appendTo,l=e.methods,s=void 0===l?{}:l,c=window,u=document.createElement("iframe");(d||document.body).appendChild(u);var m=u.contentWindow||u.contentDocument.parentWindow,f=r(n);return new i.Promise(function(e){var r=function n(r){r.origin===f&&r.data&&"handshake-reply"===r.data.penpal&&!function(){a("Parent: Received handshake reply from Child"),c.removeEventListener("message",n);var i={localName:"Parent",local:c,remote:m,remoteOrigin:r.origin},d=o(i,s),l=t(i,r.data.methodNames);l.frame=u,l.destroy=function(){d(),u.parentNode.removeChild(u)},e(l)}()};c.addEventListener("message",r),u.addEventListener("load",function(){a("Parent: Sending handshake"),setTimeout(function(){m.postMessage({penpal:"handshake",methodNames:Object.keys(s)},f)})}),a("Parent: Loading frame"),u.src=n})},i.connectToParent=function(e){var n=e.parentOrigin,r=e.methods,d=void 0===r?{}:r,l=window,s=l.parent;return new i.Promise(function(e){var r=function r(i){if((!n||i.origin===n)&&i.data&&"handshake"===i.data.penpal){a("Child: Received handshake from Parent"),l.removeEventListener("message",r),a("Child: Sending handshake reply to Parent"),i.source.postMessage({penpal:"handshake-reply",methodNames:Object.keys(d)},i.origin);var c={localName:"Child",local:l,remote:s,remoteOrigin:i.origin};o(c,d),e(t(c,i.data.methodNames))}};l.addEventListener("message",r)})},e.default=i}); |
@@ -9,21 +9,22 @@ 'use strict'; | ||
/** | ||
* Whether debug messages should be logged. | ||
* @type {boolean} | ||
*/ | ||
var debug = false; | ||
var PenPal = { | ||
/** | ||
* Promise implementation. | ||
* @type {Constructor} | ||
*/ | ||
Promise: function () { | ||
try { | ||
return window ? window.Promise : null; | ||
} catch (e) { | ||
return null; | ||
} | ||
}(), | ||
/** | ||
* Whether debug messages should be logged. | ||
* @type {boolean} | ||
*/ | ||
debug: false | ||
}; | ||
/** | ||
* Promise implementation. | ||
* @type {Constructor} | ||
*/ | ||
var Promise = function () { | ||
try { | ||
return window ? window.Promise : Promise; | ||
} catch (e) { | ||
return null; | ||
} | ||
}(); | ||
/** | ||
* @return {number} A unique ID (not universally unique) | ||
@@ -43,3 +44,3 @@ */ | ||
function log() { | ||
if (debug) { | ||
if (PenPal.debug) { | ||
var _console; | ||
@@ -86,3 +87,3 @@ | ||
log(localName + ': Sending ' + methodName + '() call'); | ||
return new Promise(function (resolve) { | ||
return new PenPal.Promise(function (resolve) { | ||
var id = generateId(); | ||
@@ -143,3 +144,3 @@ var handleMessage = function handleMessage(message) { | ||
if (methodName in methods) { | ||
Promise.resolve(methods[methodName].apply(methods, _toConsumableArray(args))).then(function (returnValue) { | ||
PenPal.Promise.resolve(methods[methodName].apply(methods, _toConsumableArray(args))).then(function (returnValue) { | ||
log(localName + ': Sending ' + methodName + '() reply'); | ||
@@ -176,3 +177,3 @@ | ||
*/ | ||
var connectToChild = exports.connectToChild = function connectToChild(_ref) { | ||
PenPal.connectToChild = function (_ref) { | ||
var url = _ref.url, | ||
@@ -189,3 +190,3 @@ appendTo = _ref.appendTo, | ||
return new Promise(function (resolve) { | ||
return new PenPal.Promise(function (resolve) { | ||
var handleMessage = function handleMessage(message) { | ||
@@ -246,3 +247,3 @@ if (message.origin === childOrigin && message.data && message.data.penpal === 'handshake-reply') { | ||
*/ | ||
var connectToParent = exports.connectToParent = function connectToParent(_ref2) { | ||
PenPal.connectToParent = function (_ref2) { | ||
var parentOrigin = _ref2.parentOrigin, | ||
@@ -255,3 +256,3 @@ _ref2$methods = _ref2.methods, | ||
return new Promise(function (resolve) { | ||
return new PenPal.Promise(function (resolve) { | ||
var handleMessage = function handleMessage(message) { | ||
@@ -287,17 +288,2 @@ if ((!parentOrigin || message.origin === parentOrigin) && message.data && message.data.penpal === 'handshake') { | ||
/** | ||
* Enabled or disables debugging. Debugging is disabled by default. | ||
* @param {boolean} value Whether debugging should be enabled. | ||
*/ | ||
var setDebug = exports.setDebug = function setDebug(value) { | ||
debug = value; | ||
}; | ||
/** | ||
* Sets the promise implementation that should be used. If not called, the native implementation | ||
* will be used. | ||
* @param {Constructor} value The Promise implementation that should be used. | ||
*/ | ||
var setPromise = exports.setPromise = function setPromise(value) { | ||
Promise = value; | ||
}; | ||
exports.default = PenPal; |
{ | ||
"name": "penpal", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "A promise-based library for communicating with iframes via postMessage.", | ||
@@ -5,0 +5,0 @@ "author": "Aaron Hardy <aaron@aaronhardy.com>", |
@@ -16,5 +16,5 @@ # PenPal | ||
```javascript | ||
const { connectToChild } from 'penpal'; | ||
const PenPal from 'penpal'; | ||
connectToChild({ | ||
PenPal.connectToChild({ | ||
// URL of page to load into iframe. | ||
@@ -39,5 +39,5 @@ url: 'http://example.com/iframe.html', | ||
```javascript | ||
const { connectToParent } from 'penpal'; | ||
const PenPal from 'penpal'; | ||
connectToParent({ | ||
PenPal.connectToParent({ | ||
// Methods child is exposing to parent | ||
@@ -94,9 +94,9 @@ methods: { | ||
### `setPromise(value:Constructor)` | ||
### `Promise` | ||
Provides a promise implementation that PenPal will use. If a promise implementation is not provided through this function, PenPal will attempt to use `window.Promise`. | ||
Setting `PenPal.Promise` to a Promise constructor provides PenPal with a promise implementation that it will use. If a promise implementation is not provided by the consumer, PenPal will attempt to use `window.Promise`. | ||
### `setDebug(value:boolean)` | ||
### `debug` | ||
Enables or disables debug logging. Debug logging is disabled by default. | ||
Setting `PenPal.debug` to `true` or `false` enables or disables debug logging. Debug logging is disabled by default. | ||
@@ -103,0 +103,0 @@ ## Inspiration |
27263
472