Skip to content

Commit

Permalink
0.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
nknapp committed Apr 21, 2024
1 parent a56ed06 commit 1ffa8e5
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

# Upcoming

# v0.3.0

Date: 2024-04-21T22:11:04.415Z

- Performance: Better handling of wait timeouts. Instead of waiting in intervals of 50ms, we check the result whenever new results are
added to the bin.
- Feature: "clear" method to remove all objects
Expand Down
29 changes: 27 additions & 2 deletions dist/QueryBin.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
class QueryBin {
constructor(
queries = {},
{ retryDelayMillis = 50, timeoutMillis = 1000 } = {},
{ retryDelayMillis = 200, timeoutMillis = 1000 } = {},
) {
this.values = [];
this.retryDelayMillis = retryDelayMillis;
this.timeoutMillis = timeoutMillis;

this.latch = new Latch();

this.queryAll = mapValues(queries, (factory) => {
return (...args) => {
const { queryAll } = factory(...args);
Expand Down Expand Up @@ -68,10 +71,16 @@ class QueryBin {

add(value) {
this.values.push(value);
this.latch.next();
}

addAll(values) {
this.values.push(...values);
this.latch.next();
}

clear() {
this.values = [];
}

all() {
Expand Down Expand Up @@ -101,7 +110,8 @@ class QueryBin {
try {
return await fn();
} catch (e) {
await delay(this.retryDelayMillis);
const remaining = this.timeoutMillis - (Date.now() - started);
await Promise.race([delay(remaining), this.latch.promise]);
}
}
await fn();
Expand All @@ -120,4 +130,19 @@ function delay(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}

class Latch {
constructor() {
this.promise = new Promise((resolve) => {
this.open = resolve;
});
}

next() {
this.open();
this.promise = new Promise((resolve) => {
this.open = resolve;
});
}
}

exports.QueryBin = QueryBin;
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "query-bin",
"version": "0.2.1",
"version": "0.3.0",
"description": "A list with queries like in the 'testing-library'",
"types": "index.d.s",
"type": "module",
Expand Down

0 comments on commit 1ffa8e5

Please sign in to comment.