Files: 8261134eb7daa7f16ad7f83ebb1549e60b37b70d / observables / timeAgo.js
754 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('obs.timeAgo') |
7 | |
8 | exports.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 | |
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