annotate light9/observable.py @ 1536:7a85229d07a0

currentgraphstate now returns readonly (not actually snapshotted) view of the graph, not a copy Ignore-this: 7db0291740fe8dee0ad25b24571b171b
author Drew Perttula <drewp@bigasterisk.com>
date Thu, 11 May 2017 05:20:15 +0000
parents 7445bcd3349d
children 7772cc48e016
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1274
7445bcd3349d observable.py use logging not print
Drew Perttula <drewp@bigasterisk.com>
parents: 838
diff changeset
1 import logging
7445bcd3349d observable.py use logging not print
Drew Perttula <drewp@bigasterisk.com>
parents: 838
diff changeset
2 log = logging.getLogger('observable')
7445bcd3349d observable.py use logging not print
Drew Perttula <drewp@bigasterisk.com>
parents: 838
diff changeset
3
838
321fc6150ee3 subcomposer's nice currently-editing DnD box
drewp@bigasterisk.com
parents:
diff changeset
4 class _NoNewVal(object):
321fc6150ee3 subcomposer's nice currently-editing DnD box
drewp@bigasterisk.com
parents:
diff changeset
5 pass
321fc6150ee3 subcomposer's nice currently-editing DnD box
drewp@bigasterisk.com
parents:
diff changeset
6
321fc6150ee3 subcomposer's nice currently-editing DnD box
drewp@bigasterisk.com
parents:
diff changeset
7 class Observable(object):
321fc6150ee3 subcomposer's nice currently-editing DnD box
drewp@bigasterisk.com
parents:
diff changeset
8 """
321fc6150ee3 subcomposer's nice currently-editing DnD box
drewp@bigasterisk.com
parents:
diff changeset
9 like knockout's observable. Hopefully this can be replaced by a
321fc6150ee3 subcomposer's nice currently-editing DnD box
drewp@bigasterisk.com
parents:
diff changeset
10 better python one
321fc6150ee3 subcomposer's nice currently-editing DnD box
drewp@bigasterisk.com
parents:
diff changeset
11
321fc6150ee3 subcomposer's nice currently-editing DnD box
drewp@bigasterisk.com
parents:
diff changeset
12 compare with:
321fc6150ee3 subcomposer's nice currently-editing DnD box
drewp@bigasterisk.com
parents:
diff changeset
13 http://knockoutjs.com/documentation/observables.html
321fc6150ee3 subcomposer's nice currently-editing DnD box
drewp@bigasterisk.com
parents:
diff changeset
14 https://github.com/drpancake/python-observable/blob/master/observable/observable.py
321fc6150ee3 subcomposer's nice currently-editing DnD box
drewp@bigasterisk.com
parents:
diff changeset
15 """
321fc6150ee3 subcomposer's nice currently-editing DnD box
drewp@bigasterisk.com
parents:
diff changeset
16 def __init__(self, val):
321fc6150ee3 subcomposer's nice currently-editing DnD box
drewp@bigasterisk.com
parents:
diff changeset
17 self.val = val
321fc6150ee3 subcomposer's nice currently-editing DnD box
drewp@bigasterisk.com
parents:
diff changeset
18 self.subscribers = set()
321fc6150ee3 subcomposer's nice currently-editing DnD box
drewp@bigasterisk.com
parents:
diff changeset
19
321fc6150ee3 subcomposer's nice currently-editing DnD box
drewp@bigasterisk.com
parents:
diff changeset
20 def __call__(self, newVal=_NoNewVal):
321fc6150ee3 subcomposer's nice currently-editing DnD box
drewp@bigasterisk.com
parents:
diff changeset
21 if newVal is _NoNewVal:
321fc6150ee3 subcomposer's nice currently-editing DnD box
drewp@bigasterisk.com
parents:
diff changeset
22 return self.val
321fc6150ee3 subcomposer's nice currently-editing DnD box
drewp@bigasterisk.com
parents:
diff changeset
23 if newVal == self.val:
1274
7445bcd3349d observable.py use logging not print
Drew Perttula <drewp@bigasterisk.com>
parents: 838
diff changeset
24 log.debug("%r unchanged from %r", newVal, self.val)
838
321fc6150ee3 subcomposer's nice currently-editing DnD box
drewp@bigasterisk.com
parents:
diff changeset
25 return
321fc6150ee3 subcomposer's nice currently-editing DnD box
drewp@bigasterisk.com
parents:
diff changeset
26 self.val = newVal
321fc6150ee3 subcomposer's nice currently-editing DnD box
drewp@bigasterisk.com
parents:
diff changeset
27 for s in self.subscribers:
321fc6150ee3 subcomposer's nice currently-editing DnD box
drewp@bigasterisk.com
parents:
diff changeset
28 s(newVal)
321fc6150ee3 subcomposer's nice currently-editing DnD box
drewp@bigasterisk.com
parents:
diff changeset
29
321fc6150ee3 subcomposer's nice currently-editing DnD box
drewp@bigasterisk.com
parents:
diff changeset
30 def subscribe(self, cb, callNow=True):
321fc6150ee3 subcomposer's nice currently-editing DnD box
drewp@bigasterisk.com
parents:
diff changeset
31 """cb is called with new values, and also right now with the
321fc6150ee3 subcomposer's nice currently-editing DnD box
drewp@bigasterisk.com
parents:
diff changeset
32 current value unless you opt out"""
321fc6150ee3 subcomposer's nice currently-editing DnD box
drewp@bigasterisk.com
parents:
diff changeset
33 self.subscribers.add(cb)
321fc6150ee3 subcomposer's nice currently-editing DnD box
drewp@bigasterisk.com
parents:
diff changeset
34 if callNow:
321fc6150ee3 subcomposer's nice currently-editing DnD box
drewp@bigasterisk.com
parents:
diff changeset
35 cb(self.val)