142 lines
3.8 KiB
JavaScript
142 lines
3.8 KiB
JavaScript
|
'use strict';
|
||
|
|
||
|
Object.defineProperty(exports, "__esModule", {
|
||
|
value: true
|
||
|
});
|
||
|
exports.tryDispose = tryDispose;
|
||
|
exports.create = create;
|
||
|
exports.empty = empty;
|
||
|
exports.all = all;
|
||
|
exports.promised = promised;
|
||
|
exports.settable = settable;
|
||
|
exports.once = once;
|
||
|
|
||
|
var _Disposable = require('./Disposable');
|
||
|
|
||
|
var _Disposable2 = _interopRequireDefault(_Disposable);
|
||
|
|
||
|
var _SettableDisposable = require('./SettableDisposable');
|
||
|
|
||
|
var _SettableDisposable2 = _interopRequireDefault(_SettableDisposable);
|
||
|
|
||
|
var _Promise = require('../Promise');
|
||
|
|
||
|
var _prelude = require('@most/prelude');
|
||
|
|
||
|
var base = _interopRequireWildcard(_prelude);
|
||
|
|
||
|
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } }
|
||
|
|
||
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
||
|
|
||
|
/** @license MIT License (c) copyright 2010-2016 original author or authors */
|
||
|
/** @author Brian Cavalier */
|
||
|
/** @author John Hann */
|
||
|
var map = base.map;
|
||
|
var identity = base.id;
|
||
|
|
||
|
/**
|
||
|
* Call disposable.dispose. If it returns a promise, catch promise
|
||
|
* error and forward it through the provided sink.
|
||
|
* @param {number} t time
|
||
|
* @param {{dispose: function}} disposable
|
||
|
* @param {{error: function}} sink
|
||
|
* @return {*} result of disposable.dispose
|
||
|
*/
|
||
|
function tryDispose(t, disposable, sink) {
|
||
|
var result = disposeSafely(disposable);
|
||
|
return (0, _Promise.isPromise)(result) ? result.catch(function (e) {
|
||
|
sink.error(t, e);
|
||
|
}) : result;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Create a new Disposable which will dispose its underlying resource
|
||
|
* at most once.
|
||
|
* @param {function} dispose function
|
||
|
* @param {*?} data any data to be passed to disposer function
|
||
|
* @return {Disposable}
|
||
|
*/
|
||
|
function create(dispose, data) {
|
||
|
return once(new _Disposable2.default(dispose, data));
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Create a noop disposable. Can be used to satisfy a Disposable
|
||
|
* requirement when no actual resource needs to be disposed.
|
||
|
* @return {Disposable|exports|module.exports}
|
||
|
*/
|
||
|
function empty() {
|
||
|
return new _Disposable2.default(identity, void 0);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Create a disposable that will dispose all input disposables in parallel.
|
||
|
* @param {Array<Disposable>} disposables
|
||
|
* @return {Disposable}
|
||
|
*/
|
||
|
function all(disposables) {
|
||
|
return create(disposeAll, disposables);
|
||
|
}
|
||
|
|
||
|
function disposeAll(disposables) {
|
||
|
return Promise.all(map(disposeSafely, disposables));
|
||
|
}
|
||
|
|
||
|
function disposeSafely(disposable) {
|
||
|
try {
|
||
|
return disposable.dispose();
|
||
|
} catch (e) {
|
||
|
return Promise.reject(e);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Create a disposable from a promise for another disposable
|
||
|
* @param {Promise<Disposable>} disposablePromise
|
||
|
* @return {Disposable}
|
||
|
*/
|
||
|
function promised(disposablePromise) {
|
||
|
return create(disposePromise, disposablePromise);
|
||
|
}
|
||
|
|
||
|
function disposePromise(disposablePromise) {
|
||
|
return disposablePromise.then(disposeOne);
|
||
|
}
|
||
|
|
||
|
function disposeOne(disposable) {
|
||
|
return disposable.dispose();
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Create a disposable proxy that allows its underlying disposable to
|
||
|
* be set later.
|
||
|
* @return {SettableDisposable}
|
||
|
*/
|
||
|
function settable() {
|
||
|
return new _SettableDisposable2.default();
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Wrap an existing disposable (which may not already have been once()d)
|
||
|
* so that it will only dispose its underlying resource at most once.
|
||
|
* @param {{ dispose: function() }} disposable
|
||
|
* @return {Disposable} wrapped disposable
|
||
|
*/
|
||
|
function once(disposable) {
|
||
|
return new _Disposable2.default(disposeMemoized, memoized(disposable));
|
||
|
}
|
||
|
|
||
|
function disposeMemoized(memoized) {
|
||
|
if (!memoized.disposed) {
|
||
|
memoized.disposed = true;
|
||
|
memoized.value = disposeSafely(memoized.disposable);
|
||
|
memoized.disposable = void 0;
|
||
|
}
|
||
|
|
||
|
return memoized.value;
|
||
|
}
|
||
|
|
||
|
function memoized(disposable) {
|
||
|
return { disposed: false, disposable: disposable, value: void 0 };
|
||
|
}
|