hoverOperation.ts 6.0 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
'use strict';

J
Johannes Rieken 已提交
7 8 9
import { RunOnceScheduler } from 'vs/base/common/async';
import { onUnexpectedError } from 'vs/base/common/errors';
import { TPromise } from 'vs/base/common/winjs.base';
E
Erich Gamma 已提交
10 11 12 13 14 15

export interface IHoverComputer<Result> {

	/**
	 * This is called after half the hover time
	 */
16
	computeAsync?: () => TPromise<Result>;
E
Erich Gamma 已提交
17 18 19 20 21 22 23 24 25

	/**
	 * This is called after all the hover time
	 */
	computeSync?: () => Result;

	/**
	 * This is called whenever one of the compute* methods returns a truey value
	 */
J
Johannes Rieken 已提交
26
	onResult: (result: Result, isFromSynchronousComputation: boolean) => void;
E
Erich Gamma 已提交
27 28 29 30 31 32 33 34 35 36

	/**
	 * This is what will be sent as progress/complete to the computation promise
	 */
	getResult: () => Result;

	getResultWithLoadingMessage: () => Result;

}

A
Alex Dima 已提交
37
const enum ComputeHoverOperationState {
E
Erich Gamma 已提交
38 39 40 41 42 43
	IDLE = 0,
	FIRST_WAIT = 1,
	SECOND_WAIT = 2,
	WAITING_FOR_ASYNC_COMPUTATION = 3
}

44 45 46 47 48
export const enum HoverStartMode {
	Delayed = 0,
	Immediate = 1
}

E
Erich Gamma 已提交
49 50 51 52 53
export class HoverOperation<Result> {

	static HOVER_TIME = 300;

	private _computer: IHoverComputer<Result>;
J
Johannes Rieken 已提交
54
	private _state: ComputeHoverOperationState;
55
	private _hoverTime: number;
E
Erich Gamma 已提交
56

A
Alex Dima 已提交
57 58 59
	private _firstWaitScheduler: RunOnceScheduler;
	private _secondWaitScheduler: RunOnceScheduler;
	private _loadingMessageScheduler: RunOnceScheduler;
60
	private _asyncComputationPromise: TPromise<void>;
J
Johannes Rieken 已提交
61
	private _asyncComputationPromiseDone: boolean;
E
Erich Gamma 已提交
62

J
Johannes Rieken 已提交
63 64 65
	private _completeCallback: (r: Result) => void;
	private _errorCallback: (err: any) => void;
	private _progressCallback: (progress: any) => void;
E
Erich Gamma 已提交
66

J
Johannes Rieken 已提交
67
	constructor(computer: IHoverComputer<Result>, success: (r: Result) => void, error: (err: any) => void, progress: (progress: any) => void) {
E
Erich Gamma 已提交
68 69
		this._computer = computer;
		this._state = ComputeHoverOperationState.IDLE;
70
		this._hoverTime = HoverOperation.HOVER_TIME;
E
Erich Gamma 已提交
71

72 73 74
		this._firstWaitScheduler = new RunOnceScheduler(() => this._triggerAsyncComputation(), 0);
		this._secondWaitScheduler = new RunOnceScheduler(() => this._triggerSyncComputation(), 0);
		this._loadingMessageScheduler = new RunOnceScheduler(() => this._showLoadingMessage(), 0);
E
Erich Gamma 已提交
75

76
		this._asyncComputationPromise = null;
E
Erich Gamma 已提交
77 78 79 80 81 82 83
		this._asyncComputationPromiseDone = false;

		this._completeCallback = success;
		this._errorCallback = error;
		this._progressCallback = progress;
	}

84 85 86 87 88 89 90 91 92 93 94 95 96 97
	public setHoverTime(hoverTime: number): void {
		this._hoverTime = hoverTime;
	}

	private _firstWaitTime(): number {
		return this._hoverTime / 2;
	}

	private _secondWaitTime(): number {
		return this._hoverTime / 2;
	}

	private _loadingMessageTime(): number {
		return 3 * this._hoverTime;
E
Erich Gamma 已提交
98 99 100 101
	}

