Commit b70e39416e0ec7a808742b9841a6cd32d45b3354
Add new backlinks filter obs function. This is a helper function to watch for links back to a message and add them to an observable list if they pass a given filter.
Gordon Martin committed on 11/26/2017, 8:17:54 PMParent: ba518865234ed63cae50900c6ee8404aa6686cfa
Files changed
backlinks/obs_filter.js | added |
backlinks/obs_filter.js | |||
---|---|---|---|
@@ -1,0 +1,80 @@ | |||
1 … | +var nest = require('depnest') | ||
2 … | +var pull = require('pull-stream') | ||
3 … | +var MutantPullReduce = require('mutant-pull-reduce') | ||
4 … | +var Value = require('mutant/value') | ||
5 … | + | ||
6 … | +exports.needs = nest({ | ||
7 … | + 'sbot.pull.backlinks': 'first' | ||
8 … | +}) | ||
9 … | + | ||
10 … | +exports.gives = nest("backlinks.filter.obs", true) | ||
11 … | + | ||
12 … | +/** | ||
13 … | + * sbot.filter.obs returns an observable list of messages that link | ||
14 … | + * back to the message with the given message ID (@id). Only messages that | ||
15 … | + * pass the filter function are added to the list. | ||
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. | ||
20 … | + * | ||
21 … | + * A 'sync' observable property is also added to the returned observable | ||
22 … | + * which is 'true' when all previously seen messages are caught up with. | ||
23 … | + * | ||
24 … | + * Note: Unlike backlinks.obs.for this does not cache the observable for | ||
25 … | + * callers that supply the same arguments. | ||
26 … | + */ | ||
27 … | +exports.create = function(api) { | ||
28 … | + function pullFilterReduceObs(id, filterFn) { | ||
29 … | + if (!id || typeof(id) !== "string") { | ||
30 … | + throw new Error("id must be a string.") | ||
31 … | + } | ||
32 … | + | ||
33 … | + if (!filterFn || typeof(filterFn) !== "function") { | ||
34 … | + throw new Error("filterFn must be a function.") | ||
35 … | + } | ||
36 … | + | ||
37 … | + var sync = Value(false) | ||
38 … | + | ||
39 … | + var msgBacklinks = api.sbot.pull.backlinks({ | ||
40 … | + query: [{ | ||
41 … | + $filter: { | ||
42 … | + dest: id | ||
43 … | + } | ||
44 … | + }], | ||
45 … | + live: true | ||
46 … | + }) | ||
47 … | + | ||
48 … | + var filteredBacklinks = pull( | ||
49 … | + msgBacklinks, | ||
50 … | + pull( | ||
51 … | + pull.map(setSyncIfSyncMsg.bind(null, sync), | ||
52 … | + pull.filter(filterFn) | ||
53 … | + )) | ||
54 … | + ) | ||
55 … | + | ||
56 … | + var backlinksObs = MutantPullReduce(filteredBacklinks, (state, msg) => { | ||
57 … | + state.push(msg) | ||
58 … | + return state; | ||
59 … | + }, { | ||
60 … | + startValue: [], | ||
61 … | + nextTick: true | ||
62 … | + }) | ||
63 … | + | ||
64 … | + backlinksObs.sync = sync | ||
65 … | + | ||
66 … | + return backlinksObs; | ||
67 … | + } | ||
68 … | + | ||
69 … | + return nest({ | ||
70 … | + "backlinks.filter.obs": (id, filterFn) => pullFilterReduceObs(id, filterFn) | ||
71 … | + }) | ||
72 … | +} | ||
73 … | + | ||
74 … | +function setSyncIfSyncMsg(syncObs, msg) { | ||
75 … | + if (msg.sync) { | ||
76 … | + syncObs.set(true) | ||
77 … | + } | ||
78 … | + | ||
79 … | + return msg | ||
80 … | +} |
Built with git-ssb-web