Changeset 32610 in project


Ignore:
Timestamp:
07/23/15 15:00:16 (5 years ago)
Author:
arthurmaciel
Message:
 
File:
1 edited

Legend:

Unmodified
Added
Removed
  • wiki/eggref/4/hooks

    r32609 r32610  
    1 [[tags: egg]]
    2 
    3 == hooks
    4 
    5 [[toc:]]
    6 
    7 === Description
    8 
    9 Hooks is a CHICKEN Scheme egg that provides a simple [[https://en.wikipedia.org/wiki/Hooking|hooking]] API.
    10 
    11 === Author
    12 
    13 [[/users/arthur-maciel|Arthur Maciel]]
    14 
    15 === Requirements
    16 
    17 None.
    18 
    19 === Documentation
    20 
    21 <procedure>(make-hook)</procedure>
    22 Creates a {{hook}} record.
    23 
    24 <procedure>(add-hook! hook proc)</procedure>
    25 Adds {{proc}} to {{hook}}.
    26 
    27 <procedure>(remove-hook! hook proc-name)</procedure>
    28 Removes the procedure with {{proc-name}} from {{hook}}.
    29 
    30 '''Warning''': global procedures (i.e. defined with {{define}} globally or with {{set!}} anywhere) can be removed passing their names unquoted to {{remove-hook}}, but local closures (i.e. defined with {{let}} or locally with {{define}}) should have their names quoted (e.g. {{(remove-hook! draw-hook 'clear-screen)}}.
    31 
    32 '''Rule of thumb''': always quote the procedure name when trying to remove it. But note this will delete '''all''' procedures with that name (this shouldn't be a problem, because it isn't wise to add procedures with same name to a hook anyway).
    33 
    34 <procedure>(reset-hook! hook)</procedure>
    35 Resets (clears) {{hook}}.
    36 
    37 <procedure>(run-hook! hook . args)</procedure>
    38 Runs all procedures associated with {{hook}} passing {{args}} as arguments.
    39 
    40 <procedure>(hook->list hook)</procedure>
    41 Presents a list of procedures registered in {{hook}}.
    42 
    43 ==== Examples
    44 <enscript highlight="scheme">
    45 (use hooks)
    46 
    47 (define debug-hook (make-hook))
    48 
    49 (define (print-to-console msg)
    50   (print msg))
    51 
    52 (define (write-to-log msg)
    53   (with-output-to-file "log"
    54                        (lambda() (write msg))))
    55 
    56 (add-hook! debug-hook print-to-console)
    57 (add-hook! debug-hook write-to-log)
    58 
    59 (hook->list debug-hook)
    60 ; => (#<procedure (write-to-log msg)> #<procedure (print-to-console msg)>)
    61 
    62 ;; In despatching part of the code...
    63 (run-hook! debug-hook "DEBUG MESSAGE")
    64 ; => should print to screen and write to "log" file the message "DEBUG MESSAGE".
    65 
    66 (remove-hook! debug-hook 'print-to-console)
    67 
    68 (hook->list debug-hook)
    69 ; => ()
    70 
    71 </enscript>
    72 
    73 === Changelog
    74 
    75 * 0.1 Initial version
    76 
    77 === License
    78 
    79   Copyright (c) 2015, Arthur Maciel
    80   All rights reserved.
    81  
    82   Redistribution and use in source and binary forms, with or without
    83   modification, are permitted provided that the following conditions are
    84   met:
    85  
    86   Redistributions of source code must retain the above copyright
    87   notice, this list of conditions and the following disclaimer.
    88  
    89   Redistributions in binary form must reproduce the above copyright
    90   notice, this list of conditions and the following disclaimer in the
    91   documentation and/or other materials provided with the distribution.
    92  
    93   Neither the name of the author nor the names of its contributors may
    94   be used to endorse or promote products derived from this software
    95   without specific prior written permission.
    96  
    97   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    98   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    99   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
    100   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
    101   COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
    102   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    103   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
    104   SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    105   HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
    106   STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    107   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
    108   OF THE POSSIBILITY OF SUCH DAMAGE.
Note: See TracChangeset for help on using the changeset viewer.