From 1ba3770c306aa147d83c5ef51c357c2b8a9954a4 Mon Sep 17 00:00:00 2001 From: Ron Buckton Date: Fri, 15 Jul 2016 12:07:29 -0700 Subject: [PATCH] Added size to AsyncStack/AsyncQueue --- docs/scheduling.md | 12 ++++++++++++ package.json | 2 +- src/lib/queue.ts | 15 +++++++++++++++ src/lib/stack.ts | 16 ++++++++++++++++ src/tests/queue.ts | 12 ++++++++++++ src/tests/stack.ts | 15 +++++++++++++++ 6 files changed, 71 insertions(+), 1 deletion(-) diff --git a/docs/scheduling.md b/docs/scheduling.md index a929f26..56138e9 100644 --- a/docs/scheduling.md +++ b/docs/scheduling.md @@ -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) @@ -36,6 +38,11 @@ export declare class AsyncQueue { Initializes a new instance of the AsyncQueue class. * `iterable` [<Iterable>][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. @@ -62,6 +69,11 @@ export declare class AsyncStack { Initializes a new instance of the AsyncStack class. * `iterable` [<Iterable>][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. diff --git a/package.json b/package.json index 3636038..49fbcb9 100644 --- a/package.json +++ b/package.json @@ -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": [ diff --git a/src/lib/queue.ts b/src/lib/queue.ts index 4590c75..2e29654 100644 --- a/src/lib/queue.ts +++ b/src/lib/queue.ts @@ -29,6 +29,21 @@ export class AsyncQueue { } } + /** + * 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. diff --git a/src/lib/stack.ts b/src/lib/stack.ts index 75463c8..d87a2af 100644 --- a/src/lib/stack.ts +++ b/src/lib/stack.ts @@ -29,6 +29,22 @@ export class AsyncStack { } } + /** + * 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. diff --git a/src/tests/queue.ts b/src/tests/queue.ts index c19660e..dca3602 100644 --- a/src/tests/queue.ts +++ b/src/tests/queue.ts @@ -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(); 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(); 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(); 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); }); }); diff --git a/src/tests/stack.ts b/src/tests/stack.ts index 33f5f09..13674bf 100644 --- a/src/tests/stack.ts +++ b/src/tests/stack.ts @@ -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(); 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(); 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(); 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(); 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); }); });