"use strict";
// -*- mode: typescript; indent-tabs-mode: nil; js-basic-offset: 4 -*-
//
// This file is part of Genie
//
// Copyright 2020 The Board of Trustees of the Leland Stanford Junior University
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Author: Giovanni Campagna <gcampagn@cs.stanford.edu>
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.waitFinish = exports.CountStream = exports.chain = exports.MapAccumulator = exports.SetAccumulator = exports.ArrayStream = exports.ArrayAccumulator = void 0;
const assert_1 = __importDefault(require("assert"));
const Stream = __importStar(require("stream"));
class ArrayAccumulator extends Stream.Writable {
    constructor() {
        super({ objectMode: true });
        this._buffer = [];
    }
    _write(obj, encoding, callback) {
        this._buffer.push(obj);
        callback();
    }
    read() {
        return new Promise((resolve, reject) => {
            this.on('finish', () => resolve(this._buffer));
            this.on('error', reject);
        });
    }
}
exports.ArrayAccumulator = ArrayAccumulator;
class SetAccumulator extends Stream.Writable {
    constructor() {
        super({ objectMode: true });
        this._buffer = new Set;
    }
    _write(obj, encoding, callback) {
        this._buffer.add(obj);
        callback();
    }
    read() {
        return new Promise((resolve, reject) => {
            this.on('finish', () => resolve(this._buffer));
            this.on('error', reject);
        });
    }
}
exports.SetAccumulator = SetAccumulator;
class MapAccumulator extends Stream.Writable {
    constructor(field = 'id') {
        super({ objectMode: true });
        this._buffer = new Map;
        this._field = field;
    }
    _write(obj, encoding, callback) {
        this._buffer.set(obj[this._field], obj);
        callback();
    }
    read() {
        return new Promise((resolve, reject) => {
            this.on('finish', () => resolve(this._buffer));
            this.on('error', reject);
        });
    }
}
exports.MapAccumulator = MapAccumulator;
class ArrayStream extends Stream.Readable {
    constructor(array, options) {
        super(options);
        this._iterator = array[Symbol.iterator]();
    }
    _read() {
        for (;;) {
            const { value, done } = this._iterator.next();
            if (done) {
                this.push(null);
                break;
            }
            const ok = this.push(value);
            if (!ok)
                break;
        }
    }
}
exports.ArrayStream = ArrayStream;
class ChainStream extends Stream.Readable {
    constructor(chain, options) {
        super(options);
        this._chain = chain;
        this._separator = options.separator;
        this._i = 0;
    }
    _read(n) {
        if (this._i >= this._chain.length) {
            this.push(null);
            return;
        }
        const next = this._chain[this._i];
        let chunk = next.read(n);
        if (chunk !== null) {
            this.push(chunk);
            return;
        }
        // ReadableStream.read returns null in three cases:
        //
        // - the stream is open and there is not enough data to read (ended === false)
        // - the stream is ended, there is data left but not enough to read
        // - the stream is ended and there is nothing left ('end' has been emitted)
        //
        // in the first case, we want to connect to readable and read more later
        // when data shows up
        //
        // in the second case, we want to consume as much data as possible,
        // then try to read the rest from the next stream
        //
        // in the third case, we want to switch to the next stream right away
        // and try to read more
        if (!next._readableState.ended) {
            // first case
            next.once('readable', () => this._read(n));
        }
        else if (next._readableState.length > 0) {
            // second case
            chunk = next.read(next._readableState.length);
            assert_1.default(chunk !== null);
            this.push(chunk);
            // stream has ended and we consumed all data, switch to the next one
            this._i++;
            if (this._i < this._chain.length && this._separator)
                this.push(this._separator);
            process.nextTick(() => this._read(n - chunk.length));
        }
        else {
            // third case
            // stream has ended and we consumed all data, switch to the next one
            this._i++;
            if (this._i < this._chain.length && this._separator)
                this.push(this._separator);
            process.nextTick(() => this._read(n));
        }
    }
}
function chain(streams, options) {
    return new ChainStream(streams, options);
}
exports.chain = chain;
class CountStream extends Stream.Duplex {
    constructor() {
        super({ objectMode: true });
        this._buffer = [];
        this._i = 0;
        this._N = 0;
        this._reading = false;
    }
    _read() {
        if (!this._reading)
            return;
        this._pushSome();
    }
    _pushSome() {
        let consumed;
        while (this._i < this._buffer.length && this.push(this._buffer[this._i++])) {
            consumed = this._i;
            if (consumed % 100 === 0)
                this.emit('progress', consumed / this._N);
        }
        consumed = this._i;
        if (consumed % 100 === 0)
            this.emit('progress', consumed / this._N);
        if (this._i === this._buffer.length) {
            this.emit('progress', 1);
            this.push(null);
        }
    }
    _write(obj, encoding, callback) {
        this._buffer.push(obj);
        this._N++;
        callback();
    }
    _final(callback) {
        this._reading = true;
        this._pushSome();
        callback();
    }
}
exports.CountStream = CountStream;
function waitFinish(stream) {
    return new Promise((resolve, reject) => {
        stream.once('finish', resolve);
        stream.on('error', reject);
    });
}
exports.waitFinish = waitFinish;
//# sourceMappingURL=stream-utils.js.map