offline-sync-handler-test
Advanced tools
Comparing version 0.1.80 to 0.1.81
@@ -526,4 +526,5 @@ 'use strict'; | ||
React.useEffect(function () { | ||
var isMounted = true; | ||
var checkConnection = /*#__PURE__*/function () { | ||
//@ts-ignore | ||
var intervalId; | ||
var checkConnectivity = /*#__PURE__*/function () { | ||
var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() { | ||
@@ -538,5 +539,3 @@ var onlineStatus; | ||
onlineStatus = _context.sent; | ||
if (isMounted) { | ||
setIsConnected(onlineStatus); | ||
} | ||
setIsConnected(onlineStatus); | ||
case 4: | ||
@@ -548,13 +547,12 @@ case "end": | ||
})); | ||
return function checkConnection() { | ||
return function checkConnectivity() { | ||
return _ref.apply(this, arguments); | ||
}; | ||
}(); | ||
intervalId = setInterval(checkConnectivity, 10000); // Check every 10 seconds | ||
// Initial check | ||
checkConnection(); | ||
// Interval to check periodically | ||
var interval = setInterval(checkConnection, 10000); // every 10 seconds | ||
checkConnectivity(); | ||
//@ts-ignore | ||
return function () { | ||
isMounted = false; | ||
clearInterval(interval); | ||
return clearInterval(intervalId); | ||
}; | ||
@@ -561,0 +559,0 @@ }, []); |
@@ -1,2 +0,2 @@ | ||
"use strict";function t(t){return t&&"object"==typeof t&&"default"in t?t.default:t}Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react"),r=t(e),n=t(require("axios")),o=t(require("localforage")),a=require("underscore"),i=t(require("is-online"));function u(){u=function(){return t};var t={},e=Object.prototype,r=e.hasOwnProperty,n=Object.defineProperty||function(t,e,r){t[e]=r.value},o="function"==typeof Symbol?Symbol:{},a=o.iterator||"@@iterator",i=o.asyncIterator||"@@asyncIterator",c=o.toStringTag||"@@toStringTag";function s(t,e,r){return Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}),t[e]}try{s({},"")}catch(t){s=function(t,e,r){return t[e]=r}}function l(t,e,r,o){var a=Object.create((e&&e.prototype instanceof h?e:h).prototype),i=new S(o||[]);return n(a,"_invoke",{value:E(t,r,i)}),a}function f(t,e,r){try{return{type:"normal",arg:t.call(e,r)}}catch(t){return{type:"throw",arg:t}}}t.wrap=l;var p={};function h(){}function v(){}function d(){}var y={};s(y,a,(function(){return this}));var g=Object.getPrototypeOf,m=g&&g(g(j([])));m&&m!==e&&r.call(m,a)&&(y=m);var w=d.prototype=h.prototype=Object.create(y);function x(t){["next","throw","return"].forEach((function(e){s(t,e,(function(t){return this._invoke(e,t)}))}))}function b(t,e){var o;n(this,"_invoke",{value:function(n,a){function i(){return new e((function(o,i){!function n(o,a,i,u){var c=f(t[o],t,a);if("throw"!==c.type){var s=c.arg,l=s.value;return l&&"object"==typeof l&&r.call(l,"__await")?e.resolve(l.__await).then((function(t){n("next",t,i,u)}),(function(t){n("throw",t,i,u)})):e.resolve(l).then((function(t){s.value=t,i(s)}),(function(t){return n("throw",t,i,u)}))}u(c.arg)}(n,a,o,i)}))}return o=o?o.then(i,i):i()}})}function E(t,e,r){var n="suspendedStart";return function(o,a){if("executing"===n)throw new Error("Generator is already running");if("completed"===n){if("throw"===o)throw a;return{value:void 0,done:!0}}for(r.method=o,r.arg=a;;){var i=r.delegate;if(i){var u=k(i,r);if(u){if(u===p)continue;return u}}if("next"===r.method)r.sent=r._sent=r.arg;else if("throw"===r.method){if("suspendedStart"===n)throw n="completed",r.arg;r.dispatchException(r.arg)}else"return"===r.method&&r.abrupt("return",r.arg);n="executing";var c=f(t,e,r);if("normal"===c.type){if(n=r.done?"completed":"suspendedYield",c.arg===p)continue;return{value:c.arg,done:r.done}}"throw"===c.type&&(n="completed",r.method="throw",r.arg=c.arg)}}}function k(t,e){var r=e.method,n=t.iterator[r];if(void 0===n)return e.delegate=null,"throw"===r&&t.iterator.return&&(e.method="return",e.arg=void 0,k(t,e),"throw"===e.method)||"return"!==r&&(e.method="throw",e.arg=new TypeError("The iterator does not provide a '"+r+"' method")),p;var o=f(n,t.iterator,e.arg);if("throw"===o.type)return e.method="throw",e.arg=o.arg,e.delegate=null,p;var a=o.arg;return a?a.done?(e[t.resultName]=a.value,e.next=t.nextLoc,"return"!==e.method&&(e.method="next",e.arg=void 0),e.delegate=null,p):a:(e.method="throw",e.arg=new TypeError("iterator result is not an object"),e.delegate=null,p)}function O(t){var e={tryLoc:t[0]};1 in t&&(e.catchLoc=t[1]),2 in t&&(e.finallyLoc=t[2],e.afterLoc=t[3]),this.tryEntries.push(e)}function L(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function S(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(O,this),this.reset(!0)}function j(t){if(t){var e=t[a];if(e)return e.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var n=-1,o=function e(){for(;++n<t.length;)if(r.call(t,n))return e.value=t[n],e.done=!1,e;return e.value=void 0,e.done=!0,e};return o.next=o}}return{next:q}}function q(){return{value:void 0,done:!0}}return v.prototype=d,n(w,"constructor",{value:d,configurable:!0}),n(d,"constructor",{value:v,configurable:!0}),v.displayName=s(d,c,"GeneratorFunction"),t.isGeneratorFunction=function(t){var e="function"==typeof t&&t.constructor;return!!e&&(e===v||"GeneratorFunction"===(e.displayName||e.name))},t.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,d):(t.__proto__=d,s(t,c,"GeneratorFunction")),t.prototype=Object.create(w),t},t.awrap=function(t){return{__await:t}},x(b.prototype),s(b.prototype,i,(function(){return this})),t.AsyncIterator=b,t.async=function(e,r,n,o,a){void 0===a&&(a=Promise);var i=new b(l(e,r,n,o),a);return t.isGeneratorFunction(r)?i:i.next().then((function(t){return t.done?t.value:i.next()}))},x(w),s(w,c,"Generator"),s(w,a,(function(){return this})),s(w,"toString",(function(){return"[object Generator]"})),t.keys=function(t){var e=Object(t),r=[];for(var n in e)r.push(n);return r.reverse(),function t(){for(;r.length;){var n=r.pop();if(n in e)return t.value=n,t.done=!1,t}return t.done=!0,t}},t.values=j,S.prototype={constructor:S,reset:function(t){if(this.prev=0,this.next=0,this.sent=this._sent=void 0,this.done=!1,this.delegate=null,this.method="next",this.arg=void 0,this.tryEntries.forEach(L),!t)for(var e in this)"t"===e.charAt(0)&&r.call(this,e)&&!isNaN(+e.slice(1))&&(this[e]=void 0)},stop:function(){this.done=!0;var t=this.tryEntries[0].completion;if("throw"===t.type)throw t.arg;return this.rval},dispatchException:function(t){if(this.done)throw t;var e=this;function n(r,n){return i.type="throw",i.arg=t,e.next=r,n&&(e.method="next",e.arg=void 0),!!n}for(var o=this.tryEntries.length-1;o>=0;--o){var a=this.tryEntries[o],i=a.completion;if("root"===a.tryLoc)return n("end");if(a.tryLoc<=this.prev){var u=r.call(a,"catchLoc"),c=r.call(a,"finallyLoc");if(u&&c){if(this.prev<a.catchLoc)return n(a.catchLoc,!0);if(this.prev<a.finallyLoc)return n(a.finallyLoc)}else if(u){if(this.prev<a.catchLoc)return n(a.catchLoc,!0)}else{if(!c)throw new Error("try statement without catch or finally");if(this.prev<a.finallyLoc)return n(a.finallyLoc)}}}},abrupt:function(t,e){for(var n=this.tryEntries.length-1;n>=0;--n){var o=this.tryEntries[n];if(o.tryLoc<=this.prev&&r.call(o,"finallyLoc")&&this.prev<o.finallyLoc){var a=o;break}}a&&("break"===t||"continue"===t)&&a.tryLoc<=e&&e<=a.finallyLoc&&(a=null);var i=a?a.completion:{};return i.type=t,i.arg=e,a?(this.method="next",this.next=a.finallyLoc,p):this.complete(i)},complete:function(t,e){if("throw"===t.type)throw t.arg;return"break"===t.type||"continue"===t.type?this.next=t.arg:"return"===t.type?(this.rval=this.arg=t.arg,this.method="return",this.next="end"):"normal"===t.type&&e&&(this.next=e),p},finish:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.finallyLoc===t)return this.complete(r.completion,r.afterLoc),L(r),p}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.tryLoc===t){var n=r.completion;if("throw"===n.type){var o=n.arg;L(r)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,r){return this.delegate={iterator:j(t),resultName:e,nextLoc:r},"next"===this.method&&(this.arg=void 0),p}},t}function c(t,e,r,n,o,a,i){try{var u=t[a](i),c=u.value}catch(t){return void r(t)}u.done?e(c):Promise.resolve(c).then(n,o)}function s(t){return function(){var e=this,r=arguments;return new Promise((function(n,o){var a=t.apply(e,r);function i(t){c(a,n,o,i,u,"next",t)}function u(t){c(a,n,o,i,u,"throw",t)}i(void 0)}))}}function l(){return(l=Object.assign?Object.assign.bind():function(t){for(var e=1;e<arguments.length;e++){var r=arguments[e];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(t[n]=r[n])}return t}).apply(this,arguments)}function f(t,e){(null==e||e>t.length)&&(e=t.length);for(var r=0,n=new Array(e);r<e;r++)n[r]=t[r];return n}function p(t,e){var r="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(r)return(r=r.call(t)).next.bind(r);if(Array.isArray(t)||(r=function(t,e){if(t){if("string"==typeof t)return f(t,void 0);var r=Object.prototype.toString.call(t).slice(8,-1);return"Object"===r&&t.constructor&&(r=t.constructor.name),"Map"===r||"Set"===r?Array.from(t):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?f(t,void 0):void 0}}(t))||e&&t&&"number"==typeof t.length){r&&(t=r);var n=0;return function(){return n>=t.length?{done:!0}:{done:!1,value:t[n++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var h=e.createContext(null),v=function(){var t=s(u().mark((function t(){return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,o.getItem("apiRequests");case 3:return t.abrupt("return",t.sent);case 6:return t.prev=6,t.t0=t.catch(0),console.error("Error getting stored API requests:",t.t0),t.abrupt("return",[]);case 10:case"end":return t.stop()}}),t,null,[[0,6]])})));return function(){return t.apply(this,arguments)}}(),d=function(){var t=s(u().mark((function t(e){return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,o.setItem("apiRequests",e);case 3:t.next=8;break;case 5:t.prev=5,t.t0=t.catch(0),console.error("Error setting store :",t.t0);case 8:case"end":return t.stop()}}),t,null,[[0,5]])})));return function(e){return t.apply(this,arguments)}}(),y=function(){var t=s(u().mark((function t(){return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,o.removeItem("apiRequests");case 3:t.next=8;break;case 5:t.prev=5,t.t0=t.catch(0),console.error("Error clearing stored API requests:",t.t0);case 8:case"end":return t.stop()}}),t,null,[[0,5]])})));return function(){return t.apply(this,arguments)}}();function g(t){return new Promise((function(e,r){try{var n={};t.forEach((function(t,e){Object.hasOwnProperty.call(n,e)?(Array.isArray(n[e])||(n[e]=[n[e]]),n[e].push(t)):n[e]=t})),e(n)}catch(t){r(t)}}))}function m(t){var e=new FormData,r=function(r){if(Object.hasOwnProperty.call(t,r)){var n=t[r];Array.isArray(n)?n.forEach((function(t){return e.append(r,t)})):e.append(r,n)}};for(var n in t)r(n);return e.forEach((function(t,e){console.log("formdata",e+" "+t)})),e}var w=function(){var t=e.useState(null),r=t[0],n=t[1];return e.useEffect((function(){var t=!0,e=function(){var e=s(u().mark((function e(){var r;return u().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,i();case 2:r=e.sent,t&&n(r);case 4:case"end":return e.stop()}}),e)})));return function(){return e.apply(this,arguments)}}();e();var r=setInterval(e,1e4);return function(){t=!1,clearInterval(r)}}),[]),r},x=n.create();exports.OfflineSyncProvider=function(t){var n,i,c=t.children,f=t.render,b=t.onStatusChange,E=t.onCallback,k=e.useState({}),O=k[0],L=k[1],S=e.useState(null==(n=null==(i=window)||null==(i=i.navigator)?void 0:i.onLine)||n),j=S[0],q=S[1],P=w();e.useEffect((function(){null!==P&&(console.log("Network status:",P?"Online":"Offline"),P?I():_())}),[P]);var I=e.useCallback((function(){A(!1),N()}),[]),_=function(){A(!0)},A=function(t){void 0===t&&(t=!0);var e=!t;null==b||b({isOnline:e}),q(e)};e.useEffect((function(){N()}),[]);var C=function(){var t=s(u().mark((function t(e){var r;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,o.getItem("apiRequests");case 3:if(t.t0=t.sent,t.t0){t.next=6;break}t.t0=[];case 6:if(r=t.t0,console.log("perform stored",{req:r,length:r.length}),!(null!=e&&e.isFormdata&&(null==e?void 0:e.data)instanceof FormData)){t.next=15;break}return t.next=11,g(e.data);case 11:r.push(a.omit(l({},e,{data:t.sent}),"onSuccess")),t.next=17;break;case 15:console.log("Saving request normally"),r.push(a.omit(l({},e),"onSuccess"));case 17:return console.log("perform forage after:",{storedRequests:r}),t.next=20,o.setItem("apiRequests",r);case 20:console.log("perform forage:",{result:t.sent}),t.next=27;break;case 24:t.prev=24,t.t1=t.catch(0),console.error("Error saving API request for offline:",t.t1);case 27:case"end":return t.stop()}}),t,null,[[0,24]])})));return function(e){return t.apply(this,arguments)}}(),F=function(){var t=s(u().mark((function t(e){var r,n;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(t.prev=0,null==e||!e.isFormdata||(null==e?void 0:e.data)instanceof FormData){t.next=8;break}return n=l({},e,{data:m(e.data)}),t.next=5,x.request(n);case 5:r=t.sent,t.next=11;break;case 8:return t.next=10,x.request(e);case 10:r=t.sent;case 11:return E&&E({config:e,data:r,sendRequest:R}),t.abrupt("return",r.data);case 15:if(t.prev=15,t.t0=t.catch(0),console.log("packageError",{error:t.t0}),!(e.retryCount<1)){t.next=25;break}return t.next=21,new Promise((function(t){return setTimeout(t,1e3)}));case 21:return e.retryCount++,t.abrupt("return",F(e));case 25:return t.next=27,C(e);case 27:return t.abrupt("return",t.t0);case 28:case"end":return t.stop()}}),t,null,[[0,15]])})));return function(e){return t.apply(this,arguments)}}(),R=function(){var t=s(u().mark((function t(e){var r;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,e.retryCount=null!=(r=e.retryCount)?r:0,e.id=String(Date.now().toString(32)+Math.random().toString(16)).replace(/\./g,""),t.next=5,F(e);case 5:return t.abrupt("return",t.sent);case 8:throw t.prev=8,t.t0=t.catch(0),t.t0;case 11:case"end":return t.stop()}}),t,null,[[0,8]])})));return function(e){return t.apply(this,arguments)}}(),N=function(){var t=s(u().mark((function t(){var e,r,n,a,i;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,v();case 2:if((e=t.sent)&&0!==e.length){t.next=5;break}return t.abrupt("return");case 5:r=[].concat(e),n=u().mark((function t(){var e,n;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(!(e=i.value)){t.next=16;break}return t.prev=2,t.next=5,F(e);case 5:n=r.filter((function(t){return t.id!==e.id})),r.splice.apply(r,[0,r.length].concat(n)),t.next=12;break;case 9:t.prev=9,t.t0=t.catch(2),console.log({error:t.t0});case 12:return t.prev=12,t.next=15,o.setItem("apiRequests",r);case 15:return t.finish(12);case 16:case"end":return t.stop()}}),t,null,[[2,9,12,16]])})),a=p(e);case 8:if((i=a()).done){t.next=12;break}return t.delegateYield(n(),"t0",10);case 10:t.next=8;break;case 12:case"end":return t.stop()}}),t)})));return function(){return t.apply(this,arguments)}}();return r.createElement(r.Fragment,null,r.createElement(h.Provider,{value:{data:O,setData:L,sendRequest:R,clearStoredRequests:y,getStoredRequests:v,setStorage:d}},null==f?void 0:f({isOnline:j}),c))},exports.useOfflineSyncContext=function(){return e.useContext(h)}; | ||
"use strict";function t(t){return t&&"object"==typeof t&&"default"in t?t.default:t}Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react"),r=t(e),n=t(require("axios")),o=t(require("localforage")),a=require("underscore"),i=t(require("is-online"));function u(){u=function(){return t};var t={},e=Object.prototype,r=e.hasOwnProperty,n=Object.defineProperty||function(t,e,r){t[e]=r.value},o="function"==typeof Symbol?Symbol:{},a=o.iterator||"@@iterator",i=o.asyncIterator||"@@asyncIterator",c=o.toStringTag||"@@toStringTag";function s(t,e,r){return Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}),t[e]}try{s({},"")}catch(t){s=function(t,e,r){return t[e]=r}}function l(t,e,r,o){var a=Object.create((e&&e.prototype instanceof h?e:h).prototype),i=new S(o||[]);return n(a,"_invoke",{value:E(t,r,i)}),a}function f(t,e,r){try{return{type:"normal",arg:t.call(e,r)}}catch(t){return{type:"throw",arg:t}}}t.wrap=l;var p={};function h(){}function v(){}function d(){}var y={};s(y,a,(function(){return this}));var g=Object.getPrototypeOf,m=g&&g(g(j([])));m&&m!==e&&r.call(m,a)&&(y=m);var w=d.prototype=h.prototype=Object.create(y);function x(t){["next","throw","return"].forEach((function(e){s(t,e,(function(t){return this._invoke(e,t)}))}))}function b(t,e){var o;n(this,"_invoke",{value:function(n,a){function i(){return new e((function(o,i){!function n(o,a,i,u){var c=f(t[o],t,a);if("throw"!==c.type){var s=c.arg,l=s.value;return l&&"object"==typeof l&&r.call(l,"__await")?e.resolve(l.__await).then((function(t){n("next",t,i,u)}),(function(t){n("throw",t,i,u)})):e.resolve(l).then((function(t){s.value=t,i(s)}),(function(t){return n("throw",t,i,u)}))}u(c.arg)}(n,a,o,i)}))}return o=o?o.then(i,i):i()}})}function E(t,e,r){var n="suspendedStart";return function(o,a){if("executing"===n)throw new Error("Generator is already running");if("completed"===n){if("throw"===o)throw a;return{value:void 0,done:!0}}for(r.method=o,r.arg=a;;){var i=r.delegate;if(i){var u=k(i,r);if(u){if(u===p)continue;return u}}if("next"===r.method)r.sent=r._sent=r.arg;else if("throw"===r.method){if("suspendedStart"===n)throw n="completed",r.arg;r.dispatchException(r.arg)}else"return"===r.method&&r.abrupt("return",r.arg);n="executing";var c=f(t,e,r);if("normal"===c.type){if(n=r.done?"completed":"suspendedYield",c.arg===p)continue;return{value:c.arg,done:r.done}}"throw"===c.type&&(n="completed",r.method="throw",r.arg=c.arg)}}}function k(t,e){var r=e.method,n=t.iterator[r];if(void 0===n)return e.delegate=null,"throw"===r&&t.iterator.return&&(e.method="return",e.arg=void 0,k(t,e),"throw"===e.method)||"return"!==r&&(e.method="throw",e.arg=new TypeError("The iterator does not provide a '"+r+"' method")),p;var o=f(n,t.iterator,e.arg);if("throw"===o.type)return e.method="throw",e.arg=o.arg,e.delegate=null,p;var a=o.arg;return a?a.done?(e[t.resultName]=a.value,e.next=t.nextLoc,"return"!==e.method&&(e.method="next",e.arg=void 0),e.delegate=null,p):a:(e.method="throw",e.arg=new TypeError("iterator result is not an object"),e.delegate=null,p)}function O(t){var e={tryLoc:t[0]};1 in t&&(e.catchLoc=t[1]),2 in t&&(e.finallyLoc=t[2],e.afterLoc=t[3]),this.tryEntries.push(e)}function L(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function S(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(O,this),this.reset(!0)}function j(t){if(t){var e=t[a];if(e)return e.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var n=-1,o=function e(){for(;++n<t.length;)if(r.call(t,n))return e.value=t[n],e.done=!1,e;return e.value=void 0,e.done=!0,e};return o.next=o}}return{next:q}}function q(){return{value:void 0,done:!0}}return v.prototype=d,n(w,"constructor",{value:d,configurable:!0}),n(d,"constructor",{value:v,configurable:!0}),v.displayName=s(d,c,"GeneratorFunction"),t.isGeneratorFunction=function(t){var e="function"==typeof t&&t.constructor;return!!e&&(e===v||"GeneratorFunction"===(e.displayName||e.name))},t.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,d):(t.__proto__=d,s(t,c,"GeneratorFunction")),t.prototype=Object.create(w),t},t.awrap=function(t){return{__await:t}},x(b.prototype),s(b.prototype,i,(function(){return this})),t.AsyncIterator=b,t.async=function(e,r,n,o,a){void 0===a&&(a=Promise);var i=new b(l(e,r,n,o),a);return t.isGeneratorFunction(r)?i:i.next().then((function(t){return t.done?t.value:i.next()}))},x(w),s(w,c,"Generator"),s(w,a,(function(){return this})),s(w,"toString",(function(){return"[object Generator]"})),t.keys=function(t){var e=Object(t),r=[];for(var n in e)r.push(n);return r.reverse(),function t(){for(;r.length;){var n=r.pop();if(n in e)return t.value=n,t.done=!1,t}return t.done=!0,t}},t.values=j,S.prototype={constructor:S,reset:function(t){if(this.prev=0,this.next=0,this.sent=this._sent=void 0,this.done=!1,this.delegate=null,this.method="next",this.arg=void 0,this.tryEntries.forEach(L),!t)for(var e in this)"t"===e.charAt(0)&&r.call(this,e)&&!isNaN(+e.slice(1))&&(this[e]=void 0)},stop:function(){this.done=!0;var t=this.tryEntries[0].completion;if("throw"===t.type)throw t.arg;return this.rval},dispatchException:function(t){if(this.done)throw t;var e=this;function n(r,n){return i.type="throw",i.arg=t,e.next=r,n&&(e.method="next",e.arg=void 0),!!n}for(var o=this.tryEntries.length-1;o>=0;--o){var a=this.tryEntries[o],i=a.completion;if("root"===a.tryLoc)return n("end");if(a.tryLoc<=this.prev){var u=r.call(a,"catchLoc"),c=r.call(a,"finallyLoc");if(u&&c){if(this.prev<a.catchLoc)return n(a.catchLoc,!0);if(this.prev<a.finallyLoc)return n(a.finallyLoc)}else if(u){if(this.prev<a.catchLoc)return n(a.catchLoc,!0)}else{if(!c)throw new Error("try statement without catch or finally");if(this.prev<a.finallyLoc)return n(a.finallyLoc)}}}},abrupt:function(t,e){for(var n=this.tryEntries.length-1;n>=0;--n){var o=this.tryEntries[n];if(o.tryLoc<=this.prev&&r.call(o,"finallyLoc")&&this.prev<o.finallyLoc){var a=o;break}}a&&("break"===t||"continue"===t)&&a.tryLoc<=e&&e<=a.finallyLoc&&(a=null);var i=a?a.completion:{};return i.type=t,i.arg=e,a?(this.method="next",this.next=a.finallyLoc,p):this.complete(i)},complete:function(t,e){if("throw"===t.type)throw t.arg;return"break"===t.type||"continue"===t.type?this.next=t.arg:"return"===t.type?(this.rval=this.arg=t.arg,this.method="return",this.next="end"):"normal"===t.type&&e&&(this.next=e),p},finish:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.finallyLoc===t)return this.complete(r.completion,r.afterLoc),L(r),p}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.tryLoc===t){var n=r.completion;if("throw"===n.type){var o=n.arg;L(r)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,r){return this.delegate={iterator:j(t),resultName:e,nextLoc:r},"next"===this.method&&(this.arg=void 0),p}},t}function c(t,e,r,n,o,a,i){try{var u=t[a](i),c=u.value}catch(t){return void r(t)}u.done?e(c):Promise.resolve(c).then(n,o)}function s(t){return function(){var e=this,r=arguments;return new Promise((function(n,o){var a=t.apply(e,r);function i(t){c(a,n,o,i,u,"next",t)}function u(t){c(a,n,o,i,u,"throw",t)}i(void 0)}))}}function l(){return(l=Object.assign?Object.assign.bind():function(t){for(var e=1;e<arguments.length;e++){var r=arguments[e];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(t[n]=r[n])}return t}).apply(this,arguments)}function f(t,e){(null==e||e>t.length)&&(e=t.length);for(var r=0,n=new Array(e);r<e;r++)n[r]=t[r];return n}function p(t,e){var r="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(r)return(r=r.call(t)).next.bind(r);if(Array.isArray(t)||(r=function(t,e){if(t){if("string"==typeof t)return f(t,void 0);var r=Object.prototype.toString.call(t).slice(8,-1);return"Object"===r&&t.constructor&&(r=t.constructor.name),"Map"===r||"Set"===r?Array.from(t):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?f(t,void 0):void 0}}(t))||e&&t&&"number"==typeof t.length){r&&(t=r);var n=0;return function(){return n>=t.length?{done:!0}:{done:!1,value:t[n++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var h=e.createContext(null),v=function(){var t=s(u().mark((function t(){return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,o.getItem("apiRequests");case 3:return t.abrupt("return",t.sent);case 6:return t.prev=6,t.t0=t.catch(0),console.error("Error getting stored API requests:",t.t0),t.abrupt("return",[]);case 10:case"end":return t.stop()}}),t,null,[[0,6]])})));return function(){return t.apply(this,arguments)}}(),d=function(){var t=s(u().mark((function t(e){return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,o.setItem("apiRequests",e);case 3:t.next=8;break;case 5:t.prev=5,t.t0=t.catch(0),console.error("Error setting store :",t.t0);case 8:case"end":return t.stop()}}),t,null,[[0,5]])})));return function(e){return t.apply(this,arguments)}}(),y=function(){var t=s(u().mark((function t(){return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,o.removeItem("apiRequests");case 3:t.next=8;break;case 5:t.prev=5,t.t0=t.catch(0),console.error("Error clearing stored API requests:",t.t0);case 8:case"end":return t.stop()}}),t,null,[[0,5]])})));return function(){return t.apply(this,arguments)}}();function g(t){return new Promise((function(e,r){try{var n={};t.forEach((function(t,e){Object.hasOwnProperty.call(n,e)?(Array.isArray(n[e])||(n[e]=[n[e]]),n[e].push(t)):n[e]=t})),e(n)}catch(t){r(t)}}))}function m(t){var e=new FormData,r=function(r){if(Object.hasOwnProperty.call(t,r)){var n=t[r];Array.isArray(n)?n.forEach((function(t){return e.append(r,t)})):e.append(r,n)}};for(var n in t)r(n);return e.forEach((function(t,e){console.log("formdata",e+" "+t)})),e}var w=function(){var t=e.useState(null),r=t[0],n=t[1];return e.useEffect((function(){var t,e=function(){var t=s(u().mark((function t(){return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,i();case 2:n(t.sent);case 4:case"end":return t.stop()}}),t)})));return function(){return t.apply(this,arguments)}}();return t=setInterval(e,1e4),e(),function(){return clearInterval(t)}}),[]),r},x=n.create();exports.OfflineSyncProvider=function(t){var n,i,c=t.children,f=t.render,b=t.onStatusChange,E=t.onCallback,k=e.useState({}),O=k[0],L=k[1],S=e.useState(null==(n=null==(i=window)||null==(i=i.navigator)?void 0:i.onLine)||n),j=S[0],q=S[1],P=w();e.useEffect((function(){null!==P&&(console.log("Network status:",P?"Online":"Offline"),P?I():_())}),[P]);var I=e.useCallback((function(){A(!1),N()}),[]),_=function(){A(!0)},A=function(t){void 0===t&&(t=!0);var e=!t;null==b||b({isOnline:e}),q(e)};e.useEffect((function(){N()}),[]);var C=function(){var t=s(u().mark((function t(e){var r;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,o.getItem("apiRequests");case 3:if(t.t0=t.sent,t.t0){t.next=6;break}t.t0=[];case 6:if(r=t.t0,console.log("perform stored",{req:r,length:r.length}),!(null!=e&&e.isFormdata&&(null==e?void 0:e.data)instanceof FormData)){t.next=15;break}return t.next=11,g(e.data);case 11:r.push(a.omit(l({},e,{data:t.sent}),"onSuccess")),t.next=17;break;case 15:console.log("Saving request normally"),r.push(a.omit(l({},e),"onSuccess"));case 17:return console.log("perform forage after:",{storedRequests:r}),t.next=20,o.setItem("apiRequests",r);case 20:console.log("perform forage:",{result:t.sent}),t.next=27;break;case 24:t.prev=24,t.t1=t.catch(0),console.error("Error saving API request for offline:",t.t1);case 27:case"end":return t.stop()}}),t,null,[[0,24]])})));return function(e){return t.apply(this,arguments)}}(),F=function(){var t=s(u().mark((function t(e){var r,n;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(t.prev=0,null==e||!e.isFormdata||(null==e?void 0:e.data)instanceof FormData){t.next=8;break}return n=l({},e,{data:m(e.data)}),t.next=5,x.request(n);case 5:r=t.sent,t.next=11;break;case 8:return t.next=10,x.request(e);case 10:r=t.sent;case 11:return E&&E({config:e,data:r,sendRequest:R}),t.abrupt("return",r.data);case 15:if(t.prev=15,t.t0=t.catch(0),console.log("packageError",{error:t.t0}),!(e.retryCount<1)){t.next=25;break}return t.next=21,new Promise((function(t){return setTimeout(t,1e3)}));case 21:return e.retryCount++,t.abrupt("return",F(e));case 25:return t.next=27,C(e);case 27:return t.abrupt("return",t.t0);case 28:case"end":return t.stop()}}),t,null,[[0,15]])})));return function(e){return t.apply(this,arguments)}}(),R=function(){var t=s(u().mark((function t(e){var r;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,e.retryCount=null!=(r=e.retryCount)?r:0,e.id=String(Date.now().toString(32)+Math.random().toString(16)).replace(/\./g,""),t.next=5,F(e);case 5:return t.abrupt("return",t.sent);case 8:throw t.prev=8,t.t0=t.catch(0),t.t0;case 11:case"end":return t.stop()}}),t,null,[[0,8]])})));return function(e){return t.apply(this,arguments)}}(),N=function(){var t=s(u().mark((function t(){var e,r,n,a,i;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,v();case 2:if((e=t.sent)&&0!==e.length){t.next=5;break}return t.abrupt("return");case 5:r=[].concat(e),n=u().mark((function t(){var e,n;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(!(e=i.value)){t.next=16;break}return t.prev=2,t.next=5,F(e);case 5:n=r.filter((function(t){return t.id!==e.id})),r.splice.apply(r,[0,r.length].concat(n)),t.next=12;break;case 9:t.prev=9,t.t0=t.catch(2),console.log({error:t.t0});case 12:return t.prev=12,t.next=15,o.setItem("apiRequests",r);case 15:return t.finish(12);case 16:case"end":return t.stop()}}),t,null,[[2,9,12,16]])})),a=p(e);case 8:if((i=a()).done){t.next=12;break}return t.delegateYield(n(),"t0",10);case 10:t.next=8;break;case 12:case"end":return t.stop()}}),t)})));return function(){return t.apply(this,arguments)}}();return r.createElement(r.Fragment,null,r.createElement(h.Provider,{value:{data:O,setData:L,sendRequest:R,clearStoredRequests:y,getStoredRequests:v,setStorage:d}},null==f?void 0:f({isOnline:j}),c))},exports.useOfflineSyncContext=function(){return e.useContext(h)}; | ||
//# sourceMappingURL=offline-sync-handler-test.cjs.production.min.js.map |
@@ -519,4 +519,5 @@ import React, { createContext, useState, useEffect, useCallback, useContext } from 'react'; | ||
useEffect(function () { | ||
var isMounted = true; | ||
var checkConnection = /*#__PURE__*/function () { | ||
//@ts-ignore | ||
var intervalId; | ||
var checkConnectivity = /*#__PURE__*/function () { | ||
var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() { | ||
@@ -531,5 +532,3 @@ var onlineStatus; | ||
onlineStatus = _context.sent; | ||
if (isMounted) { | ||
setIsConnected(onlineStatus); | ||
} | ||
setIsConnected(onlineStatus); | ||
case 4: | ||
@@ -541,13 +540,12 @@ case "end": | ||
})); | ||
return function checkConnection() { | ||
return function checkConnectivity() { | ||
return _ref.apply(this, arguments); | ||
}; | ||
}(); | ||
intervalId = setInterval(checkConnectivity, 10000); // Check every 10 seconds | ||
// Initial check | ||
checkConnection(); | ||
// Interval to check periodically | ||
var interval = setInterval(checkConnection, 10000); // every 10 seconds | ||
checkConnectivity(); | ||
//@ts-ignore | ||
return function () { | ||
isMounted = false; | ||
clearInterval(interval); | ||
return clearInterval(intervalId); | ||
}; | ||
@@ -554,0 +552,0 @@ }, []); |
{ | ||
"version": "0.1.80", | ||
"version": "0.1.81", | ||
"license": "MIT", | ||
@@ -4,0 +4,0 @@ "main": "dist/index.js", |
@@ -5,24 +5,19 @@ import { useState, useEffect } from 'react'; | ||
const useNetworkStatus = () => { | ||
const [isConnected, setIsConnected] = useState<boolean|null>(null); | ||
const [isConnected, setIsConnected] = useState<null|boolean>(null); | ||
useEffect(() => { | ||
let isMounted = true; | ||
//@ts-ignore | ||
let intervalId; | ||
const checkConnection = async () => { | ||
const checkConnectivity = async () => { | ||
const onlineStatus = await isOnline(); | ||
if (isMounted) { | ||
setIsConnected(onlineStatus); | ||
} | ||
setIsConnected(onlineStatus); | ||
}; | ||
intervalId = setInterval(checkConnectivity, 10000); // Check every 10 seconds | ||
// Initial check | ||
checkConnection(); | ||
// Interval to check periodically | ||
const interval = setInterval(checkConnection, 10000); // every 10 seconds | ||
return () => { | ||
isMounted = false; | ||
clearInterval(interval); | ||
}; | ||
checkConnectivity(); | ||
//@ts-ignore | ||
return () => clearInterval(intervalId); | ||
}, []); | ||
@@ -29,0 +24,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
165692
2158