git ssb

7+

dinoworm ๐Ÿ› / patchcore



Tree: e0439586cabe63fc84e88ff400edb18470cdec52

Files: e0439586cabe63fc84e88ff400edb18470cdec52 / lib / timeAgo.js

785 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.create = function (api) {
9 return nest('lib.obs.timeAgo', timeAgo)
10
11 function timeAgo (timestamp) {
12 var timer
13 var value = Value(Time(timestamp))
14 return computed([value], (a) => a, {
15 onListen: () => {
16 timer = setInterval(refresh, 30e3)
17 refresh()
18 },
19 onUnlisten: () => {
20 clearInterval(timer)
21 }
22 }, {
23 idle: true
24 })
25
26 function refresh () {
27 value.set(Time(timestamp))
28 }
29 }
30}
31
32function Time (timestamp) {
33 return human(new Date(timestamp))
34 .replace(/minute/, 'min')
35 .replace(/second/, 'sec')
36}
37

Built with git-ssb-web