Trying some app-side caching in the SSB app.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3725 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
Cory McWilliams 2021-12-31 18:42:37 +00:00
parent 4f1b31bce0
commit 4e359c3f5c
4 changed files with 33 additions and 37 deletions

View File

@ -1 +1 @@
{"type":"tildefriends-app","files":{"app.js":"&8DbkWJq+bDlPNGZGVWrlVyMzo+weV5GBfcleQSjIPjI=.sha256"}}
{"type":"tildefriends-app","files":{"app.js":"&5hVx7GCBbUh81CFIPkWJCyDZ9Pp1KBne6BiLbKklwCQ=.sha256"}}

View File

@ -12,7 +12,7 @@ async function main() {
var li = document.getElementById('apps').appendChild(document.createElement('li'));
var a = document.createElement('a');
a.innerText = app;
a.href = '../' + app;
a.href = '/~${core.user.credentials.session.name}/' + app + '/';
a.target = '_top';
li.appendChild(a);
}

View File

@ -1 +1 @@
{"type":"tildefriends-app","files":{"app.js":"&CuWFo7kEv6/qdqNl4gX1K9I+Hw7vnfp2RBndmqY4zhw=.sha256","index.html":"&mQ2fDeVm6D6+pwmF/6K04H1q5saq/Fl6Cq23UKlvJls=.sha256","vue-material.js":"&K5cdLqXYCENPak/TCINHQhyJhpS4G9DlZHGwoh/LF2g=.sha256","tf-user.js":"&KmUw2D8yehJU95RQ+8DWP69w/5wK7yWR6fUc5/LIpio=.sha256","tf-message.js":"&c7NMlMBi2UZE7ChUXd3h4ih0q2myuHhp3PpNUfBRvHM=.sha256","tf.js":"&I45p0703bQNAWq/5wFHdVrwM3z+5FL4K8QmDTfSnBAU=.sha256","commonmark.min.js":"&5x6ek3tFrKTZX6hXNNyFsjmhvrjmWpUkwuuaiyVV1Us=.sha256","vue.js":"&g1wvA+yHl1sVC+eufTsg9If7ZeVyMTBU+h0tks7ZNzE=.sha256","vue-material-theme-default-dark.css":"&RP2nr+2CR18BpHHw5ST9a5GJUCOG9n0G2kuGkcQioWE=.sha256","vue-material.min.css":"&kGbUM2QgFSyHZRzqQb0b+0S3EVIlZ0AXpdiAVjIhou8=.sha256","roboto.css":"&jJv43Om673mQO5JK0jj7714s5E+5Yrf82H6LcDx7wUs=.sha256","material-icons.css":"&a28PdcVvgq/DxyIvJAx/e+ZOEtOuHnr3kjLWKyzH11M=.sha256"}}
{"type":"tildefriends-app","files":{"app.js":"&W9Hxi6cX37sg8daxdiuvZMLJj0FJLoVAYE65T/X6Yp8=.sha256","index.html":"&mQ2fDeVm6D6+pwmF/6K04H1q5saq/Fl6Cq23UKlvJls=.sha256","vue-material.js":"&K5cdLqXYCENPak/TCINHQhyJhpS4G9DlZHGwoh/LF2g=.sha256","tf-user.js":"&KmUw2D8yehJU95RQ+8DWP69w/5wK7yWR6fUc5/LIpio=.sha256","tf-message.js":"&c7NMlMBi2UZE7ChUXd3h4ih0q2myuHhp3PpNUfBRvHM=.sha256","tf.js":"&I45p0703bQNAWq/5wFHdVrwM3z+5FL4K8QmDTfSnBAU=.sha256","commonmark.min.js":"&5x6ek3tFrKTZX6hXNNyFsjmhvrjmWpUkwuuaiyVV1Us=.sha256","vue.js":"&g1wvA+yHl1sVC+eufTsg9If7ZeVyMTBU+h0tks7ZNzE=.sha256","vue-material-theme-default-dark.css":"&RP2nr+2CR18BpHHw5ST9a5GJUCOG9n0G2kuGkcQioWE=.sha256","vue-material.min.css":"&kGbUM2QgFSyHZRzqQb0b+0S3EVIlZ0AXpdiAVjIhou8=.sha256","roboto.css":"&jJv43Om673mQO5JK0jj7714s5E+5Yrf82H6LcDx7wUs=.sha256","material-icons.css":"&a28PdcVvgq/DxyIvJAx/e+ZOEtOuHnr3kjLWKyzH11M=.sha256"}}

