Commit fd90a69c5a2931ed4ec735c57c4b2ebb42bcfe42
minimal chunking function
mix irving committed on 8/8/2017, 5:18:32 AMParent: 815a0b039bd2ce2cfe6eeec00539945cb5660fd1
Files changed
app/html/thread.js | changed |
app/html/thread.mcss | changed |
app/page/private.js | changed |
main.js | changed |
app/html/thread.js | ||
---|---|---|
@@ -1,7 +1,9 @@ | ||
1 | 1 | const nest = require('depnest') |
2 | -const { h } = require('mutant') | |
2 | +const { h, Array: MutantArray, map } = require('mutant') | |
3 | 3 | const pull = require('pull-stream') |
4 | +const last = require('lodash/last') | |
5 | +const get = require('lodash/last') | |
4 | 6 | |
5 | 7 | exports.gives = nest('app.html.thread') |
6 | 8 | |
7 | 9 | exports.needs = nest({ |
@@ -12,12 +14,57 @@ | ||
12 | 14 | return nest('app.html.thread', thread) |
13 | 15 | |
14 | 16 | function thread (source) { |
15 | 17 | // location here can expected to be: { page: 'home' } |
18 | + | |
19 | + const chunkedThread = buildChunkedThreadObs(source) | |
20 | + | |
16 | 21 | const { goTo } = api.app.sync |
22 | + const threadView = h('Thread', | |
23 | + map(chunkedThread, chunk => { | |
24 | + return h('div' , [ | |
25 | + h('div', '---'), | |
17 | 26 | |
18 | - return h('Thread', [ | |
19 | - 'thread content' | |
20 | - ]) | |
27 | + map(chunk, msg => { | |
28 | + return h('div', {style: { margin: '10px', background: 'white' }}, msg.value.content.text) // TODO (mix): use lodash/get | |
29 | + }) | |
30 | + ]) | |
31 | + }) | |
32 | + ) | |
33 | + | |
34 | + return threadView | |
21 | 35 | } |
22 | 36 | } |
23 | 37 | |
38 | +function buildChunkedThreadObs (source) { | |
39 | + var chunkedThread = MutantArray() | |
40 | + | |
41 | + var _chunk = null | |
42 | + var _lastMsg = null | |
43 | + | |
44 | + pull( | |
45 | + source, | |
46 | + pull.drain(msg => { | |
47 | + if (!_lastMsg || !isSameAuthor(_lastMsg, msg)) | |
48 | + createNewChunk(msg) | |
49 | + else | |
50 | + _chunk.push(msg) | |
51 | + | |
52 | + _lastMsg = msg | |
53 | + }) | |
54 | + ) | |
55 | + | |
56 | + function createNewChunk (msg) { | |
57 | + const newChunk = MutantArray() | |
58 | + newChunk.push(msg) | |
59 | + chunkedThread.push(newChunk) | |
60 | + _chunk = newChunk | |
61 | + } | |
62 | + | |
63 | + return chunkedThread | |
64 | +} | |
65 | + | |
66 | +function isSameAuthor (msgA, msgB) { | |
67 | + // TODO (mix) use lodash/get | |
68 | + return msgA.value.author === msgB.value.author | |
69 | +} | |
70 | + |
app/html/thread.mcss | ||
---|---|---|
@@ -1,4 +1,5 @@ | ||
1 | 1 | Thread { |
2 | - background-color: lightblue | |
2 | + background-color: lightgrey | |
3 | + padding: 1rem | |
3 | 4 | } |
4 | 5 |
app/page/private.js | ||
---|---|---|
@@ -1,7 +1,10 @@ | ||
1 | 1 | const nest = require('depnest') |
2 | 2 | const { h } = require('mutant') |
3 | 3 | |
4 | +const pull = require('pull-stream') | |
5 | +const dummyThread = require('../../test/fixtures/thread') | |
6 | + | |
4 | 7 | exports.gives = nest('app.page.private') |
5 | 8 | |
6 | 9 | exports.needs = nest({ |
7 | 10 | 'app.sync.goTo': 'first', |
@@ -14,9 +17,11 @@ | ||
14 | 17 | function private (location) { |
15 | 18 | // location here can expected to be an ssb-message |
16 | 19 | const { goTo } = api.app.sync |
17 | 20 | |
18 | - const thread = api.app.html.thread() | |
21 | + // TODO (mix) : swap for actual source, derived from location | |
22 | + const source = pull.values(dummyThread) | |
23 | + const thread = api.app.html.thread(source) | |
19 | 24 | |
20 | 25 | return h('div', [ |
21 | 26 | h('h1', 'Private message'), |
22 | 27 | h('div', { 'ev-click': () => goTo({ page: 'home' }) }, 'Home'), |
Built with git-ssb-web