git ssb

0+

alanz / patchwork



forked from Matt McKegg / patchwork

Tree: fa872384db2701c4af5615677115890b31d29e78

Files: fa872384db2701c4af5615677115890b31d29e78 / modules / app / html / progress-notifier.js

3184 bytesRaw
1var {computed, when, h, Value} = require('mutant')
2var nest = require('depnest')
3var sustained = require('../../../lib/sustained')
4var pull = require('pull-stream')
5
6exports.gives = nest('app.html.progressNotifier')
7
8exports.needs = nest({
9 'sbot.pull.stream': 'first',
10 'progress.html.render': 'first',
11 'progress.obs': {
12 indexes: 'first',
13 replicate: 'first',
14 migration: 'first'
15 }
16})
17
18exports.create = function (api) {
19 return nest('app.html.progressNotifier', function (id) {
20 var replicateProgress = api.progress.obs.replicate()
21 var indexes = api.progress.obs.indexes()
22 var migration = api.progress.obs.migration()
23 var waiting = Waiting()
24
25 var pending = computed(indexes, (progress) => progress.target - progress.current || 0)
26 var pendingMigration = computed(migration, (progress) => progress.target - progress.current || 0)
27
28 var indexProgress = computed(indexes, calcProgress)
29 var migrationProgress = computed(migration, calcProgress)
30
31 var downloadProgress = computed([replicateProgress.feeds, replicateProgress.incompleteFeeds], (feeds, incomplete) => {
32 if (feeds) {
33 return clamp((feeds - incomplete) / feeds)
34 } else {
35 return 1
36 }
37 })
38
39 var hidden = sustained(computed([waiting, replicateProgress.incompleteFeeds, pending, pendingMigration], (waiting, incomplete, pending, pendingMigration) => {
40 return !waiting && incomplete < 5 && !pending && !pendingMigration
41 }), 2000)
42
43 // HACK: css animations take up WAY TO MUCH cpu, remove from dom when inactive
44 var displaying = computed(sustained(hidden, 500, x => !x), hidden => !hidden)
45
46 return h('div.info', { hidden }, [
47 h('div.status', [
48 when(displaying, h('Loading -small', [
49 when(waiting, 'Waiting for Scuttlebot...',
50 when(pendingMigration,
51 [h('span.info', 'Upgrading database'), h('progress', { style: {'margin-left': '10px'}, min: 0, max: 1, value: migrationProgress })],
52 when(computed(replicateProgress.incompleteFeeds, (v) => v > 5),
53 [h('span.info', 'Downloading new messages'), h('progress', { style: {'margin-left': '10px'}, min: 0, max: 1, value: downloadProgress })],
54 when(pending, [
55 [h('span.info', 'Indexing database'), h('progress', { style: {'margin-left': '10px'}, min: 0, max: 1, value: indexProgress })]
56 ], 'Scuttling...')
57 )
58 )
59 )
60 ]))
61 ])
62 ])
63 })
64
65 // scoped
66
67 function Waiting () {
68 var waiting = Value()
69 var lastTick = Date.now()
70
71 pull(
72 api.sbot.pull.stream(sbot => sbot.patchwork.heartbeat()),
73 pull.drain((tick) => {
74 lastTick = Date.now()
75 waiting.set(false)
76 })
77 )
78
79 setInterval(function () {
80 if (lastTick < Date.now() - 1000) {
81 waiting.set(true)
82 }
83 }, 1000)
84
85 return waiting
86 }
87}
88
89function clamp (value) {
90 return Math.min(1, Math.max(0, value)) || 0
91}
92
93function calcProgress (progress) {
94 var range = progress.target - progress.start
95 if (range) {
96 return (progress.current - progress.start) / range
97 } else {
98 return 1
99 }
100}
101

Built with git-ssb-web