aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/world/file/watch.lux
blob: f1415da805707bd39c9d0d6d76a0788f6cff3505 (plain)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
(.module:
  [lux #*
   ["@" target]
   ["." ffi (#+ import:)]
   [abstract
    [predicate (#+ Predicate)]
    ["." monad (#+ do)]]
   [control
    ["." io (#+ IO)]
    ["." try (#+ Try)]
    ["." exception (#+ exception:)]
    [concurrency
     ["." promise (#+ Promise)]
     ["." stm (#+ STM Var)]]]
   [data
    ["." product]
    ["." maybe]
    ["." text
     ["%" format (#+ format)]]
    [collection
     ["." dictionary (#+ Dictionary)]
     ["." list ("#\." functor monoid fold)]
     ["." set]
     ["." array]]]
   [math
    [number
     ["n" nat]]]
   [time
    ["." instant (#+ Instant) ("#\." equivalence)]]
   [type
    [abstract (#+ abstract: :representation :abstraction)]]]
  ["." //])

(abstract: #export Concern
  {#create Bit
   #modify Bit
   #delete Bit}

  (def: none
    Concern
    (:abstraction
     {#create false
      #modify false
      #delete false}))

  (template [<concern> <predicate> <event> <create> <modify> <delete>]
    [(def: #export <concern>
       Concern
       (:abstraction
        {#create <create>
         #modify <modify>
         #delete <delete>}))

     (def: #export <predicate>
       (Predicate Concern)
       (|>> :representation (get@ <event>)))]

    [creation creation? #create
     true false false]
    [modification modification? #modify
     false true false]
    [deletion deletion? #delete
     false false true]
    )

  (def: #export (also left right)
    (-> Concern Concern Concern)
    (:abstraction
     {#create (or (..creation? left) (..creation? right))
      #modify (or (..modification? left) (..modification? right))
      #delete (or (..deletion? left) (..deletion? right))}))

  (def: #export all
    Concern
    ($_ ..also
        ..creation
        ..modification
        ..deletion
        ))
  )

(interface: #export (Watcher !)
  (: (-> Concern //.Path (! (Try Any)))
     start)
  (: (-> //.Path (! (Try Concern)))
     concern)
  (: (-> //.Path (! (Try Concern)))
     stop)
  (: (-> [] (! (Try (List [Concern //.Path]))))
     poll))

(template [<name>]
  [(exception: #export (<name> {path //.Path})
     (exception.report
      ["Path" (%.text path)]))]

  [not_being_watched]
  [cannot_poll_a_non_existent_directory]
  )

(type: File_Tracker
  (Dictionary //.Path Instant))

(type: Directory_Tracker
  (Dictionary //.Path [Concern File_Tracker]))

(def: (update_watch! new_concern path tracker)
  (-> Concern //.Path (Var Directory_Tracker) (STM Bit))
  (do {! stm.monad}
    [@tracker (stm.read tracker)]
    (case (dictionary.get path @tracker)
      (#.Some [old_concern last_modified])
      (do !
        [_ (stm.update (dictionary.put path [new_concern last_modified]) tracker)]
        (wrap true))
      
      #.None
      (wrap false))))

(def: (file_tracker fs directory)
  (-> (//.System Promise) //.Path (Promise (Try File_Tracker)))
  (do {! (try.with promise.monad)}
    [files (\ fs directory_files directory)]
    (monad.fold !
                (function (_ file tracker)
                  (do !
                    [last_modified (\ fs last_modified file)]
                    (wrap (dictionary.put file last_modified tracker))))
                (: File_Tracker
                   (dictionary.new text.hash))
                files)))

(def: (poll_files fs directory)
  (-> (//.System Promise) //.Path (Promise (Try (List [//.Path Instant]))))
  (do {! (try.with promise.monad)}
    [files (\ fs directory_files directory)]
    (monad.map ! (function (_ file)
                   (|> file
                       (\ fs last_modified)
                       (\ ! map (|>> [file]))))
               files)))

(def: (poll_directory_changes fs [directory [concern file_tracker]])
  (-> (//.System Promise) [//.Path [Concern File_Tracker]]
      (Promise (Try [[//.Path [Concern File_Tracker]]
                     [(List [//.Path Instant])
                      (List [//.Path Instant Instant])
                      (List //.Path)]])))
  (do {! (try.with promise.monad)}
    [current_files (..poll_files fs directory)
     #let [creations (if (..creation? concern)
                       (list.filter (|>> product.left (dictionary.key? file_tracker) not)
                                    current_files)
                       (list))
           available (|> current_files
                         (list\map product.left)
                         (set.from_list text.hash))
           deletions (if (..deletion? concern)
                       (|> (dictionary.entries file_tracker)
                           (list\map product.left)
                           (list.filter (|>> (set.member? available) not)))
                       (list))
           modifications (list.all (function (_ [path current_modification])
                                     (do maybe.monad
                                       [previous_modification (dictionary.get path file_tracker)]
                                       (wrap [path previous_modification current_modification])))
                                   current_files)]]
    (wrap [[directory
            [concern
             (let [with_deletions (list\fold dictionary.remove file_tracker deletions)
                   with_creations (list\fold (function (_ [path last_modified] tracker)
                                               (dictionary.put path last_modified tracker))
                                             with_deletions
                                             creations)
                   with_modifications (list\fold (function (_ [path previous_modification current_modification] tracker)
                                                   (dictionary.put path current_modification tracker))
                                                 with_creations
                                                 modifications)]
               with_modifications)]]
           [creations
            modifications
            deletions]])))

(def: #export (polling fs)
  (-> (//.System Promise) (Watcher Promise))
  (let [tracker (: (Var Directory_Tracker)
                   (stm.var (dictionary.new text.hash)))]
    (implementation
     (def: (start new_concern path)
       (do {! promise.monad}
         [exists? (\ fs directory? path)]
         (if exists?
           (do !
             [updated? (stm.commit (..update_watch! new_concern path tracker))]
             (if updated?
               (wrap (#try.Success []))
               (do (try.with !)
                 [file_tracker (..file_tracker fs path)]
                 (do !
                   [_ (stm.commit (stm.update (dictionary.put path [new_concern file_tracker]) tracker))]
                   (wrap (#try.Success []))))))
           (wrap (exception.throw ..cannot_poll_a_non_existent_directory [path])))))
     (def: (concern path)
       (stm.commit
        (do stm.monad
          [@tracker (stm.read tracker)]
          (wrap (case (dictionary.get path @tracker)
                  (#.Some [concern file_tracker])
                  (#try.Success concern)

                  #.None
                  (exception.throw ..not_being_watched [path]))))))
     (def: (stop path)
       (stm.commit
        (do {! stm.monad}
          [@tracker (stm.read tracker)]
          (case (dictionary.get path @tracker)
            (#.Some [concern file_tracker])
            (do !
              [_ (stm.update (dictionary.remove path) tracker)]
              (wrap (#try.Success concern)))

            #.None
            (wrap (exception.throw ..not_being_watched [path]))))))
     (def: (poll _)
       (do promise.monad
         [@tracker (stm.commit (stm.read tracker))]
         (do {! (try.with promise.monad)}
           [changes (|> @tracker
                        dictionary.entries
                        (monad.map ! (..poll_directory_changes fs)))
            _ (do promise.monad
                [_ (stm.commit (stm.write (|> changes
                                              (list\map product.left)
                                              (dictionary.from_list text.hash))
                                          tracker))]
                (wrap (#try.Success [])))
            #let [[creations modifications deletions]
                  (list\fold (function (_ [_ [creations modifications deletions]]
                                          [all_creations all_modifications all_deletions])
                               [(list\compose creations all_creations)
                                (list\compose modifications all_modifications)
                                (list\compose deletions all_deletions)])
                             [(list) (list) (list)]
                             changes)]]
           (wrap ($_ list\compose
                     (list\map (|>> product.left [..creation]) creations)
                     (|> modifications
                         (list.filter (function (_ [path previous_modification current_modification])
                                        (not (instant\= previous_modification current_modification))))
                         (list\map (|>> product.left [..modification])))
                     (list\map (|>> [..deletion]) deletions)
                     )))))
     )))

(def: #export (mock separator)
  (-> Text [(//.System Promise) (Watcher Promise)])
  (let [fs (//.mock separator)]
    [fs
     (..polling fs)]))

(with_expansions [<jvm> (as_is (import: java/lang/Object)

                               (import: java/lang/String)

                               (import: (java/util/List a)
                                 ["#::."
                                  (size [] int)
                                  (get [int] a)])

                               (def: (default_list list)
                                 (All [a] (-> (java/util/List a) (List a)))
                                 (let [size (.nat (java/util/List::size list))]
                                   (loop [idx 0
                                          output #.Nil]
                                     (if (n.< size idx)
                                       (recur (inc idx)
                                              (#.Cons (java/util/List::get (.int idx) list)
                                                      output))
                                       output))))
                               
                               (import: (java/nio/file/WatchEvent$Kind a))

                               (import: (java/nio/file/WatchEvent a)
                                 ["#::."
                                  (kind [] (java/nio/file/WatchEvent$Kind a))])

                               (import: java/nio/file/Watchable)

                               (import: java/nio/file/Path
                                 ["#::."
                                  (register [java/nio/file/WatchService [(java/nio/file/WatchEvent$Kind [? < java/lang/Object])]] #io #try java/nio/file/WatchKey)
                                  (toString [] java/lang/String)])

                               (import: java/nio/file/StandardWatchEventKinds
                                 ["#::."
                                  (#static ENTRY_CREATE (java/nio/file/WatchEvent$Kind java/nio/file/Path))
                                  (#static ENTRY_MODIFY (java/nio/file/WatchEvent$Kind java/nio/file/Path))
                                  (#static ENTRY_DELETE (java/nio/file/WatchEvent$Kind java/nio/file/Path))])

                               (def: (default_event_concern event)
                                 (All [a]
                                   (-> (java/nio/file/WatchEvent a) Concern))
                                 (let [kind (:as (java/nio/file/WatchEvent$Kind java/nio/file/Path)
                                                 (java/nio/file/WatchEvent::kind event))]
                                   (cond (is? (java/nio/file/StandardWatchEventKinds::ENTRY_CREATE)
                                              kind)
                                         ..creation
                                         
                                         (is? (java/nio/file/StandardWatchEventKinds::ENTRY_MODIFY)
                                              kind)
                                         ..modification
                                         
                                         (is? (java/nio/file/StandardWatchEventKinds::ENTRY_DELETE)
                                              kind)
                                         ..deletion
                                         
                                         ## else
                                         ..none
                                         )))

                               (import: java/nio/file/WatchKey
                                 ["#::."
                                  (reset [] #io boolean)
                                  (cancel [] #io void)
                                  (watchable [] java/nio/file/Watchable)
                                  (pollEvents [] #io (java/util/List (java/nio/file/WatchEvent ?)))])

                               (def: default_key_concern
                                 (-> java/nio/file/WatchKey (IO Concern))
                                 (|>> java/nio/file/WatchKey::pollEvents
                                      (\ io.monad map (|>> ..default_list
                                                           (list\map default_event_concern)
                                                           (list\fold ..also ..none)))))

                               (import: java/nio/file/WatchService
                                 ["#::."
                                  (poll [] #io #try #? java/nio/file/WatchKey)])

                               (import: java/nio/file/FileSystem
                                 ["#::."
                                  (newWatchService [] #io #try java/nio/file/WatchService)])

                               (import: java/nio/file/FileSystems
                                 ["#::."
                                  (#static getDefault [] java/nio/file/FileSystem)])

                               (import: java/io/File
                                 ["#::."
                                  (new [java/lang/String])
                                  (toPath [] java/nio/file/Path)])

                               (type: Watch_Event
                                 (java/nio/file/WatchEvent$Kind java/lang/Object))

                               (def: (default_start watch_events watcher path)
                                 (-> (List Watch_Event) java/nio/file/WatchService //.Path (Promise (Try java/nio/file/WatchKey)))
                                 (let [watch_events' (list\fold (function (_ [index watch_event] watch_events')
                                                                  (ffi.array_write index watch_event watch_events'))
                                                                (ffi.array (java/nio/file/WatchEvent$Kind java/lang/Object)
                                                                           (list.size watch_events))
                                                                (list.enumeration watch_events))]
                                   (promise.future
                                    (java/nio/file/Path::register watcher
                                                                  watch_events'
                                                                  (|> path java/io/File::new java/io/File::toPath)))))

                               (def: (default_poll watcher)
                                 (-> java/nio/file/WatchService (IO (Try (List [Concern //.Path]))))
                                 (loop [output (: (List [Concern //.Path])
                                                  (list))]
                                   (do (try.with io.monad)
                                     [?key (java/nio/file/WatchService::poll watcher)]
                                     (case ?key
                                       (#.Some key)
                                       (do {! io.monad}
                                         [valid? (java/nio/file/WatchKey::reset key)]
                                         (if valid?
                                           (do !
                                             [#let [path (|> key
                                                             java/nio/file/WatchKey::watchable
                                                             (:as java/nio/file/Path)
                                                             java/nio/file/Path::toString
                                                             (:as //.Path))]
                                              concern (..default_key_concern key)]
                                             (recur (#.Cons [concern path]
                                                            output)))
                                           (recur output)))
                                       
                                       #.None
                                       (wrap output)))))

                               (def: (watch_events concern)
                                 (-> Concern (List Watch_Event))
                                 ($_ list\compose
                                     (if (..creation? concern)
                                       (list (:as Watch_Event (java/nio/file/StandardWatchEventKinds::ENTRY_CREATE)))
                                       (list))
                                     (if (..modification? concern)
                                       (list (:as Watch_Event (java/nio/file/StandardWatchEventKinds::ENTRY_MODIFY)))
                                       (list))
                                     (if (..deletion? concern)
                                       (list (:as Watch_Event (java/nio/file/StandardWatchEventKinds::ENTRY_DELETE)))
                                       (list))
                                     ))

                               (def: #export default
                                 (IO (Try (Watcher Promise)))
                                 (do (try.with io.monad)
                                   [watcher (java/nio/file/FileSystem::newWatchService
                                             (java/nio/file/FileSystems::getDefault))
                                    #let [tracker (stm.var (: (Dictionary //.Path [Concern java/nio/file/WatchKey])
                                                              (dictionary.new text.hash)))

                                          stop (: (-> //.Path (Promise (Try Concern)))
                                                  (function (_ path)
                                                    (do {! promise.monad}
                                                      [@tracker (stm.commit (stm.read tracker))]
                                                      (case (dictionary.get path @tracker)
                                                        (#.Some [concern key])
                                                        (do !
                                                          [_ (promise.future
                                                              (java/nio/file/WatchKey::cancel key))
                                                           _ (stm.commit (stm.update (dictionary.remove path) tracker))]
                                                          (wrap (#try.Success concern)))

                                                        #.None
                                                        (wrap (exception.throw ..not_being_watched [path]))))))]]
                                   (wrap (: (Watcher Promise)
                                            (implementation
                                             (def: (start concern path)
                                               (do promise.monad
                                                 [?concern (stop path)]
                                                 (do (try.with promise.monad)
                                                   [key (..default_start (..watch_events (..also (try.default ..none ?concern)
                                                                                                 concern))
                                                                         watcher
                                                                         path)]
                                                   (do promise.monad
                                                     [_ (stm.commit (stm.update (dictionary.put path [concern key]) tracker))]
                                                     (wrap (#try.Success []))))))
                                             (def: (concern path)
                                               (do promise.monad
                                                 [@tracker (stm.commit (stm.read tracker))]
                                                 (case (dictionary.get path @tracker)
                                                   (#.Some [concern key])
                                                   (wrap (#try.Success concern))

                                                   #.None
                                                   (wrap (exception.throw ..not_being_watched [path])))))
                                             (def: stop stop)
                                             (def: (poll _)
                                               (promise.future (..default_poll watcher)))
                                             )))))
                               )]
  (for {@.old (as_is <jvm>)
        @.jvm (as_is <jvm>)}
       (as_is)))