Skip to content

Commit

Permalink
Drop events in Collector
Browse files Browse the repository at this point in the history
PR-URL: #222
  • Loading branch information
tshemsedinov committed Dec 8, 2023
1 parent 4f84c54 commit d7eeab3
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 31 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ const result = await ac;
- `exact: boolean`
- `timeout: number`
- `constructor(keys: Array<string>, options?: CollectorOptions)`
- `on(name: string, callback: Function)`
- `set(key: string, value: unknown)`
- `wait(key: string, fn: AsyncFunction, ...args?: Array<unknown>)`
- `take(key: string, fn: Function, ...args?: Array<unknown>)`
Expand Down
13 changes: 0 additions & 13 deletions lib/collector.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ class Collector {
count = 0;
exact = true;
timeout = 0;
#done = null;
#error = null;
#fulfill = null;
#reject = null;
#timer = null;
Expand All @@ -31,15 +29,6 @@ class Collector {
this.#timer = setTimeout(handler, msec);
}

on(name, callback) {
if (name === 'done') this.#done = callback;
else if (name === 'error') this.#error = callback;
if (this.done) {
if (name === 'error' && this.#cause) callback(this.#cause);
else if (name === 'done') callback(this.data);
}
}

set(key, value) {
if (this.done) return;
const has = this.data[key] !== undefined;
Expand All @@ -53,7 +42,6 @@ class Collector {
if (this.count === this.keys.length) {
this.done = true;
this.#timeout(0);
if (this.#done) this.#done(this.data);
if (this.#fulfill) this.#fulfill(this.data);
}
}
Expand Down Expand Up @@ -86,7 +74,6 @@ class Collector {
this.#timeout(0);
const err = error || new Error('Collector cancelled');
this.#cause = err;
if (this.#error) this.#error(err);
if (this.#reject) this.#reject(err);
}

Expand Down
1 change: 0 additions & 1 deletion metautil.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,6 @@ export class Collector {
exact: boolean;
timeout: number;
constructor(keys: Array<string>, options?: CollectorOptions);
on(name: string, callback: Function): void;
set(key: string, value: unknown): void;
wait(key: string, fn: AsyncFunction, ...args: Array<unknown>): void;
take(key: string, fn: Function, ...args: Array<unknown>): void;
Expand Down
33 changes: 17 additions & 16 deletions test/collector.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@ metatests.test('Collector: keys', async (test) => {

setTimeout(() => {
dc.set('key1', 1);
}, 50);

setTimeout(() => {
dc.set('key2', 2);
}, 100);

dc.on('done', (result) => {
test.strictSame(result, expectedResult);
});

const result = await dc;
test.strictSame(result, expectedResult);
test.end();
Expand All @@ -25,13 +24,8 @@ metatests.test('Collector: exact', async (test) => {
const dc = collect(['key1', 'key2']);

setTimeout(() => {
dc.set('key1', 1);
dc.set('wrongKey', 'someVal');
}, 100);

dc.on('fail', (error) => {
test.strictSame(error.message, 'Unexpected key: wrongKey');
});
}, 50);

try {
await dc;
Expand All @@ -47,7 +41,13 @@ metatests.test('Collector: not exact', async (test) => {

setTimeout(() => {
dc.set('key1', 1);
}, 50);

setTimeout(() => {
dc.set('wrongKey', 'someVal');
}, 75);

setTimeout(() => {
dc.set('key2', 2);
}, 100);

Expand All @@ -66,14 +66,19 @@ metatests.test('Collector: set after done', async (test) => {

setTimeout(() => {
dc.set('key1', 1);
}, 100);
}, 50);

setTimeout(() => {
dc.set('key2', 2);
}, 200);
}, 75);

const result = await dc;
test.strictSame(result, expectedResult);

setTimeout(() => {
dc.set('key3', 3);
}, 100);

test.end();
});

Expand Down Expand Up @@ -169,10 +174,6 @@ metatests.test('Collector: after done', async (test) => {
dc.set('key1', 1);
dc.set('key2', 2);

dc.on('done', (result) => {
test.strictSame(result, expectedResult);
});

const result = await dc;
test.strictSame(result, expectedResult);
test.end();
Expand Down

0 comments on commit d7eeab3

Please sign in to comment.