-
Notifications
You must be signed in to change notification settings - Fork 0
/
valueLoader.ts
73 lines (63 loc) · 1.81 KB
/
valueLoader.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
import callsites from 'callsites'
import { BasicDataContainer } from '../dataContainers/basicDataContainer'
import { SyncLoader } from './syncLoader'
import type { Reference } from '../dataContainers/reference'
/**
* Loader that loads an arbitrary static value.
*/
export class ValueLoader<T> extends SyncLoader<T> {
private readonly source?: string
/**
* Creates a new instance of ValueLoader.
* @param value The value to return as a load result.
* @param sourceOrFrameIndex Value source name or stack trace index to use
* (0 - direct caller; 1 - caller of the caller, etc)
*/
constructor(
private readonly value: T,
sourceOrFrameIndex: number | string = 0,
) {
super()
this.source = typeof sourceOrFrameIndex === 'string'
? sourceOrFrameIndex
: ValueLoader.getSource(sourceOrFrameIndex)
}
/**
* Loads the value synchronously.
*/
loadSync(): BasicDataContainer<T> {
return new BasicDataContainer(this, this.value)
}
/**
* Returns supported source references for given configuration object path.
*
* @param path Object path parts.
*/
getReferences(path: string[]): Reference[] {
if (this.source) {
return [
{
identifier: path.join('.'),
source: this.source,
},
]
}
return []
}
/**
* Attempts to determine the source name from the call stack.
*/
private static getSource(skipFrames: number) {
// 1st frame is this method
// 2nd frame is the constructor of this class
// 3rd frame is the direct caller
const callSite = callsites()[2 + skipFrames]
if (callSite) {
const file = callSite.getFileName()
const line = callSite.getLineNumber()
if (file) {
return file + (line ? `:${line}` : '')
}
}
}
}