-
Notifications
You must be signed in to change notification settings - Fork 30k
/
hoverOperation.ts
229 lines (194 loc) · 7.27 KB
/
hoverOperation.ts
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { AsyncIterableObject, CancelableAsyncIterableObject, createCancelableAsyncIterable, RunOnceScheduler } from '../../../../base/common/async.js';
import { CancellationToken } from '../../../../base/common/cancellation.js';
import { onUnexpectedError } from '../../../../base/common/errors.js';
import { Emitter } from '../../../../base/common/event.js';
import { Disposable } from '../../../../base/common/lifecycle.js';
import { ICodeEditor } from '../../../browser/editorBrowser.js';
import { EditorOption } from '../../../common/config/editorOptions.js';
export interface IHoverComputer<TArgs, TResult> {
/**
* This is called after half the hover time
*/
computeAsync?: (args: TArgs, token: CancellationToken) => AsyncIterableObject<TResult>;
/**
* This is called after all the hover time
*/
computeSync?: (args: TArgs) => TResult[];
}
const enum HoverOperationState {
Idle,
FirstWait,
SecondWait,
WaitingForAsync = 3,
WaitingForAsyncShowingLoading = 4,
}
export const enum HoverStartMode {
Delayed = 0,
Immediate = 1
}
export const enum HoverStartSource {
Mouse = 0,
Click = 1,
Keyboard = 2
}
export class HoverResult<TArgs, TResult> {
constructor(
public readonly value: TResult[],
public readonly isComplete: boolean,
public readonly hasLoadingMessage: boolean,
public readonly options: TArgs
) { }
}
/**
* Computing the hover is very fine tuned.
*
* Suppose the hover delay is 300ms (the default). Then, when resting the mouse at an anchor:
* - at 150ms, the async computation is triggered (i.e. semantic hover)
* - if async results already come in, they are not rendered yet.
* - at 300ms, the sync computation is triggered (i.e. decorations, markers)
* - if there are sync or async results, they are rendered.
* - at 900ms, if the async computation hasn't finished, a "Loading..." result is added.
*/
export class HoverOperation<TArgs, TResult> extends Disposable {
private readonly _onResult = this._register(new Emitter<HoverResult<TArgs, TResult>>());
public readonly onResult = this._onResult.event;
private readonly _asyncComputationScheduler = this._register(new Debouncer((options: TArgs) => this._triggerAsyncComputation(options), 0));
private readonly _syncComputationScheduler = this._register(new Debouncer((options: TArgs) => this._triggerSyncComputation(options), 0));
private readonly _loadingMessageScheduler = this._register(new Debouncer((options: TArgs) => this._triggerLoadingMessage(options), 0));
private _state = HoverOperationState.Idle;
private _asyncIterable: CancelableAsyncIterableObject<TResult> | null = null;
private _asyncIterableDone: boolean = false;
private _result: TResult[] = [];
private _options: TArgs | undefined;
constructor(
private readonly _editor: ICodeEditor,
private readonly _computer: IHoverComputer<TArgs, TResult>
) {
super();
}
public override dispose(): void {
if (this._asyncIterable) {
this._asyncIterable.cancel();
this._asyncIterable = null;
}
this._options = undefined;
super.dispose();
}
private get _hoverTime(): number {
return this._editor.getOption(EditorOption.hover).delay;
}
private get _firstWaitTime(): number {
return this._hoverTime / 2;
}
private get _secondWaitTime(): number {
return this._hoverTime - this._firstWaitTime;
}
private get _loadingMessageTime(): number {
return 3 * this._hoverTime;
}
private _setState(state: HoverOperationState, options: TArgs): void {
this._state = state;
this._fireResult(options);
}
private _triggerAsyncComputation(options: TArgs): void {
this._setState(HoverOperationState.SecondWait, options);
this._syncComputationScheduler.schedule(options, this._secondWaitTime);
if (this._computer.computeAsync) {
this._asyncIterableDone = false;
this._asyncIterable = createCancelableAsyncIterable(token => this._computer.computeAsync!(options, token));
(async () => {
try {
for await (const item of this._asyncIterable!) {
if (item) {
this._result.push(item);
this._fireResult(options);
}
}
this._asyncIterableDone = true;
if (this._state === HoverOperationState.WaitingForAsync || this._state === HoverOperationState.WaitingForAsyncShowingLoading) {
this._setState(HoverOperationState.Idle, options);
}
} catch (e) {
onUnexpectedError(e);
}
})();
} else {
this._asyncIterableDone = true;
}
}
private _triggerSyncComputation(options: TArgs): void {
if (this._computer.computeSync) {
this._result = this._result.concat(this._computer.computeSync(options));
}
this._setState(this._asyncIterableDone ? HoverOperationState.Idle : HoverOperationState.WaitingForAsync, options);
}
private _triggerLoadingMessage(options: TArgs): void {
if (this._state === HoverOperationState.WaitingForAsync) {
this._setState(HoverOperationState.WaitingForAsyncShowingLoading, options);
}
}
private _fireResult(options: TArgs): void {
if (this._state === HoverOperationState.FirstWait || this._state === HoverOperationState.SecondWait) {
// Do not send out results before the hover time
return;
}
const isComplete = (this._state === HoverOperationState.Idle);
const hasLoadingMessage = (this._state === HoverOperationState.WaitingForAsyncShowingLoading);
this._onResult.fire(new HoverResult(this._result.slice(0), isComplete, hasLoadingMessage, options));
}
public start(mode: HoverStartMode, options: TArgs): void {
if (mode === HoverStartMode.Delayed) {
if (this._state === HoverOperationState.Idle) {
this._setState(HoverOperationState.FirstWait, options);
this._asyncComputationScheduler.schedule(options, this._firstWaitTime);
this._loadingMessageScheduler.schedule(options, this._loadingMessageTime);
}
} else {
switch (this._state) {
case HoverOperationState.Idle:
this._triggerAsyncComputation(options);
this._syncComputationScheduler.cancel();
this._triggerSyncComputation(options);
break;
case HoverOperationState.SecondWait:
this._syncComputationScheduler.cancel();
this._triggerSyncComputation(options);
break;
}
}
}
public cancel(): void {
this._asyncComputationScheduler.cancel();
this._syncComputationScheduler.cancel();
this._loadingMessageScheduler.cancel();
if (this._asyncIterable) {
this._asyncIterable.cancel();
this._asyncIterable = null;
}
this._result = [];
this._options = undefined;
this._state = HoverOperationState.Idle;
}
public get options(): TArgs | undefined {
return this._options;
}
}
class Debouncer<TArgs> extends Disposable {
private readonly _scheduler: RunOnceScheduler;
private _options: TArgs | undefined;
constructor(runner: (options: TArgs) => void, debounceTimeMs: number) {
super();
this._scheduler = this._register(new RunOnceScheduler(() => runner(this._options!), debounceTimeMs));
}
schedule(options: TArgs, debounceTimeMs: number): void {
this._options = options;
this._scheduler.schedule(debounceTimeMs);
}
cancel(): void {
this._scheduler.cancel();
}
}