git ssb

0+

alanz / patchwork



forked from Matt McKegg / patchwork

Tree: 45eb38ea158546001d22126e5b308ea80452483a

Files: 45eb38ea158546001d22126e5b308ea80452483a / modules / progress / obs.js

1558 bytesRaw
1var nest = require('depnest')
2var pull = require('pull-stream')
3var {Struct, Dict, computed} = require('mutant')
4
5exports.gives = nest({
6 'progress.obs': ['global', 'peer', 'query']
7})
8
9exports.needs = nest({
10 'sbot.pull.replicateProgress': 'first',
11 'sbot.pull.queryProgress': 'first'
12})
13
14exports.create = function (api) {
15 var syncStatus = null
16 var queryProgress = null
17
18 return nest({
19 'progress.obs': {global, peer, query}
20 })
21
22 function global () {
23 load()
24 return syncStatus
25 }
26
27 function peer (id) {
28 load()
29 var result = computed(syncStatus, (status) => {
30 return status.pendingPeers[id] || 0
31 })
32 return result
33 }
34
35 function query () {
36 if (!queryProgress) {
37 queryProgress = Struct({
38 pending: 0
39 })
40
41 pull(
42 api.sbot.pull.queryProgress(),
43 pull.drain((event) => {
44 queryProgress.set(event)
45 })
46 )
47 }
48 return queryProgress
49 }
50
51 function load () {
52 if (!syncStatus) {
53 syncStatus = Struct({
54 incomplete: 0,
55 pendingCount: 0,
56 pendingPeers: Dict({}, {fixedIndexing: true}),
57 feeds: null,
58 rate: 0
59 })
60
61 pull(
62 api.sbot.pull.replicateProgress(),
63 pull.drain((event) => {
64 if (!event.sync) {
65 syncStatus.set(event)
66 }
67 })
68 )
69 }
70 }
71}
72
73function Peer (id) {
74 return Struct({
75 type: 'peer',
76 id: id,
77 pending: 0
78 })
79}
80
81function Feed (id) {
82 return Struct({
83 type: 'feed',
84 id: id,
85 available: 0,
86 local: 0
87 })
88}
89

Built with git-ssb-web