export class LogWriterStream { readonly _out: WritableStream; readonly _toFlush: (() => Promise)[] = []; readonly _id: number; constructor(out: WritableStream) { this._out = out; this._id = Math.random(); // console.log(">>>My:constructor:", this._id); } write(encoded: Uint8Array): void { const my = async (): Promise => { // const val = Math.random(); // console.log(">>>My:", encoded) try { // console.log(">>>My:getWriter:get:", this._id); const writer = this._out.getWriter(); await writer.ready; await writer.write(encoded); writer.releaseLock(); // console.log(">>>My:getWriter:release", this._id); } catch (err) { // eslint-disable-next-line no-console console.error("Chunk error:", err); } // console.log("<< void)[] = []; _flush(toFlush: (() => Promise)[] | undefined = undefined, done?: () => void): void { if (done) { this._flushDoneFns.push(done); } if (this._toFlush.length == 0) { // console.log("Flush is stopped", this._toFlush.length) this._flushIsRunning = false; this._flushDoneFns.forEach((fn) => fn()); this._flushDoneFns = []; return; } if (!toFlush && this._toFlush.length == 1 && !this._flushIsRunning) { this._flushIsRunning = true; // console.log("Flush is started", this._toFlush.length) } else if (!toFlush) { // console.log("flush queue check but is running", this._toFlush.length) return; } // console.log(">>>Msg:", this._toFlush.length) const my = this._toFlush.shift(); my?.() .catch((e) => { // eslint-disable-next-line no-console console.error("Flush error:", e); }) .finally(() => { // console.log("<<