UNPKG

monaco-editor-core

Version:
118 lines • 4.78 kB
/*--------------------------------------------------------------------------------------------- * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ import { DisposableStore, toDisposable } from '../commonFacade/deps.js'; import { DebugNameData } from '../debugName.js'; import { AutorunObserver } from './autorunImpl.js'; import { DebugLocation } from '../debugLocation.js'; /** * Runs immediately and whenever a transaction ends and an observed observable changed. * {@link fn} should start with a JS Doc using `@description` to name the autorun. */ export function autorun(fn, debugLocation = DebugLocation.ofCaller()) { return new AutorunObserver(new DebugNameData(undefined, undefined, fn), fn, undefined, debugLocation); } /** * Runs immediately and whenever a transaction ends and an observed observable changed. * {@link fn} should start with a JS Doc using `@description` to name the autorun. */ export function autorunOpts(options, fn, debugLocation = DebugLocation.ofCaller()) { return new AutorunObserver(new DebugNameData(options.owner, options.debugName, options.debugReferenceFn ?? fn), fn, undefined, debugLocation); } /** * Runs immediately and whenever a transaction ends and an observed observable changed. * {@link fn} should start with a JS Doc using `@description` to name the autorun. * * Use `changeTracker.createChangeSummary` to create a "change summary" that can collect the changes. * Use `changeTracker.handleChange` to add a reported change to the change summary. * The run function is given the last change summary. * The change summary is discarded after the run function was called. * * @see autorun */ export function autorunHandleChanges(options, fn, debugLocation = DebugLocation.ofCaller()) { return new AutorunObserver(new DebugNameData(options.owner, options.debugName, options.debugReferenceFn ?? fn), fn, options.changeTracker, debugLocation); } /** * @see autorunHandleChanges (but with a disposable store that is cleared before the next run or on dispose) */ export function autorunWithStoreHandleChanges(options, fn) { const store = new DisposableStore(); const disposable = autorunHandleChanges({ owner: options.owner, debugName: options.debugName, debugReferenceFn: options.debugReferenceFn ?? fn, changeTracker: options.changeTracker, }, (reader, changeSummary) => { store.clear(); fn(reader, changeSummary, store); }); return toDisposable(() => { disposable.dispose(); store.dispose(); }); } /** * @see autorun (but with a disposable store that is cleared before the next run or on dispose) * * @deprecated Use `autorun(reader => { reader.store.add(...) })` instead! */ export function autorunWithStore(fn) { const store = new DisposableStore(); const disposable = autorunOpts({ owner: undefined, debugName: undefined, debugReferenceFn: fn, }, reader => { store.clear(); fn(reader, store); }); return toDisposable(() => { disposable.dispose(); store.dispose(); }); } export function autorunDelta(observable, handler) { let _lastValue; return autorunOpts({ debugReferenceFn: handler }, (reader) => { const newValue = observable.read(reader); const lastValue = _lastValue; _lastValue = newValue; handler({ lastValue, newValue }); }); } /** * An autorun with a `dispose()` method on its `reader` which cancels the autorun. * It it safe to call `dispose()` synchronously. * TODO@hediet/copilot: rename to delete autorunSelfDisposable, and rename autorunSelfDisposable2 to autorunSelfDisposable. */ export function registerAutorunSelfDisposable(store, fn, debugLocation = DebugLocation.ofCaller()) { let ar; let disposeSync = false; // eslint-disable-next-line prefer-const ar = autorun(reader => { fn({ delayedStore: reader.delayedStore, store: reader.store, readObservable: reader.readObservable.bind(reader), dispose: () => { if (!ar) { // dispose on first run, ar is not initialized yet. disposeSync = true; } else { // dispose on reaction, ar is already registered. store.delete(ar); } } }); }, debugLocation); if (disposeSync) { ar.dispose(); } else { store.add(ar); } } //# sourceMappingURL=autorun.js.map