88143fe9c6a3233714b9bbb1dfc11487ddd09d4b
[jamaendo] / jamaui / postoffice.py
1 # message central
2
3 class PostOffice(object):
4     class Client(object):
5         def __init__(self):
6             self.tags = {}
7         def has_tag(self, tag):
8             return tag in self.tags
9         def notify(self, tags, data):
10             for tag in tags:
11                 cb = self.tags.get(tag)
12                 if cb:
13                     cb(data)
14         def register(self, tag, callback):
15             self.tags[tag] = callback
16
17     def __init__(self):
18         self.clients = {}
19
20     def notify(self, tags, data):
21         if not isinstance(tags, list):
22             tags = [tags]
23         for client in clients:
24             client.notify(tags, data)
25
26     def register(self, client_id, tag, callback):
27         client = self.clients.get(client_id)
28         if not client:
29             client = Client()
30             self.clients[client_id] = client
31         client.register(tag, callback)
32
33 postoffice = PostOffice()
34
35