Event Driven Features

Using this to petition Anvil for new and glorious Event Driven Features for a decoupling of logic that will be swift, and savage. People already using these concepts don’t need to be sold on how useful they are, and hopefully I can lay out a case to making these features Anvil native.

This will enhance Components (Custom + Standard) as well as Server Code.

Vision For The UI:
In the App Browser a new section called Events is now visible. It is similar in functionality to the Client Code section, in that, it allows for hierarchical event structures to be created.

All events can have 0 or N number of paramater defined as well.

When a Component is selected there is now a new section in the Toolbox called Events. This has access to the Events listed in the App Browser. There is also a way to adding an arbitrary number of mappings between the existing events and a collection of functions that will fire on that event. The scope of available functions will be limited to the Component scope in this context. Basically a 1 to many relationship of event to functions.

Vision For Programmatic Access:
A new anvil.events module will be added with functionality, demonstrating using pseudocode:

0 Param, No Return Value, Example:
Decorator with 0 paramater events. Signature of the events match the signature of the function, else an error is indicated in the UI

@anvil.server.callable
@anvil.events.fire_on_events(['Event A', 'Event B'])
# The event list is retrieved from the new Events section, and will execute this function when the appropriate events are fired
def print_something():
  print('Im printing something')

Calling events Event A and Event B from Form code

from ._anvil_designer import TOSTemplate
from anvil import *
from anvil.events import events

class TOS(TOSTemplate):
  def __init__(self, **properties):
    events.fire_event(['Event A'])
    # print_something() function is executed on the server
    events.fire_event(['Event B'])
    # print_something() function is executed on the server again
    self.init_components(**properties)

The @anvil.events.fire_on_events(['Event A', 'Event B']) decorator can also be put on functions within forms or custom components.

1 Param, No Return Value, Example:
Decorator with 1 paramater events, firing multiple events at the same time. Signature of the events match the signature of the function, else an error is indicated in the UI

@anvil.server.callable
@anvil.events.fire_on_events(['Event C', 'Event D'])
# The event list is retrieved from the new Events section, and will execute this function when the appropriate events are fired
def print_some_param(my_param):
  print(my_param)

Calling events Event C and Event D from Form code

from ._anvil_designer import TOSTemplate
from anvil import *
from anvil.events import events

class TOS(TOSTemplate):
  def __init__(self, **properties):
    events.fire_events([  ['Event C', 'hello world']],    ['Event D', 'Hello person'] ])
    # print_some_param() function is executed on the server twice
    self.init_components(**properties)

1 Param, With Return Values, 1 Event Listener Doesn’t Respond In Timeout Period Example:
Decorator with 1 paramater events, firing multiple events at the same time. Signature of the events match the signature of the function, else an error is indicated in the UI

@anvil.server.callable
@anvil.events.fire_on_events(['Special Promotion'])
# The event list is retrieved from the new Events section, and will execute this function when the appropriate events are fired
def send_sms_to_customer(message, customer):
# do stuff
return response_code


@anvil.server.callable
@anvil.events.fire_on_events(['Special Promotion'])
# The event list is retrieved from the new Events section, and will execute this function when the appropriate events are fired
def send_email_to_customer(message, customer):
# do stuff, but it takes to long to respond. All late responses will be sent to a late responses log that is viewable later by the programmer.
  return response_code

Calling event Special Promotion from Form code

from ._anvil_designer import TOSTemplate
from anvil import *
from anvil.events import events

class TOS(TOSTemplate):
  def __init__(self, **properties):
    timeout = #define timespan that if exceeded will return only available results
    results = events.fire_events(timeout_span = timeout, [  ['Special Promotion', 'You get a deal!', 'customer 123']], )
    for result in results:
        print(result) # this will return 1 response code, that is the one from the send_sms_to_customer function 
    self.init_components(**properties)