-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.rkt
68 lines (60 loc) · 2.2 KB
/
main.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#lang racket/base
(require
"./threads.rkt"
racket/contract)
(provide
file-activity-channel
file-watcher-status-channel
file-watcher-channel-try-get
file-watcher-channel-get
path-on-disk?
(all-from-out "./robust-watch.rkt")
(all-from-out "./intensive-watch.rkt")
(all-from-out "./apathetic-watch.rkt")
(contract-out
[suggest-approach (->* (#:apathetic boolean?) () procedure?)]
[watch-directories (->* ()
((listof directory-exists?)
(-> list? any)
(-> list? any)
(-> path? thread?))
thread?)]
[watch (->* () ((listof path-on-disk?)
(-> list? any)
(-> list? any)
(-> path? thread?))
thread?)]))
;; ------------------------------------------------------------------
;; Implementation
(require
racket/async-channel
"./intensive-watch.rkt"
"./apathetic-watch.rkt"
"./robust-watch.rkt"
"./filesystem.rkt")
(define (suggest-approach #:apathetic apathetic)
(define spec (system-type 'fs-change))
(define (check-support index sym) (equal? (vector-ref spec index) sym))
(define supported (check-support 0 'supported))
(define file-level (check-support 3 'file-level))
(if (and supported file-level)
(if apathetic apathetic-watch intensive-watch)
robust-watch))
(define (watch
[paths (list (current-directory))]
[on-activity displayln]
[on-status displayln]
[thread-maker (suggest-approach #:apathetic #f)])
(define watchers (map thread-maker paths))
(thread (lambda () (let loop ()
(define activity (async-channel-try-get (file-activity-channel)))
(define status (async-channel-try-get (file-watcher-status-channel)))
(when status (on-status status))
(when activity (on-activity activity))
(when (ormap thread-running? watchers) (loop))))))
(define (watch-directories
[paths (list (current-directory))]
[on-activity displayln]
[on-status displayln]
[thread-maker (suggest-approach #:apathetic #f)])
(watch paths on-activity on-status thread-maker))