git ssb

10+

Matt McKegg / patchwork



Tree: 105aa7594db658635444cb37cc121c3952b8216b

Files: 105aa7594db658635444cb37cc121c3952b8216b / modules / app / html / progress-notifier.js

3273 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(replicateProgress)
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 (progress) {
68 var waiting = Value()
69 var lastTick = Date.now()
70
71 progress && progress(update)
72
73 pull(
74 api.sbot.pull.stream(sbot => sbot.patchwork.heartbeat()),
75 pull.drain(update)
76 )
77
78 setInterval(function () {
79 if (lastTick < Date.now() - 1000) {
80 waiting.set(true)
81 }
82 }, 1000)
83
84 return waiting
85
86 // scoped
87
88 function update () {
89 lastTick = Date.now()
90 waiting.set(false)
91 }
92 }
93}
94
95function clamp (value) {
96 return Math.min(1, Math.max(0, value)) || 0
97}
98
99function calcProgress (progress) {
100 var range = progress.target - progress.start
101 if (range) {
102 return (progress.current - progress.start) / range
103 } else {
104 return 1
105 }
106}
107

Built with git-ssb-web