forked from vuejs/devtools-v6
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshared-data.js
104 lines (90 loc) · 2.05 KB
/
shared-data.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
// Initial state
const internalSharedData = {
openInEditorHost: '/',
classifyComponents: true,
theme: 'auto',
displayDensity: 'low',
recordVuex: true,
cacheVuexSnapshotsEvery: 50,
cacheVuexSnapshotsLimit: 10,
snapshotLoading: null,
recordPerf: false
}
const persisted = [
'classifyComponents',
'theme',
'displayDensity',
'recordVuex'
]
// ---- INTERNALS ---- //
let Vue
let bridge
// Storage API
let storage = null
// List of fields to persist to storage (disabled if 'false')
// This should be unique to each shared data client to prevent conflicts
let persist = false
// For reactivity, we wrap the data in a Vue instance
let vm
export function init (params) {
// Mandatory params
bridge = params.bridge
Vue = params.Vue
if (params.hasOwnProperty('storage')) {
storage = params.storage
persist = persisted
}
// Load persisted fields
if (persist) {
persist.forEach(key => {
const value = storage.get(`shared-data:${key}`)
if (value !== null) {
internalSharedData[key] = value
// Send to other shared data clients
sendValue(key, value)
}
})
}
// Wrapper Vue instance
vm = new Vue({
data: internalSharedData
})
// Update value from other shared data clients
bridge.on('shared-data:set', ({ key, value }) => {
setValue(key, value)
})
}
export function destroy () {
bridge.removeAllListeners('shared-data:set')
vm.$destroy()
}
function setValue (key, value) {
// Storage
if (persist && persist.includes(key)) {
storage.set(`shared-data:${key}`, value)
}
vm[key] = value
// Validate Proxy set trap
return true
}
function sendValue (key, value) {
bridge && bridge.send('shared-data:set', {
key,
value
})
}
export function watch (...args) {
vm.$watch(...args)
}
const proxy = {}
Object.keys(internalSharedData).forEach(key => {
Object.defineProperty(proxy, key, {
configurable: false,
get: () => vm && vm.$data[key],
set: (value) => {
sendValue(key, value)
setValue(key, value)
}
})
})
export default proxy