Files: 6e8bb78d907b33bbdb544951cbaa4d6120d2958b / modules / progress / obs.js
1797 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 |
69 | try { |
70 | source = keyFn(sbot) |
71 | } catch (err) { |
72 | progress.set(err) |
73 | return progress |
74 | } |
75 | if (source) { |
76 | pull( |
77 | source(), |
78 | pull.drain((event) => { |
79 | progress.set(event) |
80 | }) |
81 | ) |
82 | } |
83 | } |
84 | }) |
85 | |
86 | return progress |
87 | } |
88 | } |
89 | |
90 |
Built with git-ssb-web