git ssb

0+

alanz / patchwork



forked from Matt McKegg / patchwork

Tree: b1d9327f353283fcc3fb10ebbe3cdbbaf2feddea

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

3279 bytesRaw
1var {computed, when, h, Value} = require('mutant')
2var nest = require('depnest')
3var sustained = require('../../../lib/sustained')
4const 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 'intl.sync.i18n': 'first'
17})
18
19exports.create = function (api) {
20 const i18n = api.intl.sync.i18n
21 return nest('app.html.progressNotifier', function (id) {
22 var replicateProgress = api.progress.obs.replicate()
23 var indexes = api.progress.obs.indexes()
24 var migration = api.progress.obs.migration()
25 var waiting = Waiting(replicateProgress)
26
27 var pending = computed(indexes, (progress) => progress.target - progress.current || 0)
28 var pendingMigration = computed(migration, (progress) => progress.target - progress.current || 0)
29
30 var indexProgress = computed(indexes, calcProgress)
31 var migrationProgress = computed(migration, calcProgress)
32
33 var downloadProgress = computed([replicateProgress.feeds, replicateProgress.incompleteFeeds], (feeds, incomplete) => {
34 if (feeds) {
35 return clamp((feeds - incomplete) / feeds)
36 } else {
37 return 1
38 }
39 })
40
41 var hidden = sustained(computed([waiting, replicateProgress.incompleteFeeds, pending, pendingMigration], (waiting, incomplete, pending, pendingMigration) => {
42 return !waiting && incomplete < 5 && !pending && !pendingMigration
43 }), 2000)
44
45 // HACK: css animations take up WAY TO MUCH cpu, remove from dom when inactive
46 var displaying = computed(sustained(hidden, 500, x => !x), hidden => !hidden)
47
48 return h('div.info', { hidden }, [
49 h('div.status', [
50 when(displaying, h('Loading -small', [
51 when(pendingMigration,
52 [h('span.info', i18n('Upgrading database')), h('progress', { style: {'margin-left': '10px'}, min: 0, max: 1, value: migrationProgress })],
53 when(computed(replicateProgress.incompleteFeeds, (v) => v > 5),
54 [h('span.info', i18n('Downloading new messages')), h('progress', { style: {'margin-left': '10px'}, min: 0, max: 1, value: downloadProgress })],
55 when(pending, [
56 [h('span.info', i18n('Indexing database')), h('progress', { style: {'margin-left': '10px'}, min: 0, max: 1, value: indexProgress })]
57 ], i18n('Scuttling...'))
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