/
hook.js
353 lines (308 loc) · 10 KB
/
hook.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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
// this script is injected into every page.
/**
* Install the hook on window, which is an event emitter.
* Note because Chrome content scripts cannot directly modify the window object,
* we are evaling this function by inserting a script tag. That's why we have
* to inline the whole event emitter implementation here.
*
* @param {Window|global} target
*/
export function installHook (target) {
let listeners = {}
if (target.hasOwnProperty('__VUE_DEVTOOLS_GLOBAL_HOOK__')) return
const hook = {
Vue: null,
_buffer: [],
_replayBuffer (event) {
let buffer = this._buffer
this._buffer = []
for (let i = 0, l = buffer.length; i < l; i++) {
let allArgs = buffer[i]
allArgs[0] === event
? this.emit.apply(this, allArgs)
: this._buffer.push(allArgs)
}
},
on (event, fn) {
const $event = '$' + event
if (listeners[$event]) {
listeners[$event].push(fn)
} else {
listeners[$event] = [fn]
this._replayBuffer(event)
}
},
once (event, fn) {
function on () {
this.off(event, on)
fn.apply(this, arguments)
}
this.on(event, on)
},
off (event, fn) {
event = '$' + event
if (!arguments.length) {
listeners = {}
} else {
const cbs = listeners[event]
if (cbs) {
if (!fn) {
listeners[event] = null
} else {
for (let i = 0, l = cbs.length; i < l; i++) {
const cb = cbs[i]
if (cb === fn || cb.fn === fn) {
cbs.splice(i, 1)
break
}
}
}
}
}
},
emit (event) {
const $event = '$' + event
let cbs = listeners[$event]
if (cbs) {
const eventArgs = [].slice.call(arguments, 1)
cbs = cbs.slice()
for (let i = 0, l = cbs.length; i < l; i++) {
cbs[i].apply(this, eventArgs)
}
} else {
const allArgs = [].slice.call(arguments)
this._buffer.push(allArgs)
}
}
}
hook.once('init', Vue => {
hook.Vue = Vue
Vue.prototype.$inspect = function () {
const fn = target.__VUE_DEVTOOLS_INSPECT__
fn && fn(this)
}
})
hook.once('vuex:init', store => {
hook.store = store
hook.initialState = clone(store.state)
const origReplaceState = store.replaceState.bind(store)
store.replaceState = state => {
hook.initialState = clone(state)
origReplaceState(state)
}
// Dynamic modules
let origRegister, origUnregister
if (store.registerModule) {
hook.storeModules = []
origRegister = store.registerModule.bind(store)
store.registerModule = (path, module, options) => {
if (typeof path === 'string') path = [path]
hook.storeModules.push({ path, module, options })
origRegister(path, module, options)
if (process.env.NODE_ENV !== 'production') console.log('early register module', path, module, options)
}
origUnregister = store.unregisterModule.bind(store)
store.unregisterModule = (path) => {
if (typeof path === 'string') path = [path]
const key = path.join('/')
const index = hook.storeModules.findIndex(m => m.path.join('/') === key)
if (index !== -1) hook.storeModules.splice(index, 1)
origUnregister(path)
if (process.env.NODE_ENV !== 'production') console.log('early unregister module', path)
}
}
hook.flushStoreModules = () => {
store.replaceState = origReplaceState
if (store.registerModule) {
store.registerModule = origRegister
store.unregisterModule = origUnregister
}
return hook.storeModules || []
}
})
Object.defineProperty(target, '__VUE_DEVTOOLS_GLOBAL_HOOK__', {
get () {
return hook
}
})
// Clone deep utility for cloning initial state of the store
// REFERENCE: https://github.com/pvorb/clone
let NativeMap
try {
NativeMap = Map
} catch (_) {
// maybe a reference error because no `Map`. Give it a dummy value that no
// value will ever be an instanceof.
NativeMap = function () {}
}
let NativeSet
try {
NativeSet = Set
} catch (_) {
NativeSet = function () {}
}
let NativePromise
try {
NativePromise = Promise
} catch (_) {
NativePromise = function () {}
}
function clone (parent, circular, depth, prototype, includeNonEnumerable) {
if (typeof circular === 'object') {
depth = circular.depth
prototype = circular.prototype
includeNonEnumerable = circular.includeNonEnumerable
circular = circular.circular
}
// maintain two arrays for circular references, where corresponding parents
// and children have the same index
var allParents = []
var allChildren = []
var useBuffer = typeof Buffer !== 'undefined' && typeof Buffer.isBuffer === 'function'
var isBuffer = typeof window !== 'undefined' ? browserIsBuffer : Buffer.isBuffer
if (typeof circular === 'undefined') { circular = true }
if (typeof depth === 'undefined') { depth = Infinity }
// recurse this function so we don't reset allParents and allChildren
function _clone (parent, depth) {
// cloning null always returns null
if (parent === null) { return null }
if (depth === 0) { return parent }
var child
var proto
if (typeof parent !== 'object') {
return parent
}
if (_instanceof(parent, NativeMap)) {
child = new NativeMap()
} else if (_instanceof(parent, NativeSet)) {
child = new NativeSet()
} else if (_instanceof(parent, NativePromise)) {
child = new NativePromise(function (resolve, reject) {
parent.then(function (value) {
resolve(_clone(value, depth - 1))
}, function (err) {
reject(_clone(err, depth - 1))
})
})
} else if (clone.__isArray(parent)) {
child = []
} else if (clone.__isRegExp(parent)) {
child = new RegExp(parent.source, __getRegExpFlags(parent))
if (parent.lastIndex) child.lastIndex = parent.lastIndex
} else if (clone.__isDate(parent)) {
child = new Date(parent.getTime())
} else if (useBuffer && isBuffer(parent)) {
if (Buffer.from) {
// Node.js >= 5.10.0
child = Buffer.from(parent)
} else {
// Older Node.js versions
// eslint-disable-next-line node/no-deprecated-api
child = new Buffer(parent.length)
parent.copy(child)
}
return child
} else if (_instanceof(parent, Error)) {
child = Object.create(parent)
} else {
if (typeof prototype === 'undefined') {
proto = Object.getPrototypeOf(parent)
child = Object.create(proto)
} else {
child = Object.create(prototype)
proto = prototype
}
}
if (circular) {
var index = allParents.indexOf(parent)
if (index !== -1) {
return allChildren[index]
}
allParents.push(parent)
allChildren.push(child)
}
if (_instanceof(parent, NativeMap)) {
parent.forEach(function (value, key) {
var keyChild = _clone(key, depth - 1)
var valueChild = _clone(value, depth - 1)
child.set(keyChild, valueChild)
})
}
if (_instanceof(parent, NativeSet)) {
parent.forEach(function (value) {
var entryChild = _clone(value, depth - 1)
child.add(entryChild)
})
}
for (var i in parent) {
var attrs = Object.getOwnPropertyDescriptor(parent, i)
if (attrs) {
if (attrs.hasOwnProperty('get') && attrs.get.name === 'computedGetter') {
Object.defineProperty(child, i, attrs)
continue
}
child[i] = _clone(parent[i], depth - 1)
}
}
if (Object.getOwnPropertySymbols) {
var symbols = Object.getOwnPropertySymbols(parent)
for (let i = 0; i < symbols.length; i++) {
// Don't need to worry about cloning a symbol because it is a primitive,
// like a number or string.
var symbol = symbols[i]
var descriptor = Object.getOwnPropertyDescriptor(parent, symbol)
if (descriptor && !descriptor.enumerable && !includeNonEnumerable) {
continue
}
child[symbol] = _clone(parent[symbol], depth - 1)
Object.defineProperty(child, symbol, descriptor)
}
}
if (includeNonEnumerable) {
var allPropertyNames = Object.getOwnPropertyNames(parent)
for (let i = 0; i < allPropertyNames.length; i++) {
const propertyName = allPropertyNames[i]
let descriptor = Object.getOwnPropertyDescriptor(parent, propertyName)
if (descriptor && descriptor.enumerable) {
continue
}
child[propertyName] = _clone(parent[propertyName], depth - 1)
Object.defineProperty(child, propertyName, descriptor)
}
}
return child
}
return _clone(parent, depth)
}
// private utility functions
function __objToStr (o) {
return Object.prototype.toString.call(o)
}
clone.__objToStr = __objToStr
function __isDate (o) {
return typeof o === 'object' && __objToStr(o) === '[object Date]'
}
clone.__isDate = __isDate
function __isArray (o) {
return typeof o === 'object' && __objToStr(o) === '[object Array]'
}
clone.__isArray = __isArray
function __isRegExp (o) {
return typeof o === 'object' && __objToStr(o) === '[object RegExp]'
}
clone.__isRegExp = __isRegExp
function __getRegExpFlags (re) {
var flags = ''
if (re.global) flags += 'g'
if (re.ignoreCase) flags += 'i'
if (re.multiline) flags += 'm'
return flags
}
clone.__getRegExpFlags = __getRegExpFlags
function _instanceof (obj, type) {
return type != null && obj instanceof type
}
function browserIsBuffer (b) {
return !!(b != null && '_isBuffer' in b && b._isBuffer)
}
}