EventHandler

class visigoth.common.event_handler.EventHandler(js, config={})

Create an event handler based on a javascript function

Parameters:
  • js (str) – javascript function of the form function(channel,event,config,sendfn) { /* body */ }
  • config (dict) – configuration passed into each invocation (must be JSON serialisable)

Notes

The function is invoked with the following parameters:

channel is the name of the channel upon which the event is recieved obj is the event value config is the configuration of the function (this can be mutated between invocations) sendfn is a function for sending events which takes parameters obj (the event value) and channel (the name of the channel to output the event to)

Example

(Link to open example in new tab):

Source Code (Link to Github):

# -*- coding: utf-8 -*-

from visigoth.diagram import Diagram
from visigoth.common import Button
from visigoth.common import EventHandler

jscode1 = """
function(channel,obj,config,sendfn)
{
    alert("Code 1: Got event channel="+channel+",value="+obj);
}
"""

jscode2 = """
function(channel,obj,config,sendfn)
{
    alert("Code 2: Got event channel="+channel+",value="+obj);
}
"""

d = Diagram()

b1 = Button("Click Me!")
b2 = Button("Or Click Me!")

ev1 = EventHandler(jscode1,{})
ev2 = EventHandler(jscode2,{})

d.add(b1)
d.add(b2)
d.add(ev1)
d.add(ev2)

d.connect(b1,"click",ev1,"click")
d.connect(b2,"click",ev2,"click")

html = d.draw(format="html")
f = open("example.html", "w")
f.write(html)
f.close()