Files: d06c60e3b13b5795e7acad4aff8901f5039efb83 / lib / timeAgo.js
758 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.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 | |
30 | function Time (timestamp) { |
31 | return human(new Date(timestamp)) |
32 | .replace(/minute/, 'min') |
33 | .replace(/second/, 'sec') |
34 | } |
35 |
Built with git-ssb-web