Skip to content

Commit

Permalink
Library improvements
Browse files Browse the repository at this point in the history
- Typescript generics for AsyncQueue
- Replace .bind(this) for arrow functions
  • Loading branch information
AgustinSRG committed Apr 15, 2023
1 parent c3122ff commit 7c10e5d
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 23 deletions.
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": "@asanrom/async-tools",
"version": "1.0.0",
"version": "1.1.0",
"description": "Collection of tools to work with async functions.",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion src/async-interval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import { EventEmitter } from "events";

/**
* Interval that waits for async funcions to complete
* Interval that waits for async functions to complete
*/
export class AsyncInterval extends EventEmitter {
public interval: NodeJS.Timeout;
Expand Down
22 changes: 11 additions & 11 deletions src/async-queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ import { EventEmitter } from "events";

/**
* Async queue
* Handles each item with an async funcion
* Waits for the funcion to end before handling the next item
* Handles each item with an async function
* Waits for the function to end before handling the next item
* Allows a max size setting
*/
export class AsyncQueue extends EventEmitter {
export class AsyncQueue<T = any> extends EventEmitter {

// Config
private size: number;
private dispatchFunc: (t: any) => any;
private dispatchFunc: (t: T) => any;

// Status
private array: any[];
private array: T[];
private waiting: boolean;
private toResolve: () => any;
private destroyed: boolean;
Expand All @@ -27,7 +27,7 @@ export class AsyncQueue extends EventEmitter {
* @param size Max size of the queue. Set to 0 for no size limit.
* @param dispatchFunc Dispatch function
*/
constructor(size: number, dispatchFunc: (t: any) => any) {
constructor(size: number, dispatchFunc: (t: T) => any) {
super();

this.array = [];
Expand Down Expand Up @@ -62,7 +62,7 @@ export class AsyncQueue extends EventEmitter {
* @param item The item
* @returns true if it was added, false if it was dropped
*/
public push(item: any): boolean {
public push(item: T): boolean {
if (this.destroyed) {
this.emit('error', new Error("Push() was called after the queue was destroyed"));
return;
Expand Down Expand Up @@ -96,13 +96,13 @@ export class AsyncQueue extends EventEmitter {
this.dispatchFunc = async function () { };

// Wait if there is an item running
return new Promise<void>(function (resolve) {
return new Promise<void>(resolve => {
if (this.waiting) {
resolve();
} else {
this.toResolve = resolve;
}
}.bind(this));
});
}

private dispatchNext() {
Expand All @@ -118,10 +118,10 @@ export class AsyncQueue extends EventEmitter {

if (result instanceof Promise) {
result.then(this.dispatchNext.bind(this))
.catch(function (err: Error) {
.catch(err => {
this.emit('error', err);
this.dispatchNext();
}.bind(this));
})
} else {
this.dispatchNext();
}
Expand Down
6 changes: 3 additions & 3 deletions src/async-semaphore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ interface Waiter {

/**
* Async semaphore
* To create critical sections of async funcions
* To create critical sections of async functions
*/
export class AsyncSemaphore {
// Free Instances
Expand Down Expand Up @@ -52,14 +52,14 @@ export class AsyncSemaphore {
// Can be adquired
this.instances -= instances;
} else {
return new Promise<void>(function (resolve, reject) {
return new Promise<void>((resolve, reject) => {
// Add to queue
this.waiting.push({
requiredInstances: instances,
resolve: resolve,
reject: reject,
})
}.bind(this));
});
}
}

Expand Down
9 changes: 4 additions & 5 deletions tests/async-queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe("Async Queue", () => {
let items = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];

return new Promise<void>((resolve, reject) => {
const q: AsyncQueue = new AsyncQueue(0, async (item) => {
const q: AsyncQueue = new AsyncQueue<number>(0, async (item) => {
if (items[counter] !== item) {
q.destroy();
return reject(new Error(`Expected ${items[counter]} but found ${item}`));
Expand Down Expand Up @@ -46,7 +46,7 @@ describe("Async Queue", () => {
let items = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];

return new Promise<void>((resolve, reject) => {
const q: AsyncQueue = new AsyncQueue(0, async (item) => {
const q: AsyncQueue = new AsyncQueue<number>(0, async (item) => {
if (items[counter] !== item) {
q.destroy();
return reject(new Error(`Expected ${items[counter]} but found ${item}`));
Expand Down Expand Up @@ -77,7 +77,7 @@ describe("Async Queue", () => {
let destroyed = false;

return new Promise<void>((resolve, reject) => {
const q: AsyncQueue = new AsyncQueue(0, async (item) => {
const q: AsyncQueue = new AsyncQueue<number>(0, async (item) => {
if (destroyed) {
reject(new Error("Item handled after destroy"));
return;
Expand Down Expand Up @@ -110,7 +110,7 @@ describe("Async Queue", () => {
let destroyed = false;

return new Promise<void>((resolve, reject) => {
const q: AsyncQueue = new AsyncQueue(5, async (item) => {
const q: AsyncQueue = new AsyncQueue<number>(5, async (item) => {
if (destroyed) {
reject(new Error("Item handled after destroy"));
return;
Expand Down Expand Up @@ -141,5 +141,4 @@ describe("Async Queue", () => {
});
});
});

});

0 comments on commit 7c10e5d

Please sign in to comment.