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