Skip to content

Commit

Permalink
Add close method to IReader
Browse files Browse the repository at this point in the history
  • Loading branch information
athos committed Feb 16, 2020
1 parent 18953ad commit 8519c72
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 9 deletions.
9 changes: 6 additions & 3 deletions src/pogonos/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,12 @@
([file data opts]
(let [opts (cond-> opts
(string? file) (assoc :source file)
(instance? File file) (assoc :source (.getName ^File file)))]
(with-open [in ^Closeable (reader/make-file-reader file)]
(render-input in data opts))))))
(instance? File file) (assoc :source (.getName ^File file)))
in (reader/make-file-reader file)]
(try
(render-input in data opts)
(finally
(reader/close in)))))))

#?(:clj
(defn set-default-partials-base-path! [base-path]
Expand Down
3 changes: 2 additions & 1 deletion src/pogonos/protocols.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
(:refer-clojure :exclude [read-line resolve]))

(defprotocol IReader
(read-line [this]))
(read-line [this])
(close [this]))

(defprotocol IRenderable
(render [this ctx out]))
Expand Down
9 changes: 7 additions & 2 deletions src/pogonos/reader.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
[pogonos.strings :as pstr])
#?(:clj (:import [java.io Reader Closeable])))

(defn close [reader] (proto/close reader))

(deftype StringReader [src ^:unsynchronized-mutable offset]
proto/IReader
(read-line [this]
Expand All @@ -14,7 +16,8 @@
offset' (or (some-> i inc) (count src))
ret (subs src offset offset')]
(set! offset offset')
ret))))
ret)))
(close [this]))

(defn make-string-reader [s]
(StringReader. s 0))
Expand Down Expand Up @@ -85,7 +88,9 @@
(base-reader [this] in)
proto/IReader
(read-line [this]
(read-line this)))
(read-line this))
(close [this]
(proto/close in)))

(defn make-line-buffering-reader [in]
(->LineBufferingReader in nil 0 0))
Expand Down
4 changes: 1 addition & 3 deletions src/pogonos/render.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,4 @@
(parse/parse r #(render* ctx out %)
{:source name :indent (:indent this)})
(finally
#?(:clj
(when (instance? java.io.Closeable r)
(.close ^java.io.Closeable r)))))))))
(reader/close r)))))))

0 comments on commit 8519c72

Please sign in to comment.