6.7. FunctionChain

A function chain implements a compile-time observer pattern that allows a module to call back into dependent modules in a decoupled way.

See following example:

using import FunctionChain

# declare new function chain
fnchain activate

fn handler (x)
    print "handler activated with argument" x

'append activate handler

'append activate
    fn (x)
        print "last handler activated with argument" x

'prepend activate
    fn (x)
        print "first handler activated with argument" x

activate 1
activate 2
'clear activate
'append activate handler
activate 3

Running this program will output:

first handler activated with argument 1
handler activated with argument 1
last handler activated with argument 1
first handler activated with argument 2
handler activated with argument 2
last handler activated with argument 2
handler activated with argument 3
typeFunctionChain

A plain type of storage type _type.

spice__call ( ... )
inline__repr ( self )
spice__typecall ( ... )
inlineappend ( self f )

Append function f to function chain. When the function chain is called, f will be called last. The return value of f will be ignored.

inlineclear ( self )

Clear the function chain. When the function chain is applied next, no functions will be called.

inlineon ( self )

Returns a decorator that appends the provided function to the function chain.

inlineprepend ( self f )

Prepend function f to function chain. When the function chain is called, f will be called first. The return value of f will be ignored.

sugar(decorate-fnchain ... )
sugar(fnchain name )

Binds a new unique and empty function chain to identifier name. The function chain’s typename is going to incorporate the name of the module in which it was declared.