"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/**
 * @module fly
 * @private
 */
const logger_1 = require("../logger");
const streams_1 = require("../streams");
/**
 * @hidden
 */
function isFlyStream(id) {
    return typeof id === "number";
}
exports.isFlyStream = isFlyStream;
/**
 * @hidden
 */
function refToStream(id) {
    let closed = false;
    const r = new streams_1.ReadableStream({
        start(controller) {
            const cb = bridge.wrapFunction(function streamSubscribe(name, ...args) {
                logger_1.logger.debug("stream event:", name);
                if (name === "close" || name === "end") {
                    try {
                        cb.release();
                    }
                    catch (e) { }
                    if (!closed) {
                        closed = true;
                        controller.close();
                    }
                }
                else if (name === "error") {
                    try {
                        cb.release();
                    }
                    catch (e) { }
                    logger_1.logger.error("error in stream:", args[0]);
                    controller.error(new Error(args[0]));
                }
                else
                    logger_1.logger.error("unhandled event", name);
            }, { release: false });
            bridge.dispatch("streamSubscribe", id, cb); // no releasing here, we re-use it
        },
        pull(controller) {
            if (closed) {
                return Promise.resolve(null);
            }
            return new Promise(function pullPromise(resolve, reject) {
                bridge.dispatch("streamRead", id, function streamRead(err, data) {
                    if (err) {
                        controller.error(new Error(err));
                        reject(err);
                        return;
                    }
                    controller.enqueue(data);
                    resolve();
                });
            });
        },
        cancel() {
            logger_1.logger.info(`stream ${id} was cancelled`);
        }
    }, {
        highWaterMark: 0
    });
    r.flyStreamId = id;
    return r;
}
exports.default = refToStream;
//# sourceMappingURL=streams.js.map