Unified data cache into single utility class

This commit is contained in:
Max Berger
2026-03-15 22:24:00 +01:00
parent fe22538e2b
commit 2a24dc524c
9 changed files with 233 additions and 110 deletions

View File

@@ -19,17 +19,14 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { get_collections } from "../api/api.js";
import { discover_server_features } from "../api/sharing.js";
import { SERVER } from "../constants.js";
import { Collection, CollectionType } from "../models/collection.js";
import { collectionsCache } from "../utils/collections_cache.js";
import { bytesToHumanReadable } from "../utils/misc.js";
import { CreateEditCollectionScene } from "./CreateEditCollectionScene.js";
import { DeleteCollectionScene } from "./DeleteCollectionScene.js";
import { IncomingSharingScene } from "./IncomingSharingScene.js";
import { LoadingScene } from "./LoadingScene.js";
import { Scene, is_current_scene, pop_scene, push_scene } from "./scene_manager.js";
import { Scene, push_scene } from "./scene_manager.js";
import { ShareCollectionScene, maybe_enable_sharing_options } from "./ShareCollectionScene.js";
import { UploadCollectionScene } from "./UploadCollectionScene.js";
@@ -51,8 +48,6 @@ export class CollectionsScene {
/** @type {HTMLElement} */ let upload_btn = html_scene.querySelector("[data-name=upload]");
/** @type {HTMLElement} */ let incomingshares_btn = html_scene.querySelector("[data-name=incomingshares]");
/** @type {?XMLHttpRequest} */ let collections_req = null;
/** @type {?Array<Collection>} */ let child_collections = null;
/** @type {Array<HTMLElement>} */ let nodes = [];
function onnew() {
@@ -132,6 +127,13 @@ export class CollectionsScene {
let heightOfNavBar = navBar.offsetHeight + "px";
html_scene.style.marginTop = heightOfNavBar;
html_scene.style.height = "calc(100vh - " + heightOfNavBar + ")";
// Clear old nodes
nodes.forEach(function (node) {
node.parentNode.removeChild(node);
});
nodes = [];
collections.forEach(function (/** @type {Collection} */ collection) {
/** @type {HTMLElement} */ let node = /** @type {HTMLElement} */(template.cloneNode(true));
node.classList.remove("hidden");
@@ -192,43 +194,20 @@ export class CollectionsScene {
});
}
function update() {
let loading_scene = new LoadingScene();
push_scene(loading_scene);
collections_req = get_collections(user, password, principal_collection, function (child_collections_, error) {
if (!is_current_scene(loading_scene)) {
return;
}
collections_req = null;
if (error) {
onerror(error);
pop_scene();
} else {
child_collections = child_collections_;
pop_scene();
}
});
}
this.show = function () {
html_scene.classList.remove("hidden");
new_btn.onclick = onnew;
upload_btn.onclick = onupload;
incomingshares_btn.onclick = onincomingshares;
if (child_collections === null) {
update();
discover_server_features(user, password, maybe_enable_sharing_options);
} else {
// from update loading scene
show_collections(child_collections);
}
collectionsCache.getChildCollections(user, password, principal_collection, onerror, show_collections);
collectionsCache.getServerFeatures(user, password, null, maybe_enable_sharing_options);
};
this.hide = function () {
html_scene.classList.add("hidden");
new_btn.onclick = null;
upload_btn.onclick = null;
incomingshares_btn.onclick = null;
child_collections = null;
// remove collection
nodes.forEach(function (node) {
node.parentNode.removeChild(node);
@@ -236,11 +215,6 @@ export class CollectionsScene {
nodes = [];
};
this.release = function () {
if (collections_req !== null) {
collections_req.abort();
collections_req = null;
}
child_collections = null;
};
}
}