61 lines
1.6 KiB
Python
61 lines
1.6 KiB
Python
from update import update
|
|
from browser import window
|
|
|
|
|
|
class Message:
|
|
def __init__(self, *args):
|
|
self._args = args
|
|
self.error = None
|
|
|
|
def update_model(self, model):
|
|
raise NotImplementedError("This message does not implement update_model method")
|
|
|
|
def on_event(self, event):
|
|
pass
|
|
|
|
def get_arg(self, n, argtype=None):
|
|
if n > len(self._args):
|
|
window.console.log("not enough arguments: {} <= {}".format(n, len(self._args)))
|
|
|
|
result = self._args[n]
|
|
if argtype is not None:
|
|
if not type(result) is argtype:
|
|
window.console.trace("Error type check!")
|
|
return None
|
|
|
|
return result
|
|
|
|
def data_change(self):
|
|
return True
|
|
|
|
def entry_redraw(self):
|
|
return self.data_change()
|
|
|
|
def add_arg(self, arg):
|
|
self._args.append(arg)
|
|
|
|
def reset(self):
|
|
return True
|
|
|
|
def clear_args(self):
|
|
self._args = []
|
|
|
|
def no_action(self):
|
|
return False
|
|
|
|
|
|
def msg(message_class, *params):
|
|
def callback(event):
|
|
message = message_class(*params)
|
|
if not issubclass(type(message), Message):
|
|
window.console.log("Not scheduling a Message type, this will not work!")
|
|
return
|
|
|
|
message.on_event(event)
|
|
update.update_model(message)
|
|
return callback
|
|
|
|
|
|
def delayed_msg(message_class, event, timeout_ms, *params):
|
|
window.setTimeout(lambda: msg(message_class, *params)(event), timeout_ms)
|