Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'node_modules/mem/index.js')
-rw-r--r--node_modules/mem/index.js81
1 files changed, 24 insertions, 57 deletions
diff --git a/node_modules/mem/index.js b/node_modules/mem/index.js
index 51faf012c..aa5a07398 100644
--- a/node_modules/mem/index.js
+++ b/node_modules/mem/index.js
@@ -1,84 +1,51 @@
'use strict';
const mimicFn = require('mimic-fn');
-const isPromise = require('p-is-promise');
-const mapAgeCleaner = require('map-age-cleaner');
const cacheStore = new WeakMap();
-const defaultCacheKey = (...arguments_) => {
- if (arguments_.length === 0) {
- return '__defaultKey';
+const defaultCacheKey = function (x) {
+ if (arguments.length === 1 && (x === null || x === undefined || (typeof x !== 'function' && typeof x !== 'object'))) {
+ return x;
}
- if (arguments_.length === 1) {
- const [firstArgument] = arguments_;
- if (
- firstArgument === null ||
- firstArgument === undefined ||
- (typeof firstArgument !== 'function' && typeof firstArgument !== 'object')
- ) {
- return firstArgument;
- }
- }
-
- return JSON.stringify(arguments_);
+ return JSON.stringify(arguments);
};
-const mem = (fn, options) => {
- options = Object.assign({
+module.exports = (fn, opts) => {
+ opts = Object.assign({
cacheKey: defaultCacheKey,
- cache: new Map(),
- cachePromiseRejection: false
- }, options);
-
- if (typeof options.maxAge === 'number') {
- mapAgeCleaner(options.cache);
- }
-
- const {cache} = options;
- options.maxAge = options.maxAge || 0;
-
- const setData = (key, data) => {
- cache.set(key, {
- data,
- maxAge: Date.now() + options.maxAge
- });
- };
+ cache: new Map()
+ }, opts);
- const memoized = function (...arguments_) {
- const key = options.cacheKey(...arguments_);
+ const memoized = function () {
+ const cache = cacheStore.get(memoized);
+ const key = opts.cacheKey.apply(null, arguments);
if (cache.has(key)) {
- return cache.get(key).data;
- }
+ const c = cache.get(key);
- const cacheItem = fn.call(this, ...arguments_);
+ if (typeof opts.maxAge !== 'number' || Date.now() < c.maxAge) {
+ return c.data;
+ }
+ }
- setData(key, cacheItem);
+ const ret = fn.apply(null, arguments);
- if (isPromise(cacheItem) && options.cachePromiseRejection === false) {
- // Remove rejected promises from cache unless `cachePromiseRejection` is set to `true`
- cacheItem.catch(() => cache.delete(key));
- }
+ cache.set(key, {
+ data: ret,
+ maxAge: Date.now() + (opts.maxAge || 0)
+ });
- return cacheItem;
+ return ret;
};
- try {
- // The below call will throw in some host environments
- // See https://github.com/sindresorhus/mimic-fn/issues/10
- mimicFn(memoized, fn);
- } catch (_) {}
+ mimicFn(memoized, fn);
- cacheStore.set(memoized, options.cache);
+ cacheStore.set(memoized, opts.cache);
return memoized;
};
-module.exports = mem;
-// TODO: Remove this for the next major release
-module.exports.default = mem;
-
module.exports.clear = fn => {
const cache = cacheStore.get(fn);