func-cache
Advanced tools
Comparing version 2.2.0 to 2.2.2
@@ -367,2 +367,29 @@ 'use strict'; | ||
}, options.initialCache); | ||
var firstTimeDone = !!options.initialCache; | ||
var getData = /*#__PURE__*/function () { | ||
var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() { | ||
var _options$getCache; | ||
return _regeneratorRuntime().wrap(function _callee$(_context) { | ||
while (1) switch (_context.prev = _context.next) { | ||
case 0: | ||
_context.next = 2; | ||
return options.getCache == null ? void 0 : (_options$getCache = options.getCache()) == null ? void 0 : _options$getCache.then(function (data) { | ||
cached = _extends({}, data, cached); | ||
}); | ||
case 2: | ||
_context.t0 = _context.sent; | ||
if (_context.t0) { | ||
_context.next = 5; | ||
break; | ||
} | ||
case 5: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
}, _callee); | ||
})); | ||
return function getData() { | ||
return _ref.apply(this, arguments); | ||
}; | ||
}(); | ||
var updateData = options.onDataUpdate ? debounce(function () { | ||
@@ -382,68 +409,53 @@ try { | ||
}; | ||
var cache = function cache(getnewval, str) { | ||
var cachedFN = function cachedFN() { | ||
checkExpiry(); | ||
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
var str = args.join(","); | ||
if (str in cached) return cached[str]; | ||
cached[str] = getnewval(); | ||
var re = func.apply(void 0, args); | ||
//async | ||
if (options.async || !util.types.isPromise(re)) return new Promise( /*#__PURE__*/function () { | ||
var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(s, r) { | ||
var val; | ||
return _regeneratorRuntime().wrap(function _callee2$(_context2) { | ||
while (1) switch (_context2.prev = _context2.next) { | ||
case 0: | ||
_context2.prev = 0; | ||
if (firstTimeDone) { | ||
_context2.next = 5; | ||
break; | ||
} | ||
_context2.next = 4; | ||
return getData(); | ||
case 4: | ||
firstTimeDone = true; | ||
case 5: | ||
_context2.next = 7; | ||
return re; | ||
case 7: | ||
val = _context2.sent; | ||
cached[str] = val; | ||
updateData == null ? void 0 : updateData(); | ||
return _context2.abrupt("return", s(val)); | ||
case 13: | ||
_context2.prev = 13; | ||
_context2.t0 = _context2["catch"](0); | ||
r(_context2.t0); | ||
case 16: | ||
case "end": | ||
return _context2.stop(); | ||
} | ||
}, _callee2, null, [[0, 13]]); | ||
})); | ||
return function (_x, _x2) { | ||
return _ref2.apply(this, arguments); | ||
}; | ||
}()); | ||
// sync | ||
cached[str] = re; | ||
updateData == null ? void 0 : updateData(); | ||
return cached[str]; | ||
return re; | ||
}; | ||
var asyncCache = /*#__PURE__*/function () { | ||
var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(getnewval, str) { | ||
return _regeneratorRuntime().wrap(function _callee$(_context) { | ||
while (1) switch (_context.prev = _context.next) { | ||
case 0: | ||
checkExpiry(); | ||
if (!(str in cached)) { | ||
_context.next = 3; | ||
break; | ||
} | ||
return _context.abrupt("return", cached[str]); | ||
case 3: | ||
_context.next = 5; | ||
return getnewval(); | ||
case 5: | ||
cached[str] = _context.sent; | ||
updateData == null ? void 0 : updateData(); | ||
return _context.abrupt("return", cached[str]); | ||
case 8: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
}, _callee); | ||
})); | ||
return function asyncCache(_x, _x2) { | ||
return _ref.apply(this, arguments); | ||
}; | ||
}(); | ||
if (options.async || util.types.isAsyncFunction(func)) { | ||
return /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2() { | ||
var _len, | ||
args, | ||
_key, | ||
_args2 = arguments; | ||
return _regeneratorRuntime().wrap(function _callee2$(_context2) { | ||
while (1) switch (_context2.prev = _context2.next) { | ||
case 0: | ||
for (_len = _args2.length, args = new Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = _args2[_key]; | ||
} | ||
_context2.next = 3; | ||
return asyncCache(function () { | ||
return func.apply(void 0, args); | ||
}, args.join(",")); | ||
case 3: | ||
return _context2.abrupt("return", _context2.sent); | ||
case 4: | ||
case "end": | ||
return _context2.stop(); | ||
} | ||
}, _callee2); | ||
})); | ||
} | ||
var cachedFN = function cachedFN() { | ||
for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { | ||
args[_key2] = arguments[_key2]; | ||
} | ||
return cache(func.apply(void 0, args), args.join(",")); | ||
}; | ||
cachedFN.clearCache = function () { | ||
@@ -468,42 +480,58 @@ cached = { | ||
} | ||
function fSCacher(tmpPath) { | ||
var fs = require("fs"); | ||
return { | ||
initialCache: JSON.parse( | ||
// @ts-ignore | ||
fs.existsSync(tmpPath) ? fs.readFileSync(tmpPath, { | ||
encoding: "utf-8" | ||
}) : "{}"), | ||
onDataUpdate: function () { | ||
var _onDataUpdate = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3(ndata) { | ||
return _regeneratorRuntime().wrap(function _callee3$(_context3) { | ||
while (1) switch (_context3.prev = _context3.next) { | ||
case 0: | ||
_context3.prev = 0; | ||
_context3.next = 3; | ||
return fs.unlink(tmpPath); | ||
case 3: | ||
_context3.next = 7; | ||
break; | ||
case 5: | ||
_context3.prev = 5; | ||
_context3.t0 = _context3["catch"](0); | ||
case 7: | ||
_context3.next = 9; | ||
return fs.writeFile(tmpPath, JSON.stringify(ndata), { | ||
encoding: "utf-8", | ||
flag: "w" | ||
}); | ||
case 9: | ||
case "end": | ||
return _context3.stop(); | ||
} | ||
}, _callee3, null, [[0, 5]]); | ||
})); | ||
function onDataUpdate(_x3) { | ||
return _onDataUpdate.apply(this, arguments); | ||
function fSCacher(_x3) { | ||
return _fSCacher.apply(this, arguments); | ||
} | ||
function _fSCacher() { | ||
_fSCacher = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee8(tmpPath) { | ||
var fs, fsPromises; | ||
return _regeneratorRuntime().wrap(function _callee8$(_context8) { | ||
while (1) switch (_context8.prev = _context8.next) { | ||
case 0: | ||
fs = require("fs"); | ||
fsPromises = require("fs/promises"); | ||
return _context8.abrupt("return", { | ||
initialCache: JSON.parse( | ||
// @ts-ignore | ||
fs.existsSync(tmpPath) ? fs.readFileSync(tmpPath, { | ||
encoding: "utf-8" | ||
}) : "{}"), | ||
onDataUpdate: function () { | ||
var _onDataUpdate3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee7(ndata) { | ||
return _regeneratorRuntime().wrap(function _callee7$(_context7) { | ||
while (1) switch (_context7.prev = _context7.next) { | ||
case 0: | ||
_context7.prev = 0; | ||
_context7.next = 3; | ||
return fsPromises.unlink(tmpPath); | ||
case 3: | ||
_context7.next = 7; | ||
break; | ||
case 5: | ||
_context7.prev = 5; | ||
_context7.t0 = _context7["catch"](0); | ||
case 7: | ||
_context7.next = 9; | ||
return fsPromises.writeFile(tmpPath, JSON.stringify(ndata), { | ||
encoding: "utf-8", | ||
flag: "w" | ||
}); | ||
case 9: | ||
case "end": | ||
return _context7.stop(); | ||
} | ||
}, _callee7, null, [[0, 5]]); | ||
})); | ||
function onDataUpdate(_x6) { | ||
return _onDataUpdate3.apply(this, arguments); | ||
} | ||
return onDataUpdate; | ||
}() | ||
}); | ||
case 3: | ||
case "end": | ||
return _context8.stop(); | ||
} | ||
return onDataUpdate; | ||
}() | ||
}; | ||
}, _callee8); | ||
})); | ||
return _fSCacher.apply(this, arguments); | ||
} | ||
@@ -514,41 +542,41 @@ function redisCacher(tmpPath, options) { | ||
getCache: function getCache() { | ||
return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4() { | ||
return _regeneratorRuntime().wrap(function _callee4$(_context4) { | ||
while (1) switch (_context4.prev = _context4.next) { | ||
return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3() { | ||
return _regeneratorRuntime().wrap(function _callee3$(_context3) { | ||
while (1) switch (_context3.prev = _context3.next) { | ||
case 0: | ||
_context4.t0 = JSON; | ||
_context4.next = 3; | ||
_context3.t0 = JSON; | ||
_context3.next = 3; | ||
return red.get(tmpPath); | ||
case 3: | ||
_context4.t1 = _context4.sent; | ||
if (_context4.t1) { | ||
_context4.next = 6; | ||
_context3.t1 = _context3.sent; | ||
if (_context3.t1) { | ||
_context3.next = 6; | ||
break; | ||
} | ||
_context4.t1 = "{}"; | ||
_context3.t1 = "{}"; | ||
case 6: | ||
_context4.t2 = _context4.t1; | ||
return _context4.abrupt("return", _context4.t0.parse.call(_context4.t0, _context4.t2)); | ||
_context3.t2 = _context3.t1; | ||
return _context3.abrupt("return", _context3.t0.parse.call(_context3.t0, _context3.t2)); | ||
case 8: | ||
case "end": | ||
return _context4.stop(); | ||
return _context3.stop(); | ||
} | ||
}, _callee4); | ||
}, _callee3); | ||
}))(); | ||
}, | ||
onDataUpdate: function () { | ||
var _onDataUpdate2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee5(ndata) { | ||
return _regeneratorRuntime().wrap(function _callee5$(_context5) { | ||
while (1) switch (_context5.prev = _context5.next) { | ||
var _onDataUpdate = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4(ndata) { | ||
return _regeneratorRuntime().wrap(function _callee4$(_context4) { | ||
while (1) switch (_context4.prev = _context4.next) { | ||
case 0: | ||
_context5.next = 2; | ||
_context4.next = 2; | ||
return red.set(tmpPath, JSON.stringify(ndata)); | ||
case 2: | ||
case "end": | ||
return _context5.stop(); | ||
return _context4.stop(); | ||
} | ||
}, _callee5); | ||
}, _callee4); | ||
})); | ||
function onDataUpdate(_x4) { | ||
return _onDataUpdate2.apply(this, arguments); | ||
return _onDataUpdate.apply(this, arguments); | ||
} | ||
@@ -563,41 +591,41 @@ return onDataUpdate; | ||
getCache: function getCache() { | ||
return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee6() { | ||
return _regeneratorRuntime().wrap(function _callee6$(_context6) { | ||
while (1) switch (_context6.prev = _context6.next) { | ||
return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee5() { | ||
return _regeneratorRuntime().wrap(function _callee5$(_context5) { | ||
while (1) switch (_context5.prev = _context5.next) { | ||
case 0: | ||
_context6.t0 = JSON; | ||
_context6.next = 3; | ||
_context5.t0 = JSON; | ||
_context5.next = 3; | ||
return red.get(tmpPath); | ||
case 3: | ||
_context6.t1 = _context6.sent; | ||
if (_context6.t1) { | ||
_context6.next = 6; | ||
_context5.t1 = _context5.sent; | ||
if (_context5.t1) { | ||
_context5.next = 6; | ||
break; | ||
} | ||
_context6.t1 = "{}"; | ||
_context5.t1 = "{}"; | ||
case 6: | ||
_context6.t2 = _context6.t1; | ||
return _context6.abrupt("return", _context6.t0.parse.call(_context6.t0, _context6.t2)); | ||
_context5.t2 = _context5.t1; | ||
return _context5.abrupt("return", _context5.t0.parse.call(_context5.t0, _context5.t2)); | ||
case 8: | ||
case "end": | ||
return _context6.stop(); | ||
return _context5.stop(); | ||
} | ||
}, _callee6); | ||
}, _callee5); | ||
}))(); | ||
}, | ||
onDataUpdate: function () { | ||
var _onDataUpdate3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee7(ndata) { | ||
return _regeneratorRuntime().wrap(function _callee7$(_context7) { | ||
while (1) switch (_context7.prev = _context7.next) { | ||
var _onDataUpdate2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee6(ndata) { | ||
return _regeneratorRuntime().wrap(function _callee6$(_context6) { | ||
while (1) switch (_context6.prev = _context6.next) { | ||
case 0: | ||
_context7.next = 2; | ||
_context6.next = 2; | ||
return red.set(tmpPath, JSON.stringify(ndata)); | ||
case 2: | ||
case "end": | ||
return _context7.stop(); | ||
return _context6.stop(); | ||
} | ||
}, _callee7); | ||
}, _callee6); | ||
})); | ||
function onDataUpdate(_x5) { | ||
return _onDataUpdate3.apply(this, arguments); | ||
return _onDataUpdate2.apply(this, arguments); | ||
} | ||
@@ -604,0 +632,0 @@ return onDataUpdate; |
@@ -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=t(require("debounce")),r=t(require("util"));function n(){n=function(){return t};var t={},e=Object.prototype,r=e.hasOwnProperty,o=Object.defineProperty||function(t,e,r){t[e]=r.value},i="function"==typeof Symbol?Symbol:{},a=i.iterator||"@@iterator",c=i.asyncIterator||"@@asyncIterator",u=i.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 f(t,e,r,n){var i=Object.create((e&&e.prototype instanceof p?e:p).prototype),a=new S(n||[]);return o(i,"_invoke",{value:_(t,r,a)}),i}function l(t,e,r){try{return{type:"normal",arg:t.call(e,r)}}catch(t){return{type:"throw",arg:t}}}t.wrap=f;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 x(t){["next","throw","return"].forEach((function(e){s(t,e,(function(t){return this._invoke(e,t)}))}))}function b(t,e){var n;o(this,"_invoke",{value:function(o,i){function a(){return new e((function(n,a){!function n(o,i,a,c){var u=l(t[o],t,i);if("throw"!==u.type){var s=u.arg,f=s.value;return f&&"object"==typeof f&&r.call(f,"__await")?e.resolve(f.__await).then((function(t){n("next",t,a,c)}),(function(t){n("throw",t,a,c)})):e.resolve(f).then((function(t){s.value=t,a(s)}),(function(t){return n("throw",t,a,c)}))}c(u.arg)}(o,i,n,a)}))}return n=n?n.then(a,a):a()}})}function _(t,e,r){var n="suspendedStart";return function(o,i){if("executing"===n)throw new Error("Generator is already running");if("completed"===n){if("throw"===o)throw i;return{value:void 0,done:!0}}for(r.method=o,r.arg=i;;){var a=r.delegate;if(a){var c=O(a,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=l(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 O(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,O(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=l(n,t.iterator,e.arg);if("throw"===o.type)return e.method="throw",e.arg=o.arg,e.delegate=null,h;var i=o.arg;return i?i.done?(e[t.resultName]=i.value,e.next=t.nextLoc,"return"!==e.method&&(e.method="next",e.arg=void 0),e.delegate=null,h):i:(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 E(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:k}}function k(){return{value:void 0,done:!0}}return v.prototype=d,o(w,"constructor",{value:d,configurable:!0}),o(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,c,(function(){return this})),t.AsyncIterator=b,t.async=function(e,r,n,o,i){void 0===i&&(i=Promise);var a=new b(f(e,r,n,o),i);return t.isGeneratorFunction(r)?a:a.next().then((function(t){return t.done?t.value:a.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(E),!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 a.type="throw",a.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 i=this.tryEntries[o],a=i.completion;if("root"===i.tryLoc)return n("end");if(i.tryLoc<=this.prev){var c=r.call(i,"catchLoc"),u=r.call(i,"finallyLoc");if(c&&u){if(this.prev<i.catchLoc)return n(i.catchLoc,!0);if(this.prev<i.finallyLoc)return n(i.finallyLoc)}else if(c){if(this.prev<i.catchLoc)return n(i.catchLoc,!0)}else{if(!u)throw new Error("try statement without catch or finally");if(this.prev<i.finallyLoc)return n(i.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 i=o;break}}i&&("break"===t||"continue"===t)&&i.tryLoc<=e&&e<=i.finallyLoc&&(i=null);var a=i?i.completion:{};return a.type=t,a.arg=e,i?(this.method="next",this.next=i.finallyLoc,h):this.complete(a)},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),E(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;E(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 o(t,e,r,n,o,i,a){try{var c=t[i](a),u=c.value}catch(t){return void r(t)}c.done?e(u):Promise.resolve(u).then(n,o)}function i(t){return function(){var e=this,r=arguments;return new Promise((function(n,i){var a=t.apply(e,r);function c(t){o(a,n,i,c,u,"next",t)}function u(t){o(a,n,i,c,u,"throw",t)}c(void 0)}))}}function a(){return(a=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)}exports.default=function(t,o){void 0===o&&(o={lifeTime:0,debounceTimer:1e3,async:!1});var c=a({____timeOfCreation:Date.now()},o.initialCache),u=o.onDataUpdate?e((function(){try{null==o.onDataUpdate||o.onDataUpdate(c)}catch(t){o.debug&&console.error(t)}}),o.debounceTimer):void 0,s=function(){0!==o.lifeTime&&Date.now()-c.____timeOfCreation>o.lifeTime&&(c={____timeOfCreation:c.____timeOfCreation})},f=function(t,e){return s(),e in c||(c[e]=t(),null==u||u()),c[e]},l=function(){var t=i(n().mark((function t(e,r){return n().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(s(),!(r in c)){t.next=3;break}return t.abrupt("return",c[r]);case 3:return t.next=5,e();case 5:return c[r]=t.sent,null==u||u(),t.abrupt("return",c[r]);case 8:case"end":return t.stop()}}),t)})));return function(e,r){return t.apply(this,arguments)}}();if(o.async||r.types.isAsyncFunction(t))return i(n().mark((function e(){var r,o,i,a=arguments;return n().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:for(r=a.length,o=new Array(r),i=0;i<r;i++)o[i]=a[i];return e.next=3,l((function(){return t.apply(void 0,o)}),o.join(","));case 3:return e.abrupt("return",e.sent);case 4:case"end":return e.stop()}}),e)})));var h=function(){for(var e=arguments.length,r=new Array(e),n=0;n<e;n++)r[n]=arguments[n];return f(t.apply(void 0,r),r.join(","))};return h.clearCache=function(){c={____timeOfCreation:Date.now()}},h.noCache=function(){return t.apply(void 0,arguments)},h},exports.fSCacher=function(t){var e,r=require("fs");return{initialCache:JSON.parse(r.existsSync(t)?r.readFileSync(t,{encoding:"utf-8"}):"{}"),onDataUpdate:(e=i(n().mark((function e(o){return n().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.prev=0,e.next=3,r.unlink(t);case 3:e.next=7;break;case 5:e.prev=5,e.t0=e.catch(0);case 7:return e.next=9,r.writeFile(t,JSON.stringify(o),{encoding:"utf-8",flag:"w"});case 9:case"end":return e.stop()}}),e,null,[[0,5]])}))),function(t){return e.apply(this,arguments)})}},exports.localStorageCacher=function(t,e){var r=(null==e?void 0:e.localStorage)||window.localStorage;return{initialCache:JSON.parse(r.getItem(t)||"{}"),onDataUpdate:function(e){r.setItem(t,JSON.stringify(e))}}},exports.redisCacher=function(t,e){var r,o=e.client;return{getCache:function(){return i(n().mark((function e(){return n().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.t0=JSON,e.next=3,o.get(t);case 3:if(e.t1=e.sent,e.t1){e.next=6;break}e.t1="{}";case 6:return e.t2=e.t1,e.abrupt("return",e.t0.parse.call(e.t0,e.t2));case 8:case"end":return e.stop()}}),e)})))()},onDataUpdate:(r=i(n().mark((function e(r){return n().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,o.set(t,JSON.stringify(r));case 2:case"end":return e.stop()}}),e)}))),function(t){return r.apply(this,arguments)})}},exports.upstashCacher=function(t,e){var r,o=e.client;return{getCache:function(){return i(n().mark((function e(){return n().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.t0=JSON,e.next=3,o.get(t);case 3:if(e.t1=e.sent,e.t1){e.next=6;break}e.t1="{}";case 6:return e.t2=e.t1,e.abrupt("return",e.t0.parse.call(e.t0,e.t2));case 8:case"end":return e.stop()}}),e)})))()},onDataUpdate:(r=i(n().mark((function e(r){return n().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,o.set(t,JSON.stringify(r));case 2:case"end":return e.stop()}}),e)}))),function(t){return r.apply(this,arguments)})}}; | ||
"use strict";function t(t){return t&&"object"==typeof t&&"default"in t?t.default:t}Object.defineProperty(exports,"__esModule",{value:!0});var e=t(require("debounce")),r=t(require("util"));function n(){n=function(){return t};var t={},e=Object.prototype,r=e.hasOwnProperty,o=Object.defineProperty||function(t,e,r){t[e]=r.value},i="function"==typeof Symbol?Symbol:{},a=i.iterator||"@@iterator",c=i.asyncIterator||"@@asyncIterator",u=i.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 f(t,e,r,n){var i=Object.create((e&&e.prototype instanceof p?e:p).prototype),a=new E(n||[]);return o(i,"_invoke",{value:_(t,r,a)}),i}function l(t,e,r){try{return{type:"normal",arg:t.call(e,r)}}catch(t){return{type:"throw",arg:t}}}t.wrap=f;var h={};function p(){}function v(){}function d(){}var y={};s(y,a,(function(){return this}));var g=Object.getPrototypeOf,m=g&&g(g(S([])));m&&m!==e&&r.call(m,a)&&(y=m);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 n;o(this,"_invoke",{value:function(o,i){function a(){return new e((function(n,a){!function n(o,i,a,c){var u=l(t[o],t,i);if("throw"!==u.type){var s=u.arg,f=s.value;return f&&"object"==typeof f&&r.call(f,"__await")?e.resolve(f.__await).then((function(t){n("next",t,a,c)}),(function(t){n("throw",t,a,c)})):e.resolve(f).then((function(t){s.value=t,a(s)}),(function(t){return n("throw",t,a,c)}))}c(u.arg)}(o,i,n,a)}))}return n=n?n.then(a,a):a()}})}function _(t,e,r){var n="suspendedStart";return function(o,i){if("executing"===n)throw new Error("Generator is already running");if("completed"===n){if("throw"===o)throw i;return{value:void 0,done:!0}}for(r.method=o,r.arg=i;;){var a=r.delegate;if(a){var c=O(a,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=l(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 O(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,O(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=l(n,t.iterator,e.arg);if("throw"===o.type)return e.method="throw",e.arg=o.arg,e.delegate=null,h;var i=o.arg;return i?i.done?(e[t.resultName]=i.value,e.next=t.nextLoc,"return"!==e.method&&(e.method="next",e.arg=void 0),e.delegate=null,h):i:(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 k(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function E(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(L,this),this.reset(!0)}function S(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:j}}function j(){return{value:void 0,done:!0}}return v.prototype=d,o(w,"constructor",{value:d,configurable:!0}),o(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,c,(function(){return this})),t.AsyncIterator=b,t.async=function(e,r,n,o,i){void 0===i&&(i=Promise);var a=new b(f(e,r,n,o),i);return t.isGeneratorFunction(r)?a:a.next().then((function(t){return t.done?t.value:a.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=S,E.prototype={constructor:E,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(k),!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 a.type="throw",a.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 i=this.tryEntries[o],a=i.completion;if("root"===i.tryLoc)return n("end");if(i.tryLoc<=this.prev){var c=r.call(i,"catchLoc"),u=r.call(i,"finallyLoc");if(c&&u){if(this.prev<i.catchLoc)return n(i.catchLoc,!0);if(this.prev<i.finallyLoc)return n(i.finallyLoc)}else if(c){if(this.prev<i.catchLoc)return n(i.catchLoc,!0)}else{if(!u)throw new Error("try statement without catch or finally");if(this.prev<i.finallyLoc)return n(i.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 i=o;break}}i&&("break"===t||"continue"===t)&&i.tryLoc<=e&&e<=i.finallyLoc&&(i=null);var a=i?i.completion:{};return a.type=t,a.arg=e,i?(this.method="next",this.next=i.finallyLoc,h):this.complete(a)},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),k(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;k(r)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,r){return this.delegate={iterator:S(t),resultName:e,nextLoc:r},"next"===this.method&&(this.arg=void 0),h}},t}function o(t,e,r,n,o,i,a){try{var c=t[i](a),u=c.value}catch(t){return void r(t)}c.done?e(u):Promise.resolve(u).then(n,o)}function i(t){return function(){var e=this,r=arguments;return new Promise((function(n,i){var a=t.apply(e,r);function c(t){o(a,n,i,c,u,"next",t)}function u(t){o(a,n,i,c,u,"throw",t)}c(void 0)}))}}function a(){return(a=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 c(){return(c=i(n().mark((function t(e){var r,o;return n().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=require("fs"),o=require("fs/promises"),t.abrupt("return",{initialCache:JSON.parse(r.existsSync(e)?r.readFileSync(e,{encoding:"utf-8"}):"{}"),onDataUpdate:function(){var t=i(n().mark((function t(r){return n().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,o.unlink(e);case 3:t.next=7;break;case 5:t.prev=5,t.t0=t.catch(0);case 7:return t.next=9,o.writeFile(e,JSON.stringify(r),{encoding:"utf-8",flag:"w"});case 9:case"end":return t.stop()}}),t,null,[[0,5]])})));return function(e){return t.apply(this,arguments)}}()});case 3:case"end":return t.stop()}}),t)})))).apply(this,arguments)}exports.default=function(t,o){void 0===o&&(o={lifeTime:0,debounceTimer:1e3,async:!1});var c=a({____timeOfCreation:Date.now()},o.initialCache),u=!!o.initialCache,s=function(){var t=i(n().mark((function t(){var e;return n().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,null==o.getCache||null==(e=o.getCache())?void 0:e.then((function(t){c=a({},t,c)}));case 2:if(t.t0=t.sent,t.t0){t.next=5;break}case 5:case"end":return t.stop()}}),t)})));return function(){return t.apply(this,arguments)}}(),f=o.onDataUpdate?e((function(){try{null==o.onDataUpdate||o.onDataUpdate(c)}catch(t){o.debug&&console.error(t)}}),o.debounceTimer):void 0,l=function(){0!==o.lifeTime&&Date.now()-c.____timeOfCreation>o.lifeTime&&(c={____timeOfCreation:c.____timeOfCreation})},h=function(){l();for(var e=arguments.length,a=new Array(e),h=0;h<e;h++)a[h]=arguments[h];var p=a.join(",");if(p in c)return c[p];var v=t.apply(void 0,a);return o.async||!r.types.isPromise(v)?new Promise(function(){var t=i(n().mark((function t(e,r){var o;return n().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(t.prev=0,u){t.next=5;break}return t.next=4,s();case 4:u=!0;case 5:return t.next=7,v;case 7:return c[p]=o=t.sent,null==f||f(),t.abrupt("return",e(o));case 13:t.prev=13,t.t0=t.catch(0),r(t.t0);case 16:case"end":return t.stop()}}),t,null,[[0,13]])})));return function(e,r){return t.apply(this,arguments)}}()):(c[p]=v,null==f||f(),v)};return h.clearCache=function(){c={____timeOfCreation:Date.now()}},h.noCache=function(){return t.apply(void 0,arguments)},h},exports.fSCacher=function(t){return c.apply(this,arguments)},exports.localStorageCacher=function(t,e){var r=(null==e?void 0:e.localStorage)||window.localStorage;return{initialCache:JSON.parse(r.getItem(t)||"{}"),onDataUpdate:function(e){r.setItem(t,JSON.stringify(e))}}},exports.redisCacher=function(t,e){var r,o=e.client;return{getCache:function(){return i(n().mark((function e(){return n().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.t0=JSON,e.next=3,o.get(t);case 3:if(e.t1=e.sent,e.t1){e.next=6;break}e.t1="{}";case 6:return e.t2=e.t1,e.abrupt("return",e.t0.parse.call(e.t0,e.t2));case 8:case"end":return e.stop()}}),e)})))()},onDataUpdate:(r=i(n().mark((function e(r){return n().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,o.set(t,JSON.stringify(r));case 2:case"end":return e.stop()}}),e)}))),function(t){return r.apply(this,arguments)})}},exports.upstashCacher=function(t,e){var r,o=e.client;return{getCache:function(){return i(n().mark((function e(){return n().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.t0=JSON,e.next=3,o.get(t);case 3:if(e.t1=e.sent,e.t1){e.next=6;break}e.t1="{}";case 6:return e.t2=e.t1,e.abrupt("return",e.t0.parse.call(e.t0,e.t2));case 8:case"end":return e.stop()}}),e)})))()},onDataUpdate:(r=i(n().mark((function e(r){return n().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,o.set(t,JSON.stringify(r));case 2:case"end":return e.stop()}}),e)}))),function(t){return r.apply(this,arguments)})}}; | ||
//# sourceMappingURL=func-cache.cjs.production.min.js.map |
@@ -361,2 +361,29 @@ import debounce from 'debounce'; | ||
}, options.initialCache); | ||
var firstTimeDone = !!options.initialCache; | ||
var getData = /*#__PURE__*/function () { | ||
var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() { | ||
var _options$getCache; | ||
return _regeneratorRuntime().wrap(function _callee$(_context) { | ||
while (1) switch (_context.prev = _context.next) { | ||
case 0: | ||
_context.next = 2; | ||
return options.getCache == null ? void 0 : (_options$getCache = options.getCache()) == null ? void 0 : _options$getCache.then(function (data) { | ||
cached = _extends({}, data, cached); | ||
}); | ||
case 2: | ||
_context.t0 = _context.sent; | ||
if (_context.t0) { | ||
_context.next = 5; | ||
break; | ||
} | ||
case 5: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
}, _callee); | ||
})); | ||
return function getData() { | ||
return _ref.apply(this, arguments); | ||
}; | ||
}(); | ||
var updateData = options.onDataUpdate ? debounce(function () { | ||
@@ -376,68 +403,53 @@ try { | ||
}; | ||
var cache = function cache(getnewval, str) { | ||
var cachedFN = function cachedFN() { | ||
checkExpiry(); | ||
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
var str = args.join(","); | ||
if (str in cached) return cached[str]; | ||
cached[str] = getnewval(); | ||
var re = func.apply(void 0, args); | ||
//async | ||
if (options.async || !util.types.isPromise(re)) return new Promise( /*#__PURE__*/function () { | ||
var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(s, r) { | ||
var val; | ||
return _regeneratorRuntime().wrap(function _callee2$(_context2) { | ||
while (1) switch (_context2.prev = _context2.next) { | ||
case 0: | ||
_context2.prev = 0; | ||
if (firstTimeDone) { | ||
_context2.next = 5; | ||
break; | ||
} | ||
_context2.next = 4; | ||
return getData(); | ||
case 4: | ||
firstTimeDone = true; | ||
case 5: | ||
_context2.next = 7; | ||
return re; | ||
case 7: | ||
val = _context2.sent; | ||
cached[str] = val; | ||
updateData == null ? void 0 : updateData(); | ||
return _context2.abrupt("return", s(val)); | ||
case 13: | ||
_context2.prev = 13; | ||
_context2.t0 = _context2["catch"](0); | ||
r(_context2.t0); | ||
case 16: | ||
case "end": | ||
return _context2.stop(); | ||
} | ||
}, _callee2, null, [[0, 13]]); | ||
})); | ||
return function (_x, _x2) { | ||
return _ref2.apply(this, arguments); | ||
}; | ||
}()); | ||
// sync | ||
cached[str] = re; | ||
updateData == null ? void 0 : updateData(); | ||
return cached[str]; | ||
return re; | ||
}; | ||
var asyncCache = /*#__PURE__*/function () { | ||
var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(getnewval, str) { | ||
return _regeneratorRuntime().wrap(function _callee$(_context) { | ||
while (1) switch (_context.prev = _context.next) { | ||
case 0: | ||
checkExpiry(); | ||
if (!(str in cached)) { | ||
_context.next = 3; | ||
break; | ||
} | ||
return _context.abrupt("return", cached[str]); | ||
case 3: | ||
_context.next = 5; | ||
return getnewval(); | ||
case 5: | ||
cached[str] = _context.sent; | ||
updateData == null ? void 0 : updateData(); | ||
return _context.abrupt("return", cached[str]); | ||
case 8: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
}, _callee); | ||
})); | ||
return function asyncCache(_x, _x2) { | ||
return _ref.apply(this, arguments); | ||
}; | ||
}(); | ||
if (options.async || util.types.isAsyncFunction(func)) { | ||
return /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2() { | ||
var _len, | ||
args, | ||
_key, | ||
_args2 = arguments; | ||
return _regeneratorRuntime().wrap(function _callee2$(_context2) { | ||
while (1) switch (_context2.prev = _context2.next) { | ||
case 0: | ||
for (_len = _args2.length, args = new Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = _args2[_key]; | ||
} | ||
_context2.next = 3; | ||
return asyncCache(function () { | ||
return func.apply(void 0, args); | ||
}, args.join(",")); | ||
case 3: | ||
return _context2.abrupt("return", _context2.sent); | ||
case 4: | ||
case "end": | ||
return _context2.stop(); | ||
} | ||
}, _callee2); | ||
})); | ||
} | ||
var cachedFN = function cachedFN() { | ||
for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { | ||
args[_key2] = arguments[_key2]; | ||
} | ||
return cache(func.apply(void 0, args), args.join(",")); | ||
}; | ||
cachedFN.clearCache = function () { | ||
@@ -462,42 +474,58 @@ cached = { | ||
} | ||
function fSCacher(tmpPath) { | ||
var fs = require("fs"); | ||
return { | ||
initialCache: JSON.parse( | ||
// @ts-ignore | ||
fs.existsSync(tmpPath) ? fs.readFileSync(tmpPath, { | ||
encoding: "utf-8" | ||
}) : "{}"), | ||
onDataUpdate: function () { | ||
var _onDataUpdate = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3(ndata) { | ||
return _regeneratorRuntime().wrap(function _callee3$(_context3) { | ||
while (1) switch (_context3.prev = _context3.next) { | ||
case 0: | ||
_context3.prev = 0; | ||
_context3.next = 3; | ||
return fs.unlink(tmpPath); | ||
case 3: | ||
_context3.next = 7; | ||
break; | ||
case 5: | ||
_context3.prev = 5; | ||
_context3.t0 = _context3["catch"](0); | ||
case 7: | ||
_context3.next = 9; | ||
return fs.writeFile(tmpPath, JSON.stringify(ndata), { | ||
encoding: "utf-8", | ||
flag: "w" | ||
}); | ||
case 9: | ||
case "end": | ||
return _context3.stop(); | ||
} | ||
}, _callee3, null, [[0, 5]]); | ||
})); | ||
function onDataUpdate(_x3) { | ||
return _onDataUpdate.apply(this, arguments); | ||
function fSCacher(_x3) { | ||
return _fSCacher.apply(this, arguments); | ||
} | ||
function _fSCacher() { | ||
_fSCacher = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee8(tmpPath) { | ||
var fs, fsPromises; | ||
return _regeneratorRuntime().wrap(function _callee8$(_context8) { | ||
while (1) switch (_context8.prev = _context8.next) { | ||
case 0: | ||
fs = require("fs"); | ||
fsPromises = require("fs/promises"); | ||
return _context8.abrupt("return", { | ||
initialCache: JSON.parse( | ||
// @ts-ignore | ||
fs.existsSync(tmpPath) ? fs.readFileSync(tmpPath, { | ||
encoding: "utf-8" | ||
}) : "{}"), | ||
onDataUpdate: function () { | ||
var _onDataUpdate3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee7(ndata) { | ||
return _regeneratorRuntime().wrap(function _callee7$(_context7) { | ||
while (1) switch (_context7.prev = _context7.next) { | ||
case 0: | ||
_context7.prev = 0; | ||
_context7.next = 3; | ||
return fsPromises.unlink(tmpPath); | ||
case 3: | ||
_context7.next = 7; | ||
break; | ||
case 5: | ||
_context7.prev = 5; | ||
_context7.t0 = _context7["catch"](0); | ||
case 7: | ||
_context7.next = 9; | ||
return fsPromises.writeFile(tmpPath, JSON.stringify(ndata), { | ||
encoding: "utf-8", | ||
flag: "w" | ||
}); | ||
case 9: | ||
case "end": | ||
return _context7.stop(); | ||
} | ||
}, _callee7, null, [[0, 5]]); | ||
})); | ||
function onDataUpdate(_x6) { | ||
return _onDataUpdate3.apply(this, arguments); | ||
} | ||
return onDataUpdate; | ||
}() | ||
}); | ||
case 3: | ||
case "end": | ||
return _context8.stop(); | ||
} | ||
return onDataUpdate; | ||
}() | ||
}; | ||
}, _callee8); | ||
})); | ||
return _fSCacher.apply(this, arguments); | ||
} | ||
@@ -508,41 +536,41 @@ function redisCacher(tmpPath, options) { | ||
getCache: function getCache() { | ||
return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4() { | ||
return _regeneratorRuntime().wrap(function _callee4$(_context4) { | ||
while (1) switch (_context4.prev = _context4.next) { | ||
return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3() { | ||
return _regeneratorRuntime().wrap(function _callee3$(_context3) { | ||
while (1) switch (_context3.prev = _context3.next) { | ||
case 0: | ||
_context4.t0 = JSON; | ||
_context4.next = 3; | ||
_context3.t0 = JSON; | ||
_context3.next = 3; | ||
return red.get(tmpPath); | ||
case 3: | ||
_context4.t1 = _context4.sent; | ||
if (_context4.t1) { | ||
_context4.next = 6; | ||
_context3.t1 = _context3.sent; | ||
if (_context3.t1) { | ||
_context3.next = 6; | ||
break; | ||
} | ||
_context4.t1 = "{}"; | ||
_context3.t1 = "{}"; | ||
case 6: | ||
_context4.t2 = _context4.t1; | ||
return _context4.abrupt("return", _context4.t0.parse.call(_context4.t0, _context4.t2)); | ||
_context3.t2 = _context3.t1; | ||
return _context3.abrupt("return", _context3.t0.parse.call(_context3.t0, _context3.t2)); | ||
case 8: | ||
case "end": | ||
return _context4.stop(); | ||
return _context3.stop(); | ||
} | ||
}, _callee4); | ||
}, _callee3); | ||
}))(); | ||
}, | ||
onDataUpdate: function () { | ||
var _onDataUpdate2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee5(ndata) { | ||
return _regeneratorRuntime().wrap(function _callee5$(_context5) { | ||
while (1) switch (_context5.prev = _context5.next) { | ||
var _onDataUpdate = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4(ndata) { | ||
return _regeneratorRuntime().wrap(function _callee4$(_context4) { | ||
while (1) switch (_context4.prev = _context4.next) { | ||
case 0: | ||
_context5.next = 2; | ||
_context4.next = 2; | ||
return red.set(tmpPath, JSON.stringify(ndata)); | ||
case 2: | ||
case "end": | ||
return _context5.stop(); | ||
return _context4.stop(); | ||
} | ||
}, _callee5); | ||
}, _callee4); | ||
})); | ||
function onDataUpdate(_x4) { | ||
return _onDataUpdate2.apply(this, arguments); | ||
return _onDataUpdate.apply(this, arguments); | ||
} | ||
@@ -557,41 +585,41 @@ return onDataUpdate; | ||
getCache: function getCache() { | ||
return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee6() { | ||
return _regeneratorRuntime().wrap(function _callee6$(_context6) { | ||
while (1) switch (_context6.prev = _context6.next) { | ||
return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee5() { | ||
return _regeneratorRuntime().wrap(function _callee5$(_context5) { | ||
while (1) switch (_context5.prev = _context5.next) { | ||
case 0: | ||
_context6.t0 = JSON; | ||
_context6.next = 3; | ||
_context5.t0 = JSON; | ||
_context5.next = 3; | ||
return red.get(tmpPath); | ||
case 3: | ||
_context6.t1 = _context6.sent; | ||
if (_context6.t1) { | ||
_context6.next = 6; | ||
_context5.t1 = _context5.sent; | ||
if (_context5.t1) { | ||
_context5.next = 6; | ||
break; | ||
} | ||
_context6.t1 = "{}"; | ||
_context5.t1 = "{}"; | ||
case 6: | ||
_context6.t2 = _context6.t1; | ||
return _context6.abrupt("return", _context6.t0.parse.call(_context6.t0, _context6.t2)); | ||
_context5.t2 = _context5.t1; | ||
return _context5.abrupt("return", _context5.t0.parse.call(_context5.t0, _context5.t2)); | ||
case 8: | ||
case "end": | ||
return _context6.stop(); | ||
return _context5.stop(); | ||
} | ||
}, _callee6); | ||
}, _callee5); | ||
}))(); | ||
}, | ||
onDataUpdate: function () { | ||
var _onDataUpdate3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee7(ndata) { | ||
return _regeneratorRuntime().wrap(function _callee7$(_context7) { | ||
while (1) switch (_context7.prev = _context7.next) { | ||
var _onDataUpdate2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee6(ndata) { | ||
return _regeneratorRuntime().wrap(function _callee6$(_context6) { | ||
while (1) switch (_context6.prev = _context6.next) { | ||
case 0: | ||
_context7.next = 2; | ||
_context6.next = 2; | ||
return red.set(tmpPath, JSON.stringify(ndata)); | ||
case 2: | ||
case "end": | ||
return _context7.stop(); | ||
return _context6.stop(); | ||
} | ||
}, _callee7); | ||
}, _callee6); | ||
})); | ||
function onDataUpdate(_x5) { | ||
return _onDataUpdate3.apply(this, arguments); | ||
return _onDataUpdate2.apply(this, arguments); | ||
} | ||
@@ -598,0 +626,0 @@ return onDataUpdate; |
@@ -10,6 +10,6 @@ export default function funCache<T extends Function>(func: T, options?: FCOptions): T & { | ||
}; | ||
export declare function fSCacher(tmpPath: string): { | ||
export declare function fSCacher(tmpPath: string): Promise<{ | ||
initialCache: any; | ||
onDataUpdate: (ndata: any) => Promise<void>; | ||
}; | ||
}>; | ||
import type { RedisClientType } from 'redis'; | ||
@@ -16,0 +16,0 @@ export declare function redisCacher(tmpPath: string, options: { |
{ | ||
"name": "func-cache", | ||
"version": "2.2.0", | ||
"version": "2.2.2", | ||
"license": "MIT", | ||
@@ -76,10 +76,3 @@ "main": "dist/index.js", | ||
"redis": "^4.6.5" | ||
}, | ||
"exports": { | ||
".": { | ||
"import": "./dist/func-cache.esm", | ||
"require": "./dist/func-cache.cjs.production.min" | ||
}, | ||
"./package.json": "./package.json" | ||
} | ||
} |
@@ -10,2 +10,9 @@ import debounce from "debounce"; | ||
let firstTimeDone = !!options.initialCache; | ||
const getData = async () => { | ||
await options.getCache?.()?.then((data: any) => { | ||
cached = { ...data, ...cached }; | ||
}) || {}; | ||
} | ||
const updateData = options.onDataUpdate ? debounce(() => { | ||
@@ -26,35 +33,37 @@ try { | ||
const cache = (getnewval: Function, str: any) => { | ||
const cachedFN = ((...args: any) => { | ||
checkExpiry(); | ||
const str = args.join(","); | ||
if (str in cached) return cached[str]; | ||
cached[str] = getnewval(); | ||
updateData?.(); | ||
const re = func(...args); | ||
return cached[str] | ||
} | ||
//async | ||
if (options.async || !util.types.isPromise(re)) | ||
return new Promise(async (s, r) => { | ||
try { | ||
if (!firstTimeDone) { | ||
await getData(); | ||
firstTimeDone = true; | ||
} | ||
const asyncCache = async (getnewval: Function, str: any) => { | ||
checkExpiry(); | ||
const val = await re; | ||
cached[str] = val; | ||
updateData?.(); | ||
return s(val) | ||
if (str in cached) return cached[str]; | ||
cached[str] = await getnewval(); | ||
} catch (err) { | ||
r(err) | ||
} | ||
}) | ||
// sync | ||
cached[str] = re; | ||
updateData?.(); | ||
return re; | ||
}) as any; | ||
return cached[str] | ||
} | ||
if (options.async || util.types.isAsyncFunction(func)) { | ||
return (async (...args: any) => | ||
await asyncCache(() => func(...args), args.join(",")) | ||
) as any | ||
} | ||
const cachedFN = ((...args: any) => | ||
cache(func(...args), args.join(",")) | ||
) as any; | ||
cachedFN.clearCache = () => { | ||
@@ -86,4 +95,6 @@ cached = { ____timeOfCreation: Date.now() } | ||
export function fSCacher(tmpPath: string) { | ||
export async function fSCacher(tmpPath: string) { | ||
const fs = require("fs"); | ||
const fsPromises = require("fs/promises"); | ||
return { | ||
@@ -100,7 +111,7 @@ initialCache: JSON.parse( | ||
try { | ||
await fs.unlink(tmpPath); | ||
await fsPromises.unlink(tmpPath); | ||
// eslint-disable-next-line no-empty | ||
} catch { } | ||
await fs.writeFile(tmpPath, JSON.stringify(ndata), { | ||
await fsPromises.writeFile(tmpPath, JSON.stringify(ndata), { | ||
encoding: "utf-8", | ||
@@ -107,0 +118,0 @@ flag: "w", |
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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
104643
1532
7