modules/app/html/progress-notifier.jsView |
---|
1 | | -var {computed, when, h} = require('mutant') |
| 1 | +var {computed, when, h, Value} = require('mutant') |
2 | 2 | var nest = require('depnest') |
3 | 3 | var sustained = require('../../../lib/sustained') |
| 4 | +var pull = require('pull-stream') |
4 | 5 | |
5 | 6 | exports.gives = nest('app.html.progressNotifier') |
6 | 7 | |
7 | 8 | exports.needs = nest({ |
| 9 | + 'sbot.pull.stream': 'first', |
8 | 10 | 'progress.html.render': 'first', |
9 | 11 | 'progress.obs': { |
10 | 12 | indexes: 'first', |
11 | 13 | replicate: 'first', |
17 | 19 | return nest('app.html.progressNotifier', function (id) { |
18 | 20 | var replicateProgress = api.progress.obs.replicate() |
19 | 21 | var indexes = api.progress.obs.indexes() |
20 | 22 | var migration = api.progress.obs.migration() |
| 23 | + var waiting = Waiting() |
21 | 24 | |
22 | 25 | var pending = computed(indexes, (progress) => progress.target - progress.current || 0) |
23 | 26 | var pendingMigration = computed(migration, (progress) => progress.target - progress.current || 0) |
24 | 27 | |
32 | 35 | return 1 |
33 | 36 | } |
34 | 37 | }) |
35 | 38 | |
36 | | - var hidden = sustained(computed([replicateProgress.incompleteFeeds, pending, pendingMigration], (incomplete, pending, pendingMigration) => { |
37 | | - return incomplete < 5 && !pending && !pendingMigration |
| 39 | + var hidden = sustained(computed([waiting, replicateProgress.incompleteFeeds, pending, pendingMigration], (waiting, incomplete, pending, pendingMigration) => { |
| 40 | + return !waiting && incomplete < 5 && !pending && !pendingMigration |
38 | 41 | }), 2000) |
39 | 42 | |
40 | 43 | |
41 | 44 | var displaying = computed(sustained(hidden, 500, x => !x), hidden => !hidden) |
42 | 45 | |
43 | 46 | return h('div.info', { hidden }, [ |
44 | 47 | h('div.status', [ |
45 | 48 | when(displaying, h('Loading -small', [ |
46 | | - when(pendingMigration, |
47 | | - [h('span.info', 'Upgrading database'), h('progress', { style: {'margin-left': '10px'}, min: 0, max: 1, value: migrationProgress })], |
48 | | - when(computed(replicateProgress.incompleteFeeds, (v) => v > 5), |
49 | | - [h('span.info', 'Downloading new messages'), h('progress', { style: {'margin-left': '10px'}, min: 0, max: 1, value: downloadProgress })], |
50 | | - when(pending, [ |
51 | | - [h('span.info', 'Indexing database'), h('progress', { style: {'margin-left': '10px'}, min: 0, max: 1, value: indexProgress })] |
52 | | - ], 'Scuttling...') |
| 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 | + ) |
53 | 58 | ) |
54 | 59 | ) |
55 | 60 | ])) |
56 | 61 | ]) |
57 | 62 | ]) |
58 | 63 | }) |
| 64 | + |
| 65 | + |
| 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 | + } |
59 | 87 | } |
60 | 88 | |
61 | 89 | function clamp (value) { |
62 | 90 | return Math.min(1, Math.max(0, value)) || 0 |