git ssb

2+

mixmix / ticktack



Commit 4affbf9d002205c0d8fe516be4f046bf898ea555

store threads in an observable, which is also persisted!

Dominic Tarr committed on 8/11/2017, 7:33:08 AM
Parent: 695b85ae4b52e80b590f126de17de110bc7b7aae

Files changed

app/page/home.jschanged
app/page/home.jsView
@@ -5,15 +5,17 @@
55 const isObject = require('lodash/isObject')
66 const isString = require('lodash/isString')
77 const last = require('lodash/last')
88 const get = require('lodash/get')
9-
9+const More = require('hypermore')
1010 exports.gives = nest('app.page.home')
11+const morphdom = require('morphdom')
12+const Next = require('pull-next')
1113
1214 exports.needs = nest({
1315 'about.html.image': 'first',
1416 'app.html.nav': 'first',
15- 'feed.pull.public': 'first',
17+ 'sbot.pull.log': 'first',
1618 'history.sync.push': 'first',
1719 'message.sync.unbox': 'first',
1820 })
1921
@@ -23,11 +25,9 @@
2325 return text.split('\n')[0].substring(0, 80)
2426 }
2527
2628 exports.create = (api) => {
27- return nest('app.page.home', home)
28-
29- function home (location) {
29+ return nest('app.page.home', function (location) {
3030 // location here can expected to be: { page: 'home' }
3131
3232 var container = h('div.container', [])
3333
@@ -54,9 +54,9 @@
5454 function threadGroup (threads, obj, toName) {
5555 // threads = a state object for all the types of threads
5656 // obj = a map of keys to root ids, where key ∈ (channel | group | concatenated list of pubkeys)
5757 // toName = fn that derives a name from a particular thread
58-
58+
5959 var groupEl = h('div.group')
6060 for(var k in obj) {
6161 var id = obj[k]
6262 var thread = get(threads, ['roots', id])
@@ -67,46 +67,80 @@
6767 }
6868 return groupEl
6969 }
7070
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()
7475
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+ }
8284
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+ })
94107 }
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
103136 )
104137
105138 return h('Page -home', [
106139 h('h1', 'Home'),
107140 api.app.html.nav(),
108- container
141+ threadsObs,
142+ h('button', {'ev-click': threadsObs.more}, ['Show More'])
109143 ])
110- }
144+ })
111145 }
112146

Built with git-ssb-web