	private _triggerAsyncComputation(): void {
		this._state = ComputeHoverOperationState.SECOND_WAIT;
102
		this._secondWaitScheduler.schedule(this._secondWaitTime());
E
Erich Gamma 已提交
103 104 105

		if (this._computer.computeAsync) {
			this._asyncComputationPromiseDone = false;
106
			this._asyncComputationPromise = this._computer.computeAsync().then((asyncResult: Result) => {
E
Erich Gamma 已提交
107 108
				this._asyncComputationPromiseDone = true;
				this._withAsyncResult(asyncResult);
R
Ron Buckton 已提交
109
			}, (e) => this._onError(e));
E
Erich Gamma 已提交
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
		} else {
			this._asyncComputationPromiseDone = true;
		}
	}

	private _triggerSyncComputation(): void {
		if (this._computer.computeSync) {
			this._computer.onResult(this._computer.computeSync(), true);
		}

		if (this._asyncComputationPromiseDone) {
			this._state = ComputeHoverOperationState.IDLE;
			this._onComplete(this._computer.getResult());
		} else {
			this._state = ComputeHoverOperationState.WAITING_FOR_ASYNC_COMPUTATION;
			this._onProgress(this._computer.getResult());
		}
	}

	private _showLoadingMessage(): void {
		if (this._state === ComputeHoverOperationState.WAITING_FOR_ASYNC_COMPUTATION) {
			this._onProgress(this._computer.getResultWithLoadingMessage());
		}
	}

	private _withAsyncResult(asyncResult: Result): void {
		if (asyncResult) {
			this._computer.onResult(asyncResult, false);
		}

		if (this._state === ComputeHoverOperationState.WAITING_FOR_ASYNC_COMPUTATION) {
			this._state = ComputeHoverOperationState.IDLE;
			this._onComplete(this._computer.getResult());
		}
	}

J
Johannes Rieken 已提交
146
	private _onComplete(value: Result): void {
E
Erich Gamma 已提交
147 148 149 150 151
		if (this._completeCallback) {
			this._completeCallback(value);
		}
	}

J
Johannes Rieken 已提交
152
	private _onError(error: any): void {
E
Erich Gamma 已提交
153 154 155
		if (this._errorCallback) {
			this._errorCallback(error);
		} else {
A
Alex Dima 已提交
156
			onUnexpectedError(error);
E
Erich Gamma 已提交
157 158 159
		}
	}

J
Johannes Rieken 已提交
160
	private _onProgress(value: Result): void {
E
Erich Gamma 已提交
161 162 163 164 165
		if (this._progressCallback) {
			this._progressCallback(value);
		}
	}

166 167 168 169
	public start(mode: HoverStartMode): void {
		if (mode === HoverStartMode.Delayed) {
			if (this._state === ComputeHoverOperationState.IDLE) {
				this._state = ComputeHoverOperationState.FIRST_WAIT;
170 171
				this._firstWaitScheduler.schedule(this._firstWaitTime());
				this._loadingMessageScheduler.schedule(this._loadingMessageTime());
172 173 174 175 176 177 178 179 180 181 182 183 184
			}
		} else {
			switch (this._state) {
				case ComputeHoverOperationState.IDLE:
					this._triggerAsyncComputation();
					this._secondWaitScheduler.cancel();
					this._triggerSyncComputation();
					break;
				case ComputeHoverOperationState.SECOND_WAIT:
					this._secondWaitScheduler.cancel();
					this._triggerSyncComputation();
					break;
			}
E
Erich Gamma 已提交
185 186 187 188 189 190 191 192 193 194
		}
	}

	public cancel(): void {
		this._loadingMessageScheduler.cancel();
		if (this._state === ComputeHoverOperationState.FIRST_WAIT) {
			this._firstWaitScheduler.cancel();
		}
		if (this._state === ComputeHoverOperationState.SECOND_WAIT) {
			this._secondWaitScheduler.cancel();
195 196 197 198
			if (this._asyncComputationPromise) {
				this._asyncComputationPromise.cancel();
				this._asyncComputationPromise = null;
			}
E
Erich Gamma 已提交
199 200
		}
		if (this._state === ComputeHoverOperationState.WAITING_FOR_ASYNC_COMPUTATION) {
201 202 203 204
			if (this._asyncComputationPromise) {
				this._asyncComputationPromise.cancel();
				this._asyncComputationPromise = null;
			}
E
Erich Gamma 已提交
205 206 207 208 209
		}
		this._state = ComputeHoverOperationState.IDLE;
	}

}