git ssb

10+

Matt McKegg / patchwork



Tree: c173c2b41b860fbb9c38b6c51a466f9652d0461f

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

3483 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 incompleteFeedsFrom = 0
34
35 var downloadProgress = computed([replicateProgress.feeds, replicateProgress.incompleteFeeds], (feeds, incomplete) => {
36 if (incomplete > incompleteFeedsFrom) {
37 incompleteFeedsFrom = incomplete
38 } else if (incomplete === 0) {
39 incompleteFeedsFrom = 0
40 }
41 if (feeds && incomplete) {
42 return clamp((feeds - incomplete) / incompleteFeedsFrom)
43 } else {
44 return 1
45 }
46 })
47
48 var hidden = sustained(computed([waiting, downloadProgress, pending, pendingMigration], (waiting, downloadProgress, pending, pendingMigration) => {
49 return !waiting && downloadProgress === 1 && !pending && !pendingMigration
50 }), 500)
51
52 // HACK: css animations take up WAY TO MUCH cpu, remove from dom when inactive
53 var displaying = computed(sustained(hidden, 500, x => !x), hidden => !hidden)
54
55 return h('div.info', { hidden }, [
56 h('div.status', [
57 when(displaying, h('Loading -small', [
58 when(pendingMigration,
59 [h('span.info', i18n('Upgrading database')), h('progress', { style: {'margin-left': '10px'}, min: 0, max: 1, value: migrationProgress })],
60 when(computed(downloadProgress, (v) => v < 1),
61 [h('span.info', i18n('Downloading new messages')), h('progress', { style: {'margin-left': '10px'}, min: 0, max: 1, value: downloadProgress })],
62 when(pending, [
63 [h('span.info', i18n('Indexing database')), h('progress', { style: {'margin-left': '10px'}, min: 0, max: 1, value: indexProgress })]
64 ], i18n('Scuttling...'))
65 )
66 )
67 ]))
68 ])
69 ])
70 })
71
72 // scoped
73
74 function Waiting (progress) {
75 var waiting = Value()
76 var lastTick = Date.now()
77
78 progress && progress(update)
79
80 pull(
81 api.sbot.pull.stream(sbot => sbot.patchwork.heartbeat()),
82 pull.drain(update)
83 )
84
85 setInterval(function () {
86 if (lastTick < Date.now() - 1000) {
87 waiting.set(true)
88 }
89 }, 1000)
90
91 return waiting
92
93 // scoped
94
95 function update () {
96 lastTick = Date.now()
97 waiting.set(false)
98 }
99 }
100}
101
102function clamp (value) {
103 return Math.min(1, Math.max(0, value)) || 0
104}
105
106function calcProgress (progress) {
107 var range = progress.target - progress.start
108 if (range) {
109 return (progress.current - progress.start) / range
110 } else {
111 return 1
112 }
113}
114

Built with git-ssb-web