-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
99 lines (86 loc) · 2.67 KB
/
index.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
let _Vue
/**
* @param {Array|Object}
* @return {Object}
*/
function normalizeMap (map) {
return Array.isArray(map)
? map.map(key => ({ key, val: normalizeReplicantConfig(key) }))
: Object.keys(map).map(key => ({ key, val: normalizeReplicantConfig(map[key]) }))
}
function normalizeReplicantConfig(val) {
if (typeof val === 'string') {
return {
name: val,
persistent: true,
defaultValue: undefined
}
} else if (typeof val === 'object') {
return {
name: val.name,
persistent: ('persistent' in val) ? val.persistent : true,
defaultValue: ('defaultValue' in val) ? val.defaultValue : undefined
}
} else {
throw new Error(`Unexpected type ${typeof val} for normalizing replicant config`)
}
}
/**
* @param {Array|Object}
*/
function mapReplicants (replicants) {
return normalizeMap(replicants).reduce((mapping, { key, val }) => {
mapping[key] = new VueReplicant(val)
return mapping
}, {})
}
class VueReplicant {
constructor({ name, persistent, defaultValue }) {
_Vue.util.defineReactive(this, 'value')
this.value = defaultValue
this._replicant = nodecg.Replicant(name, { persistent, defaultValue })
this._replicant.on('change', (newValue) => {
if (newValue != undefined) {
this.value = JSON.parse(JSON.stringify(newValue))
}
else {
this.value = undefined
}
})
}
}
/**
* @param {Object}
* @param {Object}
* @return {Object}
*/
function extendComputed (to, from) {
let extended = {}
for (const key in to) {
extended[key] = to[key]
}
for (const key in from) {
if (key in extended) {
_Vue.util.warn(`A value for ${key} is already defined in the destination object`, to)
}
extended[key] = from[key]
}
return extended
}
export default {
install (Vue) {
_Vue = Vue
Vue.mixin({
beforeCreate () {
if ('replicants' in this.constructor.options) {
this.$replicants = mapReplicants(this.constructor.options.replicants)
let replicantGetters = Object.keys(this.$replicants).reduce((replicants, key) => {
replicants[key] = () => { return this.$replicants[key].value }
return replicants
}, {})
this.$options.computed = extendComputed(this.$options.computed || {}, replicantGetters)
}
}
})
}
}