Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
46 lines (36 sloc) 1.28 KB
from shootblues.common import log, getChannel
import json
messageSubscribers = {}
AllMessages = "*"
def send(name, **extraData):
channel = getChannel("messages")
if not channel:
data = dict(extraData)
data["__name__"] = name
def subscribe(handler, name=AllMessages):
global messageSubscribers
subscriberList = messageSubscribers.setdefault(name, [])
if handler not in subscriberList:
def unsubscribe(handler, name=AllMessages):
global messageSubscribers
subscriberList = messageSubscribers.get(name, [])
if handler in subscriberList:
def callHandler(fn, source, name, data):
fn(source, name, data)
except Exception, e:
log("Error in message handler: %r", e)
def notifyNewMessage(source, data):
global messageSubscribers
messageName = data["__name__"]
del data["__name__"]
handlers = messageSubscribers.get(AllMessages, [])
for handler in handlers:
callHandler(handler, source, messageName, data)
handlers = messageSubscribers.get(messageName, [])
for handler in handlers:
callHandler(handler, source, messageName, data)
Jump to Line
Something went wrong with that request. Please try again.