2010-02-19 06:23:24 -05:00
|
|
|
! Copyright (C) 2008, 2010 Slava Pestov.
|
2008-04-11 23:36:24 -04:00
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2008-05-15 00:23:12 -04:00
|
|
|
USING: io.backend kernel continuations destructors namespaces
|
|
|
|
sequences assocs hashtables sorting arrays threads boxes
|
2009-01-27 00:18:57 -05:00
|
|
|
io.timeouts accessors concurrency.mailboxes fry
|
2011-11-02 14:23:41 -04:00
|
|
|
system vocabs combinators ;
|
2008-04-11 23:36:24 -04:00
|
|
|
IN: io.monitors
|
|
|
|
|
|
|
|
HOOK: init-monitors io-backend ( -- )
|
|
|
|
|
|
|
|
M: object init-monitors ;
|
|
|
|
|
|
|
|
HOOK: dispose-monitors io-backend ( -- )
|
|
|
|
|
|
|
|
M: object dispose-monitors ;
|
|
|
|
|
|
|
|
: with-monitors ( quot -- )
|
|
|
|
[
|
|
|
|
init-monitors
|
|
|
|
[ dispose-monitors ] [ ] cleanup
|
|
|
|
] with-scope ; inline
|
|
|
|
|
2009-08-24 03:26:13 -04:00
|
|
|
TUPLE: monitor < disposable path queue timeout ;
|
2008-04-11 23:36:24 -04:00
|
|
|
|
|
|
|
M: monitor timeout timeout>> ;
|
|
|
|
|
2010-05-05 16:52:54 -04:00
|
|
|
M: monitor set-timeout timeout<< ;
|
2008-04-11 23:36:24 -04:00
|
|
|
|
2010-02-19 06:23:24 -05:00
|
|
|
<PRIVATE
|
|
|
|
|
|
|
|
SYMBOL: monitor-disposed
|
|
|
|
|
|
|
|
PRIVATE>
|
|
|
|
|
|
|
|
M: monitor dispose*
|
|
|
|
[ monitor-disposed ] dip queue>> mailbox-put ;
|
|
|
|
|
2008-04-14 06:07:31 -04:00
|
|
|
: new-monitor ( path mailbox class -- monitor )
|
2009-08-24 03:26:13 -04:00
|
|
|
new-disposable
|
2008-04-11 23:36:24 -04:00
|
|
|
swap >>queue
|
|
|
|
swap >>path ; inline
|
|
|
|
|
2009-01-27 00:18:57 -05:00
|
|
|
TUPLE: file-change path changed monitor ;
|
|
|
|
|
2008-04-11 23:36:24 -04:00
|
|
|
: queue-change ( path changes monitor -- )
|
2010-02-19 06:23:24 -05:00
|
|
|
3dup and and [
|
|
|
|
[ check-disposed ] keep
|
|
|
|
[ file-change boa ] keep
|
|
|
|
queue>> mailbox-put
|
|
|
|
] [ 3drop ] if ;
|
2008-04-11 23:36:24 -04:00
|
|
|
|
|
|
|
HOOK: (monitor) io-backend ( path recursive? mailbox -- monitor )
|
|
|
|
|
|
|
|
: <monitor> ( path recursive? -- monitor )
|
|
|
|
<mailbox> (monitor) ;
|
|
|
|
|
2009-01-27 00:18:57 -05:00
|
|
|
: next-change ( monitor -- change )
|
2010-02-19 06:23:24 -05:00
|
|
|
[ check-disposed ]
|
|
|
|
[
|
|
|
|
[ ] [ queue>> ] [ timeout ] tri mailbox-get-timeout
|
|
|
|
dup monitor-disposed eq? [ drop already-disposed ] [ nip ] if
|
|
|
|
] bi ;
|
2008-04-11 23:36:24 -04:00
|
|
|
|
|
|
|
SYMBOL: +add-file+
|
|
|
|
SYMBOL: +remove-file+
|
|
|
|
SYMBOL: +modify-file+
|
|
|
|
SYMBOL: +rename-file-old+
|
|
|
|
SYMBOL: +rename-file-new+
|
|
|
|
SYMBOL: +rename-file+
|
|
|
|
|
|
|
|
: with-monitor ( path recursive? quot -- )
|
2008-12-02 04:10:13 -05:00
|
|
|
[ <monitor> ] dip with-disposal ; inline
|
2008-07-02 22:52:28 -04:00
|
|
|
|
2009-01-27 00:18:57 -05:00
|
|
|
: run-monitor ( path recursive? quot -- )
|
|
|
|
'[ [ @ t ] loop ] with-monitor ; inline
|
|
|
|
|
2008-07-02 22:52:28 -04:00
|
|
|
{
|
2008-12-14 21:03:00 -05:00
|
|
|
{ [ os macosx? ] [ "io.monitors.macosx" require ] }
|
|
|
|
{ [ os linux? ] [ "io.monitors.linux" require ] }
|
2010-09-19 15:02:32 -04:00
|
|
|
{ [ os windows? ] [ "io.monitors.windows" require ] }
|
2008-07-02 22:52:28 -04:00
|
|
|
} cond
|