Skip to content

Commit

Permalink
refactor: refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
exKAZUu committed Jan 3, 2025
1 parent 3be56df commit c46161a
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 17 deletions.
6 changes: 3 additions & 3 deletions src/memoize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ export function memoizeFactory({
context?:
| ClassMethodDecoratorContext<This, (this: This, ...args: Args) => Return>
| ClassGetterDecoratorContext<This, Return>
): (this: This, ...args: Args) => Return {
) {
const cache = new Map<string, [Return, number]>();
caches?.push(cache);

return context?.kind === 'getter'
? function (this: This): Return {
? function (this: This) {
console.log(`Entering getter ${String(context.name)}.`);

const hash = calcHash(this, []);
Expand All @@ -76,7 +76,7 @@ export function memoizeFactory({
console.log(`Exiting getter ${String(context.name)}.`);
return result;
}
: function (this: This, ...args: Args): Return {
: function (this: This, ...args: Args) {
console.log(`Entering ${context ? `method ${String(context.name)}` : 'function'}(${calcHash(this, args)}).`);

const hash = calcHash(this, args);
Expand Down
6 changes: 3 additions & 3 deletions src/memoizeOne.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ export function memoizeOneFactory({
context?:
| ClassMethodDecoratorContext<This, (this: This, ...args: Args) => Return>
| ClassGetterDecoratorContext<This, Return>
): (this: This, ...args: Args) => Return {
) {
let lastCache: Return;
let lastCachedAt: number;
let lastHash: string;

return context?.kind === 'getter'
? function (this: This): Return {
? function (this: This) {
console.log(`Entering getter ${String(context.name)}.`);

const hash = calcHash(this, []);
Expand All @@ -75,7 +75,7 @@ export function memoizeOneFactory({
console.log(`Exiting getter ${String(context.name)}.`);
return lastCache;
}
: function (this: This, ...args: Args): Return {
: function (this: This, ...args: Args) {
console.log(`Entering ${context ? `method ${String(context.name)}` : 'function'}(${JSON.stringify(args)}).`);

const hash = calcHash(this, args);
Expand Down
15 changes: 4 additions & 11 deletions src/memoizeWithPersistentCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,18 @@ export function memoizeWithPersistentCacheFactory({
tryReadingCache: (persistentKey: string, hash: string) => [number, unknown] | undefined;
}) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return function <This, Args extends any[], Return>(
persistentKey: string
): (
target: ((this: This, ...args: Args) => Return) | ((...args: Args) => Return) | keyof This,
context?:
| ClassMethodDecoratorContext<This, (this: This, ...args: Args) => Return>
| ClassGetterDecoratorContext<This, Return>
) => (this: This, ...args: Args) => Return {
return function <This, Args extends any[], Return>(persistentKey: string) {
return function memoize(
target: ((this: This, ...args: Args) => Return) | ((...args: Args) => Return) | keyof This,
context?:
| ClassMethodDecoratorContext<This, (this: This, ...args: Args) => Return>
| ClassGetterDecoratorContext<This, Return>
): (this: This, ...args: Args) => Return {
) {
const cache = new Map<string, [Return, number]>();
caches?.push(cache);

return context?.kind === 'getter'
? function (this: This): Return {
? function (this: This) {
console.log(`Entering getter ${String(context.name)}.`);

const hash = calcHash(this, []);
Expand Down Expand Up @@ -126,7 +119,7 @@ export function memoizeWithPersistentCacheFactory({
console.log(`Exiting getter ${String(context.name)}.`);
return result as Return;
}
: function (this: This, ...args: { [K in keyof Args]: Args[K] }): Return {
: function (this: This, ...args: { [K in keyof Args]: Args[K] }) {
console.log(
`Entering ${context ? `method ${String(context.name)}` : 'function'}(${calcHash(this, args)}).`
);
Expand Down

0 comments on commit c46161a

Please sign in to comment.