/
dep_property.go
376 lines (327 loc) · 7.53 KB
/
dep_property.go
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
package bone
import (
"sort"
)
//WatchedTarget 可以被Watcher监听的接口
type WatchedTarget interface {
Get() interface{}
SetFrom(WatchedTarget, interface{})
Equal(interface{}) bool
AddSub(watcher *Watcher)
}
//Watcher 观测属性的变化
type Watcher struct {
source WatchedTarget
target WatchedTarget
//update func(sender interface{}, source interface{}, old, new interface{})
computer func(sender interface{}, new interface{}) interface{} //用于支持计算属性
queue *watcherQueue
}
//Update 需要更新时调用update函数
func (wr *Watcher) Update() {
newValue := wr.source.Get()
if wr.computer != nil {
newValue = wr.computer(wr, newValue)
}
if wr.target.Equal(newValue) {
return
}
wr.target.SetFrom(wr.source, newValue)
}
//Run 异步执行
func (wr *Watcher) Run() {
if wr.queue != nil {
wr.queue.Push(wr)
} else {
wr.Update()
}
}
//Dependence 依赖
type Dependence struct {
subs []*Watcher
}
//Notify 通知
func (dp *Dependence) Notify(source WatchedTarget) {
for _, v := range dp.subs {
if v.target != source {
v.Run()
}
}
}
//AddSub 添加订阅 当支持if 和 for指令时 可能需要实现RemoveSub
func (dp *Dependence) AddSub(watcher *Watcher) {
if watcher == nil {
return
}
dp.subs = append(dp.subs, watcher)
}
//DepProperty 用于数据绑定
type DepProperty interface {
Set(value interface{})
Get() interface{}
}
type depProperty struct {
Dependence
value interface{}
setted func(sender interface{}, value interface{})
getted func(sender interface{}, value interface{})
}
//NewDepProperty 返回1个依赖对象
func NewDepProperty(value interface{}, setted, getted func(sender interface{}, value interface{})) DepProperty {
return &depProperty{
value: value,
setted: setted,
getted: getted,
}
}
//Set 设置属性值
func (dp *depProperty) Set(value interface{}) {
dp.SetFrom(nil, value)
}
//Get 获取属性值
func (dp *depProperty) Get() interface{} {
if dp.getted != nil {
dp.getted(dp, dp.value)
}
return dp.value
}
func (dp *depProperty) SetFrom(source WatchedTarget, value interface{}) {
if dp.Equal(value) {
return
}
dp.value = value
if dp.setted != nil {
dp.setted(dp, dp.value)
}
dp.Notify(source)
}
//Equal value是否相等
func (dp *depProperty) Equal(value interface{}) bool {
return dp.value == value
}
//WatcherManager 用于管理Watcher
type WatcherManager interface {
Create(source, target WatchedTarget,
computer func(sender interface{}, new interface{}) interface{}) *Watcher
SetMode(async bool)
Mode() bool
}
func newWatcherManager() *watcherManager {
return &watcherManager{
watcherQueue: watcherQueue{
async: true,
slice: nil,
exist: make(map[*Watcher]bool),
},
}
}
type watcherManager struct {
watcherQueue
}
func (wm *watcherManager) Create(source, target WatchedTarget,
computer func(sender interface{}, new interface{}) interface{}) *Watcher {
wr := &Watcher{
source: source,
target: target,
computer: computer,
queue: &wm.watcherQueue,
}
return wr
}
type watcherQueue struct {
async bool //true 则入队 false直接执行
slice []*Watcher
exist map[*Watcher]bool
}
func (wq *watcherQueue) SetMode(async bool) {
if wq.async == async {
return
}
for len(wq.slice) > 0 {
wq.Run()
}
wq.async = async
}
func (wq *watcherQueue) Mode() bool {
return wq.async
}
//Push Watch入队 自动去重
func (wq *watcherQueue) Push(watcher *Watcher) {
if !wq.async {
watcher.Update()
return
}
if _, ok := wq.exist[watcher]; ok {
return
}
wq.exist[watcher] = true
wq.slice = append(wq.slice, watcher)
}
//Run 执行异步watch
func (wq *watcherQueue) Run() {
for _, v := range wq.slice {
v.Update()
}
wq.slice = wq.slice[:0]
for k := range wq.exist {
delete(wq.exist, k)
}
}
//NewSliceDepProperty 返回1个依赖对象
func NewSliceDepProperty(value []interface{}, setted, getted func(sender interface{}, value []interface{})) SliceDepProperty {
return &sliceDepProperty{
value: value,
setted: setted,
getted: getted,
}
}
//SliceDepProperty 用于数据绑定
type SliceDepProperty interface {
SetSlice(value []interface{})
GetSlice() []interface{}
Push(item interface{}) int
Pop() interface{}
Unshift(item interface{}) int
Shift() interface{}
Splice(start, howmany int, items ...interface{}) []interface{}
}
//SliceDepProperty 数组依赖属性
type sliceDepProperty struct {
Dependence
value []interface{}
setted func(sender interface{}, value []interface{})
getted func(sender interface{}, value []interface{})
}
//SliceDepProperty 接口
func (dp *sliceDepProperty) SetSlice(value []interface{}) {
dp.SetFrom(nil, value)
}
func (dp *sliceDepProperty) GetSlice() []interface{} {
return dp.Get().([]interface{})
}
func (dp *sliceDepProperty) Push(item interface{}) int {
dp.value = append(dp.value, item)
dp.notifyIfSet(nil)
return len(dp.value)
}
func (dp *sliceDepProperty) Pop() interface{} {
count := len(dp.value)
if count <= 0 {
return nil
}
tail := dp.value[count-1]
dp.value = dp.value[0 : count-1]
dp.notifyIfSet(nil)
return tail
}
func (dp *sliceDepProperty) Unshift(item interface{}) int {
value := make([]interface{}, len(dp.value)+1)
value[0] = item
for i := 0; i < len(dp.value); i++ {
value[i+1] = dp.value[i]
}
dp.value = value
dp.notifyIfSet(nil)
return len(dp.value)
}
func (dp *sliceDepProperty) Shift() interface{} {
count := len(dp.value)
if count <= 0 {
return nil
}
head := dp.value[0]
dp.value = dp.value[1:]
dp.notifyIfSet(nil)
return head
}
//用于调用排序方法
type sortInterface struct {
value []interface{}
method func(i, j interface{}) bool
}
func (si sortInterface) Len() int {
return len(si.value)
}
func (si sortInterface) Less(i, j int) bool {
return si.method(si.value[i], si.value[j])
}
func (si sortInterface) Swap(i, j int) {
si.value[i], si.value[j] = si.value[j], si.value[i]
}
func (dp *sliceDepProperty) Sort(method func(i, j interface{}) bool) {
if len(dp.value) <= 0 {
return
}
sort.Sort(sortInterface{value: dp.value, method: method})
dp.notifyIfSet(nil)
}
func (dp *sliceDepProperty) Splice(start, howmany int, items ...interface{}) []interface{} {
count := len(dp.value)
if start < 0 || howmany < 0 || start+howmany > count {
return nil
}
value := make([]interface{}, count-howmany+len(items))
j := 0
for i := 0; i < start; i++ {
value[j] = dp.value[i]
j++
}
for i := 0; i < len(items); i++ {
value[j] = items[i]
j++
}
for i := start + howmany; i < count; i++ {
value[j] = dp.value[i]
j++
}
removed := dp.value[start : start+howmany]
dp.value = value
dp.notifyIfSet(nil)
return removed
}
func (dp *sliceDepProperty) Reverse() {
count := len(dp.value)
if count <= 1 {
return
}
for i, j := 0, count-1; i < j; i, j = i+1, j-1 {
dp.value[i], dp.value[j] = dp.value[j], dp.value[i]
}
dp.notifyIfSet(nil)
}
func (dp *sliceDepProperty) notifyIfSet(source WatchedTarget) {
if dp.setted != nil {
dp.setted(dp, dp.value)
}
dp.Notify(source)
}
//Equal 数组是否相同
func (dp *sliceDepProperty) Equal(value interface{}) bool {
left := dp.value
right := value.([]interface{})
if len(left) != len(right) {
return false
}
count := len(left)
for i := 0; i < count; i++ {
if left[i] != right[i] {
return false
}
}
return true
}
//SetFrom 设置属性值
func (dp *sliceDepProperty) SetFrom(source WatchedTarget, value interface{}) {
if dp.Equal(value) {
return
}
dp.value = append([]interface{}(nil), value)
dp.notifyIfSet(source)
}
//Get 获取属性值
func (dp *sliceDepProperty) Get() interface{} {
if dp.getted != nil {
dp.getted(dp, dp.value)
}
return dp.value
}