Skip to content

Commit

Permalink
fix: cooldown adapter async
Browse files Browse the repository at this point in the history
  • Loading branch information
socram03 committed Sep 1, 2024
1 parent 65260d6 commit 1e41e44
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 38 deletions.
3 changes: 2 additions & 1 deletion packages/cooldown/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"dev": "tsc --watch",
"build": "tsc",
"lint": "biome lint --write ./src",
"format": "biome format --write ./src"
"format": "biome format --write ./src",
"test": "node --test ./test/*"
},
"devDependencies": {
"@types/node": "^22.4.0",
Expand Down
76 changes: 39 additions & 37 deletions packages/cooldown/src/manager.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { UsingClient } from 'seyfert';
import { type CooldownData, type CooldownDataInsert, Cooldowns, type CooldownType } from './resource';
import { getMilliseconds } from './clock';
import { fakePromise } from 'seyfert/lib/common';

export class CooldownManager {
resource: Cooldowns;
Expand All @@ -27,16 +28,18 @@ export class CooldownManager {
const data = this.getData(name);
if (!data) return false;

const cooldown = this.resource.get(`${name}:${data.type}:${target}`);
if (!cooldown) {
this.set(name, target, { type: data.type, interval: data.interval, remaining: data.refill - data.tokens });
return false;
}
return fakePromise(this.resource.get(`${name}:${data.type}:${target}`)).then(cooldown => {
if (!cooldown) {
return fakePromise(
this.set(name, target, { type: data.type, interval: data.interval, remaining: data.refill - data.tokens }),
).then(() => false);
}

const remaining = cooldown.remaining - data.tokens;
const remaining = cooldown.remaining - data.tokens;

if (remaining <= 0) return true;
return false;
if (remaining <= 0) return true;
return false;
});
}

/**
Expand All @@ -50,21 +53,22 @@ export class CooldownManager {
const data = this.getData(name);
if (!data) return;

const cooldown = this.resource.get(`${name}:${data.type}:${target}`);
if (!cooldown) {
this.set(name, target, {
type: data.type,
interval: data.interval,
remaining: data.refill - (tokens ?? data.tokens),
return fakePromise(this.resource.get(`${name}:${data.type}:${target}`)).then(cooldown => {
if (!cooldown) {
return fakePromise(
this.set(name, target, {
type: data.type,
interval: data.interval,
remaining: data.refill - (tokens ?? data.tokens),
}),
).then(() => true);
}

return fakePromise(this.drip(name, target, data, cooldown)).then(drip => {
if (drip.remaining >= data.tokens) return false;
return true;
});
return true;
}

const drip = this.drip(name, target, data, cooldown);

if (drip.remaining <= 0) return false;

return true;
});
}

/**
Expand All @@ -79,18 +83,17 @@ export class CooldownManager {

const refill = tokens ?? data.refill;

const cooldown = this.resource.get(`${name}:${data.type}:${target}`);
if (!cooldown) {
this.set(name, target, {
type: data.type,
interval: data.interval,
remaining: refill,
});
return true;
}

this.set(name, target, { type: data.type, interval: data.interval, remaining: refill });
return true;
return fakePromise(this.resource.get(`${name}:${data.type}:${target}`)).then(cooldown => {
if (!cooldown) {
return fakePromise(
this.set(name, target, { type: data.type, interval: data.interval, remaining: refill }),
).then(() => true);
}

return fakePromise(this.set(name, target, { type: data.type, interval: data.interval, remaining: refill })).then(
() => true,
);
});
}

/**
Expand All @@ -100,7 +103,7 @@ export class CooldownManager {
* @param data - The cooldown data to set
*/
set(name: string, target: string, data: CooldownDataInsert & { type: `${CooldownType}` }) {
this.resource.set(`${name}:${data.type}:${target}`, data);
return fakePromise(this.resource.set(`${name}:${data.type}:${target}`, data)).then(() => {});
}

/**
Expand All @@ -119,8 +122,7 @@ export class CooldownManager {
const dripAmount = deltaMS * (props.refill / props.interval);
data.remaining = Math.min(data.remaining + dripAmount, props.refill);
const result = { type: props.type, interval: props.interval, remaining: data.remaining };
this.set(name, target, result);
return result;
return fakePromise(this.set(name, target, result)).then(() => result);
}
}

Expand Down

0 comments on commit 1e41e44

Please sign in to comment.