var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); }
var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) {
    if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
    var g = generator.apply(thisArg, _arguments || []), i, q = [];
    return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i;
    function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; }
    function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }
    function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
    function fulfill(value) { resume("next", value); }
    function reject(value) { resume("throw", value); }
    function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
};
var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
var __asyncValues = (this && this.__asyncValues) || function (o) {
    if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
    var m = o[Symbol.asyncIterator], i;
    return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
    function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
    function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
};
function createAsyncIterable(port) {
    return {
        [Symbol.asyncIterator]() {
            return __asyncGenerator(this, arguments, function* _a() {
                let done = false;
                while (!done) {
                    const promise = new Promise((resolve) => {
                        port.onmessage = (event) => {
                            if (event.data === null) {
                                done = true;
                                resolve(null);
                            }
                            else {
                                resolve(event.data);
                            }
                        };
                    });
                    const value = yield __await(promise);
                    if (value !== null) {
                        yield yield __await(value);
                    }
                }
            });
        },
    };
}
const IPFSContentTransferHandler = {
    canHandle: (obj) => obj && obj.result && typeof obj.result[Symbol.asyncIterator] === 'function',
    serialize(obj) {
        if (obj === undefined) {
            return [null, []];
        }
        const { result } = obj, rest = __rest(obj, ["result"]);
        const { port1, port2 } = new MessageChannel();
        if (result) {
            (() => __awaiter(this, void 0, void 0, function* () {
                var _a, e_1, _b, _c;
                try {
                    for (var _d = true, result_1 = __asyncValues(result), result_1_1; result_1_1 = yield result_1.next(), _a = result_1_1.done, !_a;) {
                        _c = result_1_1.value;
                        _d = false;
                        try {
                            const value = _c;
                            port1.postMessage(value);
                        }
                        finally {
                            _d = true;
                        }
                    }
                }
                catch (e_1_1) { e_1 = { error: e_1_1 }; }
                finally {
                    try {
                        if (!_d && !_a && (_b = result_1.return)) yield _b.call(result_1);
                    }
                    finally { if (e_1) throw e_1.error; }
                }
                port1.postMessage(null);
                port1.close();
            }))();
        }
        return [Object.assign(Object.assign({}, rest), { port: port2 }), [port2]];
    },
    deserialize(serializedObj) {
        if (!serializedObj) {
            return undefined;
        }
        const { port } = serializedObj, rest = __rest(serializedObj, ["port"]);
        return Object.assign(Object.assign({}, rest), { result: createAsyncIterable(port) });
    },
};
export { IPFSContentTransferHandler };
//# sourceMappingURL=serializers.js.map