Permalink
Browse files

Auto-lifts values into promises.

  • Loading branch information...
1 parent 763d7f9 commit 4ca9c13ad3645f75d2509949272188c3b7fb7bc5 @robotlolita robotlolita committed Apr 1, 2013
Showing with 45 additions and 43 deletions.
  1. +5 −3 README.md
  2. +3 −2 package.json
  3. +24 −17 src/index.ls
  4. +13 −21 test/suite.ls
View
@@ -13,18 +13,20 @@ general :D
```js
var noisyo = require('noisyo')
+var spit = noisyo.spit
+var slurp = noisyo.slurp
// Slurp takes a Stream and returns a Promise of its contents.
// Spit takes a Stream, and some contents, and returns a Promise of the
// eventual draining.
-noisyo.slurp(process.stdin).then(noisyo.spit(process.stdout))
+spit(process.stdout, slurp(process.stdin))
// So, why not just `.pipe()` you faggot?
// Well, these work with both Strings and Streams interchangeably.
-var input = noisyo.slurp(process.stdin)
+var input = slurp(process.stdin)
input.then(function(data) {
console.log(data)
- noisyo.spit(fs.createWriteStream('foo.txt'), data)
+ spit(fs.createWriteStream('foo.txt'), data)
})
// Obviously, you *will* want to use Streams directly if you're interested in
View
@@ -1,6 +1,6 @@
{
"name": "noisyo",
- "version": "0.1.0",
+ "version": "0.1.1",
"description": "Drinks and vomits Stream contents. Promised to be really noisy.",
"main": "./lib/index.js",
"devDependencies": {
@@ -40,6 +40,7 @@
"node": ">=0.10.0"
},
"dependencies": {
- "pinky": "~0.1.2"
+ "pinky": "~0.1.2",
+ "pinky-combinators": "~0.1.1"
}
}
View
@@ -26,6 +26,7 @@
### -- Dependencies ----------------------------------------------------
pinky = require 'pinky'
+{all} = require 'pinky-combinators'
stream = require 'stream'
@@ -100,29 +101,29 @@ write-to = (dest, source, encoding) ->
#### λ slurp
# Drains the contents of a Stream and returns a promise for it.
#
-# :: Stream -> Promise String Error
-slurp = (source) ->
+# :: Promise Stream a -> Promise String Error
+slurp = (source-p) ->
promise = pinky!
- data = (source.read! || '').to-string!
+ data = ''
- source.on 'readable' grab-chunk
- source.on 'error' handle-failure
- source.on 'end' fulfill
- source.on 'end' clean-up
+ (pinky source-p).then (source) ->
+ data := ((source.read 0) or '').to-string!
+
+ source.on 'readable' (grab-chunk source)
+ source.on 'end' fulfill
+ source.on 'end' (clean-up source)
return promise
- function grab-chunk()
+
+ function grab-chunk(source) => ->
chunk = source.read!
if chunk isnt null => data += chunk.to-string!
- function handle-failure(err)
- promise.reject err
-
function fulfill()
promise.fulfill data
- function clean-up()
+ function clean-up(source) => ->
source.remove-listener 'readable' grab-chunk
source.remove-listener 'end' fulfill
source.remove-listener 'end' clean-up
@@ -131,13 +132,19 @@ slurp = (source) ->
#### λ spit
# Vomits some contents (String or Stream) into a Stream.
#
-# :: Stream -> String -> Promise Stream Error
-# :: Stream -> Stream -> Promise Stream Error
-spit = (dest, source) -->
- | is-stream source => pipe-from source, dest
- | otherwise => write-to dest, source, arguments[2]
+# :: Promise Stream a -> Promise String a -> Promise Stream Error
+# :: Promise Stream a -> Promise Stream a -> Promise Stream Error
+spit = (dest-p, source-p) -->
+ promise = pinky!
+ all [dest-p, source-p] .then ([dest, source]) ->
+ p2 = switch
+ | is-stream source => pipe-from source, dest
+ | otherwise => write-to dest, source, arguments[2]
+ p2.then promise.fulfill, promise.reject
+
+ return promise
### -- Exports ---------------------------------------------------------
View
@@ -11,29 +11,25 @@ x = xit
duplex = ->
d = new stream.Duplex
+ d.name = '<no>'
data = ''
- ready = false
- d.write = (buf, enc, f) ->
- data := buf
+ d._write = (buf, enc, f) ->
+ data := buf.to-string!
+ d.push data
process.next-tick -> do
- ready := true
+ d.push null
if f => f!
- d.emit 'readable'
-
-
- d.read = ->
- if ready
- process.next-tick -> d.emit 'end'
- ready := false
- return data
- else
- return null
+ d._read = (n) -> do
+ if d.haz-error => d.emit 'error', (new Error 'no u')
+ data
return d
d = null
-before-each -> d := duplex!
+before-each ->
+ d := duplex!
+ d.name = '<d>'
text = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.'
@@ -43,24 +39,20 @@ describe 'λ slurp' ->
d.write text
expect (slurp d) .to.become text
- x 'Should fail if the stream errors.' ->
- d.write text
- d.haz-error = true
- expect (slurp d) .to.be.rejected.with /no u/
describe 'λ spit' ->
describe 'with Stream' ->
o 'Should fulfill after contents have been piped.' (done) ->
t = duplex!
d.write text
- expect ((spit t, d).then slurp) .to.become text
+ expect (slurp (spit t, d)) .to.become text
o 'Should fulfill after source is read if it\'s a standard output stream.' ->
d.write text
expect (spit process.stdout, d) .to.be.fulfilled
describe 'with Strings' ->
o 'Should fulfill after writing to the source.' ->
- expect ((spit d, text).then slurp) .to.become text
+ expect (slurp (spit d, text)) .to.become text

0 comments on commit 4ca9c13

Please sign in to comment.