rev: release-0.15 scopes/doc/module-FunctionChain.rst -rw-r--r-- 2.0 KiB View raw Log this file
e53de6d7cb89 — Leonard Ritter * win32 build fix 1 year, 5 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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:

..  code-block:: none

    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

.. type:: FunctionChain

   A plain type of storage type `_type<*>`.

   .. spice:: (__call ...)
   .. inline:: (__repr self)
   .. spice:: (__typecall ...)
   .. inline:: (append 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.
   .. inline:: (clear self)
      
      Clear the function chain. When the function chain is applied next,
      no functions will be called.
   .. inline:: (on self)
      
      Returns a decorator that appends the provided function to the
      function chain.
   .. inline:: (prepend 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.