import { append, curry2, curry3, reduce } from '@most/prelude'; /** @license MIT License (c) copyright 2010-2017 original author or authors */ var dispose = function dispose(disposable) { return disposable.dispose(); }; var asyncGenerator = function () { function AwaitValue(value) { this.value = value; } function AsyncGenerator(gen) { var front, back; function send(key, arg) { return new Promise(function (resolve, reject) { var request = { key: key, arg: arg, resolve: resolve, reject: reject, next: null }; if (back) { back = back.next = request; } else { front = back = request; resume(key, arg); } }); } function resume(key, arg) { try { var result = gen[key](arg); var value = result.value; if (value instanceof AwaitValue) { Promise.resolve(value.value).then(function (arg) { resume("next", arg); }, function (arg) { resume("throw", arg); }); } else { settle(result.done ? "return" : "normal", result.value); } } catch (err) { settle("throw", err); } } function settle(type, value) { switch (type) { case "return": front.resolve({ value: value, done: true }); break; case "throw": front.reject(value); break; default: front.resolve({ value: value, done: false }); break; } front = front.next; if (front) { resume(front.key, front.arg); } else { back = null; } } this._invoke = send; if (typeof gen.return !== "function") { this.return = undefined; } } if (typeof Symbol === "function" && Symbol.asyncIterator) { AsyncGenerator.prototype[Symbol.asyncIterator] = function () { return this; }; } AsyncGenerator.prototype.next = function (arg) { return this._invoke("next", arg); }; AsyncGenerator.prototype.throw = function (arg) { return this._invoke("throw", arg); }; AsyncGenerator.prototype.return = function (arg) { return this._invoke("return", arg); }; return { wrap: function (fn) { return function () { return new AsyncGenerator(fn.apply(this, arguments)); }; }, await: function (value) { return new AwaitValue(value); } }; }(); var classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }; /** @license MIT License (c) copyright 2010-2017 original author or authors */ var disposeNone = function disposeNone() { return NONE; }; var NONE = /*#__PURE__*/new (function () { function DisposeNone() { classCallCheck(this, DisposeNone); } DisposeNone.prototype.dispose = function dispose() {}; return DisposeNone; }())(); var isDisposeNone = function isDisposeNone(d) { return d === NONE; }; /** @license MIT License (c) copyright 2010-2017 original author or authors */ // Wrap an existing disposable (which may not already have been once()d) // so that it will only dispose its underlying resource at most once. var disposeOnce = function disposeOnce(disposable) { return new DisposeOnce(disposable); }; var DisposeOnce = /*#__PURE__*/function () { function DisposeOnce(disposable) { classCallCheck(this, DisposeOnce); this.disposed = false; this.disposable = disposable; } DisposeOnce.prototype.dispose = function dispose() { if (!this.disposed) { this.disposed = true; this.disposable.dispose(); this.disposable = undefined; } }; return DisposeOnce; }(); /** @license MIT License (c) copyright 2010-2017 original author or authors */ // Create a Disposable that will use the provided // dispose function to dispose the resource var disposeWith = /*#__PURE__*/curry2(function (dispose, resource) { return disposeOnce(new DisposeWith(dispose, resource)); }); // Disposable represents a resource that must be // disposed/released. It aggregates a function to dispose // the resource and a handle to a key/id/handle/reference // that identifies the resource var DisposeWith = /*#__PURE__*/function () { function DisposeWith(dispose, resource) { classCallCheck(this, DisposeWith); this._dispose = dispose; this._resource = resource; } DisposeWith.prototype.dispose = function dispose() { this._dispose(this._resource); }; return DisposeWith; }(); /** @license MIT License (c) copyright 2010 original author or authors */ // Aggregate a list of disposables into a DisposeAll var disposeAll = function disposeAll(ds) { var merged = reduce(merge, [], ds); return merged.length === 0 ? disposeNone() : new DisposeAll(merged); }; // Convenience to aggregate 2 disposables var disposeBoth = /*#__PURE__*/curry2(function (d1, d2) { return disposeAll([d1, d2]); }); var merge = function merge(ds, d) { return isDisposeNone(d) ? ds : d instanceof DisposeAll ? ds.concat(d.disposables) : append(d, ds); }; var DisposeAll = /*#__PURE__*/function () { function DisposeAll(disposables) { classCallCheck(this, DisposeAll); this.disposables = disposables; } DisposeAll.prototype.dispose = function dispose() { throwIfErrors(disposeCollectErrors(this.disposables)); }; return DisposeAll; }(); // Dispose all, safely collecting errors into an array var disposeCollectErrors = function disposeCollectErrors(disposables) { return reduce(appendIfError, [], disposables); }; // Call dispose and if throws, append thrown error to errors var appendIfError = function appendIfError(errors, d) { try { d.dispose(); } catch (e) { errors.push(e); } return errors; }; // Throw DisposeAllError if errors is non-empty var throwIfErrors = function throwIfErrors(errors) { if (errors.length > 0) { throw new DisposeAllError(errors.length + ' errors', errors); } }; var DisposeAllError = /*#__PURE__*/function (Error) { function DisposeAllError(message, errors) { Error.call(this, message); this.message = message; this.name = DisposeAllError.name; this.errors = errors; if (Error.captureStackTrace) { Error.captureStackTrace(this, DisposeAllError); } this.stack = '' + this.stack + formatErrorStacks(this.errors); } DisposeAllError.prototype = /*#__PURE__*/Object.create(Error.prototype); return DisposeAllError; }(Error); var formatErrorStacks = function formatErrorStacks(errors) { return reduce(formatErrorStack, '', errors); }; var formatErrorStack = function formatErrorStack(s, e, i) { return s + ('\n[' + (i + 1) + '] ' + e.stack); }; /** @license MIT License (c) copyright 2010-2017 original author or authors */ // Try to dispose the disposable. If it throws, send // the error to sink.error with the provided Time value var tryDispose = /*#__PURE__*/curry3(function (t, disposable, sink) { try { disposable.dispose(); } catch (e) { sink.error(t, e); } }); /** @license MIT License (c) copyright 2010-2017 original author or authors */ export { dispose, disposeNone, isDisposeNone, disposeWith, disposeOnce, disposeAll, disposeBoth, DisposeAllError, tryDispose }; //# sourceMappingURL=index.es.js.map