git ssb

7+

dinoworm ๐Ÿ› / patchcore



Tree: 9780f1d1ea686a20193f24c7b7f4f606c46cee77

Files: 9780f1d1ea686a20193f24c7b7f4f606c46cee77 / lib / pullLookup.js

968 bytesRaw
1var Value = require('mutant/value')
2var Dict = require('mutant/dict')
3var computed = require('mutant/computed')
4var pullPause = require('pull-pause')
5var nest = require('depnest')
6var pull = require('pull-stream')
7
8exports.gives = nest('lib.obs.pullLookup')
9
10exports.create = function (api) {
11 return nest('lib.obs.pullLookup', MutantPullLookup)
12}
13
14function MutantPullLookup (stream, lookupOrKey) {
15 var pauser = pullPause((paused) => {})
16 pauser.pause()
17
18 var result = Dict({}, {
19 onListen: pauser.resume,
20 onUnlisten: pauser.pause,
21 fixedIndexing: true
22 })
23
24 var sync = Value(false)
25 result.sync = computed([sync, result], (v) => v)
26
27 pull(
28 stream,
29 pauser,
30 pull.drain((item) => {
31 if (item.sync) {
32 sync.set(true)
33 } else {
34 var key = typeof lookupOrKey === 'function' ? lookupOrKey(item) : item[lookupOrKey]
35 result.put(key, item)
36 }
37 }, () => {
38 sync.set(true)
39 })
40 )
41
42 return result
43}
44

Built with git-ssb-web