-
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathcache.js
More file actions
125 lines (104 loc) · 2.75 KB
/
Copy pathcache.js
File metadata and controls
125 lines (104 loc) · 2.75 KB
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
import { GLOBAL_KEY, CONSTANTS_KEY, COMPUTED_KEY } from '../constants';
import { warn } from '../utils';
let globalCache;
export const clearCache = () => {
globalCache = Object.create(null);
globalCache[GLOBAL_KEY] = Object.create(null);
};
clearCache();
const mutateDefinition = (definition) => {
const constants = definition.constants;
const computed = definition.computed;
warn(
definition.constant !== undefined,
`"constant" key found in styles definition. Maybe you intended to use "constants" instead`,
);
warn(
definition.computeds !== undefined,
`"computeds" key found in styles definition. Maybe you intended to use "computed" instead`,
);
definition.constants = null;
definition.computed = null;
return { constants, computed };
};
const getValueFromStorageObject = (key, object, isConstant, isComputed) => {
let obj = object;
if (isConstant) {
obj = obj[CONSTANTS_KEY];
} else if (isComputed) {
obj = obj[COMPUTED_KEY];
}
return obj && obj[key];
};
export const setInCache = (definition, namespace) => {
const { constants, computed } = mutateDefinition(definition);
let cache = globalCache;
if (namespace) {
if (!cache[namespace]) cache[namespace] = Object.create(null);
cache = cache[namespace];
} else {
cache = cache[GLOBAL_KEY];
}
Object.assign(cache, definition);
Object.assign(cache, {
[CONSTANTS_KEY]: constants,
[COMPUTED_KEY]: computed,
});
};
export const getFromCache = (
key,
namespace,
definition,
isConstant,
isComputed,
) => {
let value;
// if it's in definition
if (definition) {
value = getValueFromStorageObject(key, definition, isConstant, isComputed);
if (value) return value;
}
// if it's in the namespace
if (namespace && globalCache[namespace]) {
value = getValueFromStorageObject(
key,
globalCache[namespace],
isConstant,
isComputed,
);
} else if (
process.env.NODE_ENV !== 'production' &&
namespace &&
!globalCache[namespace]
) {
warn(
true,
`Namespace "${namespace}" does not exist or has not been imported`,
'Non-Existent-Namespace',
);
}
// was not in the namespace, try in the global cache
if (!value) {
value = getValueFromStorageObject(
key,
globalCache[GLOBAL_KEY],
isConstant,
isComputed,
);
}
// key not found
if (!value) {
warn(
!value,
`${isConstant ? 'Constant' : 'Style'} "${key}" does not exist`,
`Non-Existent-${isConstant ? 'Constant' : 'Style'}`,
);
return;
}
return value;
};
export const getConstant = (name, namespace) => {
let space =
namespace && namespace.namespace ? namespace.namespace : namespace;
return getFromCache(name, space, null, true);
};