The Writable stream interface is an abstraction for a destination that you are writing data to.
Examples of writable streams include:
process.stdout
, process.stderr
if a stream.write(chunk)
call returns false, then the "drain" event will indicate when it is appropriate to begin writing more data to the stream
(defn writeOneMillionTimes
[writer data encoding cb]
(let [i (atom 1000000)
ok (atom true)
write (fn write []
(while (and (> @i 0) @ok) ;bails if last write failed
(swap! i dec)
(if (zero? @i)
; i=0 so we write the last time!
(.write writer data encoding cb)
; no cb since we are not done yet
(reset! ok (.write writer data encoding))))
(if (> i 0) ;resume writing after drain event
(.once writer "drain" write )))]
(write)))
When the stream.end()
method has been called, and all data has
been flushed to the underlying system, this event is emitted.
(.on writer "finish" (fn [] (println "all writes are now complete")))
(dotimes [i 100]
(.write writer (str "hello #" i "!")))
(.end writer "this is the end\n")
src
: {stream.Readable}
(. writer on "pipe"
(fn [src]
(println "something is piping into the writer")
(assert (= src reader))))
(-> reader
(.pipe writer))
(. writer on "unpipe"
(fn [src]
(println "something has stopped piping into the writer")
(assert (= reader src))))
(.pipe reader writer)
(.unpipe reader writer)
stream.uncork()
][] or at
[stream.end()
][stream-end] call.chunk: {string|buffer.Buffer}
call this method when there is no more data to be written to the stream.
If cb, cb is attached as a listener for "finish"
calling steam.write
after stream.end
will raise an error
(def file (. fs createWriteStream "example.txt"))
(. file write "hello, ")
(. file end "world!")
; no more writing is allowed
chunk
: string|Bufferencoding
: string : encoding when chunk is a stringtrue
if the data was handled completely, keep writing!false
the data was buffered internally"drain"
(WritableStream options)
options
: {IMap}
{number}
stream.write()
starts returning false
16384
(16kb), or 16
for objectMode
streams.{boolean}
stream._write()
true
{boolean}
stream.write(anyObj)
is a valid operation.Buffer
/ String
data.false
Function
Function
{buffer.Buffer|string}
:decodeStrings false
{string}
{function}
stream._write
method to send data to the underlying resource.
decodeStrings true
is set at class construction, then chunk
arg can be a string and not a Buffer, relying on what you pass as encoding
Array
#js{"chunk" .... , "encoding" ...}
Can you improve this documentation?Edit on GitHub
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close