lexonomy_custom_editor/src/message/message.py

61 lines
1.6 KiB
Python
Raw Normal View History

2019-11-11 22:04:45 +00:00
from update import update
from browser import window
2019-11-11 22:04:45 +00:00
2019-11-05 21:18:20 +00:00
class Message:
def __init__(self, *args):
self._args = args
self.error = None
2019-11-05 21:18:20 +00:00
def update_model(self, model):
2019-11-11 22:04:45 +00:00
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)
2019-11-11 22:04:45 +00:00
def reset(self):
return True
2020-01-05 08:53:16 +00:00
def clear_args(self):
self._args = []
def no_action(self):
return False
2019-11-11 22:04:45 +00:00
def msg(message_class, *params):
2019-11-11 22:04:45 +00:00
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)
2019-11-11 22:04:45 +00:00
return callback
def delayed_msg(message_class, event, timeout_ms, *params):
window.setTimeout(lambda: msg(message_class, *params)(event), timeout_ms)