renamed hooks to triggers

db4
Alex Chapman 2008-03-12 13:54:01 +11:00
parent 630e9d2678
commit 99d2b29b7e
6 changed files with 44 additions and 42 deletions

View File

@ -1,14 +0,0 @@
USING: hooks kernel tools.test ;
IN: hooks.tests
SYMBOL: test-hook
test-hook reset-hook
: add-test-hook test-hook add-hook ;
[ ] [ test-hook call-hook ] unit-test
[ "op called" ] [ "op" [ "op called" ] add-test-hook test-hook call-hook ] unit-test
[ "first called" "second called" ] [
test-hook reset-hook
"second op" [ "second called" ] add-test-hook
"first op" [ "first called" ] add-test-hook
test-hook call-hook
] unit-test

View File

@ -1,28 +0,0 @@
! Copyright (C) 2008 Alex Chapman
! See http://factorcode.org/license.txt for BSD license.
USING: assocs digraphs kernel namespaces sequences ;
IN: hooks
: hooks ( -- hooks )
\ hooks global [ drop H{ } clone ] cache ;
: hook-graph ( hook -- graph )
hooks [ drop <digraph> ] cache ;
: reset-hook ( hook -- )
<digraph> swap hooks set-at ;
: add-hook ( key quot hook -- )
#! hook should be a symbol. Note that symbols with the same name but
#! different vocab are not equal
hook-graph add-vertex ;
: before ( key1 key2 hook -- )
hook-graph add-edge ;
: after ( key1 key2 hook -- )
swapd before ;
: call-hook ( hook -- )
hook-graph topological-sorted-values [ call ] each ;

View File

@ -0,0 +1 @@
Alex Chapman

View File

@ -0,0 +1 @@
triggers allow you to register code to be 'triggered'

View File

@ -0,0 +1,14 @@
USING: triggers kernel tools.test ;
IN: triggers.tests
SYMBOL: test-trigger
test-trigger reset-trigger
: add-test-trigger test-trigger add-trigger ;
[ ] [ test-trigger call-trigger ] unit-test
[ "op called" ] [ "op" [ "op called" ] add-test-trigger test-trigger call-trigger ] unit-test
[ "first called" "second called" ] [
test-trigger reset-trigger
"second op" [ "second called" ] add-test-trigger
"first op" [ "first called" ] add-test-trigger
test-trigger call-trigger
] unit-test

View File

@ -0,0 +1,28 @@
! Copyright (C) 2008 Alex Chapman
! See http://factorcode.org/license.txt for BSD license.
USING: assocs digraphs kernel namespaces sequences ;
IN: triggers
: triggers ( -- triggers )
\ triggers global [ drop H{ } clone ] cache ;
: trigger-graph ( trigger -- graph )
triggers [ drop <digraph> ] cache ;
: reset-trigger ( trigger -- )
<digraph> swap triggers set-at ;
: add-trigger ( key quot trigger -- )
#! trigger should be a symbol. Note that symbols with the same name but
#! different vocab are not equal
trigger-graph add-vertex ;
: before ( key1 key2 trigger -- )
trigger-graph add-edge ;
: after ( key1 key2 trigger -- )
swapd before ;
: call-trigger ( trigger -- )
trigger-graph topological-sorted-values [ call ] each ;