Mercurial > code > home > repos > light9
view src/light9/observable.py @ 2376:4556eebe5d73
topdir reorgs; let pdm have its src/ dir; separate vite area from light9/
author | drewp@bigasterisk.com |
---|---|
date | Sun, 12 May 2024 19:02:10 -0700 |
parents | light9/observable.py@ccdfdc8183ad |
children |
line wrap: on
line source
import logging log = logging.getLogger('observable') class _NoNewVal: pass class Observable: """ 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: log.debug("%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)