Skip to content
Snippets Groups Projects
Unverified Commit 1d2c9d8e authored by Sergei Zharinov's avatar Sergei Zharinov Committed by GitHub
Browse files

refactor(util/lazy): Strict null check (#13455)

* refactor(util/lazy): Strict null check

* Reimplement without `never` type cast

* Revert tsconfig.json

* Fix
parent 895a85a2
No related branches found
No related tags found
No related merge requests found
interface ValueResult<T> {
type: 'success';
value: T;
}
interface ErrorResult {
type: 'error';
err: Error;
}
export class Lazy<T> { export class Lazy<T> {
private _didRun = false; private _result?: ValueResult<T> | ErrorResult;
private _value?: T;
private _error: Error | undefined;
constructor(private readonly executor: () => T) {} constructor(private readonly executor: () => T) {}
hasValue(): boolean { hasValue(): boolean {
return this._didRun; return !!this._result;
} }
getValue(): T { getValue(): T {
if (!this._didRun) { const result = this._result;
try { if (result) {
this._value = this.executor(); if (result.type === 'success') {
} catch (err) { return result.value;
this._error = err;
} finally {
this._didRun = true;
} }
throw result.err;
} }
if (this._error) {
throw this._error; return this.realizeValue();
}
private realizeValue(): T {
try {
const value = this.executor();
this._result = { type: 'success', value };
return value;
} catch (err) {
this._result = { type: 'error', err };
throw err;
} }
return this._value;
} }
} }
...@@ -52,6 +52,8 @@ ...@@ -52,6 +52,8 @@
"lib/util/host-rules.ts", "lib/util/host-rules.ts",
"lib/util/html.ts", "lib/util/html.ts",
"lib/util/http/**/.ts", "lib/util/http/**/.ts",
"lib/util/lazy.ts",
"lib/util/lazy.spec.ts",
"lib/util/index.ts", "lib/util/index.ts",
"lib/util/json-writer/code-format.ts", "lib/util/json-writer/code-format.ts",
"lib/util/json-writer/editor-config.ts", "lib/util/json-writer/editor-config.ts",
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment