feature (state): revamp state management via indexedDB

This commit is contained in:
Mickael Kerjean
2022-10-11 00:02:54 +11:00
parent 88bd7d67dc
commit 456937d0cd
8 changed files with 45 additions and 36 deletions

View File

@ -1,4 +1,4 @@
"use strict"; import { setup_cache_state } from ".";
const DB_VERSION = 4; const DB_VERSION = 4;
const FILE_PATH = "file_path"; const FILE_PATH = "file_path";
@ -308,6 +308,7 @@ DataFromIndexedDB.prototype.destroy = function() {
purgeAll(db, FILE_CONTENT); purgeAll(db, FILE_CONTENT);
// We keep FILE_TAG as this was user generated and potentially frustrating // We keep FILE_TAG as this was user generated and potentially frustrating
// for users if they were to lose this // for users if they were to lose this
setup_cache_state("");
}); });
done(); done();
@ -330,9 +331,7 @@ export function setup_cache() {
cache = new DataFromMemory(); cache = new DataFromMemory();
if ("indexedDB" in window && window.indexedDB !== null) { if ("indexedDB" in window && window.indexedDB !== null) {
cache = new DataFromIndexedDB(); cache = new DataFromIndexedDB();
return new Promise((done) => { return Promise.all([cache.db, setup_cache_state()])
cache.db.then(() => done());
});
} }
return Promise.resolve(); return setup_cache_state();
} }

View File

@ -0,0 +1,25 @@
import { Session } from "../model";
let backendID = null;
export function currentShare() {
return findParams("share");
}
export function currentBackend() {
return backendID || "";
}
export function findParams(p) {
return new window.URL(location.href).searchParams.get(p) || "";
}
export function setup_cache_state(_backendID = null) {
if (_backendID !== null) {
backendID = _backendID;
return;
}
return Session.currentUser().then((r) => {
backendID = r["backendID"]
}).catch(() => backendID = null);
}

View File

@ -6,9 +6,9 @@ export { opener } from "./mimetype";
export { debounce, throttle } from "./backpressure"; export { debounce, throttle } from "./backpressure";
export { event } from "./events"; export { event } from "./events";
export { cache, setup_cache } from "./cache"; export { cache, setup_cache } from "./cache";
export { currentShare, currentBackend, findParams, setup_cache_state } from "./cache_state";
export { export {
pathBuilder, basename, dirname, absoluteToRelative, filetype, pathBuilder, basename, dirname, absoluteToRelative, filetype, appendShareToUrl,
currentShare, currentBackend, findParams, appendShareToUrl,
} from "./path"; } from "./path";
export { memory } from "./memory"; export { memory } from "./memory";
export { prepare } from "./navigate"; export { prepare } from "./navigate";

View File

@ -36,19 +36,6 @@ export function absoluteToRelative(from, to) {
return r; return r;
} }
export function currentShare() {
return findParams("share");
}
export function currentBackend() {
return "";
}
export function findParams(p) {
return new window.URL(location.href).searchParams.get(p) || "";
}
export function appendShareToUrl(link) { export function appendShareToUrl(link) {
let url = new window.URL(location.href); let url = new window.URL(location.href);
const share = url.searchParams.get("share"); const share = url.searchParams.get("share");

View File

@ -54,9 +54,9 @@ class FileSystem {
path: path, path: path,
results: null, results: null,
access_count: 0, access_count: 0,
metadata: null, permissions: null,
}, _files); }, _files);
store.metadata = response.permissions; store.permissions = response.permissions;
store.results = response.results; store.results = response.results;
if (_files && _files.results) { if (_files && _files.results) {
@ -104,7 +104,7 @@ class FileSystem {
this.obs && this.obs.next({ this.obs && this.obs.next({
status: "ok", status: "ok",
results: response.results, results: response.results,
metadata: response.metadata, permissions: response.permissions,
}); });
} }
return response; return response;
@ -117,7 +117,7 @@ class FileSystem {
this.obs && this.obs.next({ this.obs && this.obs.next({
status: "ok", status: "ok",
results: response.results, results: response.results,
metadata: response.metadata, permissions: response.permissions,
}); });
} }
response.last_access = new Date(); response.last_access = new Date();

View File

