Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default Values in Collector #231

Merged
merged 16 commits into from
Dec 20, 2023
12 changes: 10 additions & 2 deletions lib/collector.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@ class Collector {
count = 0;
exact = true;
timeout = 0;
defaultValue = {};
#fulfill = null;
#reject = null;
#timer = null;
#cause = null;

constructor(keys, { exact = true, timeout = 0 } = {}) {
constructor(keys, { exact = true, defaultValue = {}, timeout = 0 } = {}) {
this.keys = keys;
if (exact === false) this.exact = false;
if (typeof timeout === 'number') this.#timeout(timeout);
if (typeof defaultValue === 'object') this.defaultValue = defaultValue;
}

#timeout(msec) {
Expand All @@ -25,7 +27,13 @@ class Collector {
this.#timer = null;
}
if (msec === 0) return;
const handler = () => this.fail(new Error('Collector timed out'));
const handler = () => {
axbuglak marked this conversation as resolved.
Show resolved Hide resolved
if (Object.keys(this.defaultValue).length)
for (const [key, value] of Object.entries(this.defaultValue)) {
this.set(key, value);
}
else this.fail(new Error('Collector timed out'));
tshemsedinov marked this conversation as resolved.
Show resolved Hide resolved
};
this.#timer = setTimeout(handler, msec);
}

Expand Down
2 changes: 2 additions & 0 deletions metautil.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ export function sizeToBytes(size: string): number;

export interface CollectorOptions {
exact?: boolean;
defaultValue?: object;
tshemsedinov marked this conversation as resolved.
Show resolved Hide resolved
timeout?: number;
}

Expand All @@ -246,6 +247,7 @@ export class Collector {
count: number;
exact: boolean;
timeout: number;
defaultValue: object;
tshemsedinov marked this conversation as resolved.
Show resolved Hide resolved
constructor(keys: Array<string>, options?: CollectorOptions);
set(key: string, value: unknown): void;
wait(key: string, fn: AsyncFunction, ...args: Array<unknown>): void;
Expand Down
21 changes: 21 additions & 0 deletions test/collector.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,27 @@ metatests.test('Collector: timeout', async (test) => {
}
});

metatests.test('Collector: defaultValue', async (test) => {
axbuglak marked this conversation as resolved.
Show resolved Hide resolved
const defaultValue = { key1: 1 };

const dc1 = collect(['key1'], { defaultValue, timeout: 50 });
const dc2 = collect(['key1'], { defaultValue: {}, timeout: 50 });

setTimeout(() => {
dc1.set('key1', 2);
dc2.set('key1', 2);
}, 100);

try {
const result = await dc1;
test.strictSame(result, defaultValue);
await dc2;
} catch (error) {
test.strictSame(error.message, 'Collector timed out');
}
test.end();
});

metatests.test('Collector: fail', async (test) => {
const dc = collect(['key1']);

Expand Down