-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
92 lines (77 loc) · 2.3 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
import Vue from 'vue'
import { MapSchema, ArraySchema } from "@colyseus/schema"
const bindOrSet = (state, key, value) => {
if(typeof value == 'object') {
Vue.set(state, key, {})
Bind(state[key], value)
} else {
Vue.set(state, key, value)
}
}
const handleValueChange = (state, obj) => {
obj.onChange = (changes) => {
changes.forEach(change => {
if(typeof change.value == 'number')
Vue.set(state, change.field, new Number(change.value))
else if(typeof change.value == 'string' || typeof change.value == 'boolean')
Vue.set(state, change.field, change.value)
})
}
}
const bindMapSchema = (state, map) => {
for(let key in map)
bindOrSet(state, key, map[key])
map.onAdd = (obj, key) => bindOrSet(state, key, obj)
map.onRemove = (obj, key) => Vue.delete(state, key)
map.onChange = (obj, key) => {
if(typeof obj == 'object') return
Vue.set(state, key, obj)
}
}
const bindArraySchema = (state, arr) => {
for(let i = 0; i < arr.length; i++)
bindOrSet(state, i, arr[i])
arr.onAdd = (obj, key) => bindOrSet(state, key, obj)
arr.onRemove = (obj, key) => Vue.delete(state, key)
arr.onChange = (obj, key) => {
if(typeof obj == 'object') return
Vue.set(state, key, obj)
}
}
const bindObject = (state, obj) => {
Bind(state, obj)
handleValueChange(state, obj)
}
export const Bind = (hostState, source) => {
for(let key in source) {
let item = source[key]
if(typeof item == 'function') continue
if(item === undefined) {
Vue.set(hostState, key, item)
} else if(typeof item == 'string' || typeof item == 'number' || typeof item == 'boolean') {
Vue.set(hostState, key, item)
} else if(item.constructor == MapSchema) {
Vue.set(hostState, key, {})
bindMapSchema(hostState[key], item)
} else if(item.constructor == ArraySchema) {
Vue.set(hostState, key, [])
bindArraySchema(hostState[key], item)
} else if(typeof item == 'object') {
Vue.set(hostState, key, {})
bindObject(hostState[key], item)
}
}
handleValueChange(hostState, source)
}
export const mapState = ($state, keys = []) => {
const mapped = {}
keys.forEach(key => {
mapped[key] = () => {
const data = $state[key]
if(data)
return data
return undefined
}
})
return mapped
}