git ssb

7+

dinoworm ๐Ÿ› / patchcore



Tree: 73ce52087fc4eb16ae6fef1818817c6f2fcd5a52

Files: 73ce52087fc4eb16ae6fef1818817c6f2fcd5a52 / lib / timeAgo.js

758 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, 5e3)
17 refresh()
18 },
19 onUnlisten: () => {
20 clearInterval(timer)
21 }
22 })
23
24 function refresh () {
25 value.set(Time(timestamp))
26 }
27 }
28}
29
30function Time (timestamp) {
31 return human(new Date(timestamp))
32 .replace(/minute/, 'min')
33 .replace(/second/, 'sec')
34}
35

Built with git-ssb-web