management/sync_struct/Notifier: use publish function instead of object

This commit is contained in:
Sebastien Bourdeauducq 2015-01-13 18:40:59 +08:00
parent 2029613c58
commit 77a7e592cb
1 changed files with 25 additions and 25 deletions

View File

@ -72,9 +72,9 @@ class Subscriber:
class Notifier: class Notifier:
def __init__(self, backing_struct, publisher=None, path=[]): def __init__(self, backing_struct, publish=None, path=[]):
self.read = backing_struct self.read = backing_struct
self.publisher = publisher self.publish = publish
self._backing_struct = backing_struct self._backing_struct = backing_struct
self._path = path self._path = path
@ -83,44 +83,44 @@ class Notifier:
def append(self, x): def append(self, x):
self._backing_struct.append(x) self._backing_struct.append(x)
if self.publisher is not None: if self.publish is not None:
self.publisher.publish(self, {"action": "append", self.publish(self, {"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.publisher is not None: if self.publish is not None:
self.publisher.publish(self, {"action": "insert", self.publish(self, {"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.publisher is not None: if self.publish is not None:
self.publisher.publish(self, {"action": "pop", self.publish(self, {"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.publisher is not None: if self.publish is not None:
self.publisher.publish(self, {"action": "setitem", self.publish(self, {"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.publisher is not None: if self.publish is not None:
self.publisher.publish(self, {"action": "delitem", self.publish(self, {"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.publisher, self._path + [key]) return Notifier(item, self.publish, self._path + [key])
class Publisher(AsyncioServer): class Publisher(AsyncioServer):
@ -131,7 +131,7 @@ class Publisher(AsyncioServer):
self._notifier_names = {id(v): k for k, v in notifiers.items()} self._notifier_names = {id(v): k for k, v in notifiers.items()}
for notifier in notifiers.values(): for notifier in notifiers.values():
notifier.publisher = self notifier.publish = self.publish
@asyncio.coroutine @asyncio.coroutine
def _handle_connection_cr(self, reader, writer): def _handle_connection_cr(self, reader, writer):