Skip to content

Commit

Permalink
debounceのロジックを改善しました
Browse files Browse the repository at this point in the history
  • Loading branch information
kahirokunn committed Apr 15, 2024
1 parent 350aade commit 68d669f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 12 deletions.
2 changes: 1 addition & 1 deletion packages/kubekit-client/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@kubekit/client",
"version": "0.0.17",
"version": "0.0.19",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"author": "kahirokunn",
Expand Down
17 changes: 6 additions & 11 deletions packages/kubekit-client/src/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,20 +392,15 @@ class Debounce {
return max;
};

const getSortedAddedAt = () =>
Object.values(this.#cache.get(cacheKey) || {})
.sort()
.reverse();
const now = Number(new Date())
const getCurrentPushedAt = this.#cache.get(cacheKey)?.[resourceVersion] || now;
const currentElapsedTime = now - getCurrentPushedAt
const getOldestPushedAt = Object.values(this.#cache.get(cacheKey) || {})[0] || now;
const oldestElapsedTime = now - getOldestPushedAt

const sortedAddedAt = getSortedAddedAt();
const isMaxWaitTimeReached =
sortedAddedAt.length >= 2 &&
sortedAddedAt[0] - sortedAddedAt[sortedAddedAt.length - 1] >= this.#maxWaitMilliSeconds;

if (!isMaxWaitTimeReached) {
if (this.#maxWaitMilliSeconds > oldestElapsedTime && this.#waitMilliSeconds > currentElapsedTime) {
await sleep(this.#waitMilliSeconds);
}
// 1. resourceVersionは文字列で比較できる様にする
if (BigInt(resourceVersion) === getLatestResourceVersion()) {
this.#cache.delete(cacheKey);
await func();
Expand Down

0 comments on commit 68d669f

Please sign in to comment.