web: save events on timer
This commit is contained in:
parent
a5415906e9
commit
add261dd01
3 changed files with 64 additions and 55 deletions
|
@ -68,6 +68,7 @@ async function damus_web_init_ready() {
|
||||||
});
|
});
|
||||||
on_timer_timestamps();
|
on_timer_timestamps();
|
||||||
on_timer_invalidations();
|
on_timer_invalidations();
|
||||||
|
on_timer_save();
|
||||||
pool.on("open", on_pool_open);
|
pool.on("open", on_pool_open);
|
||||||
pool.on("event", on_pool_event);
|
pool.on("event", on_pool_event);
|
||||||
pool.on("notice", on_pool_notice);
|
pool.on("notice", on_pool_notice);
|
||||||
|
@ -91,6 +92,13 @@ function on_timer_invalidations() {
|
||||||
}, 1 * 1000);
|
}, 1 * 1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function on_timer_save() {
|
||||||
|
setTimeout(() => {
|
||||||
|
model_save_events(DAMUS);
|
||||||
|
on_timer_invalidations();
|
||||||
|
}, 10 * 1000);
|
||||||
|
}
|
||||||
|
|
||||||
/* on_pool_open occurs when a relay is opened. It then subscribes for the
|
/* on_pool_open occurs when a relay is opened. It then subscribes for the
|
||||||
* relative REQ as needed.
|
* relative REQ as needed.
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -121,51 +121,4 @@ function event_parse_reaction(ev) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function model_save_events(model) {
|
|
||||||
function _events_save(ev, resolve, reject) {
|
|
||||||
const db = ev.target.result;
|
|
||||||
let tx = db.transaction("events", "readwrite");
|
|
||||||
let store = tx.objectStore("events");
|
|
||||||
for (const evid in model.all_events) {
|
|
||||||
store.put(model.all_events[evid]);
|
|
||||||
}
|
|
||||||
tx.oncomplete = (ev) => {
|
|
||||||
db.close();
|
|
||||||
resolve();
|
|
||||||
log_debug("saved events!");
|
|
||||||
};
|
|
||||||
tx.onerror = (ev) => {
|
|
||||||
db.close();
|
|
||||||
log_error("failed to save events");
|
|
||||||
reject(ev);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
return dbcall(_events_save);
|
|
||||||
}
|
|
||||||
|
|
||||||
async function model_load_events(model, fn) {
|
|
||||||
function _events_load(ev, resolve, reject) {
|
|
||||||
const db = ev.target.result;
|
|
||||||
const tx = db.transaction("events", "readonly");
|
|
||||||
const store = tx.objectStore("events");
|
|
||||||
const cursor = store.openCursor();
|
|
||||||
cursor.onsuccess = (ev) => {
|
|
||||||
var cursor = ev.target.result;
|
|
||||||
if (cursor) {
|
|
||||||
fn(cursor.value);
|
|
||||||
cursor.continue();
|
|
||||||
} else {
|
|
||||||
db.close();
|
|
||||||
resolve();
|
|
||||||
log_debug("Successfully loaded events");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
cursor.onerror = (ev) => {
|
|
||||||
db.close();
|
|
||||||
reject(ev);
|
|
||||||
log_error("Could not load events.");
|
|
||||||
};
|
|
||||||
}
|
|
||||||
return dbcall(_events_load);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
|
@ -198,6 +198,17 @@ function model_subscribe_defaults(model, relay) {
|
||||||
filters_subscribe(filters, model.pool, [relay]);
|
filters_subscribe(filters, model.pool, [relay]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function model_events_arr(model) {
|
||||||
|
const events = model.all_events;
|
||||||
|
let arr = [];
|
||||||
|
for (const evid in events) {
|
||||||
|
const ev = events[evid];
|
||||||
|
const i = arr_bsearch_insert(arr, ev, event_cmp_created);
|
||||||
|
arr.splice(i, 0, ev);
|
||||||
|
}
|
||||||
|
return arr;
|
||||||
|
}
|
||||||
|
|
||||||
function test_model_events_arr() {
|
function test_model_events_arr() {
|
||||||
const arr = model_events_arr({all_events: {
|
const arr = model_events_arr({all_events: {
|
||||||
"c": {name: "c", created_at: 2},
|
"c": {name: "c", created_at: 2},
|
||||||
|
@ -221,15 +232,52 @@ function test_model_events_arr() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function model_events_arr(model) {
|
async function model_save_events(model) {
|
||||||
const events = model.all_events;
|
function _events_save(ev, resolve, reject) {
|
||||||
let arr = [];
|
const db = ev.target.result;
|
||||||
for (const evid in events) {
|
let tx = db.transaction("events", "readwrite");
|
||||||
const ev = events[evid];
|
let store = tx.objectStore("events");
|
||||||
const i = arr_bsearch_insert(arr, ev, event_cmp_created);
|
for (const evid in model.all_events) {
|
||||||
arr.splice(i, 0, ev);
|
store.put(model.all_events[evid]);
|
||||||
|
}
|
||||||
|
tx.oncomplete = (ev) => {
|
||||||
|
db.close();
|
||||||
|
resolve();
|
||||||
|
log_debug("saved events!");
|
||||||
|
};
|
||||||
|
tx.onerror = (ev) => {
|
||||||
|
db.close();
|
||||||
|
log_error("failed to save events");
|
||||||
|
reject(ev);
|
||||||
|
};
|
||||||
}
|
}
|
||||||
return arr;
|
return dbcall(_events_save);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function model_load_events(model, fn) {
|
||||||
|
function _events_load(ev, resolve, reject) {
|
||||||
|
const db = ev.target.result;
|
||||||
|
const tx = db.transaction("events", "readonly");
|
||||||
|
const store = tx.objectStore("events");
|
||||||
|
const cursor = store.openCursor();
|
||||||
|
cursor.onsuccess = (ev) => {
|
||||||
|
var cursor = ev.target.result;
|
||||||
|
if (cursor) {
|
||||||
|
fn(cursor.value);
|
||||||
|
cursor.continue();
|
||||||
|
} else {
|
||||||
|
db.close();
|
||||||
|
resolve();
|
||||||
|
log_debug("Successfully loaded events");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cursor.onerror = (ev) => {
|
||||||
|
db.close();
|
||||||
|
reject(ev);
|
||||||
|
log_error("Could not load events.");
|
||||||
|
};
|
||||||
|
}
|
||||||
|
return dbcall(_events_load);
|
||||||
}
|
}
|
||||||
|
|
||||||
function new_model() {
|
function new_model() {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue