forked from microsoft/vscode-cpptools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataBinding.ts
More file actions
75 lines (66 loc) · 2.49 KB
/
Copy pathdataBinding.ts
File metadata and controls
75 lines (66 loc) · 2.49 KB
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
/* --------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All Rights Reserved.
* See 'LICENSE' in the project root for license information.
* ------------------------------------------------------------------------------------------ */
import * as vscode from 'vscode';
import { Deferral } from './utils';
export class DataBinding<T> {
private valueChanged = new vscode.EventEmitter<T>();
private isActive: boolean = true;
private deferral?: Deferral;
/**
* Bind an event to a value so that a data model can automatically update the UI when values change.
* Since values can change quickly and cause UI to flicker, an optional delay/trigger combination can
* be specified to prevent UI elements from appearing/disappearing too quickly.
* @param value The initial value in the binding.
* @param delay An optional delay (in milliseconds) for firing the value changed event.
* @param delayValueTrigger The value that triggers an event delay.
*/
constructor(private value: T, private delay: number = 0, private delayValueTrigger?: T) {
this.isActive = true;
}
public get Value(): T {
return this.value;
}
public set Value(value: T) {
if (value !== this.value) {
if (this.delay === 0 || value !== this.delayValueTrigger) {
this.value = value;
this.valueChanged.fire(this.value);
} else {
if (this.deferral) {
this.deferral.cancel();
}
this.deferral = new Deferral(() => {
this.value = value;
this.valueChanged.fire(this.value);
}, this.delay);
}
} else if (this.deferral) {
this.deferral.cancel();
this.deferral = undefined;
}
}
public setValueIfActive(value: T): void {
if (value !== this.value) {
this.value = value;
if (this.isActive) {
this.valueChanged.fire(this.value);
}
}
}
public get ValueChanged(): vscode.Event<T> {
return this.valueChanged.event;
}
public activate(): void {
this.isActive = true;
this.valueChanged.fire(this.value);
}
public deactivate(): void {
this.isActive = false;
}
public dispose(): void {
this.deactivate();
this.valueChanged.dispose();
}
}