UNPKG

@theia/core

Version:

Theia is a cloud & desktop IDE framework implemented in TypeScript.

95 lines • 4.48 kB
// ***************************************************************************** // Copyright (C) 2025 1C-Soft LLC and others. // // This program and the accompanying materials are made available under the // terms of the Eclipse Public License v. 2.0 which is available at // http://www.eclipse.org/legal/epl-2.0. // // This Source Code may also be made available under the following Secondary // Licenses when the conditions for such availability set forth in the Eclipse // Public License v. 2.0 are satisfied: GNU General Public License, version 2 // with the GNU Classpath Exception which is available at // https://www.gnu.org/software/classpath/license.html. // // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 // ***************************************************************************** /*--------------------------------------------------------------------------------------------- * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ // copied and modified from https://github.com/microsoft/vscode/blob/1.96.3/src/vs/base/common/observableInternal/utils.ts, // https://github.com/microsoft/vscode/blob/1.96.3/src/vs/base/common/observableInternal/utilsCancellation.ts import { CancellationError } from '../cancellation.js'; import { DisposableCollection } from '../disposable.js'; import { DerivedObservable } from './derived-observable.js'; import { Autorun } from './autorun.js'; export var ObservableUtils; (function (ObservableUtils) { /** * Creates an {@link Autorun.create autorun} that passes a collector for disposable objects to the {@link run} function. * The collected disposables are disposed before the next run or when the autorun is disposed. */ function autorunWithDisposables(run, options) { let toDispose = undefined; return new class extends Autorun { dispose() { super.dispose(); toDispose?.dispose(); } }(({ autorun, isFirstRun, changeSummary }) => { toDispose?.dispose(); toDispose = new DisposableCollection(); run({ toDispose, autorun, isFirstRun, changeSummary }); }, options); } ObservableUtils.autorunWithDisposables = autorunWithDisposables; function derivedObservableWithCache(compute, options) { let value = undefined; return new DerivedObservable(({ changeSummary }) => { value = compute({ lastValue: value, changeSummary }); return value; }, options); } ObservableUtils.derivedObservableWithCache = derivedObservableWithCache; function waitForState(observable, predicate, isError, cancellationToken) { if (!predicate) { predicate = state => !!state; } return new Promise((resolve, reject) => { const stateObservable = DerivedObservable.create(() => { const state = observable.get(); return { isFinished: predicate(state), error: isError ? isError(state) : false, state }; }); const autorun_ = Autorun.create(({ autorun }) => { const { isFinished, error, state } = stateObservable.get(); if (isFinished || error) { autorun.dispose(); if (error) { reject(error === true ? state : error); } else { resolve(state); } } }); if (cancellationToken) { const subscription = cancellationToken.onCancellationRequested(() => { autorun_.dispose(); subscription.dispose(); reject(new CancellationError()); }); if (cancellationToken.isCancellationRequested) { autorun_.dispose(); subscription.dispose(); reject(new CancellationError()); } } }); } ObservableUtils.waitForState = waitForState; })(ObservableUtils || (ObservableUtils = {})); //# sourceMappingURL=observable-utils.js.map