Skip to content

Commit c77ac55

Browse files
committed
refactor: use lodash.memoize for log cache
Instead of rolling our own basic caching use lodash.memoize. It is a minimal dependency and makes the code cleaner.
1 parent 305c783 commit c77ac55

File tree

2 files changed

+6
-7
lines changed

2 files changed

+6
-7
lines changed

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434
"engines": {
3535
"node": ">=10"
3636
},
37+
"dependencies": {
38+
"lodash.memoize": "^4.1.2"
39+
},
3740
"devDependencies": {
3841
"@babel/cli": "^7.11.6",
3942
"@babel/core": "^7.11.6",

src/function-value-plugin.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1+
import memoize from 'lodash.memoize';
12
import { name } from '../package.json';
23

34
const defaultVariableResolverOptions = {
45
serviceName: name,
56
isDisabledAtPrepopulation: true
67
};
7-
const logged = {};
88

99
const format = (value, result) =>
1010
`[${name}] \${${value}} => ${JSON.stringify(result)}`;
@@ -14,13 +14,9 @@ export class FunctionValuePlugin {
1414
if (!process.env.SLS_DEBUG) {
1515
this._log = () => { };
1616
} else {
17-
this._log = (value, result) => {
18-
if (!logged[value]) {
19-
logged[value] = true;
17+
const log = (value, result) => serverless.cli.log(format(value, result));
2018

21-
serverless.cli.log(format(value, result));
22-
}
23-
};
19+
this._log = memoize(log);
2420
}
2521

2622
this._naming = serverless.getProvider('aws').naming;

0 commit comments

Comments
 (0)