Skip to content

Commit

Permalink
Merge pull request #34 from lidofinance/feature/si-807-fix-bigint-cache
Browse files Browse the repository at this point in the history
fix: fix bigInt cache
  • Loading branch information
DiRaiks committed Sep 15, 2023
2 parents 8859a5e + 3db0adc commit ee003cf
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions packages/sdk/src/common/decorators/cache.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
import { isBigint } from '../utils/index.js';

import { callConsoleMessage } from './utils.js';

const serializeArgs = (...args: any[]) =>
args.map((arg: any) => JSON.stringify(arg)).join(':');
const serializeArgs = (args: any[]) =>
args
.map((arg: any) =>
JSON.stringify(arg, (_key, value) => {
return isBigint(value) ? value.toString() : value;
}),
)
.join(':');

const getDecoratorArgsString = function <This>(this: This, args?: string[]) {
if (!args) return '';

const argsString = args.map((arg) => {
const argsStringArr = args.map((arg) => {
const field = arg
.split('.')
.reduce((a, b) => (a as { [key: string]: any })[b], this);

return arg && typeof field === 'function' ? field.call(this) : field;
});

return serializeArgs(argsString);
return serializeArgs(argsStringArr);
};

export const Cache = function (timeMs = 0, cacheArgs?: string[]) {
Expand Down Expand Up @@ -60,7 +68,12 @@ export const Cache = function (timeMs = 0, cacheArgs?: string[]) {
`Cache for method '${methodName}' set.`,
);
const result = originalMethod.call(this, ...args);
cache.set(cacheKey, { data: result, timestamp: Date.now() });
if (result instanceof Promise) {
result.then((resolvedResult) =>
cache.set(cacheKey, { data: resolvedResult, timestamp: Date.now() }),
);
} else cache.set(cacheKey, { data: result, timestamp: Date.now() });

return result;
};
return replacementMethod;
Expand Down

0 comments on commit ee003cf

Please sign in to comment.