git ssb

10+

Matt McKegg / patchwork



Tree: 1c01c513817f4cf52a799f36a80e655fabc71c66

Files: 1c01c513817f4cf52a799f36a80e655fabc71c66 / overrides / patchcore / lib / timeAgo.js

880 bytesRaw
1const Value = require('mutant/value')
2const computed = require('mutant/computed')
3const nest = require('depnest')
4const human = require('human-time')
5
6exports.gives = nest('lib.obs.timeAgo')
7
8exports.needs = nest({
9 'intl.sync.time': 'first'
10})
11
12exports.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
40function Time (timestamp) {
41 return human(new Date(timestamp))
42}
43

Built with git-ssb-web