Files: 87a18d60fcaae01360adeb9e6283861b864b5cef / modules / progress / obs.js
1677 bytesRaw
1 | var nest = require('depnest') |
2 | var pull = require('pull-stream') |
3 | var {Struct, Dict, computed, watch} = require('mutant') |
4 | |
5 | exports.gives = nest({ |
6 | 'progress.obs': [ |
7 | 'global', |
8 | 'peer', |
9 | 'query', |
10 | 'private'] |
11 | }) |
12 | |
13 | exports.needs = nest({ |
14 | 'sbot.obs.connection': 'first' |
15 | }) |
16 | |
17 | exports.create = function (api) { |
18 | var syncStatus = null |
19 | var queryProgress = null |
20 | var privateProgress = null |
21 | |
22 | return nest({ |
23 | 'progress.obs': { |
24 | global () { |
25 | load() |
26 | return syncStatus |
27 | }, |
28 | peer (id) { |
29 | load() |
30 | var result = computed(syncStatus, (status) => { |
31 | return status.pendingPeers[id] || 0 |
32 | }) |
33 | return result |
34 | }, |
35 | query () { |
36 | if (!queryProgress) { |
37 | queryProgress = ProgressStatus(x => x.query.progress) |
38 | } |
39 | return queryProgress |
40 | }, |
41 | private () { |
42 | if (!privateProgress) { |
43 | privateProgress = ProgressStatus(x => x.private.progress) |
44 | } |
45 | return privateProgress |
46 | } |
47 | } |
48 | }) |
49 | |
50 | function load () { |
51 | if (!syncStatus) { |
52 | syncStatus = ProgressStatus(x => x.replicate.changes, { |
53 | incompleteFeeds: 0, |
54 | pendingPeers: Dict({}, {fixedIndexing: true}), |
55 | feeds: null, |
56 | rate: 0 |
57 | }) |
58 | } |
59 | } |
60 | |
61 | function ProgressStatus (keyFn, attrs) { |
62 | var progress = Struct(attrs || { |
63 | pending: 0 |
64 | }) |
65 | |
66 | watch(api.sbot.obs.connection, (sbot) => { |
67 | if (sbot) { |
68 | var source = keyFn(sbot) |
69 | if (source) { |
70 | pull( |
71 | source(), |
72 | pull.drain((event) => { |
73 | progress.set(event) |
74 | }) |
75 | ) |
76 | } |
77 | } |
78 | }) |
79 | |
80 | return progress |
81 | } |
82 | } |
83 |
Built with git-ssb-web