git ssb

10+

Matt McKegg / patchwork



Tree: 7538a3793f7fab7519df35f780b48398c0a9bbef

Files: 7538a3793f7fab7519df35f780b48398c0a9bbef / modules / app / html / progress-notifier.js

2509 bytesRaw
1var {computed, when, h} = require('mutant')
2var nest = require('depnest')
3var sustained = require('../../../lib/sustained')
4
5exports.gives = nest('app.html.progressNotifier')
6
7exports.needs = nest({
8 'progress.html.render': 'first',
9 'progress.obs': {
10 indexes: 'first',
11 replicate: 'first',
12 migration: 'first'
13 }
14})
15
16exports.create = function (api) {
17 return nest('app.html.progressNotifier', function (id) {
18 var replicateProgress = api.progress.obs.replicate()
19 var indexes = api.progress.obs.indexes()
20 var migration = api.progress.obs.migration()
21
22 var pending = computed(indexes, (progress) => progress.target - progress.current || 0)
23 var pendingMigration = computed(migration, (progress) => progress.target - progress.current || 0)
24
25 var indexProgress = computed(indexes, calcProgress)
26 var migrationProgress = computed(migration, calcProgress)
27
28 var downloadProgress = computed([replicateProgress.feeds, replicateProgress.incompleteFeeds], (feeds, incomplete) => {
29 if (feeds) {
30 return clamp((feeds - incomplete) / feeds)
31 } else {
32 return 1
33 }
34 })
35
36 var hidden = sustained(computed([replicateProgress.incompleteFeeds, pending, pendingMigration], (incomplete, pending, pendingMigration) => {
37 return incomplete < 5 && !pending && !pendingMigration
38 }), 2000)
39
40 // HACK: css animations take up WAY TO MUCH cpu, remove from dom when inactive
41 var displaying = computed(sustained(hidden, 500, x => !x), hidden => !hidden)
42
43 return h('div.info', { hidden }, [
44 h('div.status', [
45 when(displaying, h('Loading -small', [
46 when(pendingMigration,
47 ['Upgrading database', h('progress', { style: {'margin-left': '10px'}, min: 0, max: 1, value: migrationProgress })],
48 when(computed(replicateProgress.incompleteFeeds, (v) => v > 5),
49 ['Downloading new messages', h('progress', { style: {'margin-left': '10px'}, min: 0, max: 1, value: downloadProgress })],
50 when(pending, [
51 ['Indexing database', h('progress', { style: {'margin-left': '10px'}, min: 0, max: 1, value: indexProgress })]
52 ], 'Scuttling...')
53 )
54 )
55 ]))
56 ])
57 ])
58 })
59}
60
61function clamp (value) {
62 return Math.min(1, Math.max(0, value)) || 0
63}
64
65function calcProgress (progress) {
66 var range = progress.target - progress.start
67 if (range) {
68 return (progress.current - progress.start) / range
69 } else {
70 return 1
71 }
72}
73

Built with git-ssb-web