Light cache invalidation
Here's my latest iteration on how to do light live-updating. (Avoiding the problem of monolithic observeable cache).
The strategy :
- if you want something to live update, subscribe to a minimal stream which will "ping" you if there's an update
- trigger and async get + re-render
This means you can have a very light cache somewhere which has keys of things you're subscribing to, and values which are emitters you can bump
Example
This is from my gatherings refactor.
about/pull/updates.js
(the central cache) :const cache = {} var listening = false function updates (key) { if (!(isMsg(key) || isFeed(key))) throw new Error(`about.pull.updates expects a valid message/ feed key, got ${key}`) startListening() cache[key] = cache[key] || Notify() return cache[key].listen() } function startListening () { if (listening) return const opts = { live: true, old: false, query: [{ $filter: { value: { timestamp: { $gt: 0 }, content: { type: 'about', about: { $truthy: true } } } } }, { $map: { about: ['value', 'content', 'about'] } }] } pull( api.sbot.pull.stream(server => server.query.read(opts)), pull.filter(m => !m.sync), pull.drain( ({ about }) => { if (!cache[about]) return cache[about](1) // emit a minimal update! }, (err) => console.error(err) ) ) listening = true }
in my view:
const state = Value()
fetchState()
const updateStream = api.about.pull.stream(gathering.key) // this is patchore style code, I'm just calling that update fn
pull(updateStream, pull.drain(m => fetchState()))
return h('GatheringShow', computed(state, state => {
return [
h('h1', state.title),
h('div.description', state.description)
// etc
]
})
function fetchState () {
scuttle.get(gaterhing.key, (err, data) => { // scuttle = require('scuttle-gathering')(sbotConnection)
if (err) return console.error('AHHHH')
state.set(data)
})
}
cc @matt (it works!) @regular @christianbundy