#1407 Checkout Implemented Memorize Decorator

This commit is contained in:
Nino Righi
2021-02-25 16:21:25 +01:00
parent 67f93b98db
commit aeb79c8bee
5 changed files with 69 additions and 6 deletions

View File

@@ -4,4 +4,5 @@ export * from './is-boolean';
export * from './is-null-or-undefined';
export * from './is-number';
export * from './is-string';
export * from './memorize.decorator';
// end:ng42.barrel

View File

@@ -0,0 +1,26 @@
import _ from 'lodash';
import { v4 } from 'uuid';
const memorizeCache: { ttl: number; result: any; args: any[]; id: string }[] = [];
export function memorize(options?: { ttl?: number; comparer?: (current: any[], prev: any[]) => boolean }): MethodDecorator {
const id = v4();
const comparer = options?.comparer || _.isEqual;
const ttl = options?.ttl || Infinity;
return (target: Object, propertyKey: string, descriptor: TypedPropertyDescriptor<any>): TypedPropertyDescriptor<any> => {
const currentMethod = descriptor.value;
descriptor.value = function (...args: any[]) {
const cacheIndex = memorizeCache.findIndex((c) => c.id === id && comparer(c.args, args));
const cache = memorizeCache[cacheIndex];
if (cache && cache.ttl > Date.now()) {
return cache.result;
} else if (cache) {
memorizeCache.splice(cacheIndex, 1);
}
const result = currentMethod.apply(this, args);
memorizeCache.push({ ttl: Date.now() + ttl, result, args, id });
return result;
};
return descriptor;
};
}

43
package-lock.json generated
View File

@@ -5568,6 +5568,12 @@
"integrity": "sha512-3c+yGKvVP5Y9TYBEibGNR+kLtijnj7mYrXRg+WpFb2X9xm04g/DXYkfg4hmzJQosc9snFNUPkbYIhu+KAm6jJw==",
"dev": true
},
"@types/lodash": {
"version": "4.14.168",
"resolved": "https://registry.npmjs.org/@types/lodash/-/lodash-4.14.168.tgz",
"integrity": "sha512-oVfRvqHV/V6D1yifJbVRU3TMp8OT6o6BG+U9MkwuJ3U8/CsDHvalRpsxBqivn71ztOFZBTfJMvETbqHiaNSj7Q==",
"dev": true
},
"@types/minimatch": {
"version": "3.0.3",
"resolved": "https://pkgs.dev.azure.com/hugendubel/_packaging/hugendubel/npm/registry/@types/minimatch/-/minimatch-3.0.3.tgz",
@@ -5631,6 +5637,12 @@
"integrity": "sha1-nAiGeYdvN061mD8VDUeHqm+zLX4=",
"dev": true
},
"@types/uuid": {
"version": "8.3.0",
"resolved": "https://registry.npmjs.org/@types/uuid/-/uuid-8.3.0.tgz",
"integrity": "sha512-eQ9qFW/fhfGJF8WKHGEHZEyVWfZxrT+6CLIJGBcZPfxUh/+BnEj+UCGYMlr9qZuX/2AltsvwrGqp0LhEW8D0zQ==",
"dev": true
},
"@types/webpack-sources": {
"version": "0.1.8",
"resolved": "https://registry.npmjs.org/@types/webpack-sources/-/webpack-sources-0.1.8.tgz",
@@ -12910,9 +12922,9 @@
}
},
"lodash": {
"version": "4.17.20",
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.20.tgz",
"integrity": "sha512-PlhdFcillOINfeV7Ni6oF1TAEayyZBoZ8bcshTHqOYJYlrqzRK5hagpagky5o4HfCzzd1TRkXPMFq6cKk9rGmA=="
"version": "4.17.21",
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz",
"integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg=="
},
"lodash.memoize": {
"version": "4.1.2",
@@ -18271,6 +18283,13 @@
"tough-cookie": "~2.4.3",
"tunnel-agent": "^0.6.0",
"uuid": "^3.3.2"
},
"dependencies": {
"uuid": {
"version": "3.4.0",
"resolved": "https://registry.npmjs.org/uuid/-/uuid-3.4.0.tgz",
"integrity": "sha512-HjSDRw6gZE5JMggctHBcjVak08+KEVhSIiDzFnT9S9aegmp85S/bReBVTb4QTFaRNptJ9kuYaNhnbNEOkbKb/A=="
}
}
},
"require-directory": {
@@ -21743,6 +21762,12 @@
"psl": "^1.1.28",
"punycode": "^2.1.1"
}
},
"uuid": {
"version": "3.4.0",
"resolved": "https://registry.npmjs.org/uuid/-/uuid-3.4.0.tgz",
"integrity": "sha512-HjSDRw6gZE5JMggctHBcjVak08+KEVhSIiDzFnT9S9aegmp85S/bReBVTb4QTFaRNptJ9kuYaNhnbNEOkbKb/A==",
"dev": true
}
}
},
@@ -21900,9 +21925,9 @@
"dev": true
},
"uuid": {
"version": "3.3.2",
"resolved": "https://registry.npmjs.org/uuid/-/uuid-3.3.2.tgz",
"integrity": "sha512-yXJmeNaw3DnnKAOKJE51sL/ZaYfWJRl1pK9dr19YFCu0ObS231AB1/LbqTKRAQ5kw8A90rA6fr4riOUpTZvQZA=="
"version": "8.3.2",
"resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz",
"integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg=="
},
"v8-compile-cache": {
"version": "2.1.1",
@@ -22579,6 +22604,12 @@
"resolved": "https://registry.npmjs.org/ansi-colors/-/ansi-colors-3.2.4.tgz",
"integrity": "sha512-hHUXGagefjN2iRrID63xckIvotOXOojhQKWIPUZ4mNUZ9nLZW+7FMNoE1lOkEhNWYsx/7ysGIuJYCiMAA9FnrA==",
"dev": true
},
"uuid": {
"version": "3.4.0",
"resolved": "https://registry.npmjs.org/uuid/-/uuid-3.4.0.tgz",
"integrity": "sha512-HjSDRw6gZE5JMggctHBcjVak08+KEVhSIiDzFnT9S9aegmp85S/bReBVTb4QTFaRNptJ9kuYaNhnbNEOkbKb/A==",
"dev": true
}
}
},

View File

@@ -64,6 +64,7 @@
"faker": "^4.1.0",
"hammerjs": "^2.0.8",
"intersection-observer": "^0.11.0",
"lodash": "^4.17.21",
"ng-connection-service": "^1.0.4",
"ngx-infinite-scroll": "^7.2.0",
"ngx-perfect-scrollbar": "^7.2.1",
@@ -76,6 +77,7 @@
"socket.io": "^2.2.0",
"tslib": "^2.0.0",
"uglify-js": "^3.4.9",
"uuid": "^8.3.2",
"web-animations-js": "^2.3.2",
"zone.js": "~0.10.2"
},
@@ -90,7 +92,9 @@
"@ngxs/logger-plugin": "^3.6.2",
"@types/jasmine": "^2.8.16",
"@types/jasminewd2": "~2.0.3",
"@types/lodash": "^4.14.168",
"@types/node": "^12.11.1",
"@types/uuid": "^8.3.0",
"codelyzer": "^5.1.2",
"husky": "^4.2.3",
"jasmine-core": "~3.5.0",

View File

@@ -11,6 +11,7 @@
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"importHelpers": false,
"allowSyntheticDefaultImports": true,
"target": "es5",
"typeRoots": [
"node_modules/@types"