def test_conditional_event(self): mgr = EventManager() self.handled = False def handler(event): self.handled = True binding = mgr.bind(handler, 'basic', condition=1) self.assertEqual( binding.__class__, Binding, 'EventManager().bind() returned something other than an instance of Binding' ) mgr.trigger(Event('basic')) self.assertFalse( self.handled, 'Conditional event handler called for the wrong event') mgr.trigger(Event('basic', [('condition', 0)])) self.assertFalse( self.handled, 'Conditional event handler called for the wrong event') mgr.trigger(Event('basic', [('condition', 1)])) self.assertTrue(self.handled, 'Conditional event handler not called')
class main: def __init__(self): self.events = EventManager() self.load() self.events.trigger(Event('reload')) def load(self): self.reactors = [] self.reactors.append(example(self.events)) self.reactors.append(example2(self.events)) self.events.bind('main app', self.reload, 'reload') self.events.trigger(Event('ready')) def reload(self, event=None, *args): self.events.trigger(Event('reloading')) self.events.clear_bindings() self.reactors = [] self.load() self.events.trigger(Event('reloaded'))
def test_basic_event(self): mgr = EventManager() self.handled = False def handler(event): self.handled = True binding = mgr.bind(handler, 'basic') self.assertEqual(binding.__class__, Binding, 'EventManager().bind() returned something other than an instance of Binding') mgr.trigger(Event('something')) self.assertFalse(self.handled, 'Basic event handler called for the wrong event') mgr.trigger(Event('basic')) self.assertTrue(self.handled, 'Basic event handler not called')
from reflex.data import Event from reflex.control import EventManager events = EventManager() def handler(event, *args): print("Hello, world!") events.bind(handler, 'example') events.trigger(Event('example')) # prints "Hello, world!"