Skip to content

Commit

Permalink
use fast-list
Browse files Browse the repository at this point in the history
  • Loading branch information
slikts committed Oct 2, 2018
1 parent b37b8d6 commit d16c58b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 12 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ A library for turning push-based collections like streams into pull-based ones t

* Buffers pushed and pulled values
* Well-typed with TypeScript
* Lightweight, no dependencies
* [Lightweight], fast
* Full test coverage

## Explanation
Expand Down Expand Up @@ -113,3 +113,4 @@ To make TypeScript know about the asnyc iterable types (`AsyncIterable<T>`, `Asy
[callback-to-async-iterator]: https://github.com/withspectrum/callback-to-async-iterator
[async]: http://2ality.com/2016/10/asynchronous-iteration.html
[options]: https://www.typescriptlang.org/docs/handbook/compiler-options.html
[lightweight]: https://bundlephobia.com/result?p=queueable
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,15 @@
"tslint-config-slikts": "^2.0.2",
"typedoc": "^0.12.0",
"typescript": "^3.0.1",
"validate-commit-msg": "^2.12.2"
"validate-commit-msg": "^2.12.2",
"@types/fast-list": "^1.0.0"
},
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
"dependencies": {
"fast-list": "^1.0.3"
}
}
6 changes: 3 additions & 3 deletions src/Balancer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export default class Balancer<A> implements AsyncIterableIterator<A> {
};
this.resolverQueue.dequeueDefault(
resolve => resolve(result),
() => void this.resultQueue.push(result),
() => void this.resultQueue.enqueue(result),
);
}

Expand All @@ -83,8 +83,8 @@ export default class Balancer<A> implements AsyncIterableIterator<A> {
if (!this.closed) {
this.closed = true;
// Clear the queues
this.resultQueue.length = 0;
this.resolverQueue.length = 0;
this.resultQueue.clear();
this.resolverQueue.clear();
}
return {
done: true,
Expand Down
27 changes: 20 additions & 7 deletions src/Queue.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
import * as FastList from 'fast-list';

// prevents rollup Error: Cannot call a namespace
const fastList = FastList;

/**
* First-in, first-out (FIFO) buffer (queue) with default item values.
*/
export default class Queue<A> extends Array<A> {
export default class Queue<A> {
private list: FastList.List<A>;
constructor() {
this.list = fastList();
}
/**
* Add an item to the end of the queue.
*
* @param a Item to be enqueued
* @param value Item to be enqueued
*/
enqueue(a: A) {
this.push(a);
enqueue(value: A) {
this.list.push(value);
}

/**
Expand All @@ -19,9 +28,13 @@ export default class Queue<A> extends Array<A> {
* @param init Callback for making the default value
*/
dequeueDefault<B>(handle: (a: A) => B, init: () => B): B {
if (this.length) {
return handle(this.shift() as A);
if (!this.list.length) {
return init();
}
return init();
return handle(this.list.shift() as A);
}

clear() {
this.list.drop();
}
}

0 comments on commit d16c58b

Please sign in to comment.