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

added strong typing for tokens; bumped jest-related packages #49

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "async-sema",
"version": "3.1.1",
"version": "3.1.2",
"description": "Semaphore using `async` and `await`",
"repository": {
"type": "git",
Expand Down Expand Up @@ -29,13 +29,13 @@
"test": "jest"
},
"devDependencies": {
"@types/jest": "27.0.1",
"@types/jest": "29.2.3",
"@types/node": "16.6.1",
"jest": "27.0.6",
"jest": "29.3.1",
"lint-staged": "11.1.2",
"pre-commit": "1.2.2",
"prettier": "2.3.2",
"ts-jest": "27.0.4",
"ts-jest": "29.0.3",
"typescript": "4.3.5"
},
"pre-commit": "lint:staged",
Expand Down
33 changes: 18 additions & 15 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ function getCapacity(capacity: number) {

// Deque is based on https://github.com/petkaantonov/deque/blob/master/js/deque.js
// Released under the MIT License: https://github.com/petkaantonov/deque/blob/6ef4b6400ad3ba82853fdcc6531a38eb4f78c18c/LICENSE
class Deque {
class Deque<T> {
private _capacity: number;
private _length: number;
private _front: number;
private arr: Array<any>;
private arr: Array<T | void>;

constructor(capacity: number) {
this._capacity = getCapacity(capacity);
Expand All @@ -43,7 +43,7 @@ class Deque {
this.arr = [];
}

push(item: any): number {
push(item: T): number {
const length = this._length;

this.checkCapacity(length + 1);
Expand All @@ -54,7 +54,7 @@ class Deque {
return length + 1;
}

pop() {
pop(): T | void {
const length = this._length;
if (length === 0) {
return void 0;
Expand Down Expand Up @@ -109,14 +109,17 @@ function isFn(x: any) {
return typeof x === 'function';
}

function defaultInit() {
return '1';
function defaultInit<T = any>(): T {
return '1' as any;
}

export class Sema {
export class Sema<T = any> {
private nrTokens: number;
private free: Deque;
private waiting: Deque;
private free: Deque<T>;
private waiting: Deque<{
resolve: (value: T | PromiseLike<T>) => void;
reject: (reason?: any) => void;
}>;
private releaseEmitter: EventEmitter;
private noTokens: boolean;
private pauseFn?: () => void;
Expand Down Expand Up @@ -152,7 +155,7 @@ export class Sema {
this.resumeFn = resumeFn;
this.paused = false;

this.releaseEmitter.on('release', (token) => {
this.releaseEmitter.on('release', (token: T) => {
const p = this.waiting.shift();
if (p) {
p.resolve(token);
Expand All @@ -171,18 +174,18 @@ export class Sema {
}
}

tryAcquire(): any | undefined {
tryAcquire(): T | void {
return this.free.pop();
}

async acquire(): Promise<any> {
async acquire(): Promise<T> {
let token = this.tryAcquire();

if (token !== void 0) {
return token;
}

return new Promise((resolve, reject) => {
return new Promise<T>((resolve, reject) => {
if (this.pauseFn && !this.paused) {
this.paused = true;
this.pauseFn();
Expand All @@ -192,11 +195,11 @@ export class Sema {
});
}

release(token?: any): void {
release(token?: T): void {
this.releaseEmitter.emit('release', this.noTokens ? '1' : token);
}

drain(): Promise<any[]> {
drain(): Promise<T[]> {
const a = new Array(this.nrTokens);
for (let i = 0; i < this.nrTokens; i++) {
a[i] = this.acquire();
Expand Down