-
Notifications
You must be signed in to change notification settings - Fork 3
/
origin.ts
68 lines (59 loc) · 1.65 KB
/
origin.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
import core from 'core-js'
var originStorage = new Map(),
unknownOrigin = Object.freeze({moduleId:undefined,moduleMember:undefined});
function ensureType(value){
if(value instanceof Origin){
return value;
}
return new Origin(value);
}
/**
* A metadata annotation that describes the origin module of the function to which it's attached.
*
* @class Origin
* @constructor
* @param {string} moduleId The origin module id.
* @param {string} moduleMember The name of the export in the origin module.
*/
export class Origin {
public moduleId;
public moduleMember;
constructor(moduleId, moduleMember?){
this.moduleId = moduleId;
this.moduleMember = moduleMember;
}
/**
* Get the Origin annotation for the specified function.
*
* @method get
* @static
* @param {Function} fn The function to inspect for Origin metadata.
* @return {Origin} Returns the Origin metadata.
*/
static get(fn){
var origin = originStorage.get(fn);
if(origin !== undefined){
return origin;
}
if(typeof fn.origin === 'function'){
originStorage.set(fn, origin = ensureType(fn.origin()));
} else if(fn.origin !== undefined){
originStorage.set(fn, origin = ensureType(fn.origin));
}
return origin || unknownOrigin;
}
/**
* Set the Origin annotation for the specified function.
*
* @method set
* @static
* @param {Function} fn The function to set the Origin metadata on.
* @param {origin} fn The Origin metadata to store on the function.
* @return {Origin} Returns the Origin metadata.
*/
static set(fn, origin){
if(Origin.get(fn) === unknownOrigin){
originStorage.set(fn, origin);
}
}
}