~subsetpark/bagatto

ref: 22683bebdf00ea054405e41739fc0ef5fdeb7666 bagatto/src/loaders.janet -rw-r--r-- 1.6 KiB
22683beb — Zach Smith Demand pool management: wait for all threads to exit before main thread exits 10 months ago
                                                                                
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
(import src/error)
(import src/threads)

(defn- make-attrs [parser filename &opt file-contents]
  (let [base-attrs @{:path filename :contents file-contents}]
    (if file-contents
      (try (parser file-contents base-attrs)
        ([err fib] (error/attrs-error err parser)))
      base-attrs)))

(defn from-file-spec-loader
  ```
  A loader that takes either a single file spec or a sequence of file
  specs and sends back a list of attributes.
  ```
  [spec-name loader parser transform-f]
  (fn [parent]
    (threads/print "Loading " spec-name "...")
    (let [loader-specs (loader)
          res (match loader-specs
                {:each ind}
                (do
                  (threads/print "[" spec-name "] Loading " (length ind) " files")
                  (-> (seq [spec :in ind]
                        (if (indexed? spec)
                          (make-attrs parser ;spec)
                          (make-attrs parser spec)))
                      (transform-f)))

                {:some spec}
                (make-attrs parser ;spec))]
      (:send parent [:res spec-name res]))))

(defn from-path-loader
  ```
  A loader that takes a literal file path and generates file
  attributes.
  ```
  [spec-name path parser]
  (fn [parent]
    (threads/print "Loading " spec-name " (" path ")...")
    (let [file-contents (slurp path)
          res (make-attrs parser path file-contents)]
      (:send parent [:res spec-name res]))))

(defn bare-attr-loader
  ```
  A loader that takes an attributes literal and returns it.
  ```
  [spec-name attrs]
  (fn [parent]
    (threads/print "Loaded " spec-name)
    (:send parent [:res spec-name attrs])))