nav tabs on admin dashboard
This commit is contained in:
+52
-21
@@ -1,47 +1,78 @@
|
||||
'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 = function (x) {
|
||||
if (arguments.length === 1 && (x === null || x === undefined || (typeof x !== 'function' && typeof x !== 'object'))) {
|
||||
return x;
|
||||
const defaultCacheKey = (...args) => {
|
||||
if (args.length === 0) {
|
||||
return '__defaultKey';
|
||||
}
|
||||
|
||||
return JSON.stringify(arguments);
|
||||
if (args.length === 1) {
|
||||
const [firstArgument] = args;
|
||||
if (
|
||||
firstArgument === null ||
|
||||
firstArgument === undefined ||
|
||||
(typeof firstArgument !== 'function' && typeof firstArgument !== 'object')
|
||||
) {
|
||||
return firstArgument;
|
||||
}
|
||||
}
|
||||
|
||||
return JSON.stringify(args);
|
||||
};
|
||||
|
||||
module.exports = (fn, opts) => {
|
||||
opts = Object.assign({
|
||||
module.exports = (fn, options) => {
|
||||
options = Object.assign({
|
||||
cacheKey: defaultCacheKey,
|
||||
cache: new Map()
|
||||
}, opts);
|
||||
cache: new Map(),
|
||||
cachePromiseRejection: false
|
||||
}, options);
|
||||
|
||||
const memoized = function () {
|
||||
const cache = cacheStore.get(memoized);
|
||||
const key = opts.cacheKey.apply(null, arguments);
|
||||
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
|
||||
});
|
||||
};
|
||||
|
||||
const memoized = function (...args) {
|
||||
const key = options.cacheKey(...args);
|
||||
|
||||
if (cache.has(key)) {
|
||||
const c = cache.get(key);
|
||||
|
||||
if (typeof opts.maxAge !== 'number' || Date.now() < c.maxAge) {
|
||||
return c.data;
|
||||
}
|
||||
return c.data;
|
||||
}
|
||||
|
||||
const ret = fn.apply(null, arguments);
|
||||
const ret = fn.call(this, ...args);
|
||||
|
||||
cache.set(key, {
|
||||
data: ret,
|
||||
maxAge: Date.now() + (opts.maxAge || 0)
|
||||
});
|
||||
setData(key, ret);
|
||||
|
||||
if (isPromise(ret) && options.cachePromiseRejection === false) {
|
||||
// Remove rejected promises from cache unless `cachePromiseRejection` is set to `true`
|
||||
ret.catch(() => cache.delete(key));
|
||||
}
|
||||
|
||||
return ret;
|
||||
};
|
||||
|
||||
mimicFn(memoized, fn);
|
||||
try {
|
||||
// The below call will throw in some host environments
|
||||
// See https://github.com/sindresorhus/mimic-fn/issues/10
|
||||
mimicFn(memoized, fn);
|
||||
} catch (_) {}
|
||||
|
||||
cacheStore.set(memoized, opts.cache);
|
||||
cacheStore.set(memoized, options.cache);
|
||||
|
||||
return memoized;
|
||||
};
|
||||
|
||||
+4
-16
@@ -1,21 +1,9 @@
|
||||
The MIT License (MIT)
|
||||
MIT License
|
||||
|
||||
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
+19
-20
@@ -1,27 +1,27 @@
|
||||
{
|
||||
"_from": "mem@^1.1.0",
|
||||
"_id": "mem@1.1.0",
|
||||
"_from": "mem@^4.0.0",
|
||||
"_id": "mem@4.1.0",
|
||||
"_inBundle": false,
|
||||
"_integrity": "sha1-Xt1StIXKHZAP5kiVUFOZoN+kX3Y=",
|
||||
"_integrity": "sha512-I5u6Q1x7wxO0kdOpYBB28xueHADYps5uty/zg936CiG8NTe5sJL8EjrCuLneuDW3PlMdZBGDIn8BirEVdovZvg==",
|
||||
"_location": "/mem",
|
||||
"_phantomChildren": {},
|
||||
"_requested": {
|
||||
"type": "range",
|
||||
"registry": true,
|
||||
"raw": "mem@^1.1.0",
|
||||
"raw": "mem@^4.0.0",
|
||||
"name": "mem",
|
||||
"escapedName": "mem",
|
||||
"rawSpec": "^1.1.0",
|
||||
"rawSpec": "^4.0.0",
|
||||
"saveSpec": null,
|
||||
"fetchSpec": "^1.1.0"
|
||||
"fetchSpec": "^4.0.0"
|
||||
},
|
||||
"_requiredBy": [
|
||||
"/yargs/os-locale"
|
||||
"/os-locale"
|
||||
],
|
||||
"_resolved": "https://registry.npmjs.org/mem/-/mem-1.1.0.tgz",
|
||||
"_shasum": "5edd52b485ca1d900fe64895505399a0dfa45f76",
|
||||
"_spec": "mem@^1.1.0",
|
||||
"_where": "C:\\xampp\\htdocs\\w4rpservices\\node_modules\\yargs\\node_modules\\os-locale",
|
||||
"_resolved": "https://registry.npmjs.org/mem/-/mem-4.1.0.tgz",
|
||||
"_shasum": "aeb9be2d21f47e78af29e4ac5978e8afa2ca5b8a",
|
||||
"_spec": "mem@^4.0.0",
|
||||
"_where": "C:\\xampp\\htdocs\\w4rpservices\\node_modules\\os-locale",
|
||||
"author": {
|
||||
"name": "Sindre Sorhus",
|
||||
"email": "sindresorhus@gmail.com",
|
||||
@@ -32,17 +32,19 @@
|
||||
},
|
||||
"bundleDependencies": false,
|
||||
"dependencies": {
|
||||
"mimic-fn": "^1.0.0"
|
||||
"map-age-cleaner": "^0.1.1",
|
||||
"mimic-fn": "^1.0.0",
|
||||
"p-is-promise": "^2.0.0"
|
||||
},
|
||||
"deprecated": false,
|
||||
"description": "Memoize functions - An optimization used to speed up consecutive function calls by caching the result of calls with identical input",
|
||||
"devDependencies": {
|
||||
"ava": "*",
|
||||
"delay": "^1.1.0",
|
||||
"xo": "*"
|
||||
"ava": "^1.0.1",
|
||||
"delay": "^4.1.0",
|
||||
"xo": "^0.23.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=4"
|
||||
"node": ">=6"
|
||||
},
|
||||
"files": [
|
||||
"index.js"
|
||||
@@ -70,8 +72,5 @@
|
||||
"scripts": {
|
||||
"test": "xo && ava"
|
||||
},
|
||||
"version": "1.1.0",
|
||||
"xo": {
|
||||
"esnext": true
|
||||
}
|
||||
"version": "4.1.0"
|
||||
}
|
||||
|
||||
+47
-27
@@ -2,11 +2,13 @@
|
||||
|
||||
> [Memoize](https://en.wikipedia.org/wiki/Memoization) functions - An optimization used to speed up consecutive function calls by caching the result of calls with identical input
|
||||
|
||||
Memory is automatically released when an item expires.
|
||||
|
||||
|
||||
## Install
|
||||
|
||||
```
|
||||
$ npm install --save mem
|
||||
$ npm install mem
|
||||
```
|
||||
|
||||
|
||||
@@ -22,11 +24,11 @@ const memoized = mem(counter);
|
||||
memoized('foo');
|
||||
//=> 1
|
||||
|
||||
// cached as it's the same arguments
|
||||
// Cached as it's the same arguments
|
||||
memoized('foo');
|
||||
//=> 1
|
||||
|
||||
// not cached anymore as the arguments changed
|
||||
// Not cached anymore as the arguments changed
|
||||
memoized('bar');
|
||||
//=> 2
|
||||
|
||||
@@ -40,35 +42,37 @@ memoized('bar');
|
||||
const mem = require('mem');
|
||||
|
||||
let i = 0;
|
||||
const counter = () => Promise.resolve(++i);
|
||||
const counter = async () => ++i;
|
||||
const memoized = mem(counter);
|
||||
|
||||
memoized().then(a => {
|
||||
console.log(a);
|
||||
(async () => {
|
||||
console.log(await memoized());
|
||||
//=> 1
|
||||
|
||||
memoized().then(b => {
|
||||
// the return value didn't increase as it's cached
|
||||
console.log(b);
|
||||
//=> 1
|
||||
});
|
||||
});
|
||||
// The return value didn't increase as it's cached
|
||||
console.log(await memoized());
|
||||
//=> 1
|
||||
})();
|
||||
```
|
||||
|
||||
```js
|
||||
const mem = require('mem');
|
||||
const got = require('got');
|
||||
const delay = require('delay');
|
||||
|
||||
const memGot = mem(got, {maxAge: 1000});
|
||||
|
||||
memGot('sindresorhus.com').then(() => {
|
||||
// this call is cached
|
||||
memGot('sindresorhus.com').then(() => {
|
||||
setTimeout(() => {
|
||||
// this call is not cached as the cache has expired
|
||||
memGot('sindresorhus.com').then(() => {});
|
||||
}, 2000);
|
||||
});
|
||||
});
|
||||
(async () => {
|
||||
await memGot('sindresorhus.com');
|
||||
|
||||
// This call is cached
|
||||
await memGot('sindresorhus.com');
|
||||
|
||||
await delay(2000);
|
||||
|
||||
// This call is not cached as the cache has expired
|
||||
await memGot('sindresorhus.com');
|
||||
})();
|
||||
```
|
||||
|
||||
|
||||
@@ -84,6 +88,8 @@ Function to be memoized.
|
||||
|
||||
#### options
|
||||
|
||||
Type: `Object`
|
||||
|
||||
##### maxAge
|
||||
|
||||
Type: `number`<br>
|
||||
@@ -104,7 +110,14 @@ You could for example change it to only cache on the first argument `x => JSON.s
|
||||
Type: `Object`<br>
|
||||
Default: `new Map()`
|
||||
|
||||
Use a different cache storage. Must implement the following methods: `.has(key)`, `.get(key)`, `.set(key, value)`, and optionally `.clear()`. You could for example use a `WeakMap` instead.
|
||||
Use a different cache storage. Must implement the following methods: `.has(key)`, `.get(key)`, `.set(key, value)`, `.delete(key)`, and optionally `.clear()`. You could for example use a `WeakMap` instead or [`quick-lru`](https://github.com/sindresorhus/quick-lru) for a LRU cache.
|
||||
|
||||
##### cachePromiseRejection
|
||||
|
||||
Type: `boolean`<br>
|
||||
Default: `false`
|
||||
|
||||
Cache rejected promises.
|
||||
|
||||
### mem.clear(fn)
|
||||
|
||||
@@ -133,15 +146,22 @@ const got = require('got');
|
||||
const cache = new StatsMap();
|
||||
const memGot = mem(got, {cache});
|
||||
|
||||
memGot('sindresorhus.com')
|
||||
.then(() => memGot('sindresorhus.com'))
|
||||
.then(() => memGot('sindresorhus.com'));
|
||||
(async () => {
|
||||
await memGot('sindresorhus.com');
|
||||
await memGot('sindresorhus.com');
|
||||
await memGot('sindresorhus.com');
|
||||
|
||||
console.log(cache.stats);
|
||||
//=> {hits: 2, misses: 1}
|
||||
console.log(cache.stats);
|
||||
//=> {hits: 2, misses: 1}
|
||||
})();
|
||||
```
|
||||
|
||||
|
||||
## Related
|
||||
|
||||
- [p-memoize](https://github.com/sindresorhus/p-memoize) - Memoize promise-returning & async functions
|
||||
|
||||
|
||||
## License
|
||||
|
||||
MIT © [Sindre Sorhus](https://sindresorhus.com)
|
||||
|
||||
Reference in New Issue
Block a user