func-cache
Advanced tools
Comparing version 1.3.2 to 1.3.4
@@ -5,2 +5,7 @@ 'use strict'; | ||
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } | ||
var debounce = _interopDefault(require('debounce')); | ||
var util = _interopDefault(require('util')); | ||
function _regeneratorRuntime() { | ||
@@ -337,36 +342,62 @@ _regeneratorRuntime = function () { | ||
} | ||
function _extends() { | ||
_extends = Object.assign ? Object.assign.bind() : function (target) { | ||
for (var i = 1; i < arguments.length; i++) { | ||
var source = arguments[i]; | ||
for (var key in source) { | ||
if (Object.prototype.hasOwnProperty.call(source, key)) { | ||
target[key] = source[key]; | ||
} | ||
} | ||
} | ||
return target; | ||
}; | ||
return _extends.apply(this, arguments); | ||
} | ||
function cache(func, options) { | ||
function funCache(func, options) { | ||
if (options === void 0) { | ||
options = { | ||
lifeTime: 0 | ||
lifeTime: 0, | ||
debounceTimer: 1000, | ||
async: false | ||
}; | ||
} | ||
var cached = {}; | ||
var timeOfCreation = Date.now(); | ||
var cache = function cache(newval, str) { | ||
if (options.lifeTime !== 0 && Date.now() - timeOfCreation > options.lifeTime) cached = {}; | ||
var cached = _extends({ | ||
____timeOfCreation: Date.now() | ||
}, options.initialCache); | ||
var updateData = options.onDataUpdate ? debounce(function () { | ||
options.onDataUpdate == null ? void 0 : options.onDataUpdate(cached); | ||
}, options.debounceTimer) : undefined; | ||
var checkExpiry = function checkExpiry() { | ||
if (options.lifeTime !== 0 && Date.now() - cached.____timeOfCreation > options.lifeTime) cached = { | ||
____timeOfCreation: cached.____timeOfCreation | ||
}; | ||
}; | ||
var cache = function cache(getnewval, str) { | ||
checkExpiry(); | ||
if (str in cached) return cached[str]; | ||
return cached[str] = newval; | ||
cached[str] = getnewval(); | ||
updateData == null ? void 0 : updateData(); | ||
return cached[str]; | ||
}; | ||
if (func.constructor.name === 'AsyncFunction') { | ||
return /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() { | ||
var _len, | ||
args, | ||
_key, | ||
_args = arguments; | ||
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: | ||
for (_len = _args.length, args = new Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = _args[_key]; | ||
checkExpiry(); | ||
if (!(str in cached)) { | ||
_context.next = 3; | ||
break; | ||
} | ||
_context.t0 = cache; | ||
_context.next = 4; | ||
return func.apply(void 0, args); | ||
case 4: | ||
_context.t1 = _context.sent; | ||
_context.t2 = args.join(","); | ||
return _context.abrupt("return", (0, _context.t0)(_context.t1, _context.t2)); | ||
case 7: | ||
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": | ||
@@ -377,2 +408,31 @@ return _context.stop(); | ||
})); | ||
return function asyncCache(_x, _x2) { | ||
return _ref.apply(this, arguments); | ||
}; | ||
}(); | ||
console.log(util.types.isAsyncFunction(func)); | ||
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); | ||
})); | ||
} | ||
@@ -387,3 +447,3 @@ return function () { | ||
exports.default = cache; | ||
exports.default = funCache; | ||
//# sourceMappingURL=func-cache.cjs.development.js.map |
@@ -1,2 +0,2 @@ | ||
"use strict";function t(){t=function(){return r};var r={},e=Object.prototype,n=e.hasOwnProperty,o=Object.defineProperty||function(t,r,e){t[r]=e.value},i="function"==typeof Symbol?Symbol:{},a=i.iterator||"@@iterator",c=i.asyncIterator||"@@asyncIterator",u=i.toStringTag||"@@toStringTag";function f(t,r,e){return Object.defineProperty(t,r,{value:e,enumerable:!0,configurable:!0,writable:!0}),t[r]}try{f({},"")}catch(t){f=function(t,r,e){return t[r]=e}}function l(t,r,e,n){var i=Object.create((r&&r.prototype instanceof p?r:p).prototype),a=new O(n||[]);return o(i,"_invoke",{value:b(t,e,a)}),i}function h(t,r,e){try{return{type:"normal",arg:t.call(r,e)}}catch(t){return{type:"throw",arg:t}}}r.wrap=l;var s={};function p(){}function v(){}function y(){}var d={};f(d,a,(function(){return this}));var g=Object.getPrototypeOf,m=g&&g(g(P([])));m&&m!==e&&n.call(m,a)&&(d=m);var w=y.prototype=p.prototype=Object.create(d);function x(t){["next","throw","return"].forEach((function(r){f(t,r,(function(t){return this._invoke(r,t)}))}))}function L(t,r){var e;o(this,"_invoke",{value:function(o,i){function a(){return new r((function(e,a){!function e(o,i,a,c){var u=h(t[o],t,i);if("throw"!==u.type){var f=u.arg,l=f.value;return l&&"object"==typeof l&&n.call(l,"__await")?r.resolve(l.__await).then((function(t){e("next",t,a,c)}),(function(t){e("throw",t,a,c)})):r.resolve(l).then((function(t){f.value=t,a(f)}),(function(t){return e("throw",t,a,c)}))}c(u.arg)}(o,i,e,a)}))}return e=e?e.then(a,a):a()}})}function b(t,r,e){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(e.method=o,e.arg=i;;){var a=e.delegate;if(a){var c=E(a,e);if(c){if(c===s)continue;return c}}if("next"===e.method)e.sent=e._sent=e.arg;else if("throw"===e.method){if("suspendedStart"===n)throw n="completed",e.arg;e.dispatchException(e.arg)}else"return"===e.method&&e.abrupt("return",e.arg);n="executing";var u=h(t,r,e);if("normal"===u.type){if(n=e.done?"completed":"suspendedYield",u.arg===s)continue;return{value:u.arg,done:e.done}}"throw"===u.type&&(n="completed",e.method="throw",e.arg=u.arg)}}}function E(t,r){var e=r.method,n=t.iterator[e];if(void 0===n)return r.delegate=null,"throw"===e&&t.iterator.return&&(r.method="return",r.arg=void 0,E(t,r),"throw"===r.method)||"return"!==e&&(r.method="throw",r.arg=new TypeError("The iterator does not provide a '"+e+"' method")),s;var o=h(n,t.iterator,r.arg);if("throw"===o.type)return r.method="throw",r.arg=o.arg,r.delegate=null,s;var i=o.arg;return i?i.done?(r[t.resultName]=i.value,r.next=t.nextLoc,"return"!==r.method&&(r.method="next",r.arg=void 0),r.delegate=null,s):i:(r.method="throw",r.arg=new TypeError("iterator result is not an object"),r.delegate=null,s)}function _(t){var r={tryLoc:t[0]};1 in t&&(r.catchLoc=t[1]),2 in t&&(r.finallyLoc=t[2],r.afterLoc=t[3]),this.tryEntries.push(r)}function j(t){var r=t.completion||{};r.type="normal",delete r.arg,t.completion=r}function O(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(_,this),this.reset(!0)}function P(t){if(t){var r=t[a];if(r)return r.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var e=-1,o=function r(){for(;++e<t.length;)if(n.call(t,e))return r.value=t[e],r.done=!1,r;return r.value=void 0,r.done=!0,r};return o.next=o}}return{next:k}}function k(){return{value:void 0,done:!0}}return v.prototype=y,o(w,"constructor",{value:y,configurable:!0}),o(y,"constructor",{value:v,configurable:!0}),v.displayName=f(y,u,"GeneratorFunction"),r.isGeneratorFunction=function(t){var r="function"==typeof t&&t.constructor;return!!r&&(r===v||"GeneratorFunction"===(r.displayName||r.name))},r.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,y):(t.__proto__=y,f(t,u,"GeneratorFunction")),t.prototype=Object.create(w),t},r.awrap=function(t){return{__await:t}},x(L.prototype),f(L.prototype,c,(function(){return this})),r.AsyncIterator=L,r.async=function(t,e,n,o,i){void 0===i&&(i=Promise);var a=new L(l(t,e,n,o),i);return r.isGeneratorFunction(e)?a:a.next().then((function(t){return t.done?t.value:a.next()}))},x(w),f(w,u,"Generator"),f(w,a,(function(){return this})),f(w,"toString",(function(){return"[object Generator]"})),r.keys=function(t){var r=Object(t),e=[];for(var n in r)e.push(n);return e.reverse(),function t(){for(;e.length;){var n=e.pop();if(n in r)return t.value=n,t.done=!1,t}return t.done=!0,t}},r.values=P,O.prototype={constructor:O,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(j),!t)for(var r in this)"t"===r.charAt(0)&&n.call(this,r)&&!isNaN(+r.slice(1))&&(this[r]=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 r=this;function e(e,n){return a.type="throw",a.arg=t,r.next=e,n&&(r.method="next",r.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 e("end");if(i.tryLoc<=this.prev){var c=n.call(i,"catchLoc"),u=n.call(i,"finallyLoc");if(c&&u){if(this.prev<i.catchLoc)return e(i.catchLoc,!0);if(this.prev<i.finallyLoc)return e(i.finallyLoc)}else if(c){if(this.prev<i.catchLoc)return e(i.catchLoc,!0)}else{if(!u)throw new Error("try statement without catch or finally");if(this.prev<i.finallyLoc)return e(i.finallyLoc)}}}},abrupt:function(t,r){for(var e=this.tryEntries.length-1;e>=0;--e){var o=this.tryEntries[e];if(o.tryLoc<=this.prev&&n.call(o,"finallyLoc")&&this.prev<o.finallyLoc){var i=o;break}}i&&("break"===t||"continue"===t)&&i.tryLoc<=r&&r<=i.finallyLoc&&(i=null);var a=i?i.completion:{};return a.type=t,a.arg=r,i?(this.method="next",this.next=i.finallyLoc,s):this.complete(a)},complete:function(t,r){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&&r&&(this.next=r),s},finish:function(t){for(var r=this.tryEntries.length-1;r>=0;--r){var e=this.tryEntries[r];if(e.finallyLoc===t)return this.complete(e.completion,e.afterLoc),j(e),s}},catch:function(t){for(var r=this.tryEntries.length-1;r>=0;--r){var e=this.tryEntries[r];if(e.tryLoc===t){var n=e.completion;if("throw"===n.type){var o=n.arg;j(e)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(t,r,e){return this.delegate={iterator:P(t),resultName:r,nextLoc:e},"next"===this.method&&(this.arg=void 0),s}},r}function r(t,r,e,n,o,i,a){try{var c=t[i](a),u=c.value}catch(t){return void e(t)}c.done?r(u):Promise.resolve(u).then(n,o)}function e(t){return function(){var e=this,n=arguments;return new Promise((function(o,i){var a=t.apply(e,n);function c(t){r(a,o,i,c,u,"next",t)}function u(t){r(a,o,i,c,u,"throw",t)}c(void 0)}))}}Object.defineProperty(exports,"__esModule",{value:!0}),exports.default=function(r,n){void 0===n&&(n={lifeTime:0});var o={},i=Date.now(),a=function(t,r){return 0!==n.lifeTime&&Date.now()-i>n.lifeTime&&(o={}),r in o?o[r]:o[r]=t};return"AsyncFunction"===r.constructor.name?e(t().mark((function e(){var n,o,i,c=arguments;return t().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:for(n=c.length,o=new Array(n),i=0;i<n;i++)o[i]=c[i];return t.t0=a,t.next=4,r.apply(void 0,o);case 4:return t.t1=t.sent,t.t2=o.join(","),t.abrupt("return",(0,t.t0)(t.t1,t.t2));case 7:case"end":return t.stop()}}),e)}))):function(){for(var t=arguments.length,e=new Array(t),n=0;n<t;n++)e[n]=arguments[n];return a(r.apply(void 0,e),e.join(","))}}; | ||
"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",u=i.asyncIterator||"@@asyncIterator",c=i.toStringTag||"@@toStringTag";function f(t,e,r){return Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}),t[e]}try{f({},"")}catch(t){f=function(t,e,r){return t[e]=r}}function l(t,e,r,n){var i=Object.create((e&&e.prototype instanceof p?e:p).prototype),a=new j(n||[]);return o(i,"_invoke",{value:_(t,r,a)}),i}function s(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 y(){}var d={};f(d,a,(function(){return this}));var g=Object.getPrototypeOf,m=g&&g(g(k([])));m&&m!==e&&r.call(m,a)&&(d=m);var w=y.prototype=p.prototype=Object.create(d);function b(t){["next","throw","return"].forEach((function(e){f(t,e,(function(t){return this._invoke(e,t)}))}))}function x(t,e){var n;o(this,"_invoke",{value:function(o,i){function a(){return new e((function(n,a){!function n(o,i,a,u){var c=s(t[o],t,i);if("throw"!==c.type){var f=c.arg,l=f.value;return l&&"object"==typeof l&&r.call(l,"__await")?e.resolve(l.__await).then((function(t){n("next",t,a,u)}),(function(t){n("throw",t,a,u)})):e.resolve(l).then((function(t){f.value=t,a(f)}),(function(t){return n("throw",t,a,u)}))}u(c.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 u=L(a,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=s(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=s(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 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 E(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function j(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(O,this),this.reset(!0)}function k(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=y,o(w,"constructor",{value:y,configurable:!0}),o(y,"constructor",{value:v,configurable:!0}),v.displayName=f(y,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,y):(t.__proto__=y,f(t,c,"GeneratorFunction")),t.prototype=Object.create(w),t},t.awrap=function(t){return{__await:t}},b(x.prototype),f(x.prototype,u,(function(){return this})),t.AsyncIterator=x,t.async=function(e,r,n,o,i){void 0===i&&(i=Promise);var a=new x(l(e,r,n,o),i);return t.isGeneratorFunction(r)?a:a.next().then((function(t){return t.done?t.value:a.next()}))},b(w),f(w,c,"Generator"),f(w,a,(function(){return this})),f(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=k,j.prototype={constructor:j,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 u=r.call(i,"catchLoc"),c=r.call(i,"finallyLoc");if(u&&c){if(this.prev<i.catchLoc)return n(i.catchLoc,!0);if(this.prev<i.finallyLoc)return n(i.finallyLoc)}else if(u){if(this.prev<i.catchLoc)return n(i.catchLoc,!0)}else{if(!c)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:k(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 u=t[i](a),c=u.value}catch(t){return void r(t)}u.done?e(c):Promise.resolve(c).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 u(t){o(a,n,i,u,c,"next",t)}function c(t){o(a,n,i,u,c,"throw",t)}u(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 u=a({____timeOfCreation:Date.now()},o.initialCache),c=o.onDataUpdate?e((function(){null==o.onDataUpdate||o.onDataUpdate(u)}),o.debounceTimer):void 0,f=function(){0!==o.lifeTime&&Date.now()-u.____timeOfCreation>o.lifeTime&&(u={____timeOfCreation:u.____timeOfCreation})},l=function(t,e){return f(),e in u||(u[e]=t(),null==c||c()),u[e]},s=function(){var t=i(n().mark((function t(e,r){return n().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(f(),!(r in u)){t.next=3;break}return t.abrupt("return",u[r]);case 3:return t.next=5,e();case 5:return u[r]=t.sent,null==c||c(),t.abrupt("return",u[r]);case 8:case"end":return t.stop()}}),t)})));return function(e,r){return t.apply(this,arguments)}}();return console.log(r.types.isAsyncFunction(t)),o.async||r.types.isAsyncFunction(t)?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,s((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)}))):function(){for(var e=arguments.length,r=new Array(e),n=0;n<e;n++)r[n]=arguments[n];return l(t.apply(void 0,r),r.join(","))}}; | ||
//# sourceMappingURL=func-cache.cjs.production.min.js.map |
@@ -0,1 +1,4 @@ | ||
import debounce from 'debounce'; | ||
import util from 'util'; | ||
function _regeneratorRuntime() { | ||
@@ -332,36 +335,62 @@ _regeneratorRuntime = function () { | ||
} | ||
function _extends() { | ||
_extends = Object.assign ? Object.assign.bind() : function (target) { | ||
for (var i = 1; i < arguments.length; i++) { | ||
var source = arguments[i]; | ||
for (var key in source) { | ||
if (Object.prototype.hasOwnProperty.call(source, key)) { | ||
target[key] = source[key]; | ||
} | ||
} | ||
} | ||
return target; | ||
}; | ||
return _extends.apply(this, arguments); | ||
} | ||
function cache(func, options) { | ||
function funCache(func, options) { | ||
if (options === void 0) { | ||
options = { | ||
lifeTime: 0 | ||
lifeTime: 0, | ||
debounceTimer: 1000, | ||
async: false | ||
}; | ||
} | ||
var cached = {}; | ||
var timeOfCreation = Date.now(); | ||
var cache = function cache(newval, str) { | ||
if (options.lifeTime !== 0 && Date.now() - timeOfCreation > options.lifeTime) cached = {}; | ||
var cached = _extends({ | ||
____timeOfCreation: Date.now() | ||
}, options.initialCache); | ||
var updateData = options.onDataUpdate ? debounce(function () { | ||
options.onDataUpdate == null ? void 0 : options.onDataUpdate(cached); | ||
}, options.debounceTimer) : undefined; | ||
var checkExpiry = function checkExpiry() { | ||
if (options.lifeTime !== 0 && Date.now() - cached.____timeOfCreation > options.lifeTime) cached = { | ||
____timeOfCreation: cached.____timeOfCreation | ||
}; | ||
}; | ||
var cache = function cache(getnewval, str) { | ||
checkExpiry(); | ||
if (str in cached) return cached[str]; | ||
return cached[str] = newval; | ||
cached[str] = getnewval(); | ||
updateData == null ? void 0 : updateData(); | ||
return cached[str]; | ||
}; | ||
if (func.constructor.name === 'AsyncFunction') { | ||
return /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() { | ||
var _len, | ||
args, | ||
_key, | ||
_args = arguments; | ||
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: | ||
for (_len = _args.length, args = new Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = _args[_key]; | ||
checkExpiry(); | ||
if (!(str in cached)) { | ||
_context.next = 3; | ||
break; | ||
} | ||
_context.t0 = cache; | ||
_context.next = 4; | ||
return func.apply(void 0, args); | ||
case 4: | ||
_context.t1 = _context.sent; | ||
_context.t2 = args.join(","); | ||
return _context.abrupt("return", (0, _context.t0)(_context.t1, _context.t2)); | ||
case 7: | ||
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": | ||
@@ -372,2 +401,31 @@ return _context.stop(); | ||
})); | ||
return function asyncCache(_x, _x2) { | ||
return _ref.apply(this, arguments); | ||
}; | ||
}(); | ||
console.log(util.types.isAsyncFunction(func)); | ||
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); | ||
})); | ||
} | ||
@@ -382,3 +440,3 @@ return function () { | ||
export default cache; | ||
export default funCache; | ||
//# sourceMappingURL=func-cache.esm.js.map |
export interface FCOptions { | ||
lifeTime: number; | ||
onDataUpdate?: (nData: any) => void; | ||
initialCache?: any; | ||
/** debounce time wait to call onDataUpdate, default 1000ms */ | ||
debounceTimer?: number; | ||
async?: boolean; | ||
} | ||
export default function cache<T extends Function>(func: T, options?: FCOptions): T; | ||
export default function funCache<T extends Function>(func: T, options?: FCOptions): T; |
{ | ||
"name": "func-cache", | ||
"version": "1.3.2", | ||
"version": "1.3.4", | ||
"private": false, | ||
@@ -40,2 +40,4 @@ "description": "TypeScript library for caching static functions", | ||
"devDependencies": { | ||
"@types/debounce": "^1.2.1", | ||
"@types/node": "^18.15.10", | ||
"@typescript-eslint/eslint-plugin": "4.23.0", | ||
@@ -112,3 +114,6 @@ "@typescript-eslint/parser": "4.23.0", | ||
] | ||
}, | ||
"dependencies": { | ||
"debounce": "^1.2.1" | ||
} | ||
} |
import { aforSec } from "aforwait" | ||
import cache from "./"; | ||
import funCache from "."; | ||
const cachedWait = cache(wait); | ||
async function wait() { | ||
const cachedWait = funCache(async () => { | ||
await aforSec(1); | ||
return "1 seconds" | ||
} | ||
}, { | ||
lifeTime: 3000, | ||
onDataUpdate: console.log, | ||
async: true | ||
}); | ||
describe("testing cache", () => { | ||
@@ -31,3 +36,12 @@ it("wait 1", async () => { | ||
expect(val1).toEqual(val2); | ||
}); | ||
// test expiration | ||
await aforSec(4); | ||
const t3 = performance.now(); | ||
await cachedWait(); | ||
const t4 = performance.now(); | ||
const diff3 = t4 - t3; | ||
expect(diff3 > 900).toBe(true); | ||
}, 10000); | ||
}); |
@@ -0,22 +1,57 @@ | ||
import debounce from "debounce"; | ||
import util from "util"; | ||
export interface FCOptions { | ||
lifeTime: number; | ||
onDataUpdate?: (nData: any) => void; | ||
initialCache?: any; | ||
/** debounce time wait to call onDataUpdate, default 1000ms */ | ||
debounceTimer?: number; | ||
async?: boolean; | ||
} | ||
export default function cache<T extends Function>(func: T, options: FCOptions = { lifeTime: 0 }): T { | ||
let cached: any = {}; | ||
const timeOfCreation = Date.now(); | ||
export default function funCache<T extends Function>(func: T, options: FCOptions = { lifeTime: 0, debounceTimer: 1000, async: false }): T { | ||
let cached: any = { | ||
____timeOfCreation: Date.now(), | ||
...options.initialCache | ||
}; | ||
const cache = (newval: any, str: any) => { | ||
if (options.lifeTime !== 0 && Date.now() - timeOfCreation > options.lifeTime) | ||
cached = {} | ||
const updateData = options.onDataUpdate ? debounce(() => { | ||
options.onDataUpdate?.(cached); | ||
}, options.debounceTimer) : undefined; | ||
const checkExpiry = () => { | ||
if (options.lifeTime !== 0 && Date.now() - cached.____timeOfCreation > options.lifeTime) | ||
cached = { ____timeOfCreation: cached.____timeOfCreation } | ||
} | ||
const cache = (getnewval: Function, str: any) => { | ||
checkExpiry(); | ||
if (str in cached) return cached[str]; | ||
return cached[str] = newval; | ||
cached[str] = getnewval(); | ||
updateData?.(); | ||
return cached[str] | ||
} | ||
if (func.constructor.name === 'AsyncFunction') { | ||
const asyncCache = async (getnewval: Function, str: any) => { | ||
checkExpiry(); | ||
if (str in cached) return cached[str]; | ||
cached[str] = await getnewval(); | ||
updateData?.(); | ||
return cached[str] | ||
} | ||
console.log(util.types.isAsyncFunction(func)) | ||
if (options.async || util.types.isAsyncFunction(func)) { | ||
return (async (...args: any) => | ||
cache(await func(...args), args.join(",")) | ||
await asyncCache(() => func(...args), args.join(",")) | ||
) as any | ||
@@ -23,0 +58,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
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
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
65855
1008
0
1
13
2
+ Addeddebounce@^1.2.1
+ Addeddebounce@1.2.1(transitive)