]> gitweb.factorcode.org Git - factor.git/blob - basis/io/monitors/monitors.factor
Disposables are now registered in a global disposables set. To take advantage of...
[factor.git] / basis / io / monitors / monitors.factor
1 ! Copyright (C) 2008, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: io.backend kernel continuations destructors namespaces
4 sequences assocs hashtables sorting arrays threads boxes
5 io.timeouts accessors concurrency.mailboxes fry
6 system vocabs.loader combinators ;
7 IN: io.monitors
8
9 HOOK: init-monitors io-backend ( -- )
10
11 M: object init-monitors ;
12
13 HOOK: dispose-monitors io-backend ( -- )
14
15 M: object dispose-monitors ;
16
17 : with-monitors ( quot -- )
18     [
19         init-monitors
20         [ dispose-monitors ] [ ] cleanup
21     ] with-scope ; inline
22
23 TUPLE: monitor < disposable path queue timeout ;
24
25 M: monitor hashcode* path>> hashcode* ;
26
27 M: monitor timeout timeout>> ;
28
29 M: monitor set-timeout (>>timeout) ;
30
31 : new-monitor ( path mailbox class -- monitor )
32     new-disposable
33         swap >>queue
34         swap >>path ; inline
35
36 TUPLE: file-change path changed monitor ;
37
38 : queue-change ( path changes monitor -- )
39     3dup and and
40     [ [ file-change boa ] keep queue>> mailbox-put ] [ 3drop ] if ;
41
42 HOOK: (monitor) io-backend ( path recursive? mailbox -- monitor )
43
44 : <monitor> ( path recursive? -- monitor )
45     <mailbox> (monitor) ;
46
47 : next-change ( monitor -- change )
48     [ queue>> ] [ timeout ] bi mailbox-get-timeout ;
49
50 SYMBOL: +add-file+
51 SYMBOL: +remove-file+
52 SYMBOL: +modify-file+
53 SYMBOL: +rename-file-old+
54 SYMBOL: +rename-file-new+
55 SYMBOL: +rename-file+
56
57 : with-monitor ( path recursive? quot -- )
58     [ <monitor> ] dip with-disposal ; inline
59
60 : run-monitor ( path recursive? quot -- )
61     '[ [ @ t ] loop ] with-monitor ; inline
62
63 {
64     { [ os macosx? ] [ "io.monitors.macosx" require ] }
65     { [ os linux? ] [ "io.monitors.linux" require ] }
66     { [ os winnt? ] [ "io.monitors.windows.nt" require ] }
67     { [ os bsd? ] [ ] }
68 } cond