@ -2,8 +2,6 @@ import { cache, currentShare, currentBackend } from "../helpers/";
class TagManager { class TagManager {
all(tagPath = "/", maxSize = -1) { all(tagPath = "/", maxSize = -1) {
// return Promise.resolve([]); // TODO: Remove this when ready
return cache.get(cache.FILE_TAG, [currentBackend(), currentShare()]).then((DB) => { return cache.get(cache.FILE_TAG, [currentBackend(), currentShare()]).then((DB) => {
if (DB === null) { if (DB === null) {
return []; return [];

View File

@ -4,7 +4,7 @@ import "./connectpage.scss";
import { Session } from "../model/"; import { Session } from "../model/";
import { Container, NgShow, Loader, ErrorPage } from "../components/"; import { Container, NgShow, Loader, ErrorPage } from "../components/";
import { ForkMe, PoweredByFilestash, Form } from "./connectpage/"; import { ForkMe, PoweredByFilestash, Form } from "./connectpage/";
import { cache, notify, urlParams } from "../helpers/"; import { cache, notify, urlParams, setup_cache_state } from "../helpers/";
function ConnectPageComponent({ error, history }) { function ConnectPageComponent({ error, history }) {
const [isLoading, setIsLoading] = useState(true); const [isLoading, setIsLoading] = useState(true);
@ -23,7 +23,7 @@ function ConnectPageComponent({ error, history }) {
user["home"] = user["home"].replace(/^\/?(.*?)\/?$/, "$1").trim(); user["home"] = user["home"].replace(/^\/?(.*?)\/?$/, "$1").trim();
if (user["home"] !== "") url = `${url}${user["home"]}/`; if (user["home"] !== "") url = `${url}${user["home"]}/`;
} }
cache.destroy(); cache.destroy().then(() => setup_cache_state(user["backendID"]));
history.push(url); history.push(url);
}); });
}; };

View File

@ -42,7 +42,7 @@ export class FilesPageComponent extends React.Component {
is_search: false, is_search: false,
files: [], files: [],
selected: [], selected: [],
metadata: null, permissions: null,
frequents: null, frequents: null,
tags: null, tags: null,
page_number: PAGE_NUMBER_INIT, page_number: PAGE_NUMBER_INIT,
@ -60,7 +60,7 @@ export class FilesPageComponent extends React.Component {
// subscriptions // subscriptions
this.props.subscribe("file.create", function() { this.props.subscribe("file.create", function() {
return onCreate.apply(this, arguments).then(() => { return onCreate.apply(this, arguments).then(() => {
if (this.state.metadata && this.state.metadata.refresh_on_create === true) { if (this.state.permissions && this.state.permissions.refresh_on_create === true) {
this.onRefresh(this.state.path, "directory"); this.onRefresh(this.state.path, "directory");
} }
return Promise.resolve(); return Promise.resolve();
@ -131,7 +131,7 @@ export class FilesPageComponent extends React.Component {
return; return;
} }
this.setState({ this.setState({
metadata: res.metadata, permissions: res.permissions,
files: sort(res.results, this.state.sort), files: sort(res.results, this.state.sort),
selected: [], selected: [],
loading: false, loading: false,
@ -219,7 +219,7 @@ export class FilesPageComponent extends React.Component {
this.setState({ this.setState({
files: sort(f, this.state.sort), files: sort(f, this.state.sort),
loading: false, loading: false,
metadata: { permissions: {
can_rename: false, can_rename: false,
can_delete: false, can_delete: false,
can_share: false, can_share: false,
@ -228,7 +228,7 @@ export class FilesPageComponent extends React.Component {
}, (err) => { }, (err) => {
this.setState({ this.setState({
loading: false, loading: false,
metadata: { permissions: {
can_rename: false, can_rename: false,
can_delete: false, can_delete: false,
can_share: false, can_share: false,
@ -292,7 +292,7 @@ export class FilesPageComponent extends React.Component {
onSearch={this.onSearch.bind(this)} onSearch={this.onSearch.bind(this)}
onViewUpdate={(value) => this.onView(value)} onViewUpdate={(value) => this.onView(value)}
onSortUpdate={(value) => this.onSort(value)} onSortUpdate={(value) => this.onSort(value)}
accessRight={this.state.metadata || {}} accessRight={this.state.permissions || {}}
selected={this.state.selected} /> selected={this.state.selected} />
<NgIf cond={!this.state.loading}> <NgIf cond={!this.state.loading}>
<FileSystem <FileSystem
@ -300,7 +300,7 @@ export class FilesPageComponent extends React.Component {
view={this.state.view} selected={this.state.selected} view={this.state.view} selected={this.state.selected}
files={this.state.files.slice(0, this.state.page_number * LOAD_PER_SCROLL)} files={this.state.files.slice(0, this.state.page_number * LOAD_PER_SCROLL)}
isSearch={this.state.is_search} isSearch={this.state.is_search}
metadata={this.state.metadata || {}} metadata={this.state.permissions || {}}
onSort={this.onSort.bind(this)} onSort={this.onSort.bind(this)}
onView={this.onView.bind(this)} /> onView={this.onView.bind(this)} />
</NgIf> </NgIf>
@ -309,7 +309,7 @@ export class FilesPageComponent extends React.Component {
<NgIf cond={this.state.loading === true}> <NgIf cond={this.state.loading === true}>
<Loader/> <Loader/>
</NgIf> </NgIf>
<MobileFileUpload path={this.state.path} accessRight={this.state.metadata || {}} /> <MobileFileUpload path={this.state.path} accessRight={this.state.permissions || {}} />
</div> </div>
</div> </div>
</SelectableGroup> </SelectableGroup>