Commit e6ada2f6daafcd03bde943759ecec74629525fb6
Changed the 'gives' name to fit the depject patchcore conventions matt mentioned. Also changed the second argument to accept 'opts' to make the space to also add an optional 'object' to filter on directly within the sbot query. Remove the 'sync' observable as mutant-pull-reduce is meant to deal with that.
Gordon Martin committed on 11/26/2017, 10:29:46 PMParent: 613698a4f9211cb8b1e75978401b1edf3831c673
Files changed
backlinks/obs_filter.js | changed |
backlinks/obs_filter.js | ||
---|---|---|
@@ -6,75 +6,63 @@ | ||
6 | 6 … | exports.needs = nest({ |
7 | 7 … | 'sbot.pull.backlinks': 'first' |
8 | 8 … | }) |
9 | 9 … | |
10 | -exports.gives = nest("backlinks.filter.obs", true) | |
10 … | +exports.gives = nest("backlinks.obs.filter", true) | |
11 | 11 … | |
12 | 12 … | /** |
13 | 13 … | * sbot.filter.obs returns an observable list of messages that link |
14 | 14 … | * back to the message with the given message ID (@id). Only messages that |
15 | - * pass the filter function are added to the list. | |
15 … | + * pass the filter are added to the list. | |
16 | 16 … | * |
17 | - * When a message arrives, if passing it to the filter function (@filterFn) | |
18 | - * does not result in it returning 'true' the message is not added to the | |
19 | - * observable list. | |
17 … | + * When a message arrives, if a filter function is given in the options (opts.filter) | |
18 … | + * and passing it to the filter function (opts.filter) not result in it returning | |
19 … | + * 'true' the message is not added to the observable list. | |
20 | 20 … | * |
21 | 21 … | * A 'sync' observable property is also added to the returned observable |
22 | 22 … | * which is 'true' when all previously seen messages are caught up with. |
23 | 23 … | * |
24 | 24 … | * Note: Unlike backlinks.obs.for this does not cache the observable for |
25 | 25 … | * callers that supply the same arguments. |
26 | 26 … | */ |
27 | 27 … | exports.create = function(api) { |
28 | - function pullFilterReduceObs(id, filterFn) { | |
28 … | + function pullFilterReduceObs(id, opts) { | |
29 | 29 … | if (!id || typeof(id) !== "string") { |
30 | 30 … | throw new Error("id must be a string.") |
31 | 31 … | } |
32 | 32 … | |
33 | - if (!filterFn || typeof(filterFn) !== "function") { | |
34 | - throw new Error("filterFn must be a function.") | |
33 … | + var sbotFilter = { | |
34 … | + $filter: { | |
35 … | + dest: id | |
36 … | + } | |
35 | 37 … | } |
36 | 38 … | |
37 | - var sync = Value(false) | |
38 | - | |
39 | 39 … | var msgBacklinks = api.sbot.pull.backlinks({ |
40 | - query: [{ | |
41 | - $filter: { | |
42 | - dest: id | |
43 | - } | |
44 | - }], | |
40 … | + query: [sbotFilter], | |
45 | 41 … | live: true |
46 | 42 … | }) |
47 | 43 … | |
48 | - var msgFilter = pull.filter((msg) => { | |
49 | - // We do not include the 'sync' message which indicates that any | |
50 | - // further messages are newly gossiped messages in the list of messages. | |
51 | - if (msg.sync) { | |
52 | - sync.set(true) | |
53 | - return false | |
54 | - } else { | |
55 | - return filterFn(msg) | |
56 | - } | |
57 | - }) | |
44 … | + // If a filter function is supplied in the options, we use it to filter | |
45 … | + // the links stream, otherwise we use all the messages from the stream | |
46 … | + var filterFunction = opts && opts.filter ? opts.filter : () => true | |
58 | 47 … | |
59 | 48 … | var filteredBacklinks = pull( |
60 | 49 … | msgBacklinks, |
61 | - msgFilter | |
50 … | + pull.filter(filterFunction) | |
62 | 51 … | ) |
63 | 52 … | |
64 | 53 … | var backlinksObs = MutantPullReduce(filteredBacklinks, (state, msg) => { |
65 | 54 … | state.push(msg) |
66 | 55 … | return state; |
67 | 56 … | }, { |
68 | 57 … | startValue: [], |
69 | - nextTick: true | |
58 … | + nextTick: true, | |
59 … | + sync: true | |
70 | 60 … | }) |
71 | 61 … | |
72 | - backlinksObs.sync = sync | |
73 | - | |
74 | 62 … | return backlinksObs; |
75 | 63 … | } |
76 | 64 … | |
77 | 65 … | return nest({ |
78 | - "backlinks.filter.obs": (id, filterFn) => pullFilterReduceObs(id, filterFn) | |
66 … | + "backlinks.obs.filter": (id, opts) => pullFilterReduceObs(id, opts) | |
79 | 67 … | }) |
80 | 68 … | } |
Built with git-ssb-web