-
Notifications
You must be signed in to change notification settings - Fork 546
/
Copy pathlazy.ts
39 lines (36 loc) · 1.05 KB
/
lazy.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
/*!
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
* Licensed under the MIT License.
*/
/**
* Helper class for lazy initialized values. Ensures the value is only generated once, and remain immutable.
*
* @deprecated Moved to the `@fluidframework/core-utils` package.
* @internal
*/
export class Lazy<T> {
private _value: T | undefined;
private _evaluated: boolean = false;
/**
* Instantiates an instance of Lazy<T>.
* @param valueGenerator - The function that will generate the value when value is accessed the first time.
*/
constructor(private readonly valueGenerator: () => T) {}
/**
* Return true if the value as been generated, otherwise false.
*/
public get evaluated(): boolean {
return this._evaluated;
}
/**
* Get the value. If this is the first call the value will be generated.
*/
public get value(): T {
if (!this._evaluated) {
this._evaluated = true;
this._value = this.valueGenerator();
}
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return this._value!;
}
}