diff --git a/dist/amd/talker.min.js b/dist/amd/talker.min.js index 331616c..cfaae53 100644 --- a/dist/amd/talker.min.js +++ b/dist/amd/talker.min.js @@ -2,5 +2,5 @@ define([],function(){/*! * © 2014 Second Street, MIT License * Talker.js 1.0.1 */ -var TALKER_TYPE="application/x-talkerjs-v1+json",TALKER_ERR_TIMEOUT="timeout",pinkySwearPromise=function(){function a(a){return"function"==typeof a}function b(a){return"object"==typeof a}function c(a){"undefined"!=typeof setImmediate?setImmediate(a):"undefined"!=typeof process&&process.nextTick?process.nextTick(a):setTimeout(a,0)}var d;return function e(){var f,g=[],h=[],i=function(a,b){return null==f&&null!=a&&(f=a,g=b,h.length&&c(function(){for(var a=0;h.length>a;a++)h[a]()})),f};return i.then=function(i,j){var k=e(),l=function(){function c(e){var f,g=0;try{if(e&&(b(e)||a(e))&&a(f=e.then)){if(e===k)throw new TypeError;f.call(e,function(){g++||c.apply(d,arguments)},function(a){g++||k(!1,[a])})}else k(!0,arguments)}catch(h){g++||k(!1,[h])}}try{var e=f?i:j;a(e)?c(e.apply(d,g||[])):k(f,g)}catch(h){k(!1,[h])}};return null!=f?c(l):h.push(l),k},i}}(),objectCreate=function(a){function b(){}return b.prototype=a,new b},Talker=function(a,b){this.remoteWindow=a,this.remoteOrigin=b,this.timeout=3e3,this.handshaken=!1,this.handshake=pinkySwearPromise(),this._id=0,this._queue=[],this._sent={};var c=this;return window.addEventListener("message",function(a){c._receiveMessage(a)},!1),this._sendHandshake(),this};Talker.prototype.send=function(a,b,c){var d=new Talker.OutgoingMessage(this,a,b,c),e=pinkySwearPromise();return this._sent[d.id]=e,this._queue.push(d),this._flushQueue(),setTimeout(function(){e(!1,[new Error(TALKER_ERR_TIMEOUT)])},this.timeout),e},Talker.prototype._receiveMessage=function(a){var b,c;try{b=JSON.parse(a.data)}catch(d){b={}}return this._isSafeMessage(a.source,a.origin,b.type)?(c=b.handshake||b.handshakeConfirmation,c?this._handleHandshake(b):this._handleMessage(b)):!1},Talker.prototype._isSafeMessage=function(a,b,c){var d,e,f;return d=a===this.remoteWindow,e="*"===this.remoteOrigin||b===this.remoteOrigin,f=c===TALKER_TYPE,d&&e&&f},Talker.prototype._handleHandshake=function(a){a.handshake&&this._sendHandshake(this.handshaken),this.handshaken=!0,this.handshake(!0,[this.handshaken]),this._flushQueue()},Talker.prototype._handleMessage=function(a){var b=new Talker.IncomingMessage(this,a.namespace,a.data,a.id),c=a.responseToId;return c?this._respondToMessage(c,b):this._broadcastMessage(b)},Talker.prototype._respondToMessage=function(a,b){this._sent[a]&&(this._sent[a](!0,[b]),delete this._sent[a])},Talker.prototype._broadcastMessage=function(a){this.onMessage&&this.onMessage.call(this,a)},Talker.prototype._sendHandshake=function(a){var b={type:TALKER_TYPE},c=a?"handshakeConfirmation":"handshake";b[c]=!0,this._postMessage(b)},Talker.prototype._nextId=function(){return this._id+=1},Talker.prototype._postMessage=function(a){this.remoteWindow&&this.remoteOrigin&&this.remoteWindow.postMessage(JSON.stringify(a),this.remoteOrigin)},Talker.prototype._flushQueue=function(){if(this.handshaken){var a=this._queue.shift();if(!a)return this._queue;if(this._postMessage(a),this._queue.length>0)return this._flushQueue()}return this._queue},Talker.Message=function(a,b,c){return this.talker=a,this.namespace=b,this.data=c,this.type=TALKER_TYPE,this},Talker.OutgoingMessage=function(a,b,c,d){Talker.Message.call(this,a,b,c),this.responseToId=d||null,this.id=this.talker._nextId()},Talker.OutgoingMessage.prototype=objectCreate(Talker.Message.prototype),Talker.OutgoingMessage.prototype.constructor=Talker.Message,Talker.OutgoingMessage.prototype.toJSON=function(){return{id:this.id,responseToId:this.responseToId,namespace:this.namespace,data:this.data,type:this.type}},Talker.IncomingMessage=function(a,b,c,d){Talker.Message.call(this,a,b,c),this.id=d},Talker.IncomingMessage.prototype=objectCreate(Talker.Message.prototype),Talker.IncomingMessage.prototype.constructor=Talker.Message,Talker.IncomingMessage.prototype.respond=function(a){return this.talker.send(null,a,this.id)}; +var TALKER_TYPE="application/x-talkerjs-v1+json",TALKER_ERR_TIMEOUT="timeout",pinkySwearPromise=function(){function a(a){return"function"==typeof a}function b(a){return"object"==typeof a}function c(a){"undefined"!=typeof setImmediate?setImmediate(a):"undefined"!=typeof process&&process.nextTick?process.nextTick(a):setTimeout(a,0)}var d;return function e(){var f,g=[],h=[],i=function(a,b){return null==f&&null!=a&&(f=a,g=b,h.length&&c(function(){for(var a=0;h.length>a;a++)h[a]()})),f};return i.then=function(i,j){var k=e(),l=function(){function c(e){var f,g=0;try{if(e&&(b(e)||a(e))&&a(f=e.then)){if(e===k)throw new TypeError;f.call(e,function(){g++||c.apply(d,arguments)},function(a){g++||k(!1,[a])})}else k(!0,arguments)}catch(h){g++||k(!1,[h])}}try{var e=f?i:j;a(e)?c(e.apply(d,g||[])):k(f,g)}catch(h){k(!1,[h])}};return null!=f?c(l):h.push(l),k},i}}(),objectCreate=function(a){function b(){}return b.prototype=a,new b},Talker=function(a,b){this.remoteWindow=a,this.remoteOrigin=b,this.timeout=3e3,this.rejectTimeouts=[],this.handshaken=!1,this.handshake=pinkySwearPromise(),this._id=0,this._queue=[],this._sent={};var c=this;return this._listener=function(a){c._receiveMessage(a)},window.removeEventListener("message",this._listener),window.addEventListener("message",this._listener),this._sendHandshake(),this};Talker.prototype.send=function(a,b,c){var d=new Talker.OutgoingMessage(this,a,b,c),e=pinkySwearPromise();this._sent[d.id]=e,this._queue.push(d),this._flushQueue();var f=setTimeout(function(){e(!1,[new Error(TALKER_ERR_TIMEOUT)])},this.timeout);return this.rejectTimeouts.push(f),e},Talker.prototype.destroy=function(){if(this.rejectTimeouts)for(var a=0,b=this.rejectTimeouts.length;b>a;a++)clearTimeout(this.rejectTimeouts[a]);this._listener&&(window.removeEventListener("message",this._listener),this._listener=null)},Talker.prototype._receiveMessage=function(a){var b,c;try{b=JSON.parse(a.data)}catch(d){b={}}return this._isSafeMessage(a.source,a.origin,b.type)?(c=b.handshake||b.handshakeConfirmation,c?this._handleHandshake(b):this._handleMessage(b)):!1},Talker.prototype._isSafeMessage=function(a,b,c){var d,e,f;return d=a===this.remoteWindow,e="*"===this.remoteOrigin||b===this.remoteOrigin,f=c===TALKER_TYPE,d&&e&&f},Talker.prototype._handleHandshake=function(a){a.handshake&&this._sendHandshake(this.handshaken),this.handshaken=!0,this.handshake(!0,[this.handshaken]),this._flushQueue()},Talker.prototype._handleMessage=function(a){var b=new Talker.IncomingMessage(this,a.namespace,a.data,a.id),c=a.responseToId;return c?this._respondToMessage(c,b):this._broadcastMessage(b)},Talker.prototype._respondToMessage=function(a,b){this._sent[a]&&(this._sent[a](!0,[b]),delete this._sent[a])},Talker.prototype._broadcastMessage=function(a){this.onMessage&&this.onMessage.call(this,a)},Talker.prototype._sendHandshake=function(a){var b={type:TALKER_TYPE},c=a?"handshakeConfirmation":"handshake";b[c]=!0,this._postMessage(b)},Talker.prototype._nextId=function(){return this._id+=1},Talker.prototype._postMessage=function(a){this.remoteWindow&&this.remoteOrigin&&this.remoteWindow.postMessage(JSON.stringify(a),this.remoteOrigin)},Talker.prototype._flushQueue=function(){if(this.handshaken){var a=this._queue.shift();if(!a)return this._queue;if(this._postMessage(a),this._queue.length>0)return this._flushQueue()}return this._queue},Talker.Message=function(a,b,c){return this.talker=a,this.namespace=b,this.data=c,this.type=TALKER_TYPE,this},Talker.OutgoingMessage=function(a,b,c,d){Talker.Message.call(this,a,b,c),this.responseToId=d||null,this.id=this.talker._nextId()},Talker.OutgoingMessage.prototype=objectCreate(Talker.Message.prototype),Talker.OutgoingMessage.prototype.constructor=Talker.Message,Talker.OutgoingMessage.prototype.toJSON=function(){return{id:this.id,responseToId:this.responseToId,namespace:this.namespace,data:this.data,type:this.type}},Talker.IncomingMessage=function(a,b,c,d){Talker.Message.call(this,a,b,c),this.id=d},Talker.IncomingMessage.prototype=objectCreate(Talker.Message.prototype),Talker.IncomingMessage.prototype.constructor=Talker.Message,Talker.IncomingMessage.prototype.respond=function(a){return this.talker.send(null,a,this.id)}; return Talker}) \ No newline at end of file diff --git a/dist/common_js/talker.min.js b/dist/common_js/talker.min.js index 6976684..ea8188e 100644 --- a/dist/common_js/talker.min.js +++ b/dist/common_js/talker.min.js @@ -2,5 +2,5 @@ * © 2014 Second Street, MIT License * Talker.js 1.0.1 */ -var TALKER_TYPE="application/x-talkerjs-v1+json",TALKER_ERR_TIMEOUT="timeout",pinkySwearPromise=function(){function a(a){return"function"==typeof a}function b(a){return"object"==typeof a}function c(a){"undefined"!=typeof setImmediate?setImmediate(a):"undefined"!=typeof process&&process.nextTick?process.nextTick(a):setTimeout(a,0)}var d;return function e(){var f,g=[],h=[],i=function(a,b){return null==f&&null!=a&&(f=a,g=b,h.length&&c(function(){for(var a=0;h.length>a;a++)h[a]()})),f};return i.then=function(i,j){var k=e(),l=function(){function c(e){var f,g=0;try{if(e&&(b(e)||a(e))&&a(f=e.then)){if(e===k)throw new TypeError;f.call(e,function(){g++||c.apply(d,arguments)},function(a){g++||k(!1,[a])})}else k(!0,arguments)}catch(h){g++||k(!1,[h])}}try{var e=f?i:j;a(e)?c(e.apply(d,g||[])):k(f,g)}catch(h){k(!1,[h])}};return null!=f?c(l):h.push(l),k},i}}(),objectCreate=function(a){function b(){}return b.prototype=a,new b},Talker=function(a,b){this.remoteWindow=a,this.remoteOrigin=b,this.timeout=3e3,this.handshaken=!1,this.handshake=pinkySwearPromise(),this._id=0,this._queue=[],this._sent={};var c=this;return window.addEventListener("message",function(a){c._receiveMessage(a)},!1),this._sendHandshake(),this};Talker.prototype.send=function(a,b,c){var d=new Talker.OutgoingMessage(this,a,b,c),e=pinkySwearPromise();return this._sent[d.id]=e,this._queue.push(d),this._flushQueue(),setTimeout(function(){e(!1,[new Error(TALKER_ERR_TIMEOUT)])},this.timeout),e},Talker.prototype._receiveMessage=function(a){var b,c;try{b=JSON.parse(a.data)}catch(d){b={}}return this._isSafeMessage(a.source,a.origin,b.type)?(c=b.handshake||b.handshakeConfirmation,c?this._handleHandshake(b):this._handleMessage(b)):!1},Talker.prototype._isSafeMessage=function(a,b,c){var d,e,f;return d=a===this.remoteWindow,e="*"===this.remoteOrigin||b===this.remoteOrigin,f=c===TALKER_TYPE,d&&e&&f},Talker.prototype._handleHandshake=function(a){a.handshake&&this._sendHandshake(this.handshaken),this.handshaken=!0,this.handshake(!0,[this.handshaken]),this._flushQueue()},Talker.prototype._handleMessage=function(a){var b=new Talker.IncomingMessage(this,a.namespace,a.data,a.id),c=a.responseToId;return c?this._respondToMessage(c,b):this._broadcastMessage(b)},Talker.prototype._respondToMessage=function(a,b){this._sent[a]&&(this._sent[a](!0,[b]),delete this._sent[a])},Talker.prototype._broadcastMessage=function(a){this.onMessage&&this.onMessage.call(this,a)},Talker.prototype._sendHandshake=function(a){var b={type:TALKER_TYPE},c=a?"handshakeConfirmation":"handshake";b[c]=!0,this._postMessage(b)},Talker.prototype._nextId=function(){return this._id+=1},Talker.prototype._postMessage=function(a){this.remoteWindow&&this.remoteOrigin&&this.remoteWindow.postMessage(JSON.stringify(a),this.remoteOrigin)},Talker.prototype._flushQueue=function(){if(this.handshaken){var a=this._queue.shift();if(!a)return this._queue;if(this._postMessage(a),this._queue.length>0)return this._flushQueue()}return this._queue},Talker.Message=function(a,b,c){return this.talker=a,this.namespace=b,this.data=c,this.type=TALKER_TYPE,this},Talker.OutgoingMessage=function(a,b,c,d){Talker.Message.call(this,a,b,c),this.responseToId=d||null,this.id=this.talker._nextId()},Talker.OutgoingMessage.prototype=objectCreate(Talker.Message.prototype),Talker.OutgoingMessage.prototype.constructor=Talker.Message,Talker.OutgoingMessage.prototype.toJSON=function(){return{id:this.id,responseToId:this.responseToId,namespace:this.namespace,data:this.data,type:this.type}},Talker.IncomingMessage=function(a,b,c,d){Talker.Message.call(this,a,b,c),this.id=d},Talker.IncomingMessage.prototype=objectCreate(Talker.Message.prototype),Talker.IncomingMessage.prototype.constructor=Talker.Message,Talker.IncomingMessage.prototype.respond=function(a){return this.talker.send(null,a,this.id)}; +var TALKER_TYPE="application/x-talkerjs-v1+json",TALKER_ERR_TIMEOUT="timeout",pinkySwearPromise=function(){function a(a){return"function"==typeof a}function b(a){return"object"==typeof a}function c(a){"undefined"!=typeof setImmediate?setImmediate(a):"undefined"!=typeof process&&process.nextTick?process.nextTick(a):setTimeout(a,0)}var d;return function e(){var f,g=[],h=[],i=function(a,b){return null==f&&null!=a&&(f=a,g=b,h.length&&c(function(){for(var a=0;h.length>a;a++)h[a]()})),f};return i.then=function(i,j){var k=e(),l=function(){function c(e){var f,g=0;try{if(e&&(b(e)||a(e))&&a(f=e.then)){if(e===k)throw new TypeError;f.call(e,function(){g++||c.apply(d,arguments)},function(a){g++||k(!1,[a])})}else k(!0,arguments)}catch(h){g++||k(!1,[h])}}try{var e=f?i:j;a(e)?c(e.apply(d,g||[])):k(f,g)}catch(h){k(!1,[h])}};return null!=f?c(l):h.push(l),k},i}}(),objectCreate=function(a){function b(){}return b.prototype=a,new b},Talker=function(a,b){this.remoteWindow=a,this.remoteOrigin=b,this.timeout=3e3,this.rejectTimeouts=[],this.handshaken=!1,this.handshake=pinkySwearPromise(),this._id=0,this._queue=[],this._sent={};var c=this;return this._listener=function(a){c._receiveMessage(a)},window.removeEventListener("message",this._listener),window.addEventListener("message",this._listener),this._sendHandshake(),this};Talker.prototype.send=function(a,b,c){var d=new Talker.OutgoingMessage(this,a,b,c),e=pinkySwearPromise();this._sent[d.id]=e,this._queue.push(d),this._flushQueue();var f=setTimeout(function(){e(!1,[new Error(TALKER_ERR_TIMEOUT)])},this.timeout);return this.rejectTimeouts.push(f),e},Talker.prototype.destroy=function(){if(this.rejectTimeouts)for(var a=0,b=this.rejectTimeouts.length;b>a;a++)clearTimeout(this.rejectTimeouts[a]);this._listener&&(window.removeEventListener("message",this._listener),this._listener=null)},Talker.prototype._receiveMessage=function(a){var b,c;try{b=JSON.parse(a.data)}catch(d){b={}}return this._isSafeMessage(a.source,a.origin,b.type)?(c=b.handshake||b.handshakeConfirmation,c?this._handleHandshake(b):this._handleMessage(b)):!1},Talker.prototype._isSafeMessage=function(a,b,c){var d,e,f;return d=a===this.remoteWindow,e="*"===this.remoteOrigin||b===this.remoteOrigin,f=c===TALKER_TYPE,d&&e&&f},Talker.prototype._handleHandshake=function(a){a.handshake&&this._sendHandshake(this.handshaken),this.handshaken=!0,this.handshake(!0,[this.handshaken]),this._flushQueue()},Talker.prototype._handleMessage=function(a){var b=new Talker.IncomingMessage(this,a.namespace,a.data,a.id),c=a.responseToId;return c?this._respondToMessage(c,b):this._broadcastMessage(b)},Talker.prototype._respondToMessage=function(a,b){this._sent[a]&&(this._sent[a](!0,[b]),delete this._sent[a])},Talker.prototype._broadcastMessage=function(a){this.onMessage&&this.onMessage.call(this,a)},Talker.prototype._sendHandshake=function(a){var b={type:TALKER_TYPE},c=a?"handshakeConfirmation":"handshake";b[c]=!0,this._postMessage(b)},Talker.prototype._nextId=function(){return this._id+=1},Talker.prototype._postMessage=function(a){this.remoteWindow&&this.remoteOrigin&&this.remoteWindow.postMessage(JSON.stringify(a),this.remoteOrigin)},Talker.prototype._flushQueue=function(){if(this.handshaken){var a=this._queue.shift();if(!a)return this._queue;if(this._postMessage(a),this._queue.length>0)return this._flushQueue()}return this._queue},Talker.Message=function(a,b,c){return this.talker=a,this.namespace=b,this.data=c,this.type=TALKER_TYPE,this},Talker.OutgoingMessage=function(a,b,c,d){Talker.Message.call(this,a,b,c),this.responseToId=d||null,this.id=this.talker._nextId()},Talker.OutgoingMessage.prototype=objectCreate(Talker.Message.prototype),Talker.OutgoingMessage.prototype.constructor=Talker.Message,Talker.OutgoingMessage.prototype.toJSON=function(){return{id:this.id,responseToId:this.responseToId,namespace:this.namespace,data:this.data,type:this.type}},Talker.IncomingMessage=function(a,b,c,d){Talker.Message.call(this,a,b,c),this.id=d},Talker.IncomingMessage.prototype=objectCreate(Talker.Message.prototype),Talker.IncomingMessage.prototype.constructor=Talker.Message,Talker.IncomingMessage.prototype.respond=function(a){return this.talker.send(null,a,this.id)}; module.exports=Talker \ No newline at end of file diff --git a/dist/named_amd/talker.min.js b/dist/named_amd/talker.min.js index eeb21f8..0888653 100644 --- a/dist/named_amd/talker.min.js +++ b/dist/named_amd/talker.min.js @@ -2,5 +2,5 @@ define("talker",[],function(){/*! * © 2014 Second Street, MIT License * Talker.js 1.0.1 */ -var TALKER_TYPE="application/x-talkerjs-v1+json",TALKER_ERR_TIMEOUT="timeout",pinkySwearPromise=function(){function a(a){return"function"==typeof a}function b(a){return"object"==typeof a}function c(a){"undefined"!=typeof setImmediate?setImmediate(a):"undefined"!=typeof process&&process.nextTick?process.nextTick(a):setTimeout(a,0)}var d;return function e(){var f,g=[],h=[],i=function(a,b){return null==f&&null!=a&&(f=a,g=b,h.length&&c(function(){for(var a=0;h.length>a;a++)h[a]()})),f};return i.then=function(i,j){var k=e(),l=function(){function c(e){var f,g=0;try{if(e&&(b(e)||a(e))&&a(f=e.then)){if(e===k)throw new TypeError;f.call(e,function(){g++||c.apply(d,arguments)},function(a){g++||k(!1,[a])})}else k(!0,arguments)}catch(h){g++||k(!1,[h])}}try{var e=f?i:j;a(e)?c(e.apply(d,g||[])):k(f,g)}catch(h){k(!1,[h])}};return null!=f?c(l):h.push(l),k},i}}(),objectCreate=function(a){function b(){}return b.prototype=a,new b},Talker=function(a,b){this.remoteWindow=a,this.remoteOrigin=b,this.timeout=3e3,this.handshaken=!1,this.handshake=pinkySwearPromise(),this._id=0,this._queue=[],this._sent={};var c=this;return window.addEventListener("message",function(a){c._receiveMessage(a)},!1),this._sendHandshake(),this};Talker.prototype.send=function(a,b,c){var d=new Talker.OutgoingMessage(this,a,b,c),e=pinkySwearPromise();return this._sent[d.id]=e,this._queue.push(d),this._flushQueue(),setTimeout(function(){e(!1,[new Error(TALKER_ERR_TIMEOUT)])},this.timeout),e},Talker.prototype._receiveMessage=function(a){var b,c;try{b=JSON.parse(a.data)}catch(d){b={}}return this._isSafeMessage(a.source,a.origin,b.type)?(c=b.handshake||b.handshakeConfirmation,c?this._handleHandshake(b):this._handleMessage(b)):!1},Talker.prototype._isSafeMessage=function(a,b,c){var d,e,f;return d=a===this.remoteWindow,e="*"===this.remoteOrigin||b===this.remoteOrigin,f=c===TALKER_TYPE,d&&e&&f},Talker.prototype._handleHandshake=function(a){a.handshake&&this._sendHandshake(this.handshaken),this.handshaken=!0,this.handshake(!0,[this.handshaken]),this._flushQueue()},Talker.prototype._handleMessage=function(a){var b=new Talker.IncomingMessage(this,a.namespace,a.data,a.id),c=a.responseToId;return c?this._respondToMessage(c,b):this._broadcastMessage(b)},Talker.prototype._respondToMessage=function(a,b){this._sent[a]&&(this._sent[a](!0,[b]),delete this._sent[a])},Talker.prototype._broadcastMessage=function(a){this.onMessage&&this.onMessage.call(this,a)},Talker.prototype._sendHandshake=function(a){var b={type:TALKER_TYPE},c=a?"handshakeConfirmation":"handshake";b[c]=!0,this._postMessage(b)},Talker.prototype._nextId=function(){return this._id+=1},Talker.prototype._postMessage=function(a){this.remoteWindow&&this.remoteOrigin&&this.remoteWindow.postMessage(JSON.stringify(a),this.remoteOrigin)},Talker.prototype._flushQueue=function(){if(this.handshaken){var a=this._queue.shift();if(!a)return this._queue;if(this._postMessage(a),this._queue.length>0)return this._flushQueue()}return this._queue},Talker.Message=function(a,b,c){return this.talker=a,this.namespace=b,this.data=c,this.type=TALKER_TYPE,this},Talker.OutgoingMessage=function(a,b,c,d){Talker.Message.call(this,a,b,c),this.responseToId=d||null,this.id=this.talker._nextId()},Talker.OutgoingMessage.prototype=objectCreate(Talker.Message.prototype),Talker.OutgoingMessage.prototype.constructor=Talker.Message,Talker.OutgoingMessage.prototype.toJSON=function(){return{id:this.id,responseToId:this.responseToId,namespace:this.namespace,data:this.data,type:this.type}},Talker.IncomingMessage=function(a,b,c,d){Talker.Message.call(this,a,b,c),this.id=d},Talker.IncomingMessage.prototype=objectCreate(Talker.Message.prototype),Talker.IncomingMessage.prototype.constructor=Talker.Message,Talker.IncomingMessage.prototype.respond=function(a){return this.talker.send(null,a,this.id)}; +var TALKER_TYPE="application/x-talkerjs-v1+json",TALKER_ERR_TIMEOUT="timeout",pinkySwearPromise=function(){function a(a){return"function"==typeof a}function b(a){return"object"==typeof a}function c(a){"undefined"!=typeof setImmediate?setImmediate(a):"undefined"!=typeof process&&process.nextTick?process.nextTick(a):setTimeout(a,0)}var d;return function e(){var f,g=[],h=[],i=function(a,b){return null==f&&null!=a&&(f=a,g=b,h.length&&c(function(){for(var a=0;h.length>a;a++)h[a]()})),f};return i.then=function(i,j){var k=e(),l=function(){function c(e){var f,g=0;try{if(e&&(b(e)||a(e))&&a(f=e.then)){if(e===k)throw new TypeError;f.call(e,function(){g++||c.apply(d,arguments)},function(a){g++||k(!1,[a])})}else k(!0,arguments)}catch(h){g++||k(!1,[h])}}try{var e=f?i:j;a(e)?c(e.apply(d,g||[])):k(f,g)}catch(h){k(!1,[h])}};return null!=f?c(l):h.push(l),k},i}}(),objectCreate=function(a){function b(){}return b.prototype=a,new b},Talker=function(a,b){this.remoteWindow=a,this.remoteOrigin=b,this.timeout=3e3,this.rejectTimeouts=[],this.handshaken=!1,this.handshake=pinkySwearPromise(),this._id=0,this._queue=[],this._sent={};var c=this;return this._listener=function(a){c._receiveMessage(a)},window.removeEventListener("message",this._listener),window.addEventListener("message",this._listener),this._sendHandshake(),this};Talker.prototype.send=function(a,b,c){var d=new Talker.OutgoingMessage(this,a,b,c),e=pinkySwearPromise();this._sent[d.id]=e,this._queue.push(d),this._flushQueue();var f=setTimeout(function(){e(!1,[new Error(TALKER_ERR_TIMEOUT)])},this.timeout);return this.rejectTimeouts.push(f),e},Talker.prototype.destroy=function(){if(this.rejectTimeouts)for(var a=0,b=this.rejectTimeouts.length;b>a;a++)clearTimeout(this.rejectTimeouts[a]);this._listener&&(window.removeEventListener("message",this._listener),this._listener=null)},Talker.prototype._receiveMessage=function(a){var b,c;try{b=JSON.parse(a.data)}catch(d){b={}}return this._isSafeMessage(a.source,a.origin,b.type)?(c=b.handshake||b.handshakeConfirmation,c?this._handleHandshake(b):this._handleMessage(b)):!1},Talker.prototype._isSafeMessage=function(a,b,c){var d,e,f;return d=a===this.remoteWindow,e="*"===this.remoteOrigin||b===this.remoteOrigin,f=c===TALKER_TYPE,d&&e&&f},Talker.prototype._handleHandshake=function(a){a.handshake&&this._sendHandshake(this.handshaken),this.handshaken=!0,this.handshake(!0,[this.handshaken]),this._flushQueue()},Talker.prototype._handleMessage=function(a){var b=new Talker.IncomingMessage(this,a.namespace,a.data,a.id),c=a.responseToId;return c?this._respondToMessage(c,b):this._broadcastMessage(b)},Talker.prototype._respondToMessage=function(a,b){this._sent[a]&&(this._sent[a](!0,[b]),delete this._sent[a])},Talker.prototype._broadcastMessage=function(a){this.onMessage&&this.onMessage.call(this,a)},Talker.prototype._sendHandshake=function(a){var b={type:TALKER_TYPE},c=a?"handshakeConfirmation":"handshake";b[c]=!0,this._postMessage(b)},Talker.prototype._nextId=function(){return this._id+=1},Talker.prototype._postMessage=function(a){this.remoteWindow&&this.remoteOrigin&&this.remoteWindow.postMessage(JSON.stringify(a),this.remoteOrigin)},Talker.prototype._flushQueue=function(){if(this.handshaken){var a=this._queue.shift();if(!a)return this._queue;if(this._postMessage(a),this._queue.length>0)return this._flushQueue()}return this._queue},Talker.Message=function(a,b,c){return this.talker=a,this.namespace=b,this.data=c,this.type=TALKER_TYPE,this},Talker.OutgoingMessage=function(a,b,c,d){Talker.Message.call(this,a,b,c),this.responseToId=d||null,this.id=this.talker._nextId()},Talker.OutgoingMessage.prototype=objectCreate(Talker.Message.prototype),Talker.OutgoingMessage.prototype.constructor=Talker.Message,Talker.OutgoingMessage.prototype.toJSON=function(){return{id:this.id,responseToId:this.responseToId,namespace:this.namespace,data:this.data,type:this.type}},Talker.IncomingMessage=function(a,b,c,d){Talker.Message.call(this,a,b,c),this.id=d},Talker.IncomingMessage.prototype=objectCreate(Talker.Message.prototype),Talker.IncomingMessage.prototype.constructor=Talker.Message,Talker.IncomingMessage.prototype.respond=function(a){return this.talker.send(null,a,this.id)}; return Talker}) \ No newline at end of file diff --git a/dist/talker.min.js b/dist/talker.min.js index da6c9ba..96de512 100644 --- a/dist/talker.min.js +++ b/dist/talker.min.js @@ -2,5 +2,5 @@ * © 2014 Second Street, MIT License * Talker.js 1.0.1 */ -var TALKER_TYPE="application/x-talkerjs-v1+json",TALKER_ERR_TIMEOUT="timeout",pinkySwearPromise=function(){function a(a){return"function"==typeof a}function b(a){return"object"==typeof a}function c(a){"undefined"!=typeof setImmediate?setImmediate(a):"undefined"!=typeof process&&process.nextTick?process.nextTick(a):setTimeout(a,0)}var d;return function e(){var f,g=[],h=[],i=function(a,b){return null==f&&null!=a&&(f=a,g=b,h.length&&c(function(){for(var a=0;h.length>a;a++)h[a]()})),f};return i.then=function(i,j){var k=e(),l=function(){function c(e){var f,g=0;try{if(e&&(b(e)||a(e))&&a(f=e.then)){if(e===k)throw new TypeError;f.call(e,function(){g++||c.apply(d,arguments)},function(a){g++||k(!1,[a])})}else k(!0,arguments)}catch(h){g++||k(!1,[h])}}try{var e=f?i:j;a(e)?c(e.apply(d,g||[])):k(f,g)}catch(h){k(!1,[h])}};return null!=f?c(l):h.push(l),k},i}}(),objectCreate=function(a){function b(){}return b.prototype=a,new b},Talker=function(a,b){this.remoteWindow=a,this.remoteOrigin=b,this.timeout=3e3,this.handshaken=!1,this.handshake=pinkySwearPromise(),this._id=0,this._queue=[],this._sent={};var c=this;return window.addEventListener("message",function(a){c._receiveMessage(a)},!1),this._sendHandshake(),this};Talker.prototype.send=function(a,b,c){var d=new Talker.OutgoingMessage(this,a,b,c),e=pinkySwearPromise();return this._sent[d.id]=e,this._queue.push(d),this._flushQueue(),setTimeout(function(){e(!1,[new Error(TALKER_ERR_TIMEOUT)])},this.timeout),e},Talker.prototype._receiveMessage=function(a){var b,c;try{b=JSON.parse(a.data)}catch(d){b={}}return this._isSafeMessage(a.source,a.origin,b.type)?(c=b.handshake||b.handshakeConfirmation,c?this._handleHandshake(b):this._handleMessage(b)):!1},Talker.prototype._isSafeMessage=function(a,b,c){var d,e,f;return d=a===this.remoteWindow,e="*"===this.remoteOrigin||b===this.remoteOrigin,f=c===TALKER_TYPE,d&&e&&f},Talker.prototype._handleHandshake=function(a){a.handshake&&this._sendHandshake(this.handshaken),this.handshaken=!0,this.handshake(!0,[this.handshaken]),this._flushQueue()},Talker.prototype._handleMessage=function(a){var b=new Talker.IncomingMessage(this,a.namespace,a.data,a.id),c=a.responseToId;return c?this._respondToMessage(c,b):this._broadcastMessage(b)},Talker.prototype._respondToMessage=function(a,b){this._sent[a]&&(this._sent[a](!0,[b]),delete this._sent[a])},Talker.prototype._broadcastMessage=function(a){this.onMessage&&this.onMessage.call(this,a)},Talker.prototype._sendHandshake=function(a){var b={type:TALKER_TYPE},c=a?"handshakeConfirmation":"handshake";b[c]=!0,this._postMessage(b)},Talker.prototype._nextId=function(){return this._id+=1},Talker.prototype._postMessage=function(a){this.remoteWindow&&this.remoteOrigin&&this.remoteWindow.postMessage(JSON.stringify(a),this.remoteOrigin)},Talker.prototype._flushQueue=function(){if(this.handshaken){var a=this._queue.shift();if(!a)return this._queue;if(this._postMessage(a),this._queue.length>0)return this._flushQueue()}return this._queue},Talker.Message=function(a,b,c){return this.talker=a,this.namespace=b,this.data=c,this.type=TALKER_TYPE,this},Talker.OutgoingMessage=function(a,b,c,d){Talker.Message.call(this,a,b,c),this.responseToId=d||null,this.id=this.talker._nextId()},Talker.OutgoingMessage.prototype=objectCreate(Talker.Message.prototype),Talker.OutgoingMessage.prototype.constructor=Talker.Message,Talker.OutgoingMessage.prototype.toJSON=function(){return{id:this.id,responseToId:this.responseToId,namespace:this.namespace,data:this.data,type:this.type}},Talker.IncomingMessage=function(a,b,c,d){Talker.Message.call(this,a,b,c),this.id=d},Talker.IncomingMessage.prototype=objectCreate(Talker.Message.prototype),Talker.IncomingMessage.prototype.constructor=Talker.Message,Talker.IncomingMessage.prototype.respond=function(a){return this.talker.send(null,a,this.id)}; +var TALKER_TYPE="application/x-talkerjs-v1+json",TALKER_ERR_TIMEOUT="timeout",pinkySwearPromise=function(){function a(a){return"function"==typeof a}function b(a){return"object"==typeof a}function c(a){"undefined"!=typeof setImmediate?setImmediate(a):"undefined"!=typeof process&&process.nextTick?process.nextTick(a):setTimeout(a,0)}var d;return function e(){var f,g=[],h=[],i=function(a,b){return null==f&&null!=a&&(f=a,g=b,h.length&&c(function(){for(var a=0;h.length>a;a++)h[a]()})),f};return i.then=function(i,j){var k=e(),l=function(){function c(e){var f,g=0;try{if(e&&(b(e)||a(e))&&a(f=e.then)){if(e===k)throw new TypeError;f.call(e,function(){g++||c.apply(d,arguments)},function(a){g++||k(!1,[a])})}else k(!0,arguments)}catch(h){g++||k(!1,[h])}}try{var e=f?i:j;a(e)?c(e.apply(d,g||[])):k(f,g)}catch(h){k(!1,[h])}};return null!=f?c(l):h.push(l),k},i}}(),objectCreate=function(a){function b(){}return b.prototype=a,new b},Talker=function(a,b){this.remoteWindow=a,this.remoteOrigin=b,this.timeout=3e3,this.rejectTimeouts=[],this.handshaken=!1,this.handshake=pinkySwearPromise(),this._id=0,this._queue=[],this._sent={};var c=this;return this._listener=function(a){c._receiveMessage(a)},window.removeEventListener("message",this._listener),window.addEventListener("message",this._listener),this._sendHandshake(),this};Talker.prototype.send=function(a,b,c){var d=new Talker.OutgoingMessage(this,a,b,c),e=pinkySwearPromise();this._sent[d.id]=e,this._queue.push(d),this._flushQueue();var f=setTimeout(function(){e(!1,[new Error(TALKER_ERR_TIMEOUT)])},this.timeout);return this.rejectTimeouts.push(f),e},Talker.prototype.destroy=function(){if(this.rejectTimeouts)for(var a=0,b=this.rejectTimeouts.length;b>a;a++)clearTimeout(this.rejectTimeouts[a]);this._listener&&(window.removeEventListener("message",this._listener),this._listener=null)},Talker.prototype._receiveMessage=function(a){var b,c;try{b=JSON.parse(a.data)}catch(d){b={}}return this._isSafeMessage(a.source,a.origin,b.type)?(c=b.handshake||b.handshakeConfirmation,c?this._handleHandshake(b):this._handleMessage(b)):!1},Talker.prototype._isSafeMessage=function(a,b,c){var d,e,f;return d=a===this.remoteWindow,e="*"===this.remoteOrigin||b===this.remoteOrigin,f=c===TALKER_TYPE,d&&e&&f},Talker.prototype._handleHandshake=function(a){a.handshake&&this._sendHandshake(this.handshaken),this.handshaken=!0,this.handshake(!0,[this.handshaken]),this._flushQueue()},Talker.prototype._handleMessage=function(a){var b=new Talker.IncomingMessage(this,a.namespace,a.data,a.id),c=a.responseToId;return c?this._respondToMessage(c,b):this._broadcastMessage(b)},Talker.prototype._respondToMessage=function(a,b){this._sent[a]&&(this._sent[a](!0,[b]),delete this._sent[a])},Talker.prototype._broadcastMessage=function(a){this.onMessage&&this.onMessage.call(this,a)},Talker.prototype._sendHandshake=function(a){var b={type:TALKER_TYPE},c=a?"handshakeConfirmation":"handshake";b[c]=!0,this._postMessage(b)},Talker.prototype._nextId=function(){return this._id+=1},Talker.prototype._postMessage=function(a){this.remoteWindow&&this.remoteOrigin&&this.remoteWindow.postMessage(JSON.stringify(a),this.remoteOrigin)},Talker.prototype._flushQueue=function(){if(this.handshaken){var a=this._queue.shift();if(!a)return this._queue;if(this._postMessage(a),this._queue.length>0)return this._flushQueue()}return this._queue},Talker.Message=function(a,b,c){return this.talker=a,this.namespace=b,this.data=c,this.type=TALKER_TYPE,this},Talker.OutgoingMessage=function(a,b,c,d){Talker.Message.call(this,a,b,c),this.responseToId=d||null,this.id=this.talker._nextId()},Talker.OutgoingMessage.prototype=objectCreate(Talker.Message.prototype),Talker.OutgoingMessage.prototype.constructor=Talker.Message,Talker.OutgoingMessage.prototype.toJSON=function(){return{id:this.id,responseToId:this.responseToId,namespace:this.namespace,data:this.data,type:this.type}},Talker.IncomingMessage=function(a,b,c,d){Talker.Message.call(this,a,b,c),this.id=d},Talker.IncomingMessage.prototype=objectCreate(Talker.Message.prototype),Talker.IncomingMessage.prototype.constructor=Talker.Message,Talker.IncomingMessage.prototype.respond=function(a){return this.talker.send(null,a,this.id)}; window.Talker=Talker})() \ No newline at end of file diff --git a/src/talker.js b/src/talker.js index 99a478d..b4a10cb 100755 --- a/src/talker.js +++ b/src/talker.js @@ -1,4 +1,4 @@ -/*! +/*! * © 2014 Second Street, MIT License * Talker.js 1.0.1 */ @@ -128,17 +128,20 @@ var Talker = function(remoteWindow, remoteOrigin) { this.remoteWindow = remoteWindow; this.remoteOrigin = remoteOrigin; this.timeout = 3000; - + this.rejectTimeouts = []; this.handshaken = false; this.handshake = pinkySwearPromise(); this._id = 0; this._queue = []; this._sent = {}; - var _this = this; - window.addEventListener('message', function(messageEvent) { _this._receiveMessage(messageEvent) }, false); + this._listener = function (messageEvent) { + _this._receiveMessage(messageEvent); + }; + + window.removeEventListener('message', this._listener); + window.addEventListener('message', this._listener); this._sendHandshake(); - return this; }; @@ -160,12 +163,27 @@ Talker.prototype.send = function(namespace, data, responseToId) { this._queue.push(message); this._flushQueue(); - setTimeout(function() { + var timeout = setTimeout(function() { promise(false, [new Error(TALKER_ERR_TIMEOUT)]); // Reject the promise }, this.timeout); + this.rejectTimeouts.push(timeout); + return promise; + }; + +Talker.prototype.destroy = function () { + if (this.rejectTimeouts) { + for (var i = 0, l = this.rejectTimeouts.length; i < l; i++) { + clearTimeout(this.rejectTimeouts[i]); + } + } + if (this._listener) { + window.removeEventListener("message", this._listener); + this._listener = null; + } }; + //endregion Public Methods //region Private Methods