core: init notes with database

This commit is contained in:
Abdullah Atta
2023-07-03 06:45:12 +05:00
committed by Abdullah Atta
parent f91ada3c48
commit a967482c4f
13 changed files with 42 additions and 74 deletions

View File

@@ -64,9 +64,6 @@ async function initializeDatabase(persistence: DatabasePersistence) {
// });
// }
// db.eventManager.subscribe(EVENTS.databaseMigrating, async ({ from, to }) => {
// });
await db.init();

View File

@@ -92,7 +92,6 @@ function DiffViewer(props) {
useEffect(() => {
(async function () {
await db.notes.init();
let note = db.notes.note(noteId);
if (!note) {
hashNavigate(`/notes/create`, { replace: true });

View File

@@ -165,7 +165,6 @@ export const BackupExportSettings: SettingsGroup[] = [
onSelectionChanged: async (value) => {
if (!db.notes || value === "-") return;
await db.notes.init();
await exportNotes(
value as "txt" | "md" | "html",
db.notes.all.map((n) => n.id)

View File

@@ -134,8 +134,6 @@ class EditorStore extends BaseStore {
};
openSession = async (noteId, force) => {
await db.notes.init();
const session = this.get().session;
if (session.id) await db.fs.cancel(session.id);

View File

@@ -89,10 +89,8 @@ class NoteStore extends BaseStore {
};
setContext = (context) => {
db.notes.init().then(() => {
this.get().context = { ...context, notes: notesFromContext(context) };
this._forceUpdate();
});
this.get().context = { ...context, notes: notesFromContext(context) };
this._forceUpdate();
};
delete = async (...ids) => {

View File

@@ -41,8 +41,6 @@ export class WebExtensionServer implements Server {
}
async getNotes(): Promise<ItemReference[] | undefined> {
await db.notes?.init();
return db.notes?.all
.filter((n) => !n.locked)
.map((note) => ({ id: note.id, title: note.title }));

View File

@@ -31,18 +31,14 @@ function Home() {
const isCompact = useStore((store) => store.viewMode === "compact");
const refresh = useStore((store) => store.refresh);
const clearContext = useStore((store) => store.clearContext);
const [isLoading, setIsLoading] = useState(true);
useNavigate("home", () => {
clearContext();
});
useNavigate("home", clearContext);
useEffect(() => {
(async function () {
const initialized = db.notes.initialized;
if (!initialized || !notes.length) {
await db.notes.init();
store.refresh();
setIsLoading(false);
}
// const note = db.notes.note("62bc3f28a1a1a10000707077").data;
// const data = await db.content.raw(note.contentId);
@@ -64,7 +60,6 @@ function Home() {
groupType="home"
compact={isCompact}
refresh={refresh}
isLoading={isLoading}
items={notes}
placeholder={<Placeholder context="notes" />}
button={{

View File

@@ -37,7 +37,6 @@ async function typeToItems(type, context) {
return ["notes", notes];
}
case "notes": {
await db.notes.init();
if (!context) return ["notes", db.notes.all];
const notes = context.notes;
let topicNotes = [];

View File

@@ -23,14 +23,10 @@ import { confirm } from "../common/dialog-controller";
import { useStore, store } from "../stores/trash-store";
import { showToast } from "../utils/toast";
import useNavigate from "../hooks/use-navigate";
import { db } from "../common/db";
import Placeholder from "../components/placeholders";
function Trash() {
useNavigate("trash", async () => {
await db.notes.init();
store.refresh();
});
useNavigate("trash", store.refresh);
const items = useStore((store) => store.trash);
const refresh = useStore((store) => store.refresh);
const clearTrash = useStore((store) => store.clear);

View File

@@ -80,6 +80,23 @@ class Database {
this.storage = storage ? new Storage(storage) : null;
this.fs = fs && storage ? new FileStorage(fs, storage) : null;
NNEventSource = eventsource;
this.session = new Session(this.storage);
this.user = new UserManager(this.storage, this);
this.mfa = new MFAManager(this.storage, this);
this.syncer = new Sync(this);
this.vault = new Vault(this);
this.lookup = new Lookup(this);
this.backup = new Backup(this);
this.settings = new Settings(this);
this.migrations = new Migrations(this);
this.outbox = new Outbox(this);
this.monographs = new Monographs(this);
this.offers = new Offers();
this.debug = new Debug();
this.pricing = new Pricing();
this.subscriptions = new Subscriptions(this.user.tokenManager);
this.trash = new Trash(this);
}
async _validate() {
@@ -105,40 +122,12 @@ class Database {
await this.fs.clear();
this.disconnectSSE();
});
EV.subscribe(EVENTS.databaseCollectionInitiated, async (collectionName) => {
switch (collectionName) {
case "notes": {
await this.monographs.init();
await this.trash.init();
await this.relations.cleanup();
break;
}
}
});
this.session = new Session(this.storage);
await this._validate();
this.user = new UserManager(this.storage, this);
this.mfa = new MFAManager(this.storage, this);
this.syncer = new Sync(this);
this.vault = new Vault(this);
this.lookup = new Lookup(this);
this.backup = new Backup(this);
this.settings = new Settings(this);
this.migrations = new Migrations(this);
this.outbox = new Outbox(this);
this.monographs = new Monographs(this);
this.offers = new Offers();
this.debug = new Debug();
this.pricing = new Pricing();
this.subscriptions = new Subscriptions(this.user.tokenManager);
await this.initCollections();
await this.settings.init();
await this.outbox.init();
await this.user.init();
await this.migrations.init();
this.isInitialized = true;
if (this.migrations.required()) {
@@ -147,9 +136,8 @@ class Database {
}
async initCollections() {
await this.settings.init();
// collections
/** @type {Notes} */
this.notes = await Notes.new(this, "notes", true, true);
/** @type {Notebooks} */
this.notebooks = await Notebooks.new(this, "notebooks");
/** @type {Tags} */
@@ -168,8 +156,13 @@ class Database {
this.reminders = await Reminders.new(this, "reminders");
/**@type {Relations} */
this.relations = await Relations.new(this, "relations");
/** @type {Notes} */
this.notes = await Notes.new(this, "notes");
this.trash = new Trash(this);
await this.trash.init();
await this.relations.cleanup();
this.monographs.init().catch(console.error);
}
disconnectSSE() {

View File

@@ -32,7 +32,7 @@ import { NEWLINE_STRIP_REGEX, formatTitle } from "../utils/title-format";
export default class Notes extends Collection {
constructor(db, name, cached) {
super(db, name, cached);
this.topicReferences = new NoteIdCache(this._db);
this.topicReferences = new NoteIdCache(this);
}
async init() {
@@ -440,16 +440,16 @@ function getNoteHeadline(note, content) {
class NoteIdCache {
/**
*
* @param {import("../api/index").default} db
* @param {Notes} notes
*/
constructor(db) {
this._db = db;
constructor(notes) {
this.notes = notes;
this.cache = new Map();
}
rebuild() {
this.cache = new Map();
const notes = this._db.notes.all;
const notes = this.notes.all;
for (const note of notes) {
const { notebooks } = note;

View File

@@ -26,10 +26,7 @@ export default class Trash {
*/
constructor(db) {
this._db = db;
this.collections = {
notes: db.notes,
notebooks: db.notebooks
};
this.collections = ["notes", "notebooks"];
}
async init() {
@@ -48,16 +45,16 @@ export default class Trash {
get all() {
let trashItems = [];
for (let key in this.collections) {
const collection = this.collections[key];
for (const key of this.collections) {
const collection = this._db[key];
trashItems.push(...collection.deleted);
}
return trashItems;
}
_getItem(id) {
for (let key in this.collections) {
const collection = this.collections[key]._collection;
for (const key of this.collections) {
const collection = this._db[key]._collection;
if (collection.has(id)) return [collection.getItem(id), collection];
}
return [];
@@ -67,7 +64,7 @@ export default class Trash {
const collection = collectionNameFromItem(item);
if (!item || !item.type || !collection) return;
await this.collections[collection]._collection.updateItem({
await this._db[collection]._collection.updateItem({
...item,
id: item.itemId || item.id,
type: "trash",
@@ -104,9 +101,9 @@ export default class Trash {
delete item.itemType;
if (item.type === "note") {
await this.collections.notes.add(item);
await this._db.notes.add(item);
} else if (item.type === "notebook") {
await this.collections.notebooks.add(item);
await this._db.notebooks.add(item);
}
}
this._db.notes.topicReferences.rebuild();

View File

@@ -48,7 +48,6 @@ export default class CachedCollection extends IndexedCollection {
// }
this.map = new MapStub.Map(data, this.type);
this.invalidateCache();
}
async clear() {