This repository was archived by the owner on Nov 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 150
/
Copy pathQueue.ts
152 lines (132 loc) · 5.21 KB
/
Queue.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import { InvalidOperationError, ObjectDisposedError } from "./Error";
import { IDetachable } from "./IDetachable";
import { IDisposable } from "./IDisposable";
import { List } from "./List";
import { Deferred, Promise, PromiseHelper } from "./Promise";
export interface IQueue<TItem> extends IDisposable {
Enqueue(item: TItem): void;
EnqueueFromPromise(promise: Promise<TItem>): void;
Dequeue(): Promise<TItem>;
Peek(): Promise<TItem>;
Length(): number;
}
enum SubscriberType {
Dequeue,
Peek,
}
export class Queue<TItem> implements IQueue<TItem> {
private promiseStore: List<Promise<TItem>> = new List<Promise<TItem>>();
private list: List<TItem>;
private detachables: IDetachable[];
private subscribers: List<{ type: SubscriberType, deferral: Deferred<TItem> }>;
private isDrainInProgress: boolean = false;
private isDisposing: boolean = false;
private disposeReason: string = null;
public constructor(list?: List<TItem>) {
this.list = list ? list : new List<TItem>();
this.detachables = [];
this.subscribers = new List<{ type: SubscriberType, deferral: Deferred<TItem> }>();
this.detachables.push(this.list.OnAdded(this.Drain));
}
public Enqueue = (item: TItem): void => {
this.ThrowIfDispose();
this.EnqueueFromPromise(PromiseHelper.FromResult(item));
}
public EnqueueFromPromise = (promise: Promise<TItem>): void => {
this.ThrowIfDispose();
this.promiseStore.Add(promise);
promise.Finally(() => {
while (this.promiseStore.Length() > 0) {
if (!this.promiseStore.First().Result().IsCompleted) {
break;
} else {
const p = this.promiseStore.RemoveFirst();
if (!p.Result().IsError) {
this.list.Add(p.Result().Result);
} else {
// TODO: Log as warning.
}
}
}
});
}
public Dequeue = (): Promise<TItem> => {
this.ThrowIfDispose();
const deferredSubscriber = new Deferred<TItem>();
this.subscribers.Add({ deferral: deferredSubscriber, type: SubscriberType.Dequeue });
this.Drain();
return deferredSubscriber.Promise();
}
public Peek = (): Promise<TItem> => {
this.ThrowIfDispose();
const deferredSubscriber = new Deferred<TItem>();
this.subscribers.Add({ deferral: deferredSubscriber, type: SubscriberType.Peek });
this.Drain();
return deferredSubscriber.Promise();
}
public Length = (): number => {
this.ThrowIfDispose();
return this.list.Length();
}
public IsDisposed = (): boolean => {
return this.subscribers == null;
}
public DrainAndDispose = (pendingItemProcessor: (pendingItemInQueue: TItem) => void, reason?: string): Promise<boolean> => {
if (!this.IsDisposed() && !this.isDisposing) {
this.disposeReason = reason;
this.isDisposing = true;
while (this.subscribers.Length() > 0) {
const subscriber = this.subscribers.RemoveFirst();
// TODO: this needs work (Resolve(null) instead?).
subscriber.deferral.Reject("Disposed");
}
for (const detachable of this.detachables) {
detachable.Detach();
}
if (this.promiseStore.Length() > 0 && pendingItemProcessor) {
return PromiseHelper
.WhenAll(this.promiseStore.ToArray())
.ContinueWith(() => {
this.subscribers = null;
this.list.ForEach((item: TItem, index: number): void => {
pendingItemProcessor(item);
});
this.list = null;
return true;
});
} else {
this.subscribers = null;
this.list = null;
}
}
return PromiseHelper.FromResult(true);
}
public Dispose = (reason?: string): void => {
this.DrainAndDispose(null, reason);
}
private Drain = (): void => {
if (!this.isDrainInProgress && !this.isDisposing) {
this.isDrainInProgress = true;
while (this.list.Length() > 0 && this.subscribers.Length() > 0 && !this.isDisposing) {
const subscriber = this.subscribers.RemoveFirst();
if (subscriber.type === SubscriberType.Peek) {
subscriber.deferral.Resolve(this.list.First());
} else {
const dequeuedItem = this.list.RemoveFirst();
subscriber.deferral.Resolve(dequeuedItem);
}
}
this.isDrainInProgress = false;
}
}
private ThrowIfDispose = (): void => {
if (this.IsDisposed()) {
if (this.disposeReason) {
throw new InvalidOperationError(this.disposeReason);
}
throw new ObjectDisposedError("Queue");
} else if (this.isDisposing) {
throw new InvalidOperationError("Queue disposing");
}
}
}