git ssb

7+

dinoworm ๐Ÿ› / patchcore



Tree: 8261134eb7daa7f16ad7f83ebb1549e60b37b70d

Files: 8261134eb7daa7f16ad7f83ebb1549e60b37b70d / observables / timeAgo.js

754 bytesRaw
1const Value = require('mutant/value')
2const computed = require('mutant/computed')
3const nest = require('depnest')
4const human = require('human-time')
5
6exports.gives = nest('obs.timeAgo')
7
8exports.create = function (api) {
9 return nest('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