Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 | 3x 3x 660x 660x 3x 22x 22x 660x 396x 374x 22x 22x 3x 22x 22x 21x 1x 22x 3x 1x 22x 1x 1x 22x 16x 1x 22x 22x 22x 1x 21x 22x 22x 22x 22x 22x 22x 22x 22x 22x 15x 9x 4x 4x 5x 5x 4x 4x 24x 22x 5x 2x 4x 2x 1x 22x 1x 1x 1x 1x 1x | import Stream from "./stream";
import { StreamCallback, StreamInterface } from "./types";
interface constructorParams {
broadcast: boolean;
onListen?: StreamCallback;
onPause?: StreamCallback;
onResume?: StreamCallback;
onCancel?: StreamCallback;
}
// to mirror the dart stream interface, we want to get rid of the extra
// methods created to add and listen to events on the stream directly.
/** @ignore */
const _validProp = (prop: string) => {
const blacklist = new Set([
"addEventListener",
"removeEventListener",
"add",
"addError",
"pause",
"resume",
"cancel",
"close",
"isPaused",
"isClosed",
]);
return !blacklist.has(prop) && prop[0] !== "_";
};
/** @ignore */
const createStreamProxy = <T>(stream: any): StreamInterface<T> => {
const target: any = {};
Object.getOwnPropertyNames(Stream.prototype).forEach((prop) => {
if (!_validProp(prop)) return;
if (typeof stream[prop] === "function") {
target[prop] = stream[prop].bind(stream);
} else {
Object.defineProperty(target, prop, {
get: () => stream[prop],
configurable: false,
enumerable: true,
});
}
});
return target as StreamInterface<T>;
};
export default class StreamController<T> {
private _srcStream: Stream<T>;
private _dstStream: Stream<T>;
private _sink: _streamSink<T>;
private _streamProxy: StreamInterface<T>;
private _done: Promise<void> = Promise.resolve();
onListen: StreamCallback | undefined;
onPause: StreamCallback | undefined;
onResume: StreamCallback | undefined;
onCancel: StreamCallback | undefined;
private _onListen = () => {
if (this.onListen) {
this.onListen();
}
};
private _onPause = () => {
if (this.onPause) {
this.onPause();
}
};
private _onResume = () => {
Eif (this.onResume) {
this.onResume();
}
};
private _onCancel = () => {
if (this.onCancel) {
this.onCancel();
}
};
/**
* StreamController Constructor
*
* Controller that allows sending data, error and done events on its stream.
* This class is used to control and expose a stream that other code can listen to.
*
* @param params listeners: onListen, onPause, onResume, onCancel
* @template T the type of data to be passed on the stream
*/
constructor(params: constructorParams = { broadcast: false }) {
this._srcStream = new Stream<T>();
this._sink = new _streamSink(this);
if (params.broadcast) {
this._dstStream = this._srcStream.asBroadcastStream() as Stream<T>;
} else {
this._dstStream = this._srcStream;
}
this._streamProxy = createStreamProxy(this._dstStream);
this.onListen = params.onListen;
this.onPause = params.onPause;
this.onResume = params.onResume;
this.onCancel = params.onCancel;
this._dstStream.addEventListener("onListen", this._onListen);
this._dstStream.addEventListener("onPause", this._onPause);
this._dstStream.addEventListener("onResume", this._onResume);
this._dstStream.addEventListener("onCancel", this._onCancel);
}
add(data: T) {
this._srcStream.add(data);
}
addError(error: string | Error) {
this._srcStream.addError(error);
}
addStream(
stream: StreamInterface<T>,
options = { cancelOnError: false }
): Promise<void> {
const p = new Promise<void>((resolve) => {
stream.listen(this.add.bind(this), {
onDone: resolve,
onError: (e) => {
this.addError(e);
Iif (options.cancelOnError) resolve();
},
cancelOnError: options.cancelOnError,
});
});
this._done = p;
return p;
}
close() {
this._srcStream.close();
}
get stream(): StreamInterface<T> {
return this._streamProxy;
}
get sink() {
return this._sink;
}
get isPaused(): boolean {
return this._srcStream.isPaused;
}
get isClosed(): boolean {
return this._srcStream.isClosed;
}
get done(): Promise<void> {
return this._done;
}
static broadcast<T>(): StreamController<T> {
return new StreamController({ broadcast: true });
}
}
/**
* Object that only contains add, addError and close methods
* for a given stream.
*/
/** @ignore */
class _streamSink<T> {
constructor(private _streamController: StreamController<T>) {}
add(data: T) {
this._streamController.add(data);
}
addError(error: string | Error) {
this._streamController.addError(error);
}
addStream(
stream: StreamInterface<T>,
options = { cancelOnError: false }
): Promise<void> {
return this._streamController.addStream(stream, options);
}
close() {
this._streamController.close();
}
get done() {
return this._streamController.done;
}
}
|