Files: 55335207126eb15c3a0f1834ffb8bbcbd32658a8 / app / html / connection.js
1802 bytesRaw
1 | const nest = require('depnest') |
2 | const { h, Value, computed } = require('mutant') |
3 | const pull = require('pull-stream') |
4 | |
5 | exports.gives = nest('app.html.connection') |
6 | |
7 | exports.needs = nest({ |
8 | 'app.html.menuItem': 'map', |
9 | 'app.sync.goTo': 'first', |
10 | 'sbot.obs.connection': 'first', |
11 | 'sbot.obs.localPeers': 'first', |
12 | 'sbot.pull.stream': 'first' |
13 | }) |
14 | |
15 | exports.create = function (api) { |
16 | var _connection |
17 | |
18 | return nest('app.html.connection', function connectionStatus () { |
19 | if (_connection) return _connection |
20 | |
21 | const newMessageClass = NewMessageClass(api) |
22 | const { connection, localPeers } = api.sbot.obs |
23 | |
24 | return h('Connection', computed([connection, localPeers()], (connection, localPeers) => { |
25 | if (localPeers.length) { |
26 | return h('i.fa.fa-user-circle-o', { |
27 | title: 'peers on your local network!', |
28 | 'ev-click': () => api.app.sync.goTo('/network'), |
29 | className: newMessageClass |
30 | }) |
31 | } |
32 | |
33 | if (connection) return h('i.fa.fa-circle-thin', { className: newMessageClass }) |
34 | |
35 | return h('i.fa.fa-exclamation-circle', { |
36 | title: 'something has gone wrong, try restarting patchbay' |
37 | }) |
38 | })) |
39 | }) |
40 | } |
41 | |
42 | function NewMessageClass (api) { |
43 | var _class = Value('') |
44 | var timeOut |
45 | |
46 | pull( |
47 | api.sbot.pull.stream(sbot => { |
48 | const query = [{ |
49 | $filter: { |
50 | timestamp: { $gt: 0 } |
51 | } |
52 | }, { |
53 | $map: { |
54 | author: ['value', 'author'] |
55 | } |
56 | }] |
57 | return sbot.query.read({ live: true, old: false, query }) |
58 | }), |
59 | // pull.filter(a => a !== myKey), // could filter out my own messages |
60 | pull.drain(m => { |
61 | if (timeOut) return |
62 | |
63 | _class.set('-newMsg') |
64 | timeOut = setTimeout(() => { |
65 | _class.set('') |
66 | timeOut = null |
67 | }, 100) |
68 | }) |
69 | ) |
70 | |
71 | return _class |
72 | } |
73 |
Built with git-ssb-web