New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

offline-sync-handler-test

Package Overview
Dependencies
Maintainers
1
Versions
81
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

offline-sync-handler-test - npm Package Compare versions

Comparing version 0.1.21 to 0.1.22

56

dist/offline-sync-handler-test.cjs.development.js

@@ -401,16 +401,9 @@ 'use strict';

});
console.log("pckg:", {
callbackName: callbackName
});
if (index !== -1) {
var callback = callbacks[index].callback;
console.log("pckg:", {
callback: callback
});
callback(data);
callbacks.splice(index, 1); // Remove the triggered callback from the list
} else {
console.log("Callback '" + callbackName + "' not found.");
}
}
function addCallBack(_ref2) {

@@ -433,3 +426,3 @@ var name = _ref2.name,

var RETRY_LIMIT = 3;
var RETRY_DELAY_MS = 5000;
var RETRY_DELAY_MS = 1000;
var API_REQUESTS_STORAGE_KEY = 'apiRequests';

@@ -509,2 +502,27 @@ var api = /*#__PURE__*/axios.create();

}();
var clearStoredRequests = /*#__PURE__*/function () {
var _ref3 = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3() {
return _regeneratorRuntime().wrap(function _callee3$(_context3) {
while (1) switch (_context3.prev = _context3.next) {
case 0:
_context3.prev = 0;
_context3.next = 3;
return localForage.removeItem(API_REQUESTS_STORAGE_KEY);
case 3:
_context3.next = 8;
break;
case 5:
_context3.prev = 5;
_context3.t0 = _context3["catch"](0);
console.error('Error clearing stored API requests:', _context3.t0);
case 8:
case "end":
return _context3.stop();
}
}, _callee3, null, [[0, 5]]);
}));
return function clearStoredRequests() {
return _ref3.apply(this, arguments);
};
}();
// Function to perform the actual API request and handle retries

@@ -567,15 +585,18 @@ var performRequest = /*#__PURE__*/function () {

// Perform the API request and handle retries
_context5.next = 5;
if (!navigator.onLine) {
reactToastify.toast.info('You are currently offline. We will automatically resend the request when you are back online.');
}
_context5.next = 6;
return performRequest(config);
case 5:
case 6:
return _context5.abrupt("return", _context5.sent);
case 8:
_context5.prev = 8;
case 9:
_context5.prev = 9;
_context5.t0 = _context5["catch"](0);
throw _context5.t0;
case 11:
case 12:
case "end":
return _context5.stop();
}
}, _callee5, null, [[0, 8]]);
}, _callee5, null, [[0, 9]]);
}));

