~subsetpark/bagatto

d4f43c82fb988f1e77bb64c61c157d507741fb3d — Zach Smith 2 months ago afc04f6
Use wait-for-fibs
2 files changed, 12 insertions(+), 15 deletions(-)

M src/core.janet
M src/writers.janet
M src/core.janet => src/core.janet +10 -12
@@ 78,19 78,17 @@
  ```
  [output-dir writer-specs]

  (def spec-count (length writer-specs))
  (def handler (writers/write-handler output-dir))
  (def cache @{})
  (let [spec-count (length writer-specs)
        handler (writers/write-handler output-dir @{})
        supervisor (ev/chan spec-count)
        fibers (map |(ev/go (fn [] (handler $)) nil supervisor) writer-specs)]

  (printf "Writing %d output specs..." spec-count)
    (printf "Writing %d output specs..." spec-count)

  (def supervisor (ev/chan spec-count))
  (each spec writer-specs
    (ev/go (fiber/new (fn [] (handler spec cache)) :a) nil supervisor))

  (var oked 0)
  (while (< oked spec-count)
    (ev/take supervisor)
    (++ oked))
    (repeat spec-count
      (let [[sig fiber] (ev/take supervisor)]
        (unless (= sig :ok)
          (each f fibers (ev/cancel f "sibling canceled"))
          (propagate (fiber/last-value fiber) fiber)))))

  (printf "Done writing."))

M src/writers.janet => src/writers.janet +2 -3
@@ 15,11 15,10 @@
      (put cache s true))))

(defn write-handler
  [output-dir]
  [output-dir path-cache]

  (fn handler
    [msg path-cache]

    [msg]
    (match msg
      [:write path contents]
      (let [path (if output-dir (path/join output-dir path) path)