Files: 82d1bb1396515575dc15b40dff81c0bb7ecf07b8 / overrides / patchcore / lib / timeAgo.js
880 bytesRaw
1 | const Value = require('mutant/value') |
2 | const computed = require('mutant/computed') |
3 | const nest = require('depnest') |
4 | const human = require('human-time') |
5 | |
6 | exports.gives = nest('lib.obs.timeAgo') |
7 | |
8 | exports.needs = nest({ |
9 | 'intl.sync.time': 'first' |
10 | }) |
11 | |
12 | exports.create = function (api) { |
13 | return nest('lib.obs.timeAgo', timeAgo) |
14 | |
15 | function timeAgo (timestamp) { |
16 | var timer |
17 | var value = Value(TimeIntl(timestamp)) |
18 | return computed([value], (a) => a, { |
19 | onListen: () => { |
20 | timer = setInterval(refresh, 30e3) |
21 | refresh() |
22 | }, |
23 | onUnlisten: () => { |
24 | clearInterval(timer) |
25 | } |
26 | }, { |
27 | idle: true |
28 | }) |
29 | |
30 | function refresh () { |
31 | value.set(TimeIntl(timestamp)) |
32 | } |
33 | |
34 | function TimeIntl (timestamp) { |
35 | return api.intl.sync.time(Time(timestamp)) |
36 | } |
37 | } |
38 | } |
39 | |
40 | function Time (timestamp) { |
41 | return human(new Date(timestamp)) |
42 | } |
43 |
Built with git-ssb-web