-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathcontext.js
47 lines (39 loc) · 1.06 KB
/
context.js
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
const hasHandler = { has }
const allHandlers = { has, get }
const globals = new Set()
let temp
let globalObj
if (typeof window !== 'undefined') globalObj = window // eslint-disable-line
else if (typeof global !== 'undefined') globalObj = global // eslint-disable-line
else if (typeof self !== 'undefined') globalObj = self // eslint-disable-line
globalObj.$nxCompileToSandbox = toSandbox
globalObj.$nxClearSandbox = clearSandbox
export function expose (...globalNames) {
for (let globalName of globalNames) {
globals.add(globalName)
}
}
export function hide (...globalNames) {
for (let globalName of globalNames) {
globals.delete(globalName)
}
}
export function hideAll () {
globals.clear()
}
function has (target, key) {
return globals.has(key) ? (key in target) : true
}
function get (target, key) {
return key in temp ? temp[key] : target[key]
}
function toSandbox (obj, tempVars) {
if (tempVars) {
temp = tempVars
return new Proxy(obj, allHandlers)
}
return new Proxy(obj, hasHandler)
}
function clearSandbox () {
temp = undefined
}