-! Copyright (C) 2008, 2009 Slava Pestov.\r
-! See http://factorcode.org/license.txt for BSD license.\r
-USING: accessors assocs command-line concurrency.messaging\r
-continuations init io.backend io.files io.monitors io.pathnames\r
-kernel namespaces sequences sets splitting threads\r
-tr vocabs vocabs.loader vocabs.refresh vocabs.cache ;\r
-IN: vocabs.refresh.monitor\r
-\r
-TR: convert-separators "/\\" ".." ;\r
-\r
-: vocab-dir>vocab-name ( path -- vocab )\r
- trim-head-separators\r
- trim-tail-separators\r
- convert-separators ;\r
-\r
-: path>vocab-name ( path -- vocab )\r
- dup ".factor" tail? [ parent-directory ] when ;\r
-\r
-: chop-vocab-root ( path -- path' )\r
- "resource:" prepend-path normalize-path\r
- dup vocab-roots get\r
- [ normalize-path ] map\r
- [ head? ] with find nip\r
- ?head drop ;\r
-\r
-: path>vocab ( path -- vocab )\r
- chop-vocab-root path>vocab-name vocab-dir>vocab-name ;\r
-\r
-: monitor-loop ( -- )\r
- #! On OS X, monitors give us the full path, so we chop it\r
- #! off if its there.\r
- receive path>> path>vocab changed-vocab\r
- reset-cache\r
- monitor-loop ;\r
-\r
-: add-monitor-for-path ( path -- )\r
- dup exists? [ t my-mailbox (monitor) ] when drop ;\r
-\r
-: monitor-thread ( -- )\r
- [\r
- [\r
- vocab-roots get [ add-monitor-for-path ] each\r
-\r
- H{ } clone changed-vocabs set-global\r
- vocabs [ changed-vocab ] each\r
-\r
- monitor-loop\r
- ] with-monitors\r
- ] ignore-errors ;\r
-\r
-: start-monitor-thread ( -- )\r
- #! Silently ignore errors during monitor creation since\r
- #! monitors are not supported on all platforms.\r
- [ monitor-thread ] "Vocabulary monitor" spawn drop ;\r
-\r
-[\r
- "-no-monitors" (command-line) member?\r
- [ start-monitor-thread ] unless\r
-] "vocabs.refresh.monitor" add-startup-hook\r
+! Copyright (C) 2008, 2010 Slava Pestov.
+! See http://factorcode.org/license.txt for BSD license.
+USING: accessors assocs command-line concurrency.messaging
+continuations init io.backend io.files io.monitors io.pathnames
+kernel namespaces sequences sets splitting threads fry
+tr vocabs vocabs.loader vocabs.refresh vocabs.cache ;
+IN: vocabs.refresh.monitor
+
+TR: convert-separators "/\\" ".." ;
+
+: vocab-dir>vocab-name ( path -- vocab )
+ trim-head-separators
+ trim-tail-separators
+ convert-separators ;
+
+: path>vocab-name ( path -- vocab )
+ dup ".factor" tail? [ parent-directory ] when ;
+
+: chop-vocab-root ( path -- path' )
+ "resource:" prepend-path normalize-path
+ dup vocab-roots get
+ [ normalize-path ] map
+ [ head? ] with find nip
+ ?head drop ;
+
+: path>vocab ( path -- vocab )
+ chop-vocab-root path>vocab-name vocab-dir>vocab-name ;
+
+: monitor-loop ( monitor -- )
+ #! On OS X, monitors give us the full path, so we chop it
+ #! off if its there.
+ [ next-change path>> path>vocab changed-vocab reset-cache ]
+ [ monitor-loop ]
+ bi ;
+
+: (start-vocab-monitor) ( vocab-root -- )
+ dup exists?
+ [ [ t <monitor> monitor-loop ] with-monitors ] [ drop ] if ;
+
+: start-vocab-monitor ( vocab-root -- )
+ [ '[ [ _ (start-vocab-monitor) ] ignore-errors ] ]
+ [ "Root monitor: " prepend ]
+ bi spawn drop ;
+
+: init-vocab-monitor ( -- )
+ H{ } clone changed-vocabs set-global
+ vocabs [ changed-vocab ] each ;
+
+[
+ "-no-monitors" (command-line) member? [
+ [ drop ] add-vocab-root-hook set-global
+ f changed-vocabs set-global
+ ] [
+ init-vocab-monitor
+ vocab-roots get [ start-vocab-monitor ] each
+ [ start-vocab-monitor ] add-vocab-root-hook set-global
+ ] if
+] "vocabs.refresh.monitor" add-startup-hook