forked from vuejs/devtools-v6
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
130 lines (116 loc) · 3.22 KB
/
proxy.ts
File metadata and controls
130 lines (116 loc) · 3.22 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
126
127
128
129
130
import type { Context, DevtoolsPluginApi, Hookable } from './api/index.js'
import type { PluginDescriptor } from './plugin.js'
import { HOOK_PLUGIN_SETTINGS_SET } from './const.js'
import { now } from './time.js'
interface QueueItem {
method: string
args: any[]
resolve?: (value?: any) => void
}
export class ApiProxy<TTarget extends DevtoolsPluginApi<any> = DevtoolsPluginApi<any>> {
target: TTarget | null
targetQueue: QueueItem[]
proxiedTarget: TTarget
onQueue: QueueItem[]
proxiedOn: Hookable<Context>
plugin: PluginDescriptor
hook: any
fallbacks: Record<string, any>
constructor (plugin: PluginDescriptor, hook: any) {
this.target = null
this.targetQueue = []
this.onQueue = []
this.plugin = plugin
this.hook = hook
const defaultSettings: Record<string, any> = {}
if (plugin.settings) {
for (const id in plugin.settings) {
const item = plugin.settings[id]
defaultSettings[id] = item.defaultValue
}
}
const localSettingsSaveId = `__vue-devtools-plugin-settings__${plugin.id}`
let currentSettings = Object.assign({}, defaultSettings)
try {
const raw = localStorage.getItem(localSettingsSaveId)
const data = JSON.parse(raw)
Object.assign(currentSettings, data)
} catch (e) {
// noop
}
this.fallbacks = {
getSettings () {
return currentSettings
},
setSettings (value) {
try {
localStorage.setItem(localSettingsSaveId, JSON.stringify(value))
} catch (e) {
// noop
}
currentSettings = value
},
now () {
return now()
},
}
if (hook) {
hook.on(HOOK_PLUGIN_SETTINGS_SET, (pluginId, value) => {
if (pluginId === this.plugin.id) {
this.fallbacks.setSettings(value)
}
})
}
this.proxiedOn = new Proxy({} as Hookable<Context>, {
get: (_target, prop: string) => {
if (this.target) {
return this.target.on[prop]
} else {
return (...args) => {
this.onQueue.push({
method: prop,
args,
})
}
}
},
})
this.proxiedTarget = new Proxy({} as TTarget, {
get: (_target, prop: string) => {
if (this.target) {
return this.target[prop]
} else if (prop === 'on') {
return this.proxiedOn
} else if (Object.keys(this.fallbacks).includes(prop)) {
return (...args) => {
this.targetQueue.push({
method: prop,
args,
resolve: () => { /* noop */ },
})
return this.fallbacks[prop](...args)
}
} else {
return (...args) => {
return new Promise(resolve => {
this.targetQueue.push({
method: prop,
args,
resolve,
})
})
}
}
},
})
}
async setRealTarget (target: TTarget) {
this.target = target
for (const item of this.onQueue) {
this.target.on[item.method](...item.args)
}
for (const item of this.targetQueue) {
item.resolve(await this.target[item.method](...item.args))
}
}
}