Skip to content

Commit 1aaa3df

Browse files
committed
feat: work on new memoize
1 parent 3469164 commit 1aaa3df

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

packages/core/src/shared/awsClientBuilderV3.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import { extensionVersion } from './vscode/env'
3333
import { getLogger } from './logger/logger'
3434
import { partialClone } from './utilities/collectionUtils'
3535
import { selectFrom } from './utilities/tsUtils'
36+
import { memoize } from './utilities/functionUtils'
3637

3738
export type AwsClientConstructor<C> = new (o: AwsClientOptions) => C
3839

@@ -77,6 +78,8 @@ export class AWSClientBuilderV3 {
7778
return shim
7879
}
7980

81+
public getAwsService = memoize(this.createAwsService.bind(this))
82+
8083
public async createAwsService<C extends AwsClient>(
8184
type: AwsClientConstructor<C>,
8285
options?: Partial<AwsClientOptions>,

packages/core/src/shared/utilities/functionUtils.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,18 @@ export function memoize<T, U extends any[]>(fn: (...args: U) => T): (...args: U)
7777
return (...args) => (cache[args.map(String).join(':')] ??= fn(...args))
7878
}
7979

80+
export function memoizeAsync<T, U extends any[]>(fn: (...args: U) => Promise<T>): (...args: U) => Promise<T> {
81+
const cache: Map<string, T> = new Map()
82+
83+
return async (...args) => {
84+
const key = args.map(String).join(':')
85+
if (!cache.has(key)) {
86+
cache.set(key, await fn(...args))
87+
}
88+
return cache.get(key)!
89+
}
90+
}
91+
8092
/**
8193
* Prevents a function from executing until {@link delay} milliseconds have passed
8294
* since the last invocation. Omitting {@link delay} will not execute the function for

0 commit comments

Comments
 (0)