management/sync_struct: always publish with root notifier

This commit is contained in:
Sebastien Bourdeauducq 2015-01-14 11:36:28 +08:00
parent 4160d24abc
commit ebdd92c7c4
1 changed files with 25 additions and 21 deletions

View File

@ -78,9 +78,13 @@ class Subscriber:
class Notifier: class Notifier:
def __init__(self, backing_struct, publish=None, path=[]): def __init__(self, backing_struct, root=None, path=[]):
self.read = backing_struct self.read = backing_struct
self.publish = publish if root is None:
self.root = self
self.publish = None
else:
self.root = root
self._backing_struct = backing_struct self._backing_struct = backing_struct
self._path = path self._path = path
@ -89,44 +93,44 @@ class Notifier:
def append(self, x): def append(self, x):
self._backing_struct.append(x) self._backing_struct.append(x)
if self.publish is not None: if self.root.publish is not None:
self.publish(self, {"action": "append", self.root.publish(self.root, {"action": "append",
"path": self._path, "path": self._path,
"x": x}) "x": x})
def insert(self, i, x): def insert(self, i, x):
self._backing_struct.insert(i, x) self._backing_struct.insert(i, x)
if self.publish is not None: if self.root.publish is not None:
self.publish(self, {"action": "insert", self.root.publish(self.root, {"action": "insert",
"path": self._path, "path": self._path,
"i": i, "x": x}) "i": i, "x": x})
def pop(self, i=-1): def pop(self, i=-1):
r = self._backing_struct.pop(i) r = self._backing_struct.pop(i)
if self.publish is not None: if self.root.publish is not None:
self.publish(self, {"action": "pop", self.root.publish(self.root, {"action": "pop",
"path": self._path, "path": self._path,
"i": i}) "i": i})
return r return r
def __setitem__(self, key, value): def __setitem__(self, key, value):
self._backing_struct.__setitem__(key, value) self._backing_struct.__setitem__(key, value)
if self.publish is not None: if self.root.publish is not None:
self.publish(self, {"action": "setitem", self.root.publish(self.root, {"action": "setitem",
"path": self._path, "path": self._path,
"key": key, "key": key,
"value": value}) "value": value})
def __delitem__(self, key): def __delitem__(self, key):
self._backing_struct.__delitem__(key) self._backing_struct.__delitem__(key)
if self.publish is not None: if self.root.publish is not None:
self.publish(self, {"action": "delitem", self.root.publish(self.root, {"action": "delitem",
"path": self._path, "path": self._path,
"key": key}) "key": key})
def __getitem__(self, key): def __getitem__(self, key):
item = getitem(self._backing_struct, key) item = getitem(self._backing_struct, key)
return Notifier(item, self.publish, self._path + [key]) return Notifier(item, self.root, self._path + [key])
class Publisher(AsyncioServer): class Publisher(AsyncioServer):