UNPKG

monaco-editor-core

Version:
85 lines 3.67 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 }); }); } //# sourceMappingURL=autorun.js.map