forked from webRunes/Plus-WRIO-App
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.min.js
11 lines (10 loc) · 4.32 KB
/
client.min.js
1
2
3
4
5
6
7
8
9
10
11
/**
* cross-storage - Cross domain local storage
*
* @version 0.6.0
* @link https://github.com/zendesk/cross-storage
* @author Daniel St. Jules <danielst.jules@gmail.com>
* @copyright Zendesk
* @license Apache-2.0
*/
function CrossStorageClient(e,t){t=t||{},this._id=CrossStorageClient._generateUUID(),this._promise=t.promise||Promise,this._frameId=t.frameId||"CrossStorageClient-"+this._id,this._origin=CrossStorageClient._getOrigin(e),this._requests={},this._connected=!1,this._closed=!1,this._count=0,this._timeout=t.timeout||3e3,this._listener=null,this._installListener();var r;t.frameId&&(r=document.getElementById(t.frameId)),r&&this._poll(),r=r||this._createFrame(e),this._hub=r.contentWindow}CrossStorageClient.frameStyle={display:"none",position:"absolute",top:"-999px",left:"-999px"},CrossStorageClient._getOrigin=function(e){var t,r;return t=document.createElement("a"),t.href=e,t.host||(t=window.location),r=t.protocol+"//"+t.host,r=r.replace(/:80$|:443$/,"")},CrossStorageClient._generateUUID=function(){return"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,function(e){var t=16*Math.random()|0,r="x"==e?t:3&t|8;return r.toString(16)})},CrossStorageClient.prototype.onConnect=function(){var e=this;return this._connected?this._promise.resolve():this._closed?this._promise.reject(new Error("CrossStorageClient has closed")):(this._requests.connect||(this._requests.connect=[]),new this._promise(function(t,r){var o=setTimeout(function(){r(new Error("CrossStorageClient could not connect"))},e._timeout);e._requests.connect.push(function(e){return clearTimeout(o),e?r(e):(t(),void 0)})}))},CrossStorageClient.prototype.set=function(e,t,r){return this._request("set",{key:e,value:t,ttl:r})},CrossStorageClient.prototype.get=function(){var e=Array.prototype.slice.call(arguments);return this._request("get",{keys:e})},CrossStorageClient.prototype.del=function(){var e=Array.prototype.slice.call(arguments);return this._request("del",{keys:e})},CrossStorageClient.prototype.clear=function(){return this._request("clear")},CrossStorageClient.prototype.getKeys=function(){return this._request("getKeys")},CrossStorageClient.prototype.close=function(){var e=document.getElementById(this._frameId);e&&e.parentNode.removeChild(e),window.removeEventListener?window.removeEventListener("message",this._listener,!1):window.detachEvent("onmessage",this._listener),this._connected=!1,this._closed=!0},CrossStorageClient.prototype._installListener=function(){var e=this;this._listener=function(t){var r,o,s;if(!e._closed&&t.origin===e._origin)if("cross-storage:unavailable"!==t.data){if(-1!==t.data.indexOf("cross-storage:")&&!e._connected){if(e._connected=!0,!e._requests.connect)return;for(r=0;r<e._requests.connect.length;r++)e._requests.connect[r](o);delete e._requests.connect}if("cross-storage:ready"!==t.data){try{s=JSON.parse(t.data)}catch(n){return}s.id&&e._requests[s.id]&&e._requests[s.id](s.error,s.result)}}else{if(e._closed||e.close(),!e._requests.connect)return;for(o=new Error("Closing client. Could not access localStorage in hub."),r=0;r<e._requests.connect.length;r++)e._requests.connect[r](o)}},window.addEventListener?window.addEventListener("message",this._listener,!1):window.attachEvent("onmessage",this._listener)},CrossStorageClient.prototype._poll=function(){var e,t;e=this,t=setInterval(function(){return e._connected?clearInterval(t):(e._hub&&e._hub.postMessage("cross-storage:poll",e._origin),void 0)},1e3)},CrossStorageClient.prototype._createFrame=function(e){var t,r;t=window.document.createElement("iframe"),t.id=this._frameId;for(r in CrossStorageClient.frameStyle)CrossStorageClient.frameStyle.hasOwnProperty(r)&&(t.style[r]=CrossStorageClient.frameStyle[r]);return window.document.body.appendChild(t),t.src=e,t},CrossStorageClient.prototype._request=function(e,t){var r,o;return this._closed?this._promise.reject(new Error("CrossStorageClient has closed")):(o=this,o._count++,r={id:this._id+":"+o._count,method:"cross-storage:"+e,params:t},new this._promise(function(e,t){var s,n;s=setTimeout(function(){o._requests[r.id]&&(delete o._requests[r.id],t(new Error("Timeout: could not perform "+r.method)))},o._timeout),o._requests[r.id]=function(r,o){return clearTimeout(s),r?t(new Error(r)):(e(o),void 0)},Array.prototype.toJSON&&(n=Array.prototype.toJSON,Array.prototype.toJSON=null),o._hub.postMessage(JSON.stringify(r),o._origin),n&&(Array.prototype.toJSON=n)}))};