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 pathStream.ts
134 lines (116 loc) · 3.75 KB
/
Stream.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
import { InvalidOperationError } from "./Error";
import { CreateNoDashGuid } from "./Guid";
import { IStringDictionary } from "./IDictionary";
import { Promise } from "./Promise";
import { Queue } from "./Queue";
import { IStreamChunk } from "./Stream";
export interface IStreamChunk<TBuffer> {
IsEnd: boolean;
Buffer: TBuffer;
}
export class Stream<TBuffer> {
private id: string;
private readerIdCounter: number = 1;
private streambuffer: Array<IStreamChunk<TBuffer>>;
private isEnded: boolean = false;
private readerQueues: IStringDictionary<Queue<IStreamChunk<TBuffer>>>;
public constructor(streamId?: string) {
this.id = streamId ? streamId : CreateNoDashGuid();
this.streambuffer = [];
this.readerQueues = {};
}
public get IsClosed(): boolean {
return this.isEnded;
}
public get Id(): string {
return this.id;
}
public Write = (buffer: TBuffer): void => {
this.ThrowIfClosed();
this.WriteStreamChunk({
Buffer: buffer,
IsEnd: false,
});
}
public GetReader = (): StreamReader<TBuffer> => {
const readerId = this.readerIdCounter;
this.readerIdCounter++;
const readerQueue = new Queue<IStreamChunk<TBuffer>>();
const currentLength = this.streambuffer.length;
this.readerQueues[readerId] = readerQueue;
for (let i = 0; i < currentLength; i++) {
readerQueue.Enqueue(this.streambuffer[i]);
}
return new StreamReader(
this.id,
readerQueue,
() => {
delete this.readerQueues[readerId];
});
}
public Close = (): void => {
if (!this.isEnded) {
this.WriteStreamChunk({
Buffer: null,
IsEnd: true,
});
this.isEnded = true;
}
}
private WriteStreamChunk = (streamChunk: IStreamChunk<TBuffer>): void => {
this.ThrowIfClosed();
this.streambuffer.push(streamChunk);
for (const readerId in this.readerQueues) {
if (!this.readerQueues[readerId].IsDisposed()) {
try {
this.readerQueues[readerId].Enqueue(streamChunk);
} catch (e) {
// Do nothing
}
}
}
}
private ThrowIfClosed = (): void => {
if (this.isEnded) {
throw new InvalidOperationError("Stream closed");
}
}
}
// tslint:disable-next-line:max-classes-per-file
export class StreamReader<TBuffer> {
private readerQueue: Queue<IStreamChunk<TBuffer>>;
private onClose: () => void;
private isClosed: boolean = false;
private streamId: string;
public constructor(streamId: string, readerQueue: Queue<IStreamChunk<TBuffer>>, onClose: () => void) {
this.readerQueue = readerQueue;
this.onClose = onClose;
this.streamId = streamId;
}
public get IsClosed(): boolean {
return this.isClosed;
}
public get StreamId(): string {
return this.streamId;
}
public Read = (): Promise<IStreamChunk<TBuffer>> => {
if (this.IsClosed) {
throw new InvalidOperationError("StreamReader closed");
}
return this.readerQueue
.Dequeue()
.OnSuccessContinueWith((streamChunk: IStreamChunk<TBuffer>) => {
if (streamChunk.IsEnd) {
this.readerQueue.Dispose("End of stream reached");
}
return streamChunk;
});
}
public Close = (): void => {
if (!this.isClosed) {
this.isClosed = true;
this.readerQueue.Dispose("StreamReader closed");
this.onClose();
}
}
}