factor/unmaintained/cont-responder/callbacks.factor

123 lines
3.8 KiB
Factor
Raw Normal View History

2008-02-29 01:57:38 -05:00
! Copyright (C) 2004 Chris Double.
! Copyright (C) 2006, 2008 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
2008-05-28 19:17:58 -04:00
USING: http http.server io kernel math namespaces
2008-03-20 16:30:59 -04:00
continuations calendar sequences assocs hashtables
2008-06-02 16:00:03 -04:00
accessors arrays alarms quotations combinators fry
http.server.redirection furnace assocs.lib urls ;
IN: furnace.callbacks
2008-02-29 01:57:38 -05:00
SYMBOL: responder
TUPLE: callback-responder responder callbacks ;
: <callback-responder> ( responder -- responder' )
H{ } clone callback-responder boa ;
2008-02-29 01:57:38 -05:00
TUPLE: callback cont quot expires alarm responder ;
: timeout 20 minutes ;
: timeout-callback ( callback -- )
2008-03-11 04:39:09 -04:00
[ alarm>> cancel-alarm ]
[ dup responder>> callbacks>> delete-at ]
bi ;
2008-02-29 01:57:38 -05:00
: touch-callback ( callback -- )
dup expires>> [
dup alarm>> [ cancel-alarm ] when*
2008-03-11 04:39:09 -04:00
dup '[ , timeout-callback ] timeout later >>alarm
2008-02-29 01:57:38 -05:00
] when drop ;
: <callback> ( cont quot expires? -- callback )
f callback-responder get callback boa
2008-03-11 04:39:09 -04:00
dup touch-callback ;
2008-02-29 01:57:38 -05:00
2008-03-11 04:39:09 -04:00
: invoke-callback ( callback -- response )
[ touch-callback ]
[ quot>> request get exit-continuation get 3array ]
[ cont>> continue-with ]
tri ;
2008-02-29 01:57:38 -05:00
: register-callback ( cont quot expires? -- id )
2008-03-11 04:39:09 -04:00
<callback> callback-responder get callbacks>> set-at-unique ;
2008-02-29 01:57:38 -05:00
2008-06-02 16:00:03 -04:00
: forward-to-url ( url -- * )
2008-02-29 01:57:38 -05:00
#! When executed inside a 'show' call, this will force a
#! HTTP 302 to occur to instruct the browser to forward to
#! the request URL.
2008-03-11 04:39:09 -04:00
<temporary-redirect> exit-with ;
2008-02-29 01:57:38 -05:00
: cont-id "factorcontid" ;
: forward-to-id ( id -- * )
#! When executed inside a 'show' call, this will force a
#! HTTP 302 to occur to instruct the browser to forward to
#! the request URL.
2008-06-02 16:00:03 -04:00
<url>
swap cont-id set-query-param forward-to-url ;
2008-02-29 01:57:38 -05:00
: restore-request ( pair -- )
2008-03-11 04:39:09 -04:00
first3 exit-continuation set request set call ;
2008-02-29 01:57:38 -05:00
SYMBOL: post-refresh-get?
: redirect-to-here ( -- )
#! Force a redirect to the client browser so that the browser
#! goes to the current point in the code. This forces an URL
#! change on the browser so that refreshing that URL will
#! immediately run from this code point. This prevents the
#! "this request will issue a POST" warning from the browser
#! and prevents re-running the previous POST logic. This is
#! known as the 'post-refresh-get' pattern.
post-refresh-get? get [
[
[ ] t register-callback forward-to-id
] callcc1 restore-request
] [
post-refresh-get? on
] if ;
SYMBOL: current-show
: store-current-show ( -- )
#! Store the current continuation in the variable 'current-show'
#! so it can be returned to later by 'quot-id'. Note that it
#! recalls itself when the continuation is called to ensure that
#! it resets its value back to the most recent show call.
[ current-show set f ] callcc1
[ restore-request store-current-show ] when* ;
: show-final ( quot -- * )
2008-05-26 01:47:27 -04:00
[ redirect-to-here store-current-show ] dip
2008-03-11 04:39:09 -04:00
call exit-with ; inline
2008-02-29 01:57:38 -05:00
2008-03-11 04:39:09 -04:00
: resuming-callback ( responder request -- id )
2008-06-02 16:00:03 -04:00
url>> cont-id query-param swap callbacks>> at ;
2008-03-11 04:39:09 -04:00
M: callback-responder call-responder* ( path responder -- response )
2008-03-17 05:31:13 -04:00
'[
, ,
2008-03-11 04:39:09 -04:00
2008-03-17 05:31:13 -04:00
[ callback-responder set ]
[ request get resuming-callback ] bi
[
invoke-callback
] [
callback-responder get responder>> call-responder
] ?if
] with-exit-continuation ;
2008-02-29 01:57:38 -05:00
: show-page ( quot -- )
2008-05-26 01:47:27 -04:00
[ redirect-to-here store-current-show ] dip
2008-02-29 01:57:38 -05:00
[
2008-03-11 04:39:09 -04:00
[ ] t register-callback swap call exit-with
2008-03-03 03:19:36 -05:00
] callcc1 restore-request ; inline
2008-02-29 01:57:38 -05:00
: quot-id ( quot -- id )
current-show get swap t register-callback ;
: quot-url ( quot -- url )
2008-03-11 04:39:09 -04:00
quot-id f swap cont-id associate derive-url ;