Friday, March 10, 2017

ELENA 3.0 : event handler

In this short article I will show how to implement event handler using a new attribute : event

To declare a new event handler we should declare the field with event attribute:

event(func1,onStart) theStart.

The first parameter indicates the action type (func1 is an action with a single generic parameter), the second one - the property name to be used for attaching the handler.

To attach the handler we have to call the property with our action:

    object onStart &func1:x [ /*...*/ ].

The simple example code is below:

#import extensions.

class MyClass
{
    event(func1,onStart) theStart.

    event(func1,onEnd) theEnd.
        
    start : o
    [
        ($nil != theStart)
            ? [ theStart eval:o ].        
    ]
        
    stop : o
    [
        ($nil != theEnd)
            ? [ theEnd eval:o ].        
    ]
}
      
symbol program =
[
    var o := MyClass new.
    
    o onStart &func1:x [ console writeLine:"a:":x. ].
    o onStart &func1:x [ console writeLine:"b:":x. ].

    o start:1.
].

No comments:

Post a Comment