@@ -602,3 +623,3 @@ return function sendRequest(_x3) {

case 5:
reactToastify.toast.info("Syncing data with server");
reactToastify.toast.info("We're online! Your requests will sync with the server now");
_loop = /*#__PURE__*/_regeneratorRuntime().mark(function _loop() {

@@ -713,3 +734,4 @@ var request, _storedRequests$find;

setData: setData,
sendRequest: sendRequest
sendRequest: sendRequest,
clearStoredRequests: clearStoredRequests
}

@@ -716,0 +738,0 @@ }, render == null ? void 0 : render({

@@ -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=require("react-toastify");require("react-toastify/dist/ReactToastify.css");var o=t(require("axios")),a=t(require("localforage")),i=require("underscore"),c=e.createContext(null);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 p?e:p).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 h={};function p(){}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=p.prototype=Object.create(y);function b(t){["next","throw","return"].forEach((function(e){s(t,e,(function(t){return this._invoke(e,t)}))}))}function x(t,e){var o;n(this,"_invoke",{value:function(n,a){function i(){return new e((function(o,i){!function n(o,a,i,c){var u=f(t[o],t,a);if("throw"!==u.type){var s=u.arg,l=s.value;return l&&"object"==typeof l&&r.call(l,"__await")?e.resolve(l.__await).then((function(t){n("next",t,i,c)}),(function(t){n("throw",t,i,c)})):e.resolve(l).then((function(t){s.value=t,i(s)}),(function(t){return n("throw",t,i,c)}))}c(u.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 c=k(i,r);if(c){if(c===h)continue;return c}}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 u=f(t,e,r);if("normal"===u.type){if(n=r.done?"completed":"suspendedYield",u.arg===h)continue;return{value:u.arg,done:r.done}}"throw"===u.type&&(n="completed",r.method="throw",r.arg=u.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")),h;var o=f(n,t.iterator,e.arg);if("throw"===o.type)return e.method="throw",e.arg=o.arg,e.delegate=null,h;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,h):a:(e.method="throw",e.arg=new TypeError("iterator result is not an object"),e.delegate=null,h)}function L(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 O(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function S(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(L,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:P}}function P(){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}},b(x.prototype),s(x.prototype,i,(function(){return this})),t.AsyncIterator=x,t.async=function(e,r,n,o,a){void 0===a&&(a=Promise);var i=new x(l(e,r,n,o),a);return t.isGeneratorFunction(r)?i:i.next().then((function(t){return t.done?t.value:i.next()}))},b(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(O),!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 c=r.call(a,"catchLoc"),u=r.call(a,"finallyLoc");if(c&&u){if(this.prev<a.catchLoc)return n(a.catchLoc,!0);if(this.prev<a.finallyLoc)return n(a.finallyLoc)}else if(c){if(this.prev<a.catchLoc)return n(a.catchLoc,!0)}else{if(!u)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,h):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),h},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),O(r),h}},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;O(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),h}},t}function s(t,e,r,n,o,a,i){try{var c=t[a](i),u=c.value}catch(t){return void r(t)}c.done?e(u):Promise.resolve(u).then(n,o)}function l(t){return function(){var e=this,r=arguments;return new Promise((function(n,o){var a=t.apply(e,r);function i(t){s(a,n,o,i,c,"next",t)}function c(t){s(a,n,o,i,c,"throw",t)}i(void 0)}))}}function f(){return(f=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 h(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 h(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)?h(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 v=[];function d(t){var e=t.name,r=t.data,n=v.findIndex((function(t){return t.name===e}));if(console.log("pckg:",{callbackName:e}),-1!==n){var o=v[n].callback;console.log("pckg:",{callback:o}),o(r),v.splice(n,1)}else console.log("Callback '"+e+"' not found.")}function y(t){var e=t.name,r=t.callbackFn;-1===v.findIndex((function(t){return t.name===e}))&&v.push({name:e,callback:r})}var g=o.create(),m=function(){var t=l(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,a.getItem("apiRequests");case 3:if(t.t0=t.sent,t.t0){t.next=6;break}t.t0=[];case 6:return(r=t.t0).push(i.omit(f({},e),"onSuccess")),y({name:e.id,callbackFn:e.onSuccess}),t.next=11,a.setItem("apiRequests",r);case 11:t.next=16;break;case 13:t.prev=13,t.t1=t.catch(0),console.error("Error saving API request for offline:",t.t1);case 16:case"end":return t.stop()}}),t,null,[[0,13]])})));return function(e){return t.apply(this,arguments)}}(),w=function(){var t=l(u().mark((function t(){return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,a.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)}}(),b=function(){var t=l(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,g.request(e);case 3:return d({name:e.id,data:r=t.sent}),t.abrupt("return",r.data);case 8:if(t.prev=8,t.t0=t.catch(0),!(e.retryCount<3)){t.next=17;break}return t.next=13,new Promise((function(t){return setTimeout(t,5e3)}));case 13:return e.retryCount++,t.abrupt("return",b(e));case 17:return t.next=19,m(e);case 19:throw new Error("Exceeded retry limit, request saved for offline sync.");case 20:case"end":return t.stop()}}),t,null,[[0,8]])})));return function(e){return t.apply(this,arguments)}}(),x=function(){var t=l(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,b(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)}}(),E=function(){var t=l(u().mark((function t(){var e,r,o,i;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,w();case 2:if((e=t.sent)&&0!==e.length){t.next=5;break}return t.abrupt("return");case 5:n.toast.info("Syncing data with server"),r=u().mark((function t(){var r,n;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(!(r=i.value)){t.next=13;break}return t.prev=2,t.next=5,b(r);case 5:return e.splice(e.indexOf(r),null==(n=e.find((function(t){return t.id===r.id})))?void 0:n.length),t.next=8,a.setItem("apiRequests",e);case 8:t.next=13;break;case 10:t.prev=10,t.t0=t.catch(2),console.log("venom: perform request",{error:t.t0});case 13:case"end":return t.stop()}}),t,null,[[2,10]])})),o=p(e);case 8:if((i=o()).done){t.next=12;break}return t.delegateYield(r(),"t0",10);case 10:t.next=8;break;case 12:case"end":return t.stop()}}),t)})));return function(){return t.apply(this,arguments)}}();exports.OfflineSyncProvider=function(t){var o,a=t.children,i=t.render,u=t.onStatusChange,s=t.toastConfig,l=e.useState({}),f=l[0],h=l[1],p=e.useState(null==(o=window.navigator.onLine)||o),v=p[0],d=p[1];e.useEffect((function(){if("undefined"!=typeof window)return window.addEventListener("online",y),window.addEventListener("offline",g),function(){window.removeEventListener("offline",g),window.removeEventListener("online",y)}}),[]);var y=e.useCallback((function(){m(!1),E()}),[]),g=function(){m(!0)},m=function(t){void 0===t&&(t=!0);var e=!t;null==u||u({isOnline:e}),d(e)};return r.createElement(r.Fragment,null,r.createElement(c.Provider,{value:{data:f,setData:h,sendRequest:x}},null==i?void 0:i({isOnline:v}),a),r.createElement(n.ToastContainer,Object.assign({position:"top-right",autoClose:5e3,hideProgressBar:!1,newestOnTop:!0,closeOnClick:!0,rtl:!1,pauseOnFocusLoss:!0,draggable:!0,pauseOnHover:!0,theme:"dark"},s)))},exports.useOfflineSyncContext=function(){return e.useContext(c)};
"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=require("react-toastify");require("react-toastify/dist/ReactToastify.css");var o=t(require("axios")),a=t(require("localforage")),i=require("underscore"),u=e.createContext(null);function c(){c=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",u=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 p?e:p).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 h={};function p(){}function v(){}function d(){}var y={};s(y,a,(function(){return this}));var m=Object.getPrototypeOf,g=m&&m(m(j([])));g&&g!==e&&r.call(g,a)&&(y=g);var w=d.prototype=p.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=L(i,r);if(u){if(u===h)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===h)continue;return{value:c.arg,done:r.done}}"throw"===c.type&&(n="completed",r.method="throw",r.arg=c.arg)}}}function L(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,L(t,e),"throw"===e.method)||"return"!==r&&(e.method="throw",e.arg=new TypeError("The iterator does not provide a '"+r+"' method")),h;var o=f(n,t.iterator,e.arg);if("throw"===o.type)return e.method="throw",e.arg=o.arg,e.delegate=null,h;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,h):a:(e.method="throw",e.arg=new TypeError("iterator result is not an object"),e.delegate=null,h)}function k(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 O(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function S(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(k,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,u,"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,u,"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,u,"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(O),!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,h):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),h},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),O(r),h}},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;O(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),h}},t}function s(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 l(t){return function(){var e=this,r=arguments;return new Promise((function(n,o){var a=t.apply(e,r);function i(t){s(a,n,o,i,u,"next",t)}function u(t){s(a,n,o,i,u,"throw",t)}i(void 0)}))}}function f(){return(f=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 h(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 h(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)?h(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 v=[];function d(t){var e=t.name,r=t.data,n=v.findIndex((function(t){return t.name===e}));-1!==n&&((0,v[n].callback)(r),v.splice(n,1))}function y(t){var e=t.name,r=t.callbackFn;-1===v.findIndex((function(t){return t.name===e}))&&v.push({name:e,callback:r})}var m=o.create(),g=function(){var t=l(c().mark((function t(e){var r;return c().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,a.getItem("apiRequests");case 3:if(t.t0=t.sent,t.t0){t.next=6;break}t.t0=[];case 6:return(r=t.t0).push(i.omit(f({},e),"onSuccess")),y({name:e.id,callbackFn:e.onSuccess}),t.next=11,a.setItem("apiRequests",r);case 11:t.next=16;break;case 13:t.prev=13,t.t1=t.catch(0),console.error("Error saving API request for offline:",t.t1);case 16:case"end":return t.stop()}}),t,null,[[0,13]])})));return function(e){return t.apply(this,arguments)}}(),w=function(){var t=l(c().mark((function t(){return c().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,a.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)}}(),x=function(){var t=l(c().mark((function t(){return c().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,a.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)}}(),b=function(){var t=l(c().mark((function t(e){var r;return c().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,m.request(e);case 3:return d({name:e.id,data:r=t.sent}),t.abrupt("return",r.data);case 8:if(t.prev=8,t.t0=t.catch(0),!(e.retryCount<3)){t.next=17;break}return t.next=13,new Promise((function(t){return setTimeout(t,1e3)}));case 13:return e.retryCount++,t.abrupt("return",b(e));case 17:return t.next=19,g(e);case 19:throw new Error("Exceeded retry limit, request saved for offline sync.");case 20:case"end":return t.stop()}}),t,null,[[0,8]])})));return function(e){return t.apply(this,arguments)}}(),E=function(){var t=l(c().mark((function t(e){var r;return c().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,""),navigator.onLine||n.toast.info("You are currently offline. We will automatically resend the request when you are back online."),t.next=6,b(e);case 6:return t.abrupt("return",t.sent);case 9:throw t.prev=9,t.t0=t.catch(0),t.t0;case 12:case"end":return t.stop()}}),t,null,[[0,9]])})));return function(e){return t.apply(this,arguments)}}(),L=function(){var t=l(c().mark((function t(){var e,r,o,i;return c().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,w();case 2:if((e=t.sent)&&0!==e.length){t.next=5;break}return t.abrupt("return");case 5:n.toast.info("We're online! Your requests will sync with the server now"),r=c().mark((function t(){var r,n;return c().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(!(r=i.value)){t.next=13;break}return t.prev=2,t.next=5,b(r);case 5:return e.splice(e.indexOf(r),null==(n=e.find((function(t){return t.id===r.id})))?void 0:n.length),t.next=8,a.setItem("apiRequests",e);case 8:t.next=13;break;case 10:t.prev=10,t.t0=t.catch(2),console.log("venom: perform request",{error:t.t0});case 13:case"end":return t.stop()}}),t,null,[[2,10]])})),o=p(e);case 8:if((i=o()).done){t.next=12;break}return t.delegateYield(r(),"t0",10);case 10:t.next=8;break;case 12:case"end":return t.stop()}}),t)})));return function(){return t.apply(this,arguments)}}();exports.OfflineSyncProvider=function(t){var o,a=t.children,i=t.render,c=t.onStatusChange,s=t.toastConfig,l=e.useState({}),f=l[0],h=l[1],p=e.useState(null==(o=window.navigator.onLine)||o),v=p[0],d=p[1];e.useEffect((function(){if("undefined"!=typeof window)return window.addEventListener("online",y),window.addEventListener("offline",m),function(){window.removeEventListener("offline",m),window.removeEventListener("online",y)}}),[]);var y=e.useCallback((function(){g(!1),L()}),[]),m=function(){g(!0)},g=function(t){void 0===t&&(t=!0);var e=!t;null==c||c({isOnline:e}),d(e)};return r.createElement(r.Fragment,null,r.createElement(u.Provider,{value:{data:f,setData:h,sendRequest:E,clearStoredRequests:x}},null==i?void 0:i({isOnline:v}),a),r.createElement(n.ToastContainer,Object.assign({position:"top-right",autoClose:5e3,hideProgressBar:!1,newestOnTop:!0,closeOnClick:!0,rtl:!1,pauseOnFocusLoss:!0,draggable:!0,pauseOnHover:!0,theme:"dark"},s)))},exports.useOfflineSyncContext=function(){return e.useContext(u)};
//# sourceMappingURL=offline-sync-handler-test.cjs.production.min.js.map

@@ -394,16 +394,9 @@ import React, { createContext, useState, useEffect, useCallback, useContext } from 'react';

});
console.log("pckg:", {
callbackName: callbackName
});
if (index !== -1) {
var callback = callbacks[index].callback;
console.log("pckg:", {
callback: callback
});
callback(data);
callbacks.splice(index, 1); // Remove the triggered callback from the list
} else {
console.log("Callback '" + callbackName + "' not found.");
}
}
function addCallBack(_ref2) {

@@ -426,3 +419,3 @@ var name = _ref2.name,

var RETRY_LIMIT = 3;
var RETRY_DELAY_MS = 5000;
var RETRY_DELAY_MS = 1000;
var API_REQUESTS_STORAGE_KEY = 'apiRequests';

@@ -502,2 +495,27 @@ var api = /*#__PURE__*/axios.create();

}();
var clearStoredRequests = /*#__PURE__*/function () {
var _ref3 = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3() {
return _regeneratorRuntime().wrap(function _callee3$(_context3) {
while (1) switch (_context3.prev = _context3.next) {
case 0:
_context3.prev = 0;
_context3.next = 3;
return localForage.removeItem(API_REQUESTS_STORAGE_KEY);
case 3:
_context3.next = 8;
break;
case 5:
_context3.prev = 5;
_context3.t0 = _context3["catch"](0);
console.error('Error clearing stored API requests:', _context3.t0);
case 8:
case "end":
return _context3.stop();
}
}, _callee3, null, [[0, 5]]);
}));
return function clearStoredRequests() {
return _ref3.apply(this, arguments);
};
}();
// Function to perform the actual API request and handle retries

@@ -560,15 +578,18 @@ var performRequest = /*#__PURE__*/function () {

// Perform the API request and handle retries
_context5.next = 5;
if (!navigator.onLine) {
toast.info('You are currently offline. We will automatically resend the request when you are back online.');
}
_context5.next = 6;
return performRequest(config);
case 5:
case 6:
return _context5.abrupt("return", _context5.sent);
case 8:
_context5.prev = 8;
case 9:
_context5.prev = 9;
_context5.t0 = _context5["catch"](0);
throw _context5.t0;
case 11:
case 12:
case "end":
return _context5.stop();
}
}, _callee5, null, [[0, 8]]);
}, _callee5, null, [[0, 9]]);
}));

