-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(@kubekit/client): support finalizeHandler and initializedHandler
- Loading branch information
1 parent
299953f
commit 1f3d1a4
Showing
5 changed files
with
195 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 34 additions & 9 deletions
43
packages/kubekit-client/src/client/concurrent_task_runner.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { sleep } from './sleep'; | ||
import { type ObjectReference } from './types'; | ||
|
||
type CacheValue = Record<string, number>; | ||
|
||
export class Debounce { | ||
#cache = new Map<string, CacheValue>(); | ||
#waitMilliSeconds: number; | ||
#maxWaitMilliSeconds: number; | ||
#latestVersion: string; | ||
|
||
constructor(waitMilliSeconds: number, maxWaitMilliSeconds: number) { | ||
this.#waitMilliSeconds = waitMilliSeconds; | ||
this.#maxWaitMilliSeconds = maxWaitMilliSeconds; | ||
this.#latestVersion = ''; | ||
} | ||
|
||
public static getCacheKey({ namespace, name }: ObjectReference) { | ||
return `${namespace || ''}/${name}`; | ||
} | ||
|
||
pushLatestItem(cacheKey: string, version: string) { | ||
this.#latestVersion = version; | ||
this.#cache.set(cacheKey, { | ||
...(this.#cache.get(cacheKey) || {}), | ||
[version]: Number(new Date()), | ||
}); | ||
} | ||
|
||
async skipOrExec(cacheKey: string, version: string, func: () => unknown | Promise<unknown>) { | ||
const now = Number(new Date()); | ||
const getCurrentPushedAt = this.#cache.get(cacheKey)?.[version] || now; | ||
const currentElapsedTime = now - getCurrentPushedAt; | ||
const getOldestPushedAt = Object.values(this.#cache.get(cacheKey) || {})[0] || now; | ||
const oldestElapsedTime = now - getOldestPushedAt; | ||
|
||
if (this.#maxWaitMilliSeconds > oldestElapsedTime && this.#waitMilliSeconds > currentElapsedTime) { | ||
await sleep(this.#waitMilliSeconds - currentElapsedTime); | ||
} | ||
if (version === this.#latestVersion) { | ||
this.#cache.delete(cacheKey); | ||
await func(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.