UNPKG

react-query

Version:

Hooks for managing, caching and syncing asynchronous and remote data in React

3 lines (2 loc) 10.7 kB
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).ReactQueryBroadcastQueryClientExperimental={})}(this,(function(e){"use strict";function t(e){return e||(e=0),new Promise((function(t){return setTimeout(t,e)}))}function n(){return Math.random().toString(36).substring(2)}var r=0,o=0;function s(){var e=(new Date).getTime();return e===r?1e3*e+ ++o:(r=e,o=0,1e3*e)}var i="[object process]"===Object.prototype.toString.call("undefined"!=typeof process?process:0);var a={create:function(e){var t={messagesCallback:null,bc:new BroadcastChannel(e),subFns:[]};return t.bc.onmessage=function(e){t.messagesCallback&&t.messagesCallback(e.data)},t},close:function(e){e.bc.close(),e.subFns=[]},onMessage:function(e,t){e.messagesCallback=t},postMessage:function(e,t){e.bc.postMessage(t,!1)},canBeUsed:function(){if(i&&"undefined"==typeof window)return!1;if("function"==typeof BroadcastChannel){if(BroadcastChannel._pubkey)throw new Error("BroadcastChannel: Do not overwrite window.BroadcastChannel with this module, this is not a polyfill");return!0}return!1},type:"native",averageResponseTime:function(){return 150},microSeconds:s};function u(){return(new Date).getTime()}var c=function(e){var t=new Set,n=new Map;this.has=t.has.bind(t),this.add=function(r){n.set(r,u()),t.add(r),function(){var r=u()-e,o=t[Symbol.iterator]();for(;;){var s=o.next().value;if(!s)return;if(!(n.get(s)<r))return;n.delete(s),t.delete(s)}}()},this.clear=function(){t.clear(),n.clear()}};function d(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},t=JSON.parse(JSON.stringify(e));return void 0===t.webWorkerSupport&&(t.webWorkerSupport=!0),t.idb||(t.idb={}),t.idb.ttl||(t.idb.ttl=45e3),t.idb.fallbackInterval||(t.idb.fallbackInterval=150),e.idb&&"function"==typeof e.idb.onclose&&(t.idb.onclose=e.idb.onclose),t.localstorage||(t.localstorage={}),t.localstorage.removeTimeout||(t.localstorage.removeTimeout=6e4),e.methods&&(t.methods=e.methods),t.node||(t.node={}),t.node.ttl||(t.node.ttl=12e4),void 0===t.node.useFastPath&&(t.node.useFastPath=!0),t}var l="messages";function f(){if("undefined"!=typeof indexedDB)return indexedDB;if("undefined"!=typeof window){if(void 0!==window.mozIndexedDB)return window.mozIndexedDB;if(void 0!==window.webkitIndexedDB)return window.webkitIndexedDB;if(void 0!==window.msIndexedDB)return window.msIndexedDB}return!1}function m(e,t){var n=e.transaction(l).objectStore(l),r=[];return new Promise((function(e){(function(){try{var e=IDBKeyRange.bound(t+1,1/0);return n.openCursor(e)}catch(e){return n.openCursor()}}()).onsuccess=function(n){var o=n.target.result;o?o.value.id<t+1?o.continue(t+1):(r.push(o.value),o.continue()):e(r)}}))}function h(e,t){return function(e,t){var n=(new Date).getTime()-t,r=e.transaction(l).objectStore(l),o=[];return new Promise((function(e){r.openCursor().onsuccess=function(t){var r=t.target.result;if(r){var s=r.value;if(!(s.time<n))return void e(o);o.push(s),r.continue()}else e(o)}}))}(e,t).then((function(t){return Promise.all(t.map((function(t){return function(e,t){var n=e.transaction([l],"readwrite").objectStore(l).delete(t);return new Promise((function(e){n.onsuccess=function(){return e()}}))}(e,t.id)})))}))}function p(e){e.closed||v(e).then((function(){return t(e.options.idb.fallbackInterval)})).then((function(){return p(e)}))}function v(e){return e.closed?Promise.resolve():e.messagesCallback?m(e.db,e.lastCursorId).then((function(t){var n=t.filter((function(e){return!!e})).map((function(t){return t.id>e.lastCursorId&&(e.lastCursorId=t.id),t})).filter((function(t){return function(e,t){return!(e.uuid===t.uuid||t.eMIs.has(e.id)||e.data.time<t.messagesCallbackTime)}(t,e)})).sort((function(e,t){return e.time-t.time}));return n.forEach((function(t){e.messagesCallback&&(e.eMIs.add(t.id),e.messagesCallback(t.data))})),Promise.resolve()})):Promise.resolve()}var g={create:function(e,t){return t=d(t),function(e){var t="pubkey.broadcast-channel-0-"+e,n=f().open(t,1);return n.onupgradeneeded=function(e){e.target.result.createObjectStore(l,{keyPath:"id",autoIncrement:!0})},new Promise((function(e,t){n.onerror=function(e){return t(e)},n.onsuccess=function(){e(n.result)}}))}(e).then((function(r){var o={closed:!1,lastCursorId:0,channelName:e,options:t,uuid:n(),eMIs:new c(2*t.idb.ttl),writeBlockPromise:Promise.resolve(),messagesCallback:null,readQueuePromises:[],db:r};return r.onclose=function(){o.closed=!0,t.idb.onclose&&t.idb.onclose()},p(o),o}))},close:function(e){e.closed=!0,e.db.close()},onMessage:function(e,t,n){e.messagesCallbackTime=n,e.messagesCallback=t,v(e)},postMessage:function(e,t){return e.writeBlockPromise=e.writeBlockPromise.then((function(){return function(e,t,n){var r={uuid:t,time:(new Date).getTime(),data:n},o=e.transaction([l],"readwrite");return new Promise((function(e,t){o.oncomplete=function(){return e()},o.onerror=function(e){return t(e)},o.objectStore(l).add(r)}))}(e.db,e.uuid,t)})).then((function(){var t,n;0===(t=0,n=10,Math.floor(Math.random()*(n-t+1)+t))&&h(e.db,e.options.idb.ttl)})),e.writeBlockPromise},canBeUsed:function(){return!i&&!!f()},type:"idb",averageResponseTime:function(e){return 2*e.idb.fallbackInterval},microSeconds:s};function b(){var e;if("undefined"==typeof window)return null;try{e=window.localStorage,e=window["ie8-eventlistener/storage"]||window.localStorage}catch(e){}return e}function w(e){return"pubkey.broadcastChannel-"+e}function y(){if(i)return!1;var e=b();if(!e)return!1;try{var t="__broadcastchannel_check";e.setItem(t,"works"),e.removeItem(t)}catch(e){return!1}return!0}var k={create:function(e,t){if(t=d(t),!y())throw new Error("BroadcastChannel: localstorage cannot be used");var r=n(),o=new c(t.localstorage.removeTimeout),s={channelName:e,uuid:r,eMIs:o};return s.listener=function(e,t){var n=w(e),r=function(e){e.key===n&&t(JSON.parse(e.newValue))};return window.addEventListener("storage",r),r}(e,(function(e){s.messagesCallback&&e.uuid!==r&&e.token&&!o.has(e.token)&&(e.data.time&&e.data.time<s.messagesCallbackTime||(o.add(e.token),s.messagesCallback(e.data)))})),s},close:function(e){var t;t=e.listener,window.removeEventListener("storage",t)},onMessage:function(e,t,n){e.messagesCallbackTime=n,e.messagesCallback=t},postMessage:function(e,r){return new Promise((function(o){t().then((function(){var t=w(e.channelName),s={token:n(),time:(new Date).getTime(),data:r,uuid:e.uuid},i=JSON.stringify(s);b().setItem(t,i);var a=document.createEvent("Event");a.initEvent("storage",!0,!0),a.key=t,a.newValue=i,window.dispatchEvent(a),o()}))}))},canBeUsed:y,type:"localstorage",averageResponseTime:function(){var e=navigator.userAgent.toLowerCase();return e.includes("safari")&&!e.includes("chrome")?240:120},microSeconds:s},C=s,_=new Set;var P={create:function(e){var t={name:e,messagesCallback:null};return _.add(t),t},close:function(e){_.delete(e)},onMessage:function(e,t){e.messagesCallback=t},postMessage:function(e,t){return new Promise((function(n){return setTimeout((function(){Array.from(_).filter((function(t){return t.name===e.name})).filter((function(t){return t!==e})).filter((function(e){return!!e.messagesCallback})).forEach((function(e){return e.messagesCallback(t)})),n()}),5)}))},canBeUsed:function(){return!0},type:"simulate",averageResponseTime:function(){return 5},microSeconds:C},M=[a,g,k];if(i){var S=require("../../src/methods/node.js");"function"==typeof S.canBeUsed&&M.push(S)}var B=function(e,t){var n,r,o;this.name=e,this.options=d(t),this.method=function(e){var t=[].concat(e.methods,M).filter(Boolean);if(e.type){if("simulate"===e.type)return P;var n=t.find((function(t){return t.type===e.type}));if(n)return n;throw new Error("method-type "+e.type+" not found")}e.webWorkerSupport||i||(t=t.filter((function(e){return"idb"!==e.type})));var r=t.find((function(e){return e.canBeUsed()}));if(r)return r;throw new Error("No useable methode found:"+JSON.stringify(M.map((function(e){return e.type}))))}(this.options),this._iL=!1,this._onML=null,this._addEL={message:[],internal:[]},this._befC=[],this._prepP=null,r=(n=this).method.create(n.name,n.options),(o=r)&&"function"==typeof o.then?(n._prepP=r,r.then((function(e){n._state=e}))):n._state=r};function E(e,t,n){var r={time:e.method.microSeconds(),type:t,data:n};return(e._prepP?e._prepP:Promise.resolve()).then((function(){return e.method.postMessage(e._state,r)}))}function L(e){return e._addEL.message.length>0||e._addEL.internal.length>0}function I(e,t,n){e._addEL[t].push(n),function(e){if(!e._iL&&L(e)){var t=function(t){e._addEL[t.type].forEach((function(e){t.time>=e.time&&e.fn(t.data)}))},n=e.method.microSeconds();e._prepP?e._prepP.then((function(){e._iL=!0,e.method.onMessage(e._state,t,n)})):(e._iL=!0,e.method.onMessage(e._state,t,n))}}(e)}function T(e,t,n){e._addEL[t]=e._addEL[t].filter((function(e){return e!==n})),function(e){if(e._iL&&!L(e)){e._iL=!1;var t=e.method.microSeconds();e.method.onMessage(e._state,null,t)}}(e)}B._pubkey=!0,B.prototype={postMessage:function(e){if(this.closed)throw new Error("BroadcastChannel.postMessage(): Cannot post message after channel has closed");return E(this,"message",e)},postInternal:function(e){return E(this,"internal",e)},set onmessage(e){var t={time:this.method.microSeconds(),fn:e};T(this,"message",this._onML),e&&"function"==typeof e?(this._onML=t,I(this,"message",t)):this._onML=null},addEventListener:function(e,t){I(this,e,{time:this.method.microSeconds(),fn:t})},removeEventListener:function(e,t){T(this,e,this._addEL[e].find((function(e){return e.fn===t})))},close:function(){var e=this;if(!this.closed){this.closed=!0;var t=this._prepP?this._prepP:Promise.resolve();return this._onML=null,this._addEL.message=[],t.then((function(){return Promise.all(e._befC.map((function(e){return e()})))})).then((function(){return e.method.close(e._state)}))}},get type(){return this.method.type}},e.broadcastQueryClient=function({queryClient:e,broadcastChannel:t="react-query"}){let n=!1;const r=new B(t,{webWorkerSupport:!1}),o=e.getQueryCache();e.getQueryCache().subscribe((e=>{if(n)return;const{query:{queryHash:t,queryKey:o,state:s}}=e;"updated"===e.type&&"success"===e.action.type&&r.postMessage({type:"updated",queryHash:t,queryKey:o,state:s}),"removed"===e.type&&r.postMessage({type:"removed",queryHash:t,queryKey:o})})),r.onmessage=t=>{var r;null!=t&&t.type&&(r=()=>{const{type:n,queryHash:r,queryKey:s,state:i}=t;if("updated"===n){const t=o.get(r);if(t)return void t.setState(i);o.build(e,{queryKey:s,queryHash:r},i)}else if("removed"===n){const e=o.get(r);e&&o.remove(e)}},n=!0,r(),n=!1)}},Object.defineProperty(e,"__esModule",{value:!0})})); //# sourceMappingURL=broadcastQueryClient-experimental.production.min.js.map