Skip to content

Commit 9083b0c

Browse files
committed
use a linked list for the async iterable queue so that eviction is O(1)
1 parent 86f9ef8 commit 9083b0c

3 files changed

Lines changed: 12 additions & 7 deletions

File tree

packages/polling/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"watch": "tsc --build --watch"
3939
},
4040
"dependencies": {
41+
"@lumino/collections": "^2.0.4",
4142
"@lumino/coreutils": "^2.2.2",
4243
"@lumino/disposable": "^2.1.5",
4344
"@lumino/signaling": "^2.1.5"

packages/polling/src/poll.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (c) Jupyter Development Team.
22
// Distributed under the terms of the Modified BSD License.
33

4+
import { LinkedList } from '@lumino/collections';
5+
46
import { JSONExt, PromiseDelegate } from '@lumino/coreutils';
57

68
import { IObservableDisposable } from '@lumino/disposable';
@@ -142,20 +144,21 @@ export class Poll<T = any, U = any, V extends string = 'standby'>
142144
* Return an async iterator that yields every tick.
143145
*/
144146
async *[Symbol.asyncIterator](): AsyncIterableIterator<IPoll.State<T, U, V>> {
145-
const queue: IPoll.State<T, U, V>[] = [this.state];
147+
const queue = new LinkedList<IPoll.State<T, U, V>>();
146148
const enqueue = (_: unknown, state: IPoll.State<T, U, V>) => {
147-
if (queue.length >= Private.MAX_QUEUE_SIZE) {
148-
queue.shift();
149+
if (queue.size >= Private.MAX_QUEUE_SIZE) {
150+
queue.removeFirst();
149151
}
150-
queue.push(state);
152+
queue.addLast(state);
151153
};
154+
queue.addLast(this.state);
152155
this.ticked.connect(enqueue);
153156
try {
154157
while (!this.isDisposed) {
155-
if (queue.length) {
156-
yield queue.shift()!;
157-
} else {
158+
if (queue.isEmpty) {
158159
await this.tick.catch(() => undefined);
160+
} else {
161+
yield queue.removeFirst()!;
159162
}
160163
}
161164
} finally {

yarn.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,7 @@ __metadata:
697697
resolution: "@lumino/polling@workspace:packages/polling"
698698
dependencies:
699699
"@lumino/buildutils": ^2.0.3
700+
"@lumino/collections": ^2.0.4
700701
"@lumino/coreutils": ^2.2.2
701702
"@lumino/disposable": ^2.1.5
702703
"@lumino/signaling": ^2.1.5

0 commit comments

Comments
 (0)