View File

@ -3,7 +3,14 @@
const k_posts_max = 20;
const k_votes_max = 20;
var g_following_cache = {};
var g_followers_cache = {};
var g_following_deep_cache = {};
async function following(db, id) {
if (g_following_cache[id]) {
return g_following_cache[id];
}
var o = await db.get(id + ":following");
const k_version = 5;
var f = o ? JSON.parse(o) : o;
@ -37,6 +44,7 @@ async function following(db, id) {
if (o != j) {
await db.set(id + ":following", j);
}
g_following_cache[id] = f.users;
return f.users;
}
@ -44,48 +52,33 @@ async function followingDeep(db, seed_ids, depth) {
if (depth <= 0) {
return seed_ids;
}
var key = JSON.stringify([seed_ids, depth]);
if (g_following_deep_cache[key]) {
return g_following_deep_cache[key];
}
var f = await Promise.all(seed_ids.map(x => following(db, x)));
var ids = [].concat(...f);
var x = await followingDeep(db, [...new Set(ids)].sort(), depth - 1);
x = [].concat(...x, ...seed_ids);
g_following_deep_cache[key] = x;
return x;
}
async function followers(db, id) {
var o = await db.get(id + ":followers");
const k_version = 3;
var f = o ? JSON.parse(o) : o;
if (!f || f.version != k_version) {
f = {users: [], rowid: 0, version: k_version};
if (g_followers_cache[id]) {
return g_followers_cache[id];
}
f.users = new Set(f.users);
await ssb.sqlStream(
"SELECT "+
" rowid, "+
" author AS contact, "+
" json_extract(content, '$.following') AS following "+
"FROM messages "+
"WHERE "+
" rowid > $1 AND "+
" json_extract(content, '$.type') = 'contact' AND "+
" json_extract(content, '$.contact') = $2 "+
"UNION SELECT MAX(rowid) as rowid, NULL, NULL FROM messages "+
"ORDER BY rowid",
[f.rowid, id],
function(row) {
if (row.following) {
f.users.add(row.contact);
} else {
f.users.delete(row.contact);
var results = [];
var me = await ssb.whoami();
var visible_users = await followingDeep(db, [me], 2);
for (let user of visible_users) {
var followed = await following(db, user);
if (followed.indexOf(id)) {
results.push(user);
}
f.rowid = row.rowid;
});
f.users = Array.from(f.users);
var j = JSON.stringify(f);
if (o != j) {
await db.set(id + ":followers", j);
}
return f.users;
g_followers_cache[id] = results;
return results;
}
async function sendUser(db, id) {
@ -151,7 +144,7 @@ function fnv32a(value)
async function getRecentPostIds(db, id, ids, limit) {
const k_version = 11;
const k_batch_max = 8;
const k_batch_max = 16;
var o = await db.get(id + ':recent_posts');
var recent = [];
var f = o ? JSON.parse(o) : o;
@ -178,7 +171,7 @@ async function getRecentPostIds(db, id, ids, limit) {
" rowid > ? AND "+
" rowid <= ? AND "+
" author IN (" + ids_batch.map(x => '?').join(", ") + ") AND "+
" json_extract(content, '$.type') IN ('post', 'tildefriends-app') "+
" json_extract(content, '$.type') = 'post' "+
"ORDER BY timestamp DESC LIMIT ?",
[].concat([f.rowid, row_id_max], ids_batch, [limit]),
function(row) {
@ -262,6 +255,9 @@ core.register('onConnectionsChanged', async function() {
});
async function refresh() {
g_following_cache = {};
g_followers_cache = {};
g_following_deep_cache = {};
await app.postMessage({clear: true});
var whoami = await ssb.whoami();
var db = await database("ssb");