Commit 4affbf9d002205c0d8fe516be4f046bf898ea555
store threads in an observable, which is also persisted!
Dominic Tarr committed on 8/11/2017, 7:33:08 AMParent: 695b85ae4b52e80b590f126de17de110bc7b7aae
Files changed
app/page/home.js | changed |
app/page/home.js | ||
---|---|---|
@@ -5,15 +5,17 @@ | ||
5 | 5 | const isObject = require('lodash/isObject') |
6 | 6 | const isString = require('lodash/isString') |
7 | 7 | const last = require('lodash/last') |
8 | 8 | const get = require('lodash/get') |
9 | - | |
9 | +const More = require('hypermore') | |
10 | 10 | exports.gives = nest('app.page.home') |
11 | +const morphdom = require('morphdom') | |
12 | +const Next = require('pull-next') | |
11 | 13 | |
12 | 14 | exports.needs = nest({ |
13 | 15 | 'about.html.image': 'first', |
14 | 16 | 'app.html.nav': 'first', |
15 | - 'feed.pull.public': 'first', | |
17 | + 'sbot.pull.log': 'first', | |
16 | 18 | 'history.sync.push': 'first', |
17 | 19 | 'message.sync.unbox': 'first', |
18 | 20 | }) |
19 | 21 | |
@@ -23,11 +25,9 @@ | ||
23 | 25 | return text.split('\n')[0].substring(0, 80) |
24 | 26 | } |
25 | 27 | |
26 | 28 | exports.create = (api) => { |
27 | - return nest('app.page.home', home) | |
28 | - | |
29 | - function home (location) { | |
29 | + return nest('app.page.home', function (location) { | |
30 | 30 | // location here can expected to be: { page: 'home' } |
31 | 31 | |
32 | 32 | var container = h('div.container', []) |
33 | 33 | |
@@ -54,9 +54,9 @@ | ||
54 | 54 | function threadGroup (threads, obj, toName) { |
55 | 55 | // threads = a state object for all the types of threads |
56 | 56 | // obj = a map of keys to root ids, where key ∈ (channel | group | concatenated list of pubkeys) |
57 | 57 | // toName = fn that derives a name from a particular thread |
58 | - | |
58 | + | |
59 | 59 | var groupEl = h('div.group') |
60 | 60 | for(var k in obj) { |
61 | 61 | var id = obj[k] |
62 | 62 | var thread = get(threads, ['roots', id]) |
@@ -67,46 +67,80 @@ | ||
67 | 67 | } |
68 | 68 | return groupEl |
69 | 69 | } |
70 | 70 | |
71 | - pull( | |
72 | - api.feed.pull.public({reverse: true, limit: 1000}), | |
73 | - pull.collect(function (err, messages) { | |
71 | + var initial | |
72 | + try { initial = JSON.parse(localStorage.threadsState) } | |
73 | + catch (_) { } | |
74 | + var lastTimestamp = initial ? initial.last : Date.now() | |
74 | 75 | |
75 | - var threads = messages | |
76 | - .map(function (data) { | |
77 | - if(isObject(data.value.content)) return data | |
78 | - return api.message.sync.unbox(data) | |
79 | - }) | |
80 | - .filter(Boolean) | |
81 | - .reduce(threadReduce, null) | |
76 | + var timer | |
77 | + function update (threadsState) { | |
78 | + clearTimeout(timer) | |
79 | + setTimeout(function () { | |
80 | + threadsState.last = lastTimestamp | |
81 | + localStorage.threadsState = JSON.stringify(threadsState) | |
82 | + }, 1000) | |
83 | + } | |
82 | 84 | |
83 | - container.appendChild(threadGroup( | |
84 | - threads, | |
85 | - threads.private, | |
86 | - function (_, msg) { | |
87 | - // NB: msg passed in is actually a 'thread', but only care about root msg | |
88 | - | |
89 | - return h('div.recps', [ | |
90 | - msg.value.content.recps.map(function (link) { | |
91 | - return api.about.html.image(isString(link) ? link : link.link) | |
92 | - }) | |
93 | - ]) | |
85 | + var threadsObs = More( | |
86 | + threadReduce, | |
87 | + pull( | |
88 | + Next(function () { | |
89 | + return api.sbot.pull.log({reverse: true, limit: 500, lte: lastTimestamp}) | |
90 | + }), | |
91 | + pull.map(function (data) { | |
92 | + lastTimestamp = data.timestamp | |
93 | + if(isObject(data.value.content)) return data | |
94 | + return api.message.sync.unbox(data) | |
95 | + }), | |
96 | + pull.filter(Boolean), | |
97 | + function (read) { | |
98 | + return function (abort, cb) { | |
99 | + read(abort, function (err, data) { | |
100 | + try { | |
101 | + cb(err, data) | |
102 | + } catch (err) { | |
103 | + console.error(err) | |
104 | + read(err, function () {}) | |
105 | + } | |
106 | + }) | |
94 | 107 | } |
95 | - )) | |
96 | - | |
97 | - container.appendChild(threadGroup( | |
98 | - threads, | |
99 | - threads.channels, | |
100 | - ch => h('h2.title', '#'+ch) | |
101 | - )) | |
102 | - }) | |
108 | + } | |
109 | + ), | |
110 | + function render (threadsState) { | |
111 | + update(threadsState) | |
112 | + morphdom(container, | |
113 | + h('div.container', [ | |
114 | + threadGroup( | |
115 | + threadsState, | |
116 | + threadsState.private, | |
117 | + function (_, msg) { | |
118 | + // NB: msg passed in is actually a 'thread', but only care about root msg | |
119 | + return h('div.recps', [ | |
120 | + msg.value.content.recps.map(function (link) { | |
121 | + return api.about.html.image(isString(link) ? link : link.link) | |
122 | + }) | |
123 | + ]) | |
124 | + } | |
125 | + ), | |
126 | + threadGroup( | |
127 | + threadsState, | |
128 | + threadsState.channels, | |
129 | + ch => h('h2.title', '#'+ch) | |
130 | + ) | |
131 | + ]) | |
132 | + ) | |
133 | + return container | |
134 | + }, | |
135 | + initial | |
103 | 136 | ) |
104 | 137 | |
105 | 138 | return h('Page -home', [ |
106 | 139 | h('h1', 'Home'), |
107 | 140 | api.app.html.nav(), |
108 | - container | |
141 | + threadsObs, | |
142 | + h('button', {'ev-click': threadsObs.more}, ['Show More']) | |
109 | 143 | ]) |
110 | - } | |
144 | + }) | |
111 | 145 | } |
112 | 146 |
Built with git-ssb-web