-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResolverBase.ts
145 lines (128 loc) · 3.16 KB
/
ResolverBase.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
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*!
* @author electricessence / https://github.com/electricessence/
* @license MIT
*/
import DisposableBase from '@tsdotnet/disposable';
type Func<T> = () => T;
const NAME: string = 'ResolverBase';
/* eslint-disable @typescript-eslint/no-explicit-any */
/**
* The ResolverBase class handles resolving a factory method and detects recursion.
* Since JS does not have a synchronization mechanism (lock or otherwise)
* we have to prevent getValue from double triggering the value factory (optimistic concurrency)
* or returning return a value that is intermediate between resolving and resolved.
*/
export default abstract class ResolverBase<T>
extends DisposableBase
{
protected readonly _resolveState: {
created: null | boolean; // null = 'creating'
value?: T,
factory?: Func<T>,
error?: unknown
};
protected constructor (
valueFactory: Func<T>,
private readonly _allowReset: boolean = false
)
{
super(NAME);
if(!valueFactory) throw new Error('\'valueFactory\' cannot be null or undefined.');
this._resolveState = {
created: false,
factory: valueFactory
};
}
/**
* True if the resolution faulted.
* @returns {boolean}
*/
get isFaulted (): boolean
{
return !!this._resolveState.error;
}
/**
* The error value if previous faulted.
*/
get error (): any
{
return this.getError();
}
/**
* Returns true if the value has been created.
*/
get isValueCreated (): boolean
{
return !!this._resolveState.created;
}
/**
* Will return true if allowReset is true and a value factory still exists.
* @returns {boolean}
*/
get canReset (): boolean
{
return this._allowReset && !!this._resolveState.factory;
}
/**
* Uses the provided factory to generate the value and returns that value for subsequent requests.
*/
getValue (): T
{
this.throwIfDisposed();
const state = this._resolveState;
// Do not continue if already faulted.
if(state.error) throw state.error;
if(state.created===null) throw new Error('Recursion detected.');
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
else if(state.created) return state.value!;
const c = state.factory;
if(!c) throw new Error('Unexpected resolution state.');
state.created = null; // Mark this as 'resolving'.
try
{
if(!this._allowReset) state.factory = undefined;
const v = c();
state.value = v;
state.created = true;
state.error = undefined;
return v;
}
catch(ex)
{
state.value = undefined;
state.created = false;
state.error = ex;
throw ex;
}
}
/**
* Will attempt to reset this resolve if possible and returns true if successfully reset.
* @returns {boolean}
*/
tryReset (): boolean
{
if(!this._allowReset) return false;
const state = this._resolveState;
if(!state.factory) return false;
else
{
state.created = false;
state.value = undefined;
state.error = undefined;
return true;
}
}
// Allows for overriding this behavior.
protected getError (): unknown
{
return this._resolveState.error;
}
protected _onDispose (): void
{
const state = this._resolveState;
state.factory = undefined;
state.value = undefined;
state.created = null;
Object.freeze(state);
}
}