Defer media loading and strengthen persistence tests
All checks were successful
Deploy Sokko G / deploy (push) Successful in 18s
All checks were successful
Deploy Sokko G / deploy (push) Successful in 18s
This commit is contained in:
parent
09a2d4b71a
commit
d0e1fd73b1
19 changed files with 459 additions and 71 deletions
|
|
@ -2,6 +2,7 @@
|
|||
import { useCallback, useEffect, useRef, useState } from "react";
|
||||
import {
|
||||
getAllModuleData as dbGetAllModuleData,
|
||||
getModuleDataEntries as dbGetModuleDataEntries,
|
||||
getLinks as dbGetLinks,
|
||||
getSetting as dbGetSetting,
|
||||
getStorageEstimate,
|
||||
|
|
@ -23,6 +24,7 @@ import {
|
|||
import { applyModuleDataValue, removeModuleDataKeys } from "./optimisticModuleData.js";
|
||||
import { createPersistenceQueue } from "./persistenceQueue.js";
|
||||
import { createQuotaRefresh } from "./quotaRefresh.js";
|
||||
import { DEFERRED_MODULE_DATA, getDeferredModuleKeys, mergeLoadedModuleData, splitModuleData } from "./deferredModuleData.js";
|
||||
|
||||
function normalizeToolboxes(toolboxes) {
|
||||
return compactToolboxesForStorage(toolboxes).map(normalizeToolbox).filter(Boolean);
|
||||
|
|
@ -33,6 +35,7 @@ export function useIndexedToolboxes(onError) {
|
|||
const [loadError, setLoadError] = useState("");
|
||||
const [loadAttempt, setLoadAttempt] = useState(0);
|
||||
const writableRef = useRef(false);
|
||||
const pendingReadsRef = useRef(new Map());
|
||||
const [state, setState] = useState({ toolboxes: [], links: {}, moduleData: {}, toastPosition: "right" });
|
||||
const [queue] = useState(() => createPersistenceQueue(state, setState));
|
||||
const [storageUsage, setStorageUsage] = useState({ used: 0, limit: 0, ratio: 0 });
|
||||
|
|
@ -64,14 +67,20 @@ export function useIndexedToolboxes(onError) {
|
|||
let cancelled = false;
|
||||
async function loadStore() {
|
||||
try {
|
||||
const [toolboxes, links, modules, toastPosition] = await Promise.all([
|
||||
dbGetToolboxes(), dbGetLinks(), dbGetAllModuleData(), dbGetSetting("toastPosition", "right")
|
||||
const [toolboxes, links, toastPosition] = await Promise.all([
|
||||
dbGetToolboxes(), dbGetLinks(), dbGetSetting("toastPosition", "right")
|
||||
]);
|
||||
const normalizedToolboxes = (Array.isArray(toolboxes) ? toolboxes : []).map(normalizeToolbox).filter(Boolean);
|
||||
const deferredKeys = getDeferredModuleKeys(normalizedToolboxes);
|
||||
const modules = await dbGetAllModuleData(deferredKeys);
|
||||
if (cancelled) return;
|
||||
queue.initialize({
|
||||
toolboxes: (Array.isArray(toolboxes) ? toolboxes : []).map(normalizeToolbox).filter(Boolean),
|
||||
toolboxes: normalizedToolboxes,
|
||||
links: links && typeof links === "object" ? links : {},
|
||||
moduleData: Object.fromEntries((modules || []).map((entry) => [entry.key, entry.data])),
|
||||
moduleData: Object.fromEntries([
|
||||
...deferredKeys.map((key) => [key, DEFERRED_MODULE_DATA]),
|
||||
...(modules || []).map((entry) => [entry.key, entry.data])
|
||||
]),
|
||||
toastPosition: toastPosition === "left" ? "left" : "right"
|
||||
});
|
||||
writableRef.current = true;
|
||||
|
|
@ -125,6 +134,7 @@ export function useIndexedToolboxes(onError) {
|
|||
|
||||
function updateModuleData(toolboxId, moduleId, value, moduleType = "") {
|
||||
if (!writableRef.current) return false;
|
||||
if (queue.getCurrent().moduleData[moduleStorageKey(toolboxId, moduleId)] === DEFERRED_MODULE_DATA) return false;
|
||||
const compact = prepareModuleDataForStorage(queue.getCurrent().toolboxes, toolboxId, moduleId, value, moduleType);
|
||||
const key = moduleStorageKey(toolboxId, moduleId);
|
||||
const hasModule = (current) => current.toolboxes.some((toolbox) => toolbox.id === toolboxId && toolbox.modules.some((module) => module.id === moduleId));
|
||||
|
|
@ -160,7 +170,38 @@ export function useIndexedToolboxes(onError) {
|
|||
return enqueue((current) => {
|
||||
const next = updater(current);
|
||||
return { ...current, ...next, toolboxes: normalizeToolboxes(next.toolboxes) };
|
||||
}, (next) => dbSetToolboxSnapshot(compactToolboxesForStorage(next.toolboxes), next.links, next.moduleData));
|
||||
}, (next) => {
|
||||
const { stored, deferred } = splitModuleData(next.moduleData);
|
||||
return dbSetToolboxSnapshot(compactToolboxesForStorage(next.toolboxes), next.links, stored, deferred);
|
||||
});
|
||||
}
|
||||
|
||||
const ensureModuleData = useCallback((toolboxId, moduleId) => {
|
||||
const key = moduleStorageKey(toolboxId, moduleId);
|
||||
if (queue.getCurrent().moduleData[key] !== DEFERRED_MODULE_DATA) return Promise.resolve();
|
||||
if (pendingReadsRef.current.has(key)) return pendingReadsRef.current.get(key);
|
||||
const request = (async () => {
|
||||
const entries = await dbGetModuleDataEntries([key]);
|
||||
if (!writableRef.current) return;
|
||||
await queue.enqueue((current) => ({
|
||||
...current, moduleData: mergeLoadedModuleData(current.moduleData, [key], entries)
|
||||
}), () => {});
|
||||
})();
|
||||
pendingReadsRef.current.set(key, request);
|
||||
void request.then(() => pendingReadsRef.current.delete(key), () => pendingReadsRef.current.delete(key));
|
||||
return request;
|
||||
}, [queue]);
|
||||
|
||||
async function getExportSnapshot(toolboxId) {
|
||||
if (!writableRef.current) return null;
|
||||
let snapshot;
|
||||
const success = await queue.enqueue((current) => current, async (current) => {
|
||||
const { deferred } = splitModuleData(current.moduleData);
|
||||
const keys = deferred.filter((key) => !toolboxId || key.startsWith(`${toolboxId}:`));
|
||||
const entries = await dbGetModuleDataEntries(keys);
|
||||
snapshot = { ...current, moduleData: mergeLoadedModuleData(current.moduleData, keys, entries) };
|
||||
}, reportStorageError);
|
||||
return success ? snapshot : null;
|
||||
}
|
||||
|
||||
function setToastPosition(position) {
|
||||
|
|
@ -181,6 +222,9 @@ export function useIndexedToolboxes(onError) {
|
|||
setToolboxes: persistToolboxes,
|
||||
setLinks: persistLinks,
|
||||
getModuleData,
|
||||
ensureModuleData,
|
||||
isModuleDataReady: (toolboxId, moduleId) => state.moduleData[moduleStorageKey(toolboxId, moduleId)] !== DEFERRED_MODULE_DATA,
|
||||
getExportSnapshot,
|
||||
updateModuleData,
|
||||
removeModuleData,
|
||||
removeToolboxModuleData,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue