git ssb

2+

Dominic / pull-stream



Tree: fd5a385051c12f9b5331d8d77e51257f6d93bdc5

Files: fd5a385051c12f9b5331d8d77e51257f6d93bdc5 / sources.js

1586 bytesRaw
1
2var keys = exports.keys =
3function (object) {
4 return values(Object.keys(object))
5}
6
7function abortCb(cb, abort, onAbort) {
8 cb(abort)
9 onAbort && onAbort(abort === true ? null: abort)
10 return
11}
12
13var once = exports.once =
14function (value, onAbort) {
15 return function (abort, cb) {
16 if(abort)
17 return abortCb(cb, abort, onAbort)
18 if(value != null) {
19 var _value = value; value = null
20 cb(null, _value)
21 } else
22 cb(true)
23 }
24}
25
26var values = exports.values = exports.readArray =
27function (array, onAbort) {
28 if(!array)
29 return function (abort, cb) {
30 if(abort) return abortCb(cb, abort, onAbort)
31 return cb(true)
32 }
33 if(!Array.isArray(array))
34 array = Object.keys(array).map(function (k) {
35 return array[k]
36 })
37 var i = 0
38 return function (abort, cb) {
39 if(abort)
40 return abortCb(cb, abort, onAbort)
41 cb(i >= array.length || null, array[i++])
42 }
43}
44
45
46var count = exports.count =
47function (max) {
48 var i = 0; max = max || Infinity
49 return function (end, cb) {
50 if(end) return cb && cb(end)
51 if(i > max)
52 return cb(true)
53 cb(null, i++)
54 }
55}
56
57var infinite = exports.infinite =
58function (generate) {
59 generate = generate || Math.random
60 return function (end, cb) {
61 if(end) return cb && cb(end)
62 return cb(null, generate())
63 }
64}
65
66//a stream that ends immediately.
67var empty = exports.empty = function () {
68 return function (abort, cb) {
69 cb(true)
70 }
71}
72
73//a stream that errors immediately.
74var error = exports.error = function (err) {
75 return function (abort, cb) {
76 cb(err)
77 }
78}
79
80

Built with git-ssb-web