|
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- import copy
-
- class Controller:
- def __init__(self, c):
- self.callbacks = []
- self.context = c
- self.functions = []
- self.isProcessingQueue = False
- self.queue = []
-
- def executeFunctions(self):
- c = self.queue.pop(0)
- for f in self.functions:
- ctx = f(c)
- if ctx.recentField != "none":
- self.queue.append(ctx)
- self.context = c
- self.reportContext()
-
- def processQueue(self):
- # Decline recursion.
- if self.isProcessingQueue:
- return
- self.isProcessingQueue = True
- while len(self.queue) > 0:
- self.executeFunctions()
- self.isProcessingQueue = False
-
- def registerCallback(self, cb):
- self.callbacks.append(cb)
-
- def registerFieldCallback(self, fieldName, cb):
- def execCB(c):
- if c.recentField == fieldName:
- cb(c)
- self.callbacks.append(execCB)
-
- def registerFunction(self, f):
- self.functions.append(f)
-
- def reportContext(self):
- for cb in self.callbacks:
- cb(self.context)
-
- def set(self, fieldName, value):
- c = copy.deepcopy(self.context)
- setattr(c, fieldName, value)
- c.recentField = fieldName
- self.queue.append(c)
- self.processQueue()
|