Skip to content

Commit a4c53a8

Browse files
committed
Ensures errors are handled properly
1 parent 3d17279 commit a4c53a8

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

src/system/lazy.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
/** Provides lazy initialization support */
22
export class Lazy<T> {
3+
private _evaluated: boolean = false;
4+
get evaluated(): boolean {
5+
return this._evaluated;
6+
}
7+
8+
private _exception: Error | undefined;
39
private _value?: T;
4-
private _initialized: boolean = false;
510

611
/**
712
* Creates a new instance of Lazy<T> that uses the specified initialization function.
@@ -11,11 +16,19 @@ export class Lazy<T> {
1116

1217
/** Gets the lazily initialized value of the current Lazy<T> instance */
1318
get value(): T {
14-
if (!this._initialized) {
15-
this._value = this.valueProvider();
16-
this._initialized = true;
19+
if (!this._evaluated) {
20+
try {
21+
this._value = this.valueProvider();
22+
} catch (ex) {
23+
this._exception = ex;
24+
throw ex;
25+
} finally {
26+
this._evaluated = true;
27+
}
1728
}
1829

30+
if (this._exception) throw this._exception;
31+
1932
return this._value!;
2033
}
2134
}

0 commit comments

Comments
 (0)