git ssb

2+

Dominic / pull-stream



Commit 3584caaf3ca81314dcd4b0437ecbf1b538ab973d

Merge branch 'preread_reduce'

Dominic Tarr committed on 10/31/2016, 11:39:26 AM
Parent: 9e8b7714e605551f6eb22a0160194142cb7c3e47
Parent: 6c5caa6f225a8f887bd50cc0ca3fe79876b24357

Files changed

sinks/reduce.jschanged
sinks/reduce.jsView
@@ -1,32 +1,22 @@
11 'use strict'
22
33 var drain = require('./drain')
44
5-module.exports = function reduce (/* reducer, acc, cb */) {
6- var reducer = arguments[0]
7- var acc
8- var cb
9- if (arguments.length === 3) {
10- acc = arguments[1]
11- cb = arguments[2]
12- return drain(function (data) {
13- acc = reducer(acc, data)
14- }, function (err) {
15- cb(err, acc)
16- })
17- } else {
18- cb = arguments[1]
19- var first = true
20- return drain(function (data) {
21- if (first) {
22- acc = data
23- first = false
24- } else {
25- acc = reducer(acc, data)
26- }
27- }, function (err) {
28- cb(err, acc)
29- })
30- }
5+module.exports = function reduce (reducer, acc, cb ) {
6+ if(!cb) cb = acc, acc = null
7+ var sink = drain(function (data) {
8+ acc = reducer(acc, data)
9+ }, function (err) {
10+ cb(err, acc)
11+ })
12+ if (arguments.length === 2)
13+ return function (source) {
14+ source(null, function (end, data) {
15+ //if ended immediately, and no initial...
16+ if(end) return cb(end === true ? null : end)
17+ acc = data; sink(source)
18+ })
19+ }
20+ else
21+ return sink
3122 }
32-

Built with git-ssb-web