git ssb

2+

mixmix / ticktack



Commit 1ff52dc468c28cdb7ae8aacd80a6a310a42e91b3

caching for the level 1 context (mutant-scroll)

mix irving committed on 11/21/2017, 4:25:56 AM
Parent: 88efa57edc6f7800ae827d2a28a7aab3065c1159

Files changed

app/html/context.jschanged
app/html/scroller.jschanged
package-lock.jsonchanged
package.jsonchanged
app/html/context.jsView
@@ -1,6 +1,6 @@
11 const nest = require('depnest')
2-const { h, computed, map, when, Dict, dictToCollection, Array: MutantArray, resolve } = require('mutant')
2+const { h, computed, map, when, Dict, dictToCollection, Array: MutantArray, Value, resolve } = require('mutant')
33 const pull = require('pull-stream')
44 const next = require('pull-next-step')
55 const get = require('lodash/get')
66 const isEmpty = require('lodash/isEmpty')
@@ -19,35 +19,20 @@
1919 'sbot.obs.localPeers': 'first',
2020 'translations.sync.strings': 'first',
2121 })
2222
23-
2423 exports.create = (api) => {
25- return nest('app.html.context', (location) => {
24+ var recentMsgCache = MutantArray()
25+ var userMsgCache = Dict() // { id: [ msgs ] }
2626
27+ return nest('app.html.context', context)
28+
29+ function context (location) {
2730 const strings = api.translations.sync.strings()
2831 const myKey = api.keys.sync.id()
2932
3033 var nearby = api.sbot.obs.localPeers()
31- var recentMsgLog = Dict ()
32- function updateRecentMsgLog (msg) {
33- const { author, timestamp } = msg.value
3434
35- if (!recentMsgLog.has(author)) {
36- recentMsgLog.put(author, msg)
37- return
38- }
39-
40- const currentWinner = recentMsgLog.get(author)
41- if (timestamp > currentWinner.value.timestamp) {
42- recentMsgLog.put(author, msg)
43- }
44- }
45- function isLatestMsg (msg) {
46- const { author, timestamp } = msg.value
47- return recentMsgLog.get(author).value.timestamp === timestamp
48- }
49-
5035 return h('Context -feed', [
5136 LevelOneContext(),
5237 LevelTwoContext()
5338 ])
@@ -67,9 +52,9 @@
6752 notifications: Math.random() > 0.7 ? Math.floor(Math.random()*9+1) : 0, // TODO
6853 imageEl: api.about.html.avatar(feedId, 'small'),
6954 label: api.about.obs.name(feedId),
7055 selected: location.feed === feedId,
71- location: computed(recentMsgLog, recent => {
56+ location: computed(recentMsgCache, recent => {
7257 const lastMsg = recent[feedId]
7358 return lastMsg
7459 ? Object.assign(lastMsg, { feed: feedId })
7560 : { page: 'threadNew', feed: feedId }
@@ -95,14 +80,15 @@
9580 stream: api.feed.pull.private,
9681 filter: () => pull(
9782 pull.filter(msg => msg.value.content.type === 'post'), // TODO is this the best way to protect against votes?
9883 pull.filter(msg => msg.value.author != myKey),
99- pull.filter(msg => msg.value.content.recps),
100- pull.through(updateRecentMsgLog),
101- pull.filter(isLatestMsg)
102- //pull.through( // trim exisiting from content up Top case) // do this with new updateTop in mutant-scroll
84+ pull.filter(msg => msg.value.content.recps)
10385 ),
104- render: (msg) => {
86+ store: recentMsgCache,
87+ updateTop: updateRecentMsgCache,
88+ updateBottom: updateRecentMsgCache,
89+ render: (msgObs) => {
90+ const msg = resolve(msgObs)
10591 const { author } = msg.value
10692 if (nearby.has(author)) return
10793
10894 return Option({
@@ -113,8 +99,45 @@
11399 location: Object.assign({}, msg, { feed: author }) // TODO make obs?
114100 })
115101 }
116102 })
103+
104+ function updateRecentMsgCache (soFar, newMsg) {
105+ soFar.transaction(function () {
106+ const { author, timestamp } = newMsg.value
107+ const index = indexOf(soFar, (msg) => author === resolve(msg).value.author)
108+ var object = Value()
109+
110+ if (index >= 0) {
111+ // reference already exists, lets use this instead!
112+ const existingMsg = soFar.get(index)
113+
114+ if (resolve(existingMsg).value.timestamp > timestamp) return
115+ // but abort if the existing reference is newer
116+
117+ object = existingMsg
118+ soFar.deleteAt(index)
119+ }
120+
121+ object.set(newMsg)
122+
123+ const justOlderPosition = indexOf(soFar, (msg) => timestamp > resolve(msg).value.timestamp)
124+ if (justOlderPosition > -1) {
125+ soFar.insert(object, justOlderPosition)
126+ } else {
127+ soFar.push(object)
128+ }
129+ })
130+ }
131+
132+ function indexOf (array, fn) {
133+ for (var i = 0; i < array.getLength(); i++) {
134+ if (fn(array.get(i))) {
135+ return i
136+ }
137+ }
138+ return -1
139+ }
117140 }
118141
119142 function LevelTwoContext () {
120143 const { key, value, feed: targetUser, page } = location
@@ -172,7 +195,7 @@
172195 ]),
173196 h('div.label', { 'ev-click': goToLocation }, label)
174197 ])
175198 }
176- })
199+ }
177200 }
178201
app/html/scroller.jsView
@@ -16,12 +16,9 @@
1616 function createScroller (opts = {}) {
1717 const {
1818 stream,
1919 filter = () => pull.filter((msg) => true),
20- // render,
21- // classList = [],
22- // prepend = [],
23- // append = []
20+
2421 } = opts
2522
2623 const streamToTop = pull(
2724 next(stream, {old: false, limit: 100}, ['value', 'timestamp']),
@@ -33,8 +30,19 @@
3330 filter()
3431 )
3532
3633 return Scroller(Object.assign({}, opts, { streamToTop, streamToBottom }))
34+ // valid Scroller opts : see github.com/mixmix/mutant-scroll
35+ // classList = [],
36+ // prepend = [],
37+ // append = [],
38+ // streamToTop,
39+ // streamToBottom,
40+ // render,
41+ // updateTop = updateTopDefault,
42+ // updateBottom = updateBottomDefault,
43+ // store = MutantArray(),
44+ // cb = (err) => { if (err) throw err }
3745 }
3846 }
3947
4048 function keyscroll (content) {
package-lock.jsonView
The diff is too large to show. Use a local git client to view these changes.
Old file size: 203359 bytes
New file size: 203359 bytes
package.jsonView
@@ -33,9 +33,9 @@
3333 "markdown-summary": "^1.0.3",
3434 "micro-css": "^2.0.1",
3535 "morphdom": "^2.3.3",
3636 "mutant": "^3.21.2",
37- "mutant-scroll": "0.0.1",
37+ "mutant-scroll": "0.0.3",
3838 "obv-debounce": "^1.0.2",
3939 "open-external": "^0.1.1",
4040 "patch-profile": "^1.0.2",
4141 "patch-settings": "^1.0.0",

Built with git-ssb-web