Files: e0439586cabe63fc84e88ff400edb18470cdec52 / lib / pullLookup.js
968 bytesRaw
1 | var Value = require('mutant/value') |
2 | var Dict = require('mutant/dict') |
3 | var computed = require('mutant/computed') |
4 | var pullPause = require('pull-pause') |
5 | var nest = require('depnest') |
6 | var pull = require('pull-stream') |
7 | |
8 | exports.gives = nest('lib.obs.pullLookup') |
9 | |
10 | exports.create = function (api) { |
11 | return nest('lib.obs.pullLookup', MutantPullLookup) |
12 | } |
13 | |
14 | function 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