Skip to content

Commit

Permalink
Added size to AsyncStack/AsyncQueue
Browse files Browse the repository at this point in the history
  • Loading branch information
rbuckton committed Jul 15, 2016
1 parent 85da900 commit 1ba3770
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 1 deletion.
12 changes: 12 additions & 0 deletions docs/scheduling.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ See LICENSE file in the project root for details.
### Table of Contents
* [Class: AsyncQueue](#class-asyncqueue)
* [new AsyncQueue(iterable?)](#new-asyncqueueiterable)
* [queue.size](#queuesize)
* [queue.put(value)](#queueputvalue)
* [queue.get()](#queueget)
* [Class: AsyncStack](#class-asyncstack)
* [new AsyncStack(iterable?)](#new-asyncstackiterable)
* [stack.size](#stacksize)
* [stack.push(value)](#stackpushvalue)
* [stack.pop()](#stackpop)
* [Function: delay(msec, value?)](#function-delaymsec-value)
Expand All @@ -36,6 +38,11 @@ export declare class AsyncQueue<T> {
Initializes a new instance of the AsyncQueue class.
* `iterable` [&lt;Iterable&gt;][Iterable] An optional iterable of values or promises.

## queue.size
Gets the number of entries in the queue.
When positive, indicates the number of entries available to get.
When negative, indicates the number of requests waiting to be fulfilled.

## queue.put(value)
Adds a value to the end of the queue. If the queue is empty but has a pending
dequeue request, the value will be dequeued and the request fulfilled.
Expand All @@ -62,6 +69,11 @@ export declare class AsyncStack<T> {
Initializes a new instance of the AsyncStack class.
* `iterable` [&lt;Iterable&gt;][Iterable] An optional iterable of values or promises.

## stack.size
Gets the number of entries in the stack.
When positive, indicates the number of entries available to get.
When negative, indicates the number of requests waiting to be fulfilled.

## stack.push(value)
Adds a value to the top of the stack. If the stack is empty but has a pending
pop request, the value will be popped and the request fulfilled.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "prex",
"version": "0.1.1",
"version": "0.2.0",
"description": "Async coordination primitives and extensions on top of ES6 Promises",
"license": "Apache-2.0",
"keywords": [
Expand Down
15 changes: 15 additions & 0 deletions src/lib/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,21 @@ export class AsyncQueue<T> {
}
}

/**
* Gets the number of entries in the queue.
* When positive, indicates the number of entries available to get.
* When negative, indicates the number of requests waiting to be fulfilled.
*/
public get size() {
if (this._available && this._available.length > 0) {
return this._available.length;
}
if (this._pending && this._pending.length > 0) {
return -this._pending.length;
}
return 0;
}

/**
* Adds a value to the end of the queue. If the queue is empty but has a pending
* dequeue request, the value will be dequeued and the request fulfilled.
Expand Down
16 changes: 16 additions & 0 deletions src/lib/stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,22 @@ export class AsyncStack<T> {
}
}

/**
* Gets the number of entries in the stack.
* When positive, indicates the number of entries available to get.
* When negative, indicates the number of requests waiting to be fulfilled.
*/
public get size() {
if (this._available && this._available.length > 0) {
return this._available.length;
}
if (this._pending && this._pending.length > 0) {
return -this._pending.length;
}
return 0;
}


/**
* Adds a value to the top of the stack. If the stack is empty but has a pending
* pop request, the value will be popped and the request fulfilled.
Expand Down
12 changes: 12 additions & 0 deletions src/tests/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,47 @@ describe("async queue", () => {
})
it("from iterable", async () => {
const queue = new AsyncQueue([1, 2, 3]);
const sizeAfterConstruct = queue.size;
const value1 = await queue.get();
const value2 = await queue.get();
const value3 = await queue.get();
assert.strictEqual(value1, 1);
assert.strictEqual(value2, 2);
assert.strictEqual(value3, 3);
assert.strictEqual(sizeAfterConstruct, 3);
assert.strictEqual(queue.size, 0);
});
});
it("put1 get1", async () => {
const queue = new AsyncQueue<number>();
queue.put(1);
const sizeAfterPut = queue.size;
const value = await queue.get();
assert.strictEqual(value, 1);
assert.strictEqual(sizeAfterPut, 1);
assert.strictEqual(queue.size, 0);
});
it("get1 put1", async () => {
const queue = new AsyncQueue<number>();
const getPromise = queue.get();
const sizeAfterGet = queue.size;
await Promise.resolve();
queue.put(1);
const value = await getPromise;
assert.strictEqual(value, 1);
assert.strictEqual(sizeAfterGet, -1);
assert.strictEqual(queue.size, 0);
});
it("put2 get2", async () => {
const queue = new AsyncQueue<number>();
queue.put(1);
queue.put(2);
const sizeAfterPut = queue.size;
const value1 = await queue.get();
const value2 = await queue.get();
assert.strictEqual(value1, 1);
assert.strictEqual(value2, 2);
assert.strictEqual(sizeAfterPut, 2);
assert.strictEqual(queue.size, 0);
});
});
15 changes: 15 additions & 0 deletions src/tests/stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,47 +15,62 @@ describe("async stack", () => {
})
it("from iterable", async () => {
const stack = new AsyncStack([1, 2, 3]);
const sizeAfterConstruct = stack.size;
const value1 = await stack.pop();
const value2 = await stack.pop();
const value3 = await stack.pop();
assert.strictEqual(value1, 3);
assert.strictEqual(value2, 2);
assert.strictEqual(value3, 1);
assert.strictEqual(sizeAfterConstruct, 3);
assert.strictEqual(stack.size, 0);
});
});

it("push1 pop1", async () => {
const stack = new AsyncStack<number>();
stack.push(1);
const sizeAfterPush = stack.size;
const value = await stack.pop();
assert.strictEqual(value, 1);
assert.strictEqual(sizeAfterPush, 1);
assert.strictEqual(stack.size, 0);
});
it("pop1 push1", async () => {
const stack = new AsyncStack<number>();
const popPromise = stack.pop();
const sizeAfterPop = stack.size;
await Promise.resolve();
stack.push(1);
const value = await popPromise;
assert.strictEqual(value, 1);
assert.strictEqual(sizeAfterPop, -1);
assert.strictEqual(stack.size, 0);
});
it("push2 pop2", async () => {
const stack = new AsyncStack<number>();
stack.push(1);
stack.push(2);
const sizeAfterPush = stack.size;
const value1 = await stack.pop();
const value2 = await stack.pop();
assert.strictEqual(value1, 2);
assert.strictEqual(value2, 1);
assert.strictEqual(sizeAfterPush, 2);
assert.strictEqual(stack.size, 0);
});
it("pop2 push2", async () => {
const stack = new AsyncStack<number>();
const pop1 = stack.pop();
const pop2 = stack.pop();
const sizeAfterPop = stack.size;
stack.push(1);
stack.push(2);
const value1 = await pop1;
const value2 = await pop2;
assert.strictEqual(value1, 1);
assert.strictEqual(value2, 2);
assert.strictEqual(sizeAfterPop, -2);
assert.strictEqual(stack.size, 0);
});
});

0 comments on commit 1ba3770

Please sign in to comment.