Files: 05e5e97a80eb654ea1c48fa695db2c863c34a3c5 / research / 1_nested_callbacks.js
2902 bytesRaw
1 | const pull = require('pull-stream') |
2 | const Client = require('ssb-client') |
3 | const config = require('./config') |
4 | |
5 | Client(config.keys, config, (err, server) => { |
6 | if (err) throw err |
7 | |
8 | const myKey = server.id |
9 | |
10 | const startTime = new Date() |
11 | |
12 | // a) get all my `about` messages where I say something about attending |
13 | pull( |
14 | attendanceStreamForFeed(myKey, server), |
15 | pull.collect((err, attendingData) => { |
16 | console.log('a) time:', (new Date() - startTime) / 1000, 's') |
17 | |
18 | const attending = attendingData.reduce((soFar, item) => { |
19 | // b) only keep the ones where the last thing I said about it was that I was attending (remove === null) |
20 | if (last(item.rm) === null) soFar.push(item.root) |
21 | |
22 | return soFar |
23 | }, []) |
24 | console.log('b) time:', (new Date() - startTime) / 1000, 's') |
25 | |
26 | // c) get any gathering which matches one of those things I'm attending |
27 | pull( |
28 | gatheringStreamFilteredByKeys(attending, server), |
29 | pull.collect((err, gatherings) => { |
30 | console.log('c) time:', (new Date() - startTime) / 1000, 's') |
31 | |
32 | // console.log(gatherings) |
33 | |
34 | server.close() |
35 | }) |
36 | ) |
37 | }) |
38 | ) |
39 | }) |
40 | |
41 | function attendanceStreamForFeed (feedId, server) { |
42 | const attendanceQuery = { |
43 | query: [ |
44 | { |
45 | $filter: { |
46 | value: { |
47 | author: feedId, // i authored the message |
48 | content: { |
49 | type: 'about', |
50 | about: { $prefix: '%' }, // it's referencing a message |
51 | attendee: { |
52 | link: feedId // i'm asserting something about myself |
53 | } |
54 | } |
55 | } |
56 | } |
57 | }, |
58 | { |
59 | $map: { |
60 | root: ['value', 'content', 'about'], // could be a gathering |
61 | remove: ['value', 'content', 'attendee', 'remove'], // not attending |
62 | ts: ['value', 'timestamp'] // timestamp |
63 | } |
64 | }, |
65 | { |
66 | $reduce: { |
67 | root: 'root', |
68 | rm: { $collect: 'remove' } |
69 | } |
70 | } |
71 | ] |
72 | } |
73 | |
74 | // server.query.explain(attendanceQuery, (err, a) => console.log('Resolved level query (reveals index in use)', a)) |
75 | |
76 | return server.query.read(attendanceQuery) |
77 | } |
78 | |
79 | function gatheringStreamFilteredByKeys (keys, server) { |
80 | const gatheringQuery = { |
81 | query: [ |
82 | { |
83 | $filter: { |
84 | // key: {$in: keys}, // needs a new map-filter-reduce |
85 | value: { |
86 | timestamp: { $gt: 0 }, // this forces an index which is ordered by published ts |
87 | content: { type: 'gathering' } |
88 | } |
89 | } |
90 | } |
91 | ] |
92 | } |
93 | |
94 | // server.query.explain(gatheringQuery, (err, a) => console.log('Resolved level query (reveals index in use)', a)) |
95 | |
96 | // return server.query.read(gatheringQuery) |
97 | return pull( |
98 | server.query.read(gatheringQuery), |
99 | pull.filter(m => keys.includes(m.key)) |
100 | ) |
101 | } |
102 | |
103 | function last (arr) { |
104 | if (!arr.length) return |
105 | |
106 | return arr[arr.length - 1] |
107 | } |
108 |
Built with git-ssb-web