@@ -595,3 +616,3 @@ return function sendRequest(_x3) {

case 5:
toast.info("Syncing data with server");
toast.info("We're online! Your requests will sync with the server now");
_loop = /*#__PURE__*/_regeneratorRuntime().mark(function _loop() {

@@ -706,3 +727,4 @@ var request, _storedRequests$find;

setData: setData,
sendRequest: sendRequest
sendRequest: sendRequest,
clearStoredRequests: clearStoredRequests
}

@@ -709,0 +731,0 @@ }, render == null ? void 0 : render({

{
"version": "0.1.21",
"version": "0.1.22",
"license": "MIT",

@@ -4,0 +4,0 @@ "main": "dist/index.js",

@@ -8,3 +8,3 @@ import axios from 'axios';

const RETRY_LIMIT = 3;
const RETRY_DELAY_MS = 5000;
const RETRY_DELAY_MS = 1000;
const API_REQUESTS_STORAGE_KEY = 'apiRequests';

@@ -77,2 +77,5 @@

// Perform the API request and handle retries
if(!navigator.onLine){
toast.info('You are currently offline. We will automatically resend the request when you are back online.')
}
return await performRequest(config);

@@ -91,3 +94,3 @@ } catch (error) {

toast.info(`Syncing data with server`);
toast.info(`We're online! Your requests will sync with the server now`);
for (const request of storedRequests) {

@@ -94,0 +97,0 @@ if (request) {

@@ -6,11 +6,8 @@ const callbacks: Array<Function | any> = [];

const index = callbacks.findIndex(item => item.name === callbackName);
console.log("pckg:",{callbackName})
if (index !== -1) {
const { callback } = callbacks[index];
console.log("pckg:",{callback})
callback(data);
callbacks.splice(index, 1); // Remove the triggered callback from the list
} else {
console.log(`Callback '${callbackName}' not found.`);
}
}
}

@@ -17,0 +14,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

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc