/
flag.ts
41 lines (37 loc) · 849 Bytes
/
flag.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
/** Represents an easily switchable flag-like value. */
export class Flag<T = boolean>
{
/** Returns the current value. */
public get value(): T
{
return this._value;
}
private _value: T;
/**
* Creates a new flag object.
* @param value Flag value to start with.
*/
public constructor(value: T)
{
this._value = value;
}
/**
* Replaces the current value with the new value, and returns the old value.
* @param value New flag value.
* @returns Old value.
*/
public exchange(value: T): T
{
const previous = this._value;
this.update(value);
return previous;
}
/**
* Changes the current value.
* @param value New flag value.
*/
public update(value: T): void
{
this._value = value;
}
}