Site Tools


event_driven_programming

Event Driven Programming

Event Driven Programming is a philosophy where a program commits itself to running forever, waiting for things to happen, and then handling them. An “event” then is defined as “something which has to be handled”.

Things happen

But let us start with the idea of “things that happen”. Most of the time, a program like EPIC sits quietly, sleeping. EPIC tells the operating system “wake me up when any of these things occur”. These are the kinds of things EPIC wakes up for

  1. You press a key (user input)
  2. There is network activity (the server sends us data)
  3. A timeout expires (periodic tasks)

Event Loops handle those things

Every time EPIC is awaken from sleep, it is because an Event has occurred. EPIC is asleep at all times it is not handling an Event. This process of going to sleep, being woken up, handling an event, and then going back to sleep, is called an “event loop” or a “looper”. EPIC's core operation is as an event looper.

Events are how you explain what thing just happened

The simplest thing that has to be “handled” are messages that come from people on irc. If someone sends you a msg, that message comes in from the IRC server. EPIC determines that you have receivced a new msg and needs to display it on your screen. Therefore, displaying a msg to your screen is an example of an “event”. Inside EPIC, this is called a “MSG” event.

You can control or extend event handling

EPIC will ordinarily display a MSG event to your screen. But you may not like how EPIC formats the display of the event, and you want to do it your own way.

The fundamental operating principle of EPIC is that

  1. EPIC determines what kind of Event occurred
  2. It asks you if _you_ want to handle it
  3. If you don't handle it, there is a hardcoded fallback “default behavior”

For most events, the default behavior is fine, and you don't want to change it. It's for those events that you do want to change the default behavior, you need a way to provide your intentions to EPIC.

Event Handlers are called ON hooks

The on command is how you register an event handler with EPIC. You must know the “type” of the event, you must know the “pattern” of the event, and you must provide code to be run. A simple example of overriding the MSG event default behavior:

  on ^msg * {echo msg from $0: $1-}

Unpacking an ON

tbd

What happens when an Event is Handled

tbd

What happens when your callback is called

tbd

What happens after your callback is finished

tbd

event_driven_programming.txt · Last modified: 2021/07/05 18:01 by 127.0.0.1