aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/world/file/watch.lux
blob: 89abd42b16f80e6af5b2e0ab438ee1a8cc839f73 (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
459
460
461
462
463
464
465
466
467
468
469
470
(.require
 [library
  [lux (.except all and)
   ["[0]" ffi (.only import)]
   [abstract
    ["[0]" monad (.only do)]]
   [control
    ["[0]" io (.only IO)]
    ["[0]" maybe]
    ["[0]" try (.only Try)]
    ["[0]" exception (.only Exception)]
    [concurrency
     ["[0]" async (.only Async)]
     ["[0]" stm (.only STM Var)]]
    [function
     [predicate (.only Predicate)]]]
   [data
    ["[0]" product]
    ["[0]" text (.only)
     ["%" \\format (.only format)]]
    [collection
     ["[0]" dictionary (.only Dictionary)]
     ["[0]" list (.use "[1]#[0]" functor monoid mix)]
     ["[0]" set]
     ["[0]" array]]]
   [math
    [number
     ["n" nat]]]
   [meta
    ["@" target]
    [type
     ["[0]" nominal (.only representation abstraction)]]]
   [world
    [time
     ["[0]" instant (.only Instant) (.use "[1]#[0]" equivalence)]]]]]
 ["[0]" //])

(nominal.def .public Concern
  (Record
   [#creation Bit
    #modification Bit
    #deletion Bit])

  (def none
    Concern
    (abstraction
     [#creation false
      #modification false
      #deletion false]))

  (with_template [<concern> <predicate> <event> <create> <modify> <delete>]
    [(def .public <concern>
       Concern
       (abstraction
        [#creation <create>
         #modification <modify>
         #deletion <delete>]))

     (def .public <predicate>
       (Predicate Concern)
       (|>> representation (the <event>)))]

    [creation creation? #creation
     true false false]
    [modification modification? #modification
     false true false]
    [deletion deletion? #deletion
     false false true]
    )

  (def .public (and left right)
    (-> Concern Concern Concern)
    (abstraction
     [#creation (or (..creation? left) (..creation? right))
      #modification (or (..modification? left) (..modification? right))
      #deletion (or (..deletion? left) (..deletion? right))]))

  (def .public all
    Concern
    (.all ..and
          ..creation
          ..modification
          ..deletion
          ))
  )

(type .public (Watcher !)
  (Interface
   (is (-> Concern //.Path (! (Try Any)))
       start)
   (is (-> //.Path (! (Try Concern)))
       concern)
   (is (-> //.Path (! (Try Concern)))
       stop)
   (is (-> [] (! (Try (List [Concern //.Path]))))
       poll)))

(with_template [<name>]
  [(exception.def .public (<name> path)
     (Exception //.Path)
     (exception.report
      (list ["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)]
    (when (dictionary.value path @tracker)
      {.#Some [old_concern last_modified]}
      (do !
        [_ (stm.update (dictionary.has path [new_concern last_modified]) tracker)]
        (in true))
      
      {.#None}
      (in false))))

(def (file_tracker fs directory)
  (-> (//.System Async) //.Path (Async (Try File_Tracker)))
  (do [! (try.with async.monad)]
    [files (of fs directory_files directory)]
    (monad.mix !
               (function (_ file tracker)
                 (do !
                   [last_modified (of fs last_modified file)]
                   (in (dictionary.has file last_modified tracker))))
               (is File_Tracker
                   (dictionary.empty text.hash))
               files)))

(def (available_files fs directory)
  (-> (//.System Async) //.Path (Async (Try (List [//.Path Instant]))))
  (do [! (try.with async.monad)]
    [files (of fs directory_files directory)]
    (monad.each ! (function (_ file)
                    (|> file
                        (of fs last_modified)
                        (of ! each (|>> [file]))))
                files)))

(def (available_directory_changes fs [directory [the_concern file_tracker]])
  (-> (//.System Async) [//.Path [Concern File_Tracker]]
      (Async (Try [[//.Path [Concern File_Tracker]]
                   [(List [//.Path Instant])
                    (List [//.Path Instant Instant])
                    (List //.Path)]])))
  (do [! (try.with async.monad)]
    [current_files (..available_files fs directory)
     .let [creations (if (..creation? the_concern)
                       (list.only (|>> product.left (dictionary.key? file_tracker) not)
                                  current_files)
                       (list))
           available (|> current_files
                         (list#each product.left)
                         (set.of_list text.hash))
           deletions (if (..deletion? the_concern)
                       (|> (dictionary.entries file_tracker)
                           (list#each product.left)
                           (list.only (|>> (set.member? available) not)))
                       (list))
           modifications (list.all (function (_ [path current_modification])
                                     (do maybe.monad
                                       [previous_modification (dictionary.value path file_tracker)]
                                       (in [path previous_modification current_modification])))
                                   current_files)]]
    (in [[directory
          [the_concern
           (let [with_deletions (list#mix dictionary.lacks file_tracker deletions)
                 with_creations (list#mix (function (_ [path last_modified] tracker)
                                            (dictionary.has path last_modified tracker))
                                          with_deletions
                                          creations)
                 with_modifications (list#mix (function (_ [path previous_modification current_modification] tracker)
                                                (dictionary.has path current_modification tracker))
                                              with_creations
                                              modifications)]
             with_modifications)]]
         [creations
          modifications
          deletions]])))

(def .public (polling fs)
  (-> (//.System Async) (Watcher Async))
  (let [tracker (is (Var Directory_Tracker)
                    (stm.var (dictionary.empty text.hash)))]
    (implementation
     (def (start new_concern path)
       (do [! async.monad]
         [exists? (of fs directory? path)]
         (if exists?
           (do !
             [updated? (stm.commit! (..update_watch! new_concern path tracker))]
             (if updated?
               (in {try.#Success []})
               (do (try.with !)
                 [file_tracker (..file_tracker fs path)]
                 (do !
                   [_ (stm.commit! (stm.update (dictionary.has path [new_concern file_tracker]) tracker))]
                   (in {try.#Success []})))))
           (in (exception.except ..cannot_poll_a_non_existent_directory [path])))))
     (def (concern path)
       (stm.commit!
        (do stm.monad
          [@tracker (stm.read tracker)]
          (in (when (dictionary.value path @tracker)
                {.#Some [it file_tracker]}
                {try.#Success it}

                {.#None}
                (exception.except ..not_being_watched [path]))))))
     (def (stop path)
       (stm.commit!
        (do [! stm.monad]
          [@tracker (stm.read tracker)]
          (when (dictionary.value path @tracker)
            {.#Some [the_concern file_tracker]}
            (do !
              [_ (stm.update (dictionary.lacks path) tracker)]
              (in {try.#Success the_concern}))

            {.#None}
            (in (exception.except ..not_being_watched [path]))))))
     (def (poll _)
       (do async.monad
         [@tracker (stm.commit! (stm.read tracker))]
         (do [! (try.with async.monad)]
           [changes (|> @tracker
                        dictionary.entries
                        (monad.each ! (..available_directory_changes fs)))
            _ (do async.monad
                [_ (stm.commit! (stm.write (|> changes
                                               (list#each product.left)
                                               (dictionary.of_list text.hash))
                                           tracker))]
                (in {try.#Success []}))
            .let [[creations modifications deletions]
                  (list#mix (function (_ [_ [creations modifications deletions]]
                                         [all_creations all_modifications all_deletions])
                              [(list#composite creations all_creations)
                               (list#composite modifications all_modifications)
                               (list#composite deletions all_deletions)])
                            [(list) (list) (list)]
                            changes)]]
           (in (.all list#composite
                     (list#each (|>> product.left [..creation]) creations)
                     (|> modifications
                         (list.only (function (_ [path previous_modification current_modification])
                                      (not (instant#= previous_modification current_modification))))
                         (list#each (|>> product.left [..modification])))
                     (list#each (|>> [..deletion]) deletions)
                     )))))
     )))

(def .public (mock separator)
  (-> Text [(//.System Async) (Watcher Async)])
  (let [fs (//.mock separator)]
    [fs
     (..polling fs)]))

(with_expansions [<jvm> (these (import java/lang/Object
                                 "[1]::[0]")

                               (import java/lang/String
                                 "[1]::[0]")

                               (import (java/util/List a)
                                 "[1]::[0]"
                                 (size [] int)
                                 (get [int] a))

                               (def (default_list list)
                                 (All (_ a) (-> (java/util/List a) (List a)))
                                 (let [size (.nat (ffi.of_int (java/util/List::size list)))]
                                   (loop (again [idx 0
                                                 output {.#End}])
                                     (if (n.< size idx)
                                       (again (++ idx)
                                              {.#Item (java/util/List::get (ffi.as_int (.int idx)) list)
                                                      output})
                                       output))))
                               
                               (import (java/nio/file/WatchEvent$Kind a)
                                 "[1]::[0]")

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

                               (import java/nio/file/Watchable
                                 "[1]::[0]")

                               (import java/nio/file/Path
                                 "[1]::[0]"
                                 (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
                                 "[1]::[0]"
                                 ("read_only" "static" ENTRY_CREATE (java/nio/file/WatchEvent$Kind java/nio/file/Path))
                                 ("read_only" "static" ENTRY_MODIFY (java/nio/file/WatchEvent$Kind java/nio/file/Path))
                                 ("read_only" "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 (same? (java/nio/file/StandardWatchEventKinds::ENTRY_CREATE)
                                                kind)
                                         ..creation
                                         
                                         (same? (java/nio/file/StandardWatchEventKinds::ENTRY_MODIFY)
                                                kind)
                                         ..modification
                                         
                                         (same? (java/nio/file/StandardWatchEventKinds::ENTRY_DELETE)
                                                kind)
                                         ..deletion
                                         
                                         ... else
                                         ..none
                                         )))

                               (import java/nio/file/WatchKey
                                 "[1]::[0]"
                                 (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
                                      (of io.monad each (|>> ..default_list
                                                             (list#each default_event_concern)
                                                             (list#mix ..and ..none)))))

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

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

                               (import java/nio/file/FileSystems
                                 "[1]::[0]"
                                 ("static" getDefault [] java/nio/file/FileSystem))

                               (import java/io/File
                                 "[1]::[0]"
                                 (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 (Async (Try java/nio/file/WatchKey)))
                                 (let [watch_events' (list#mix (function (_ [index watch_event] watch_events')
                                                                 (ffi.write! index watch_event watch_events'))
                                                               (ffi.array (java/nio/file/WatchEvent$Kind java/lang/Object)
                                                                          (list.size watch_events))
                                                               (list.enumeration watch_events))]
                                   (async.future
                                    (java/nio/file/Path::register watcher
                                                                  watch_events'
                                                                  (|> path ffi.as_string java/io/File::new java/io/File::toPath)))))

                               (def (default_poll watcher)
                                 (-> java/nio/file/WatchService (IO (Try (List [Concern //.Path]))))
                                 (loop (again [output (is (List [Concern //.Path])
                                                          (list))])
                                   (do (try.with io.monad)
                                     [?key (java/nio/file/WatchService::poll watcher)]
                                     (when ?key
                                       {.#Some key}
                                       (do [! io.monad]
                                         [valid? (java/nio/file/WatchKey::reset key)]
                                         (if (ffi.of_boolean valid?)
                                           (do !
                                             [.let [path (|> key
                                                             java/nio/file/WatchKey::watchable
                                                             (as java/nio/file/Path)
                                                             java/nio/file/Path::toString
                                                             ffi.of_string
                                                             (as //.Path))]
                                              the_concern (..default_key_concern key)]
                                             (again {.#Item [the_concern path]
                                                            output}))
                                           (again output)))
                                       
                                       {.#None}
                                       (in output)))))

                               (def (watch_events concern)
                                 (-> Concern (List Watch_Event))
                                 (.all list#composite
                                       (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 .public default
                                 (IO (Try (Watcher Async)))
                                 (do (try.with io.monad)
                                   [watcher (java/nio/file/FileSystem::newWatchService
                                             (java/nio/file/FileSystems::getDefault))
                                    .let [tracker (stm.var (is (Dictionary //.Path [Concern java/nio/file/WatchKey])
                                                               (dictionary.empty text.hash)))

                                          stop (is (-> //.Path (Async (Try Concern)))
                                                   (function (_ path)
                                                     (do [! async.monad]
                                                       [@tracker (stm.commit! (stm.read tracker))]
                                                       (when (dictionary.value path @tracker)
                                                         {.#Some [the_concern key]}
                                                         (do !
                                                           [_ (async.future
                                                               (java/nio/file/WatchKey::cancel key))
                                                            _ (stm.commit! (stm.update (dictionary.lacks path) tracker))]
                                                           (in {try.#Success the_concern}))

                                                         {.#None}
                                                         (in (exception.except ..not_being_watched [path]))))))]]
                                   (in (is (Watcher Async)
                                           (implementation
                                            (def (start the_concern path)
                                              (do async.monad
                                                [?concern (stop path)]
                                                (do (try.with async.monad)
                                                  [key (..default_start (..watch_events (..and (try.else ..none ?concern)
                                                                                               the_concern))
                                                                        watcher
                                                                        path)]
                                                  (do async.monad
                                                    [_ (stm.commit! (stm.update (dictionary.has path [the_concern key]) tracker))]
                                                    (in {try.#Success []})))))
                                            (def (concern path)
                                              (do async.monad
                                                [@tracker (stm.commit! (stm.read tracker))]
                                                (when (dictionary.value path @tracker)
                                                  {.#Some [it key]}
                                                  (in {try.#Success it})

                                                  {.#None}
                                                  (in (exception.except ..not_being_watched [path])))))
                                            (def stop stop)
                                            (def (poll _)
                                              (async.future (..default_poll watcher)))
                                            )))))
                               )]
  (for @.old (these <jvm>)
       @.jvm (these <jvm>)
       (these)))