-
Notifications
You must be signed in to change notification settings - Fork 0
/
delta_update.py
134 lines (107 loc) · 3.8 KB
/
delta_update.py
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
# coding=utf-8
from functools import wraps
class DeltaUpdateException(Exception):
pass
class _DeltaUpdate(object):
def __init__(self):
self.m_listItem = []
self.m_bCollect = False
def CollectChangeItem(self, item):
def _BeforeCollect():
if not self.m_bCollect:
raise DeltaUpdateException()
if item in self.m_listItem:
raise DeltaUpdateException()
self.BeforeCollectChangeItem(item)
def _AfterCollect():
self.AfterCollectChangeItem(item)
def Decorate(func):
@wraps(func)
def Wrapper(*args, **kwargs):
bCollect = True
try:
_BeforeCollect()
except DeltaUpdateException:
bCollect = False
res = func(*args, **kwargs)
if bCollect:
self.m_listItem.append(item)
_AfterCollect()
return res
return Wrapper
return Decorate
def BeforeCollectChangeItem(self, item):
pass
def AfterCollectChangeItem(self, item):
pass
def RegisterUpdateChange(self, *args, **kwargs):
def _BeforeUpdateChange():
self.m_listItem = []
self.BeforeUpdateChange(*args, **kwargs)
def _AfterUpdateChange():
self.m_bCollect = False
self.AfterUpdateChange(*args, **kwargs)
def Decorate(func):
@wraps(func)
def Wrapper(*ar, **kw):
bUpdate = True
try:
self.m_bCollect = True
_BeforeUpdateChange()
except DeltaUpdateException:
self.m_bCollect = False
bUpdate = False
res = func(*ar, **kw)
if bUpdate:
self.UpdateChange(*args, **kwargs)
_AfterUpdateChange()
return res
return Wrapper
return Decorate
def BeforeUpdateChange(self, *args, **kwargs):
pass
def UpdateChange(self, *args, **kwargs):
raise NotImplementedError
def AfterUpdateChange(self, *args, **kwargs):
pass
class DeltaUpdate(_DeltaUpdate):
def CollectChangeItem(self, item):
"""
收集发生变化的元素
使用装饰器 @self.CollectChangeItem("a")
当被装饰函数被调用时,即元素a发生了改变,会被收集。
前提是BeforeCollectChangeItem不拦截这次收集
"""
return super(DeltaUpdate, self).CollectChangeItem(item)
def BeforeCollectChangeItem(self, item):
"""
勾子,收集元素之前
可通过 raise DeltaUpdateException(),中止此次收集
"""
pass
def AfterCollectChangeItem(self, item):
"""勾子,成功收集元素之后"""
pass
def RegisterUpdateChange(self, *args, **kwargs):
"""
注册同步改变
使用装饰器 @self.RegisterUpdateChange()
如被装饰函数m,当函数m被调用,函数m的子函数会收集变化元素
函数m执行完毕,会调用UpdateChange
"""
return super(DeltaUpdate, self).RegisterUpdateChange(*args, **kwargs)
def BeforeUpdateChange(self, *args, **kwargs):
"""
勾子,UpdateChange之前
可通过 raise DeltaUpdateException(),中止此次UpdateChange
"""
pass
def UpdateChange(self, *args, **kwargs):
"""
自定的同步动作
可通过self.m_listItem查看,收集到的元素,决定同步的内容
"""
raise NotImplementedError
def AfterUpdateChange(self, *args, **kwargs):
"""勾子,触发UpdateChange之后"""
pass