mirror of
https://github.com/vrtmrz/obsidian-livesync.git
synced 2026-06-28 21:17:06 +03:00
- Rewrite the service's binding/handler assignment systems - Removed loopholes that allowed traversal between services to clarify dependencies. - Consolidated the hidden state-related state, the handler, and the addition of bindings to the handler into a single object. - Currently, functions that can have handlers added implement either addHandler or setHandler directly on the function itself. I understand there are differing opinions on this, but for now, this is how it stands. - Services now possess a Context. Please ensure each platform has a class that inherits from ServiceContext. - To permit services to be dynamically bound, the services themselves are now defined by interfaces.
47 lines
2.0 KiB
TypeScript
47 lines
2.0 KiB
TypeScript
import { $msg } from "../../lib/src/common/i18n";
|
|
import { LiveSyncLocalDB } from "../../lib/src/pouchdb/LiveSyncLocalDB.ts";
|
|
import { initializeStores } from "../../common/stores.ts";
|
|
import { AbstractModule } from "../AbstractModule.ts";
|
|
import { LiveSyncManagers } from "../../lib/src/managers/LiveSyncManagers.ts";
|
|
import type { LiveSyncCore } from "../../main.ts";
|
|
|
|
export class ModuleLocalDatabaseObsidian extends AbstractModule {
|
|
_everyOnloadStart(): Promise<boolean> {
|
|
return Promise.resolve(true);
|
|
}
|
|
private async _openDatabase(): Promise<boolean> {
|
|
if (this.localDatabase != null) {
|
|
await this.localDatabase.close();
|
|
}
|
|
const vaultName = this.services.vault.getVaultName();
|
|
this._log($msg("moduleLocalDatabase.logWaitingForReady"));
|
|
const getDB = () => this.core.localDatabase.localDatabase;
|
|
const getSettings = () => this.core.settings;
|
|
this.core.managers = new LiveSyncManagers({
|
|
get database() {
|
|
return getDB();
|
|
},
|
|
getActiveReplicator: () => this.core.replicator,
|
|
id2path: this.services.path.id2path,
|
|
// path2id: this.core.$$path2id.bind(this.core),
|
|
path2id: this.services.path.path2id,
|
|
get settings() {
|
|
return getSettings();
|
|
},
|
|
});
|
|
this.core.localDatabase = new LiveSyncLocalDB(vaultName, this.core);
|
|
|
|
initializeStores(vaultName);
|
|
return await this.localDatabase.initializeDatabase();
|
|
}
|
|
|
|
_isDatabaseReady(): boolean {
|
|
return this.localDatabase != null && this.localDatabase.isReady;
|
|
}
|
|
onBindFunction(core: LiveSyncCore, services: typeof core.services): void {
|
|
services.database.isDatabaseReady.setHandler(this._isDatabaseReady.bind(this));
|
|
services.appLifecycle.onInitialise.addHandler(this._everyOnloadStart.bind(this));
|
|
services.database.openDatabase.setHandler(this._openDatabase.bind(this));
|
|
}
|
|
}
|