Mercurial > code > home > repos > light9
view light9/observable.py @ 1178:a296d842d0da
better cmdline option error
Ignore-this: a9be79c6c6fd69de5d5798f0cb628f24
author | Drew Perttula <drewp@bigasterisk.com> |
---|---|
date | Sun, 15 Jun 2014 07:17:48 +0000 |
parents | 321fc6150ee3 |
children | 7445bcd3349d |
line wrap: on
line source
class _NoNewVal(object): pass class Observable(object): """ like knockout's observable. Hopefully this can be replaced by a better python one compare with: http://knockoutjs.com/documentation/observables.html https://github.com/drpancake/python-observable/blob/master/observable/observable.py """ def __init__(self, val): self.val = val self.subscribers = set() def __call__(self, newVal=_NoNewVal): if newVal is _NoNewVal: return self.val if newVal == self.val: print "%r unchanged from %r" % (newVal, self.val) return self.val = newVal for s in self.subscribers: s(newVal) def subscribe(self, cb, callNow=True): """cb is called with new values, and also right now with the current value unless you opt out""" self.subscribers.add(cb) if callNow: cb(self.val)