Welcome to mirror list, hosted at ThFree Co, Russian Federation.

autorun.ts « observableImpl « common « base « vs « src - github.com/microsoft/vscode.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6efe47367839f8e2b2fca19de4ff15e478ee396a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

import { DisposableStore, IDisposable, toDisposable } from 'vs/base/common/lifecycle';
import { IReader, IObservable, IObserver } from 'vs/base/common/observableImpl/base';
import { getLogger } from 'vs/base/common/observableImpl/logging';

export function autorun(debugName: string, fn: (reader: IReader) => void): IDisposable {
	return new AutorunObserver(debugName, fn, undefined);
}

interface IChangeContext {
	readonly changedObservable: IObservable<any, any>;
	readonly change: unknown;

	didChange<T, TChange>(observable: IObservable<T, TChange>): this is { change: TChange };
}

export function autorunHandleChanges(
	debugName: string,
	options: {
		/**
		 * Returns if this change should cause a re-run of the autorun.
		*/
		handleChange: (context: IChangeContext) => boolean;
	},
	fn: (reader: IReader) => void
): IDisposable {
	return new AutorunObserver(debugName, fn, options.handleChange);
}

export function autorunWithStore(
	fn: (reader: IReader, store: DisposableStore) => void,
	debugName: string
): IDisposable {
	const store = new DisposableStore();
	const disposable = autorun(
		debugName,
		reader => {
			store.clear();
			fn(reader, store);
		}
	);
	return toDisposable(() => {
		disposable.dispose();
		store.dispose();
	});
}

export class AutorunObserver implements IObserver, IReader, IDisposable {
	public needsToRun = true;
	private updateCount = 0;
	private disposed = false;

	/**
	 * The actual dependencies.
	*/
	private _dependencies = new Set<IObservable<any>>();
	public get dependencies() {
		return this._dependencies;
	}

	/**
	 * Dependencies that have to be removed when {@link runFn} ran through.
	*/
	private staleDependencies = new Set<IObservable<any>>();

	constructor(
		public readonly debugName: string,
		private readonly runFn: (reader: IReader) => void,
		private readonly _handleChange: ((context: IChangeContext) => boolean) | undefined
	) {
		getLogger()?.handleAutorunCreated(this);
		this.runIfNeeded();
	}

	public subscribeTo<T>(observable: IObservable<T>) {
		// In case the run action disposes the autorun
		if (this.disposed) {
			return;
		}
		this._dependencies.add(observable);
		if (!this.staleDependencies.delete(observable)) {
			observable.addObserver(this);
		}
	}

	public handleChange<T, TChange>(observable: IObservable<T, TChange>, change: TChange): void {
		const shouldReact = this._handleChange ? this._handleChange({
			changedObservable: observable,
			change,
			didChange: o => o === observable as any,
		}) : true;
		this.needsToRun = this.needsToRun || shouldReact;

		if (this.updateCount === 0) {
			this.runIfNeeded();
		}
	}

	public beginUpdate(): void {
		this.updateCount++;
	}

	public endUpdate(): void {
		this.updateCount--;
		if (this.updateCount === 0) {
			this.runIfNeeded();
		}
	}

	private runIfNeeded(): void {
		if (!this.needsToRun) {
			return;
		}
		// Assert: this.staleDependencies is an empty set.
		const emptySet = this.staleDependencies;
		this.staleDependencies = this._dependencies;
		this._dependencies = emptySet;

		this.needsToRun = false;

		getLogger()?.handleAutorunTriggered(this);

		try {
			this.runFn(this);
		} finally {
			// We don't want our observed observables to think that they are (not even temporarily) not being observed.
			// Thus, we only unsubscribe from observables that are definitely not read anymore.
			for (const o of this.staleDependencies) {
				o.removeObserver(this);
			}
			this.staleDependencies.clear();
		}
	}

	public dispose(): void {
		this.disposed = true;
		for (const o of this._dependencies) {
			o.removeObserver(this);
		}
		this._dependencies.clear();
	}

	public toString(): string {
		return `Autorun<${this.debugName}>`;
	}
}

export namespace autorun {
	export const Observer = AutorunObserver;
}
export function autorunDelta<T>(
	name: string,
	observable: IObservable<T>,
	handler: (args: { lastValue: T | undefined; newValue: T }) => void
): IDisposable {
	let _lastValue: T | undefined;
	return autorun(name, (reader) => {
		const newValue = observable.read(reader);
		const lastValue = _lastValue;
		_lastValue = newValue;
		handler({ lastValue, newValue });
	});
}