2008-01-26 02:40:09 -05:00
|
|
|
! Copyright (C) 2008 Slava Pestov.
|
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2008-02-02 17:23:04 -05:00
|
|
|
USING: io.backend kernel continuations namespaces sequences
|
2008-02-28 02:20:44 -05:00
|
|
|
assocs hashtables sorting arrays threads boxes io.timeouts ;
|
2008-02-05 19:00:24 -05:00
|
|
|
IN: io.monitors
|
2008-01-26 02:40:09 -05:00
|
|
|
|
2008-02-02 17:23:04 -05:00
|
|
|
<PRIVATE
|
|
|
|
|
|
|
|
TUPLE: monitor queue closed? ;
|
|
|
|
|
|
|
|
: check-monitor ( monitor -- )
|
|
|
|
monitor-closed? [ "Monitor closed" throw ] when ;
|
|
|
|
|
|
|
|
: (monitor) ( delegate -- monitor )
|
|
|
|
H{ } clone {
|
|
|
|
set-delegate
|
|
|
|
set-monitor-queue
|
|
|
|
} monitor construct ;
|
|
|
|
|
2008-02-12 03:19:18 -05:00
|
|
|
GENERIC: fill-queue ( monitor -- )
|
2008-02-02 17:23:04 -05:00
|
|
|
|
|
|
|
: changed-file ( changed path -- )
|
2008-02-02 19:14:26 -05:00
|
|
|
namespace [ append ] change-at ;
|
2008-02-02 17:23:04 -05:00
|
|
|
|
|
|
|
: dequeue-change ( assoc -- path changes )
|
|
|
|
delete-any prune natural-sort >array ;
|
|
|
|
|
2008-02-12 03:19:18 -05:00
|
|
|
M: monitor dispose
|
|
|
|
dup check-monitor
|
|
|
|
t over set-monitor-closed?
|
|
|
|
delegate dispose ;
|
|
|
|
|
|
|
|
! Simple monitor; used on Linux and Mac OS X. On Windows,
|
|
|
|
! monitors are full-fledged ports.
|
2008-02-28 02:20:44 -05:00
|
|
|
TUPLE: simple-monitor handle callback timeout ;
|
|
|
|
|
|
|
|
M: simple-monitor timeout simple-monitor-timeout ;
|
|
|
|
|
|
|
|
M: simple-monitor set-timeout set-simple-monitor-timeout ;
|
2008-02-12 03:19:18 -05:00
|
|
|
|
|
|
|
: <simple-monitor> ( handle -- simple-monitor )
|
2008-02-19 15:38:02 -05:00
|
|
|
f (monitor) <box> {
|
2008-02-12 03:33:06 -05:00
|
|
|
set-simple-monitor-handle
|
2008-02-12 03:19:18 -05:00
|
|
|
set-delegate
|
2008-02-19 15:38:02 -05:00
|
|
|
set-simple-monitor-callback
|
2008-02-12 03:19:18 -05:00
|
|
|
} simple-monitor construct ;
|
|
|
|
|
|
|
|
: construct-simple-monitor ( handle class -- simple-monitor )
|
|
|
|
>r <simple-monitor> r> construct-delegate ; inline
|
|
|
|
|
|
|
|
: notify-callback ( simple-monitor -- )
|
2008-02-29 20:10:30 -05:00
|
|
|
simple-monitor-callback [ resume ] if-box? ;
|
2008-02-12 03:19:18 -05:00
|
|
|
|
2008-02-28 02:20:44 -05:00
|
|
|
M: simple-monitor timed-out
|
|
|
|
notify-callback ;
|
|
|
|
|
2008-02-12 03:19:18 -05:00
|
|
|
M: simple-monitor fill-queue ( monitor -- )
|
2008-02-28 02:20:44 -05:00
|
|
|
[
|
|
|
|
[ swap simple-monitor-callback >box ]
|
|
|
|
"monitor" suspend drop
|
|
|
|
] with-timeout
|
2008-02-12 03:19:18 -05:00
|
|
|
check-monitor ;
|
|
|
|
|
|
|
|
M: simple-monitor dispose ( monitor -- )
|
|
|
|
dup delegate dispose notify-callback ;
|
|
|
|
|
2008-02-02 17:23:04 -05:00
|
|
|
PRIVATE>
|
|
|
|
|
2008-01-26 22:38:30 -05:00
|
|
|
HOOK: <monitor> io-backend ( path recursive? -- monitor )
|
2008-01-26 02:40:09 -05:00
|
|
|
|
2008-02-02 17:23:04 -05:00
|
|
|
: next-change ( monitor -- path changed )
|
|
|
|
dup check-monitor
|
|
|
|
dup monitor-queue dup assoc-empty? [
|
2008-02-04 12:50:02 -05:00
|
|
|
drop dup fill-queue next-change
|
2008-02-02 17:23:04 -05:00
|
|
|
] [ nip dequeue-change ] if ;
|
2008-01-26 22:38:30 -05:00
|
|
|
|
2008-02-01 18:13:57 -05:00
|
|
|
SYMBOL: +add-file+
|
|
|
|
SYMBOL: +remove-file+
|
|
|
|
SYMBOL: +modify-file+
|
|
|
|
SYMBOL: +rename-file+
|
2008-01-26 22:38:30 -05:00
|
|
|
|
|
|
|
: with-monitor ( path recursive? quot -- )
|
2008-01-31 01:52:06 -05:00
|
|
|
>r <monitor> r> with-disposal ; inline
|