Commit b58dd46b8f88f0917c480faf8286bdd209933739
add timestamp using an observable
wooo!Michael Williams committed on 2/13/2017, 9:24:28 AM
Parent: 692e5b60efe4cf58df0598ab494c553c8910a017
Files changed
components/message/timestamp.js | changed |
observables/timeAgo.js | added |
package.json | changed |
plugs/message_layout/default.js | changed |
plugs/message_layout/mini.js | changed |
components/message/timestamp.js | ||
---|---|---|
@@ -1,16 +1,17 @@ | ||
1 | 1 … | const h = require('mutant/h') |
2 … | +const nest = require('depnest') | |
2 | 3 … | |
3 | -exports.gives = { | |
4 | - message_components: true | |
5 | -} | |
4 … | +exports.gives = nest('message.timestamp') | |
5 … | +exports.needs = nest('obs.timeAgo', 'first') | |
6 | 6 … | |
7 | 7 … | exports.create = function (api) { |
8 | - return { | |
9 | - message_components | |
10 | - } | |
8 … | + return nest('message.timestamp', timestamp) | |
11 | 9 … | |
12 | - function message_components (msg) { | |
13 | - return h('div', new Date(msg.value.timestamp).toString()) | |
10 … | + function timestamp (msg) { | |
11 … | + return h('a.Timestamp', { | |
12 … | + href: msg.key, | |
13 … | + title: new Date(msg.value.timestamp) | |
14 … | + }, api.obs.timeAgo(msg.value.timestamp)) | |
14 | 15 … | } |
15 | 16 … | } |
16 | 17 … |
observables/timeAgo.js | ||
---|---|---|
@@ -1,0 +1,34 @@ | ||
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 … | +} |
package.json | ||
---|---|---|
@@ -25,10 +25,12 @@ | ||
25 | 25 … | "homepage": "https://github.com/ssbc/patchcore#readme", |
26 | 26 … | "dependencies": { |
27 | 27 … | "bulk-require": "^1.0.0", |
28 | 28 … | "bulkify": "^1.4.2", |
29 … | + "depnest": "^1.0.2", | |
29 | 30 … | "emoji-named-characters": "^1.0.2", |
30 | 31 … | "es2040": "^1.2.4", |
32 … | + "human-time": "0.0.1", | |
31 | 33 … | "mutant": "^3.13.2", |
32 | 34 … | "pull-abortable": "^4.1.0", |
33 | 35 … | "pull-reconnect": "0.0.3", |
34 | 36 … | "pull-stream": "^3.5.0", |
plugs/message_layout/default.js | ||
---|---|---|
@@ -3,9 +3,12 @@ | ||
3 | 3 … | exports.needs = { |
4 | 4 … | message_backlinks: 'first', |
5 | 5 … | message_author: 'first', |
6 | 6 … | message_meta: 'map', |
7 | - message_action: 'map' | |
7 … | + message_action: 'map', | |
8 … | + message: { | |
9 … | + timestamp: 'first' | |
10 … | + } | |
8 | 11 … | } |
9 | 12 … | |
10 | 13 … | exports.gives = { |
11 | 14 … | message_layout: true |
@@ -21,8 +24,9 @@ | ||
21 | 24 … | return h('div', { |
22 | 25 … | classList: 'Message' |
23 | 26 … | }, [ |
24 | 27 … | h('header.author', {}, api.message_author(msg)), |
28 … | + h('section.timestamp', {}, api.message.timestamp(msg)), | |
25 | 29 … | h('section.title', {}, opts.title), |
26 | 30 … | h('section.meta', {}, api.message_meta(msg)), |
27 | 31 … | h('section.content', {}, opts.content), |
28 | 32 … | h('section.raw-content'), |
plugs/message_layout/mini.js | ||
---|---|---|
@@ -2,9 +2,12 @@ | ||
2 | 2 … | |
3 | 3 … | exports.needs = { |
4 | 4 … | message_backlinks: 'first', |
5 | 5 … | message_author: 'first', |
6 | - message_meta: 'map' | |
6 … | + message_meta: 'map', | |
7 … | + message: { | |
8 … | + timestamp: 'first' | |
9 … | + } | |
7 | 10 … | } |
8 | 11 … | |
9 | 12 … | exports.gives = { |
10 | 13 … | message_layout: true |
@@ -20,8 +23,9 @@ | ||
20 | 23 … | return h('div', { |
21 | 24 … | classList: 'Message -mini' |
22 | 25 … | }, [ |
23 | 26 … | h('header.author', {}, api.message_author(msg, { size: 'mini' })), |
27 … | + h('section.timestamp', {}, api.message.timestamp(msg)), | |
24 | 28 … | h('section.meta', {}, api.message_meta(msg)), |
25 | 29 … | h('section.content', {}, opts.content), |
26 | 30 … | h('section.raw-content') |
27 | 31 … | ]) |
Built with git-ssb-web