Extract settings from database
This commit is contained in:
parent
aef5952c4d
commit
614e4a780a
20 changed files with 344 additions and 319 deletions
|
|
@ -18,7 +18,8 @@ import {
|
||||||
Syncer,
|
Syncer,
|
||||||
SyncHistory,
|
SyncHistory,
|
||||||
SyncService,
|
SyncService,
|
||||||
initialize
|
initialize,
|
||||||
|
Settings
|
||||||
} from "sync-client";
|
} from "sync-client";
|
||||||
|
|
||||||
export default class VaultLinkPlugin extends Plugin {
|
export default class VaultLinkPlugin extends Plugin {
|
||||||
|
|
@ -32,21 +33,38 @@ export default class VaultLinkPlugin extends Plugin {
|
||||||
|
|
||||||
await initialize();
|
await initialize();
|
||||||
|
|
||||||
|
let state = (await this.loadData()) ?? {
|
||||||
|
settings: undefined,
|
||||||
|
database: undefined
|
||||||
|
};
|
||||||
const database = new Database(
|
const database = new Database(
|
||||||
await this.loadData(),
|
state.database,
|
||||||
this.saveData.bind(this)
|
async (data: unknown): Promise<void> => {
|
||||||
|
state = { ...state, database: data };
|
||||||
|
return this.saveData(state);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
const settings = new Settings(
|
||||||
|
state.settings,
|
||||||
|
async (data: unknown): Promise<void> => {
|
||||||
|
state = { ...state, settings: data };
|
||||||
|
return this.saveData(state);
|
||||||
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
const syncService = new SyncService(database);
|
const syncService = new SyncService(database);
|
||||||
|
|
||||||
const syncer = new Syncer(
|
const syncer = new Syncer(
|
||||||
database,
|
database,
|
||||||
|
settings,
|
||||||
syncService,
|
syncService,
|
||||||
this.operations,
|
this.operations,
|
||||||
this.history
|
this.history
|
||||||
);
|
);
|
||||||
|
|
||||||
const statusDescription = new StatusDescription(
|
const statusDescription = new StatusDescription(
|
||||||
|
settings,
|
||||||
database,
|
database,
|
||||||
syncService,
|
syncService,
|
||||||
this.history,
|
this.history,
|
||||||
|
|
@ -56,22 +74,22 @@ export default class VaultLinkPlugin extends Plugin {
|
||||||
this.settingsTab = new SyncSettingsTab({
|
this.settingsTab = new SyncSettingsTab({
|
||||||
app: this.app,
|
app: this.app,
|
||||||
plugin: this,
|
plugin: this,
|
||||||
database,
|
settings,
|
||||||
syncService,
|
syncService,
|
||||||
statusDescription,
|
statusDescription,
|
||||||
syncer
|
syncer
|
||||||
});
|
});
|
||||||
this.addSettingTab(this.settingsTab);
|
this.addSettingTab(this.settingsTab);
|
||||||
|
|
||||||
new StatusBar(database, this, this.history, syncer);
|
new StatusBar(settings, this, this.history, syncer);
|
||||||
|
|
||||||
this.registerView(
|
this.registerView(
|
||||||
HistoryView.TYPE,
|
HistoryView.TYPE,
|
||||||
(leaf) => new HistoryView(leaf, database, this.history)
|
(leaf) => new HistoryView(leaf, settings, this.history)
|
||||||
);
|
);
|
||||||
this.registerView(
|
this.registerView(
|
||||||
LogsView.TYPE,
|
LogsView.TYPE,
|
||||||
(leaf) => new LogsView(this, database, leaf)
|
(leaf) => new LogsView(this, settings, leaf)
|
||||||
);
|
);
|
||||||
|
|
||||||
this.addRibbonIcon(
|
this.addRibbonIcon(
|
||||||
|
|
@ -117,21 +135,23 @@ export default class VaultLinkPlugin extends Plugin {
|
||||||
});
|
});
|
||||||
|
|
||||||
this.registerRemoteEventListener(
|
this.registerRemoteEventListener(
|
||||||
|
settings,
|
||||||
database,
|
database,
|
||||||
syncService,
|
syncService,
|
||||||
syncer,
|
syncer,
|
||||||
database.getSettings().fetchChangesUpdateIntervalMs
|
settings.getSettings().fetchChangesUpdateIntervalMs
|
||||||
);
|
);
|
||||||
|
|
||||||
database.addOnSettingsChangeHandlers((settings, oldSettings) => {
|
settings.addOnSettingsChangeHandlers((newSettings, oldSettings) => {
|
||||||
this.registerRemoteEventListener(
|
this.registerRemoteEventListener(
|
||||||
|
settings,
|
||||||
database,
|
database,
|
||||||
syncService,
|
syncService,
|
||||||
syncer,
|
syncer,
|
||||||
settings.fetchChangesUpdateIntervalMs
|
newSettings.fetchChangesUpdateIntervalMs
|
||||||
);
|
);
|
||||||
|
|
||||||
if (!oldSettings.isSyncEnabled && settings.isSyncEnabled) {
|
if (!oldSettings.isSyncEnabled && newSettings.isSyncEnabled) {
|
||||||
syncer
|
syncer
|
||||||
.scheduleSyncForOfflineChanges()
|
.scheduleSyncForOfflineChanges()
|
||||||
.catch((_error: unknown) => {
|
.catch((_error: unknown) => {
|
||||||
|
|
@ -182,6 +202,7 @@ export default class VaultLinkPlugin extends Plugin {
|
||||||
}
|
}
|
||||||
|
|
||||||
private registerRemoteEventListener(
|
private registerRemoteEventListener(
|
||||||
|
settings: Settings,
|
||||||
database: Database,
|
database: Database,
|
||||||
syncService: SyncService,
|
syncService: SyncService,
|
||||||
syncer: Syncer,
|
syncer: Syncer,
|
||||||
|
|
@ -195,6 +216,7 @@ export default class VaultLinkPlugin extends Plugin {
|
||||||
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
||||||
async () =>
|
async () =>
|
||||||
applyRemoteChangesLocally({
|
applyRemoteChangesLocally({
|
||||||
|
settings,
|
||||||
database,
|
database,
|
||||||
syncService,
|
syncService,
|
||||||
syncer
|
syncer
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ import type { IconName, WorkspaceLeaf } from "obsidian";
|
||||||
import { ItemView, setIcon } from "obsidian";
|
import { ItemView, setIcon } from "obsidian";
|
||||||
|
|
||||||
import { intlFormatDistance } from "date-fns";
|
import { intlFormatDistance } from "date-fns";
|
||||||
import type { SyncHistory, HistoryEntry, Database } from "sync-client";
|
import type { SyncHistory, HistoryEntry, Settings } from "sync-client";
|
||||||
import { SyncType, SyncSource, SyncStatus, Logger } from "sync-client";
|
import { SyncType, SyncSource, SyncStatus, Logger } from "sync-client";
|
||||||
|
|
||||||
export class HistoryView extends ItemView {
|
export class HistoryView extends ItemView {
|
||||||
|
|
@ -12,7 +12,7 @@ export class HistoryView extends ItemView {
|
||||||
|
|
||||||
public constructor(
|
public constructor(
|
||||||
leaf: WorkspaceLeaf,
|
leaf: WorkspaceLeaf,
|
||||||
private readonly database: Database,
|
private readonly settings: Settings,
|
||||||
private readonly history: SyncHistory
|
private readonly history: SyncHistory
|
||||||
) {
|
) {
|
||||||
super(leaf);
|
super(leaf);
|
||||||
|
|
@ -101,7 +101,7 @@ export class HistoryView extends ItemView {
|
||||||
.filter(
|
.filter(
|
||||||
(entry) =>
|
(entry) =>
|
||||||
entry.status !== SyncStatus.NO_OP ||
|
entry.status !== SyncStatus.NO_OP ||
|
||||||
this.database.getSettings().displayNoopSyncEvents
|
this.settings.getSettings().displayNoopSyncEvents
|
||||||
);
|
);
|
||||||
|
|
||||||
entries.forEach((entry) => {
|
entries.forEach((entry) => {
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
import type { WorkspaceLeaf } from "obsidian";
|
import type { WorkspaceLeaf } from "obsidian";
|
||||||
import { ItemView } from "obsidian";
|
import { ItemView } from "obsidian";
|
||||||
import type VaultLinkPlugin from "src/vault-link-plugin";
|
import type VaultLinkPlugin from "src/vault-link-plugin";
|
||||||
import type { Database } from "sync-client";
|
import type { Settings } from "sync-client";
|
||||||
import { Logger } from "sync-client";
|
import { Logger } from "sync-client";
|
||||||
|
|
||||||
export class LogsView extends ItemView {
|
export class LogsView extends ItemView {
|
||||||
|
|
@ -10,7 +10,7 @@ export class LogsView extends ItemView {
|
||||||
|
|
||||||
public constructor(
|
public constructor(
|
||||||
private readonly plugin: VaultLinkPlugin,
|
private readonly plugin: VaultLinkPlugin,
|
||||||
private readonly database: Database,
|
private readonly settings: Settings,
|
||||||
leaf: WorkspaceLeaf
|
leaf: WorkspaceLeaf
|
||||||
) {
|
) {
|
||||||
super(leaf);
|
super(leaf);
|
||||||
|
|
@ -19,7 +19,7 @@ export class LogsView extends ItemView {
|
||||||
this.updateView();
|
this.updateView();
|
||||||
});
|
});
|
||||||
|
|
||||||
database.addOnSettingsChangeHandlers((newSettings, oldSettings) => {
|
settings.addOnSettingsChangeHandlers((newSettings, oldSettings) => {
|
||||||
if (newSettings.minimumLogLevel !== oldSettings.minimumLogLevel) {
|
if (newSettings.minimumLogLevel !== oldSettings.minimumLogLevel) {
|
||||||
this.updateView();
|
this.updateView();
|
||||||
}
|
}
|
||||||
|
|
@ -79,7 +79,7 @@ export class LogsView extends ItemView {
|
||||||
);
|
);
|
||||||
|
|
||||||
const logs = Logger.getInstance().getMessages(
|
const logs = Logger.getInstance().getMessages(
|
||||||
this.database.getSettings().minimumLogLevel
|
this.settings.getSettings().minimumLogLevel
|
||||||
);
|
);
|
||||||
|
|
||||||
if (logs.length === 0) {
|
if (logs.length === 0) {
|
||||||
|
|
|
||||||
|
|
@ -5,14 +5,14 @@ import type VaultLinkPlugin from "src/vault-link-plugin";
|
||||||
import type { StatusDescription } from "./status-description";
|
import type { StatusDescription } from "./status-description";
|
||||||
import { LogsView } from "./logs-view";
|
import { LogsView } from "./logs-view";
|
||||||
import { HistoryView } from "./history-view";
|
import { HistoryView } from "./history-view";
|
||||||
import type { SyncService, Syncer, Database } from "sync-client";
|
import type { SyncService, Syncer, Settings } from "sync-client";
|
||||||
import { Logger, LogLevel } from "sync-client";
|
import { Logger, LogLevel } from "sync-client";
|
||||||
|
|
||||||
export class SyncSettingsTab extends PluginSettingTab {
|
export class SyncSettingsTab extends PluginSettingTab {
|
||||||
private editedVaultName: string;
|
private editedVaultName: string;
|
||||||
|
|
||||||
private readonly plugin: VaultLinkPlugin;
|
private readonly plugin: VaultLinkPlugin;
|
||||||
private readonly database: Database;
|
private readonly settings: Settings;
|
||||||
private readonly syncService: SyncService;
|
private readonly syncService: SyncService;
|
||||||
private readonly statusDescription: StatusDescription;
|
private readonly statusDescription: StatusDescription;
|
||||||
private readonly syncer: Syncer;
|
private readonly syncer: Syncer;
|
||||||
|
|
@ -21,27 +21,27 @@ export class SyncSettingsTab extends PluginSettingTab {
|
||||||
public constructor({
|
public constructor({
|
||||||
app,
|
app,
|
||||||
plugin,
|
plugin,
|
||||||
database,
|
settings,
|
||||||
syncService,
|
syncService,
|
||||||
statusDescription,
|
statusDescription,
|
||||||
syncer
|
syncer
|
||||||
}: {
|
}: {
|
||||||
app: App;
|
app: App;
|
||||||
plugin: VaultLinkPlugin;
|
plugin: VaultLinkPlugin;
|
||||||
database: Database;
|
settings: Settings;
|
||||||
syncService: SyncService;
|
syncService: SyncService;
|
||||||
statusDescription: StatusDescription;
|
statusDescription: StatusDescription;
|
||||||
syncer: Syncer;
|
syncer: Syncer;
|
||||||
}) {
|
}) {
|
||||||
super(app, plugin);
|
super(app, plugin);
|
||||||
this.plugin = plugin;
|
this.plugin = plugin;
|
||||||
this.database = database;
|
this.settings = settings;
|
||||||
this.syncService = syncService;
|
this.syncService = syncService;
|
||||||
this.statusDescription = statusDescription;
|
this.statusDescription = statusDescription;
|
||||||
this.syncer = syncer;
|
this.syncer = syncer;
|
||||||
|
|
||||||
this.editedVaultName = this.database.getSettings().vaultName;
|
this.editedVaultName = this.settings.getSettings().vaultName;
|
||||||
this.database.addOnSettingsChangeHandlers(
|
this.settings.addOnSettingsChangeHandlers(
|
||||||
(newSettings, oldSettings) => {
|
(newSettings, oldSettings) => {
|
||||||
if (newSettings.vaultName !== oldSettings.vaultName) {
|
if (newSettings.vaultName !== oldSettings.vaultName) {
|
||||||
this.editedVaultName = newSettings.vaultName;
|
this.editedVaultName = newSettings.vaultName;
|
||||||
|
|
@ -130,9 +130,9 @@ export class SyncSettingsTab extends PluginSettingTab {
|
||||||
.addText((text) =>
|
.addText((text) =>
|
||||||
text
|
text
|
||||||
.setPlaceholder("https://example.com:3030")
|
.setPlaceholder("https://example.com:3030")
|
||||||
.setValue(this.database.getSettings().remoteUri)
|
.setValue(this.settings.getSettings().remoteUri)
|
||||||
.onChange(async (value) =>
|
.onChange(async (value) =>
|
||||||
this.database.setSetting("remoteUri", value)
|
this.settings.setSetting("remoteUri", value)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
.addButton((button) =>
|
.addButton((button) =>
|
||||||
|
|
@ -154,9 +154,9 @@ export class SyncSettingsTab extends PluginSettingTab {
|
||||||
.addTextArea((text) =>
|
.addTextArea((text) =>
|
||||||
text
|
text
|
||||||
.setPlaceholder("ey...")
|
.setPlaceholder("ey...")
|
||||||
.setValue(this.database.getSettings().token)
|
.setValue(this.settings.getSettings().token)
|
||||||
.onChange(async (value) =>
|
.onChange(async (value) =>
|
||||||
this.database.setSetting("token", value)
|
this.settings.setSetting("token", value)
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
@ -169,18 +169,18 @@ export class SyncSettingsTab extends PluginSettingTab {
|
||||||
.addText((text) =>
|
.addText((text) =>
|
||||||
text
|
text
|
||||||
.setPlaceholder("My Obsidian Vault")
|
.setPlaceholder("My Obsidian Vault")
|
||||||
.setValue(this.database.getSettings().vaultName)
|
.setValue(this.settings.getSettings().vaultName)
|
||||||
.onChange((value) => (this.editedVaultName = value))
|
.onChange((value) => (this.editedVaultName = value))
|
||||||
)
|
)
|
||||||
.addButton((button) =>
|
.addButton((button) =>
|
||||||
button.setButtonText("Apply").onClick(async () => {
|
button.setButtonText("Apply").onClick(async () => {
|
||||||
if (
|
if (
|
||||||
this.editedVaultName ===
|
this.editedVaultName ===
|
||||||
this.database.getSettings().vaultName
|
this.settings.getSettings().vaultName
|
||||||
) {
|
) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
await this.database.setSetting(
|
await this.settings.setSetting(
|
||||||
"vaultName",
|
"vaultName",
|
||||||
this.editedVaultName
|
this.editedVaultName
|
||||||
);
|
);
|
||||||
|
|
@ -223,11 +223,11 @@ export class SyncSettingsTab extends PluginSettingTab {
|
||||||
.setDynamicTooltip()
|
.setDynamicTooltip()
|
||||||
.setInstant(false)
|
.setInstant(false)
|
||||||
.setValue(
|
.setValue(
|
||||||
this.database.getSettings()
|
this.settings.getSettings()
|
||||||
.fetchChangesUpdateIntervalMs / 1000
|
.fetchChangesUpdateIntervalMs / 1000
|
||||||
)
|
)
|
||||||
.onChange(async (value) =>
|
.onChange(async (value) =>
|
||||||
this.database.setSetting(
|
this.settings.setSetting(
|
||||||
"fetchChangesUpdateIntervalMs",
|
"fetchChangesUpdateIntervalMs",
|
||||||
value * 1000
|
value * 1000
|
||||||
)
|
)
|
||||||
|
|
@ -244,9 +244,9 @@ export class SyncSettingsTab extends PluginSettingTab {
|
||||||
.setLimits(1, 16, 1)
|
.setLimits(1, 16, 1)
|
||||||
.setDynamicTooltip()
|
.setDynamicTooltip()
|
||||||
.setInstant(false)
|
.setInstant(false)
|
||||||
.setValue(this.database.getSettings().syncConcurrency)
|
.setValue(this.settings.getSettings().syncConcurrency)
|
||||||
.onChange(async (value) =>
|
.onChange(async (value) =>
|
||||||
this.database.setSetting("syncConcurrency", value)
|
this.settings.setSetting("syncConcurrency", value)
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
@ -260,9 +260,9 @@ export class SyncSettingsTab extends PluginSettingTab {
|
||||||
.setLimits(0, 32, 1)
|
.setLimits(0, 32, 1)
|
||||||
.setDynamicTooltip()
|
.setDynamicTooltip()
|
||||||
.setInstant(false)
|
.setInstant(false)
|
||||||
.setValue(this.database.getSettings().maxFileSizeMB)
|
.setValue(this.settings.getSettings().maxFileSizeMB)
|
||||||
.onChange(async (value) =>
|
.onChange(async (value) =>
|
||||||
this.database.setSetting("maxFileSizeMB", value)
|
this.settings.setSetting("maxFileSizeMB", value)
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
@ -276,9 +276,9 @@ export class SyncSettingsTab extends PluginSettingTab {
|
||||||
)
|
)
|
||||||
.addToggle((toggle) =>
|
.addToggle((toggle) =>
|
||||||
toggle
|
toggle
|
||||||
.setValue(this.database.getSettings().isSyncEnabled)
|
.setValue(this.settings.getSettings().isSyncEnabled)
|
||||||
.onChange(async (value) =>
|
.onChange(async (value) =>
|
||||||
this.database.setSetting("isSyncEnabled", value)
|
this.settings.setSetting("isSyncEnabled", value)
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
@ -293,9 +293,9 @@ export class SyncSettingsTab extends PluginSettingTab {
|
||||||
)
|
)
|
||||||
.addToggle((toggle) =>
|
.addToggle((toggle) =>
|
||||||
toggle
|
toggle
|
||||||
.setValue(this.database.getSettings().displayNoopSyncEvents)
|
.setValue(this.settings.getSettings().displayNoopSyncEvents)
|
||||||
.onChange(async (value) =>
|
.onChange(async (value) =>
|
||||||
this.database.setSetting("displayNoopSyncEvents", value)
|
this.settings.setSetting("displayNoopSyncEvents", value)
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
@ -312,9 +312,9 @@ export class SyncSettingsTab extends PluginSettingTab {
|
||||||
[LogLevel.WARNING]: LogLevel.WARNING,
|
[LogLevel.WARNING]: LogLevel.WARNING,
|
||||||
[LogLevel.ERROR]: LogLevel.ERROR
|
[LogLevel.ERROR]: LogLevel.ERROR
|
||||||
})
|
})
|
||||||
.setValue(this.database.getSettings().minimumLogLevel)
|
.setValue(this.settings.getSettings().minimumLogLevel)
|
||||||
.onChange(async (value) =>
|
.onChange(async (value) =>
|
||||||
this.database.setSetting(
|
this.settings.setSetting(
|
||||||
"minimumLogLevel",
|
"minimumLogLevel",
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
|
||||||
value as LogLevel
|
value as LogLevel
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
import type { Database, HistoryStats, SyncHistory, Syncer } from "sync-client";
|
import type { HistoryStats, Settings, SyncHistory, Syncer } from "sync-client";
|
||||||
import type VaultLinkPlugin from "src/vault-link-plugin";
|
import type VaultLinkPlugin from "src/vault-link-plugin";
|
||||||
|
|
||||||
export class StatusBar {
|
export class StatusBar {
|
||||||
|
|
@ -8,7 +8,7 @@ export class StatusBar {
|
||||||
private lastRemaining: number | undefined;
|
private lastRemaining: number | undefined;
|
||||||
|
|
||||||
public constructor(
|
public constructor(
|
||||||
private readonly database: Database,
|
private readonly settings: Settings,
|
||||||
private readonly plugin: VaultLinkPlugin,
|
private readonly plugin: VaultLinkPlugin,
|
||||||
history: SyncHistory,
|
history: SyncHistory,
|
||||||
syncer: Syncer
|
syncer: Syncer
|
||||||
|
|
@ -24,7 +24,7 @@ export class StatusBar {
|
||||||
this.updateStatus();
|
this.updateStatus();
|
||||||
});
|
});
|
||||||
|
|
||||||
database.addOnSettingsChangeHandlers(() => {
|
settings.addOnSettingsChangeHandlers(() => {
|
||||||
this.updateStatus();
|
this.updateStatus();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
@ -57,7 +57,7 @@ export class StatusBar {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!hasShownMessage) {
|
if (!hasShownMessage) {
|
||||||
if (this.database.getSettings().isSyncEnabled) {
|
if (this.settings.getSettings().isSyncEnabled) {
|
||||||
container.createSpan({ text: "VaultLink is idle" });
|
container.createSpan({ text: "VaultLink is idle" });
|
||||||
} else {
|
} else {
|
||||||
const button = container.createEl("button", {
|
const button = container.createEl("button", {
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,8 @@ import type {
|
||||||
SyncService,
|
SyncService,
|
||||||
SyncHistory,
|
SyncHistory,
|
||||||
Syncer,
|
Syncer,
|
||||||
Database
|
Database,
|
||||||
|
Settings
|
||||||
} from "sync-client";
|
} from "sync-client";
|
||||||
|
|
||||||
export class StatusDescription {
|
export class StatusDescription {
|
||||||
|
|
@ -15,6 +16,7 @@ export class StatusDescription {
|
||||||
private statusChangeListeners: (() => void)[] = [];
|
private statusChangeListeners: (() => void)[] = [];
|
||||||
|
|
||||||
public constructor(
|
public constructor(
|
||||||
|
private readonly settings: Settings,
|
||||||
private readonly database: Database,
|
private readonly database: Database,
|
||||||
private readonly syncService: SyncService,
|
private readonly syncService: SyncService,
|
||||||
history: SyncHistory,
|
history: SyncHistory,
|
||||||
|
|
@ -32,7 +34,7 @@ export class StatusDescription {
|
||||||
this.updateDescription();
|
this.updateDescription();
|
||||||
});
|
});
|
||||||
|
|
||||||
database.addOnSettingsChangeHandlers(() => {
|
settings.addOnSettingsChangeHandlers(() => {
|
||||||
void this.updateConnectionState();
|
void this.updateConnectionState();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
@ -73,8 +75,8 @@ export class StatusDescription {
|
||||||
|
|
||||||
container.createSpan({ text: "VaultLink is connected to the server " });
|
container.createSpan({ text: "VaultLink is connected to the server " });
|
||||||
container.createEl("a", {
|
container.createEl("a", {
|
||||||
text: this.database.getSettings().remoteUri,
|
text: this.settings.getSettings().remoteUri,
|
||||||
href: this.database.getSettings().remoteUri
|
href: this.settings.getSettings().remoteUri
|
||||||
});
|
});
|
||||||
|
|
||||||
container.createSpan({
|
container.createSpan({
|
||||||
|
|
@ -93,7 +95,7 @@ export class StatusDescription {
|
||||||
(this.lastHistoryStats?.success ?? 0) === 0 &&
|
(this.lastHistoryStats?.success ?? 0) === 0 &&
|
||||||
(this.lastHistoryStats?.error ?? 0) === 0
|
(this.lastHistoryStats?.error ?? 0) === 0
|
||||||
) {
|
) {
|
||||||
if (this.database.getSettings().isSyncEnabled) {
|
if (this.settings.getSettings().isSyncEnabled) {
|
||||||
container.createSpan({
|
container.createSpan({
|
||||||
text: "Syncing is enabled but VaultLink hasn't found anything to sync yet."
|
text: "Syncing is enabled but VaultLink hasn't found anything to sync yet."
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -1,186 +0,0 @@
|
||||||
import type { SyncSettings } from "./sync-settings";
|
|
||||||
import { DEFAULT_SETTINGS } from "./sync-settings";
|
|
||||||
import type {
|
|
||||||
DocumentId,
|
|
||||||
DocumentMetadata,
|
|
||||||
RelativePath,
|
|
||||||
VaultUpdateId
|
|
||||||
} from "./document-metadata";
|
|
||||||
import { Logger } from "src/tracing/logger";
|
|
||||||
|
|
||||||
interface StoredDatabase {
|
|
||||||
documents: Map<RelativePath, DocumentMetadata>;
|
|
||||||
settings: SyncSettings;
|
|
||||||
lastSeenUpdateId: VaultUpdateId | undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Todo: split it into settings and documents
|
|
||||||
export class Database {
|
|
||||||
private _documents = new Map<RelativePath, DocumentMetadata>();
|
|
||||||
private _settings: SyncSettings;
|
|
||||||
private _lastSeenUpdateId: VaultUpdateId | undefined;
|
|
||||||
|
|
||||||
private readonly onSettingsChangeHandlers: ((
|
|
||||||
newSettings: SyncSettings,
|
|
||||||
oldSettings: SyncSettings
|
|
||||||
) => void)[] = [];
|
|
||||||
|
|
||||||
public constructor(
|
|
||||||
initialState: Partial<StoredDatabase> | undefined,
|
|
||||||
private readonly saveData: (data: unknown) => Promise<void>
|
|
||||||
) {
|
|
||||||
initialState ??= {};
|
|
||||||
if (
|
|
||||||
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
|
|
||||||
Object.prototype.hasOwnProperty.call(initialState, "documents") &&
|
|
||||||
initialState.documents
|
|
||||||
) {
|
|
||||||
for (const [relativePath, metadata] of Object.entries(
|
|
||||||
initialState.documents
|
|
||||||
)) {
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
|
|
||||||
this._documents.set(relativePath, metadata as DocumentMetadata);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Logger.getInstance().debug(`Loaded ${this._documents.size} documents`);
|
|
||||||
|
|
||||||
this._settings = {
|
|
||||||
...DEFAULT_SETTINGS,
|
|
||||||
...(initialState.settings ?? {})
|
|
||||||
};
|
|
||||||
|
|
||||||
Logger.getInstance().debug(
|
|
||||||
`Loaded settings: ${JSON.stringify(this._settings, null, 2)}`
|
|
||||||
);
|
|
||||||
|
|
||||||
this._lastSeenUpdateId = initialState.lastSeenUpdateId;
|
|
||||||
|
|
||||||
Logger.getInstance().debug(
|
|
||||||
`Loaded last seen update id: ${this._lastSeenUpdateId}`
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
public getDocuments(): Map<RelativePath, DocumentMetadata> {
|
|
||||||
return this._documents;
|
|
||||||
}
|
|
||||||
|
|
||||||
public getSettings(): SyncSettings {
|
|
||||||
return this._settings;
|
|
||||||
}
|
|
||||||
|
|
||||||
public async setSettings(value: SyncSettings): Promise<void> {
|
|
||||||
const oldSettings = this._settings;
|
|
||||||
this._settings = value;
|
|
||||||
this.onSettingsChangeHandlers.forEach((handler) => {
|
|
||||||
handler(value, oldSettings);
|
|
||||||
});
|
|
||||||
await this.save();
|
|
||||||
}
|
|
||||||
|
|
||||||
public addOnSettingsChangeHandlers(
|
|
||||||
handler: (settings: SyncSettings, oldSettings: SyncSettings) => void
|
|
||||||
): void {
|
|
||||||
this.onSettingsChangeHandlers.push(handler);
|
|
||||||
}
|
|
||||||
|
|
||||||
public async setSetting<T extends keyof SyncSettings>(
|
|
||||||
key: T,
|
|
||||||
value: SyncSettings[T]
|
|
||||||
): Promise<void> {
|
|
||||||
const newSettings = { ...this._settings, [key]: value };
|
|
||||||
Logger.getInstance().debug(
|
|
||||||
`Setting ${key} to ${value}, new settings: ${JSON.stringify(
|
|
||||||
newSettings,
|
|
||||||
null,
|
|
||||||
2
|
|
||||||
)}`
|
|
||||||
);
|
|
||||||
await this.setSettings(newSettings);
|
|
||||||
}
|
|
||||||
|
|
||||||
public getLastSeenUpdateId(): VaultUpdateId | undefined {
|
|
||||||
return this._lastSeenUpdateId;
|
|
||||||
}
|
|
||||||
|
|
||||||
public async setLastSeenUpdateId(
|
|
||||||
value: VaultUpdateId | undefined
|
|
||||||
): Promise<void> {
|
|
||||||
this._lastSeenUpdateId = value;
|
|
||||||
await this.save();
|
|
||||||
}
|
|
||||||
|
|
||||||
public async resetSyncState(): Promise<void> {
|
|
||||||
this._documents = new Map();
|
|
||||||
this._lastSeenUpdateId = 0;
|
|
||||||
await this.save();
|
|
||||||
}
|
|
||||||
|
|
||||||
public getDocumentByDocumentId(
|
|
||||||
documentId: DocumentId
|
|
||||||
): [RelativePath, DocumentMetadata] | undefined {
|
|
||||||
return [...this._documents.entries()].find(
|
|
||||||
([_, metadata]) => metadata.documentId === documentId
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
public async setDocument({
|
|
||||||
documentId,
|
|
||||||
relativePath,
|
|
||||||
parentVersionId,
|
|
||||||
hash
|
|
||||||
}: {
|
|
||||||
documentId: DocumentId;
|
|
||||||
relativePath: RelativePath;
|
|
||||||
parentVersionId: VaultUpdateId;
|
|
||||||
hash: string;
|
|
||||||
}): Promise<void> {
|
|
||||||
this._documents.set(relativePath, {
|
|
||||||
documentId,
|
|
||||||
parentVersionId,
|
|
||||||
hash
|
|
||||||
});
|
|
||||||
await this.save();
|
|
||||||
}
|
|
||||||
|
|
||||||
public async moveDocument({
|
|
||||||
documentId,
|
|
||||||
oldRelativePath,
|
|
||||||
relativePath,
|
|
||||||
parentVersionId,
|
|
||||||
hash
|
|
||||||
}: {
|
|
||||||
documentId: DocumentId;
|
|
||||||
oldRelativePath: RelativePath;
|
|
||||||
relativePath: RelativePath;
|
|
||||||
parentVersionId: VaultUpdateId;
|
|
||||||
hash: string;
|
|
||||||
}): Promise<void> {
|
|
||||||
this._documents.delete(oldRelativePath);
|
|
||||||
this._documents.set(relativePath, {
|
|
||||||
documentId,
|
|
||||||
parentVersionId,
|
|
||||||
hash
|
|
||||||
});
|
|
||||||
await this.save();
|
|
||||||
}
|
|
||||||
|
|
||||||
public async removeDocument(relativePath: RelativePath): Promise<void> {
|
|
||||||
this._documents.delete(relativePath);
|
|
||||||
await this.save();
|
|
||||||
}
|
|
||||||
|
|
||||||
public getDocument(
|
|
||||||
relativePath: RelativePath
|
|
||||||
): DocumentMetadata | undefined {
|
|
||||||
return this._documents.get(relativePath);
|
|
||||||
}
|
|
||||||
|
|
||||||
private async save(): Promise<void> {
|
|
||||||
await this.saveData({
|
|
||||||
documents: Object.fromEntries(this._documents.entries()),
|
|
||||||
settings: this._settings,
|
|
||||||
lastSeenUpdateId: this._lastSeenUpdateId
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,9 +0,0 @@
|
||||||
export type VaultUpdateId = number;
|
|
||||||
export type DocumentId = string;
|
|
||||||
export type RelativePath = string;
|
|
||||||
|
|
||||||
export interface DocumentMetadata {
|
|
||||||
parentVersionId: VaultUpdateId;
|
|
||||||
documentId: DocumentId;
|
|
||||||
hash: string;
|
|
||||||
}
|
|
||||||
|
|
@ -1,25 +0,0 @@
|
||||||
import { LogLevel } from "src/tracing/logger";
|
|
||||||
|
|
||||||
export interface SyncSettings {
|
|
||||||
remoteUri: string;
|
|
||||||
token: string;
|
|
||||||
vaultName: string;
|
|
||||||
fetchChangesUpdateIntervalMs: number;
|
|
||||||
syncConcurrency: number;
|
|
||||||
isSyncEnabled: boolean;
|
|
||||||
displayNoopSyncEvents: boolean;
|
|
||||||
minimumLogLevel: LogLevel;
|
|
||||||
maxFileSizeMB: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
export const DEFAULT_SETTINGS: SyncSettings = {
|
|
||||||
remoteUri: "",
|
|
||||||
token: "",
|
|
||||||
vaultName: "default",
|
|
||||||
fetchChangesUpdateIntervalMs: 1000,
|
|
||||||
syncConcurrency: 1,
|
|
||||||
isSyncEnabled: false,
|
|
||||||
displayNoopSyncEvents: false,
|
|
||||||
minimumLogLevel: LogLevel.INFO,
|
|
||||||
maxFileSizeMB: 10
|
|
||||||
};
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
import type { RelativePath } from "src/database/document-metadata";
|
import type { RelativePath } from "src/persistence/database";
|
||||||
|
|
||||||
export interface FileOperations {
|
export interface FileOperations {
|
||||||
listAllFiles: () => Promise<RelativePath[]>;
|
listAllFiles: () => Promise<RelativePath[]>;
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,14 @@
|
||||||
export { applyRemoteChangesLocally } from "./sync-operations/apply-remote-changes-locally";
|
export { applyRemoteChangesLocally } from "./sync-operations/apply-remote-changes-locally";
|
||||||
|
|
||||||
export {
|
export {
|
||||||
|
Database,
|
||||||
type RelativePath,
|
type RelativePath,
|
||||||
type DocumentId,
|
type DocumentId,
|
||||||
type VaultUpdateId,
|
type VaultUpdateId,
|
||||||
type DocumentMetadata
|
type DocumentMetadata
|
||||||
} from "./database/document-metadata";
|
} from "./persistence/database";
|
||||||
|
|
||||||
export { Database } from "./database/database";
|
export { Settings, type SyncSettings } from "./persistence/settings";
|
||||||
|
|
||||||
export {
|
export {
|
||||||
SyncService,
|
SyncService,
|
||||||
|
|
@ -32,12 +33,6 @@ export { type FileOperations } from "./file-operations";
|
||||||
import init from "sync_lib";
|
import init from "sync_lib";
|
||||||
import wasmBin from "sync_lib/sync_lib_bg.wasm";
|
import wasmBin from "sync_lib/sync_lib_bg.wasm";
|
||||||
|
|
||||||
export const initialize = async (): Promise<void> => {
|
|
||||||
await init(
|
|
||||||
// eslint-disable-next-line
|
|
||||||
(wasmBin as any).default // it is loaded as a base64 string by webpack
|
|
||||||
);
|
|
||||||
};
|
|
||||||
export {
|
export {
|
||||||
isFileTypeMergable,
|
isFileTypeMergable,
|
||||||
mergeText,
|
mergeText,
|
||||||
|
|
@ -46,3 +41,10 @@ export {
|
||||||
merge,
|
merge,
|
||||||
isBinary
|
isBinary
|
||||||
} from "sync_lib";
|
} from "sync_lib";
|
||||||
|
|
||||||
|
export const initialize = async (): Promise<void> => {
|
||||||
|
await init(
|
||||||
|
// eslint-disable-next-line
|
||||||
|
(wasmBin as any).default // it is loaded as a base64 string by webpack
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
|
||||||
130
frontend/sync-client/src/persistence/database.ts
Normal file
130
frontend/sync-client/src/persistence/database.ts
Normal file
|
|
@ -0,0 +1,130 @@
|
||||||
|
export type VaultUpdateId = number;
|
||||||
|
export type DocumentId = string;
|
||||||
|
export type RelativePath = string;
|
||||||
|
|
||||||
|
export interface DocumentMetadata {
|
||||||
|
parentVersionId: VaultUpdateId;
|
||||||
|
documentId: DocumentId;
|
||||||
|
hash: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
import { Logger } from "src/tracing/logger";
|
||||||
|
|
||||||
|
export interface StoredDatabase {
|
||||||
|
documents: Map<RelativePath, DocumentMetadata>;
|
||||||
|
lastSeenUpdateId: VaultUpdateId | undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class Database {
|
||||||
|
private documents = new Map<RelativePath, DocumentMetadata>();
|
||||||
|
private lastSeenUpdateId: VaultUpdateId | undefined;
|
||||||
|
|
||||||
|
public constructor(
|
||||||
|
initialState: Partial<StoredDatabase> | undefined,
|
||||||
|
private readonly saveData: (data: unknown) => Promise<void>
|
||||||
|
) {
|
||||||
|
initialState ??= {};
|
||||||
|
if (initialState.documents) {
|
||||||
|
for (const [relativePath, metadata] of Object.entries(
|
||||||
|
initialState.documents
|
||||||
|
)) {
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
|
||||||
|
this.documents.set(relativePath, metadata as DocumentMetadata);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Logger.getInstance().debug(`Loaded ${this.documents.size} documents`);
|
||||||
|
|
||||||
|
this.lastSeenUpdateId = initialState.lastSeenUpdateId;
|
||||||
|
Logger.getInstance().debug(
|
||||||
|
`Loaded last seen update id: ${this.lastSeenUpdateId}`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public getDocuments(): Map<RelativePath, DocumentMetadata> {
|
||||||
|
return this.documents;
|
||||||
|
}
|
||||||
|
|
||||||
|
public getLastSeenUpdateId(): VaultUpdateId | undefined {
|
||||||
|
return this.lastSeenUpdateId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async setLastSeenUpdateId(
|
||||||
|
value: VaultUpdateId | undefined
|
||||||
|
): Promise<void> {
|
||||||
|
this.lastSeenUpdateId = value;
|
||||||
|
await this.save();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async resetSyncState(): Promise<void> {
|
||||||
|
this.documents = new Map();
|
||||||
|
this.lastSeenUpdateId = 0;
|
||||||
|
await this.save();
|
||||||
|
}
|
||||||
|
|
||||||
|
public getDocumentByDocumentId(
|
||||||
|
documentId: DocumentId
|
||||||
|
): [RelativePath, DocumentMetadata] | undefined {
|
||||||
|
return [...this.documents.entries()].find(
|
||||||
|
([_, metadata]) => metadata.documentId === documentId
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async setDocument({
|
||||||
|
documentId,
|
||||||
|
relativePath,
|
||||||
|
parentVersionId,
|
||||||
|
hash
|
||||||
|
}: {
|
||||||
|
documentId: DocumentId;
|
||||||
|
relativePath: RelativePath;
|
||||||
|
parentVersionId: VaultUpdateId;
|
||||||
|
hash: string;
|
||||||
|
}): Promise<void> {
|
||||||
|
this.documents.set(relativePath, {
|
||||||
|
documentId,
|
||||||
|
parentVersionId,
|
||||||
|
hash
|
||||||
|
});
|
||||||
|
await this.save();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async moveDocument({
|
||||||
|
documentId,
|
||||||
|
oldRelativePath,
|
||||||
|
relativePath,
|
||||||
|
parentVersionId,
|
||||||
|
hash
|
||||||
|
}: {
|
||||||
|
documentId: DocumentId;
|
||||||
|
oldRelativePath: RelativePath;
|
||||||
|
relativePath: RelativePath;
|
||||||
|
parentVersionId: VaultUpdateId;
|
||||||
|
hash: string;
|
||||||
|
}): Promise<void> {
|
||||||
|
this.documents.delete(oldRelativePath);
|
||||||
|
this.documents.set(relativePath, {
|
||||||
|
documentId,
|
||||||
|
parentVersionId,
|
||||||
|
hash
|
||||||
|
});
|
||||||
|
await this.save();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async removeDocument(relativePath: RelativePath): Promise<void> {
|
||||||
|
this.documents.delete(relativePath);
|
||||||
|
await this.save();
|
||||||
|
}
|
||||||
|
|
||||||
|
public getDocument(
|
||||||
|
relativePath: RelativePath
|
||||||
|
): DocumentMetadata | undefined {
|
||||||
|
return this.documents.get(relativePath);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async save(): Promise<void> {
|
||||||
|
await this.saveData({
|
||||||
|
documents: Object.fromEntries(this.documents.entries()),
|
||||||
|
lastSeenUpdateId: this.lastSeenUpdateId
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
86
frontend/sync-client/src/persistence/settings.ts
Normal file
86
frontend/sync-client/src/persistence/settings.ts
Normal file
|
|
@ -0,0 +1,86 @@
|
||||||
|
import { Logger, LogLevel } from "src/tracing/logger";
|
||||||
|
|
||||||
|
export interface SyncSettings {
|
||||||
|
remoteUri: string;
|
||||||
|
token: string;
|
||||||
|
vaultName: string;
|
||||||
|
fetchChangesUpdateIntervalMs: number;
|
||||||
|
syncConcurrency: number;
|
||||||
|
isSyncEnabled: boolean;
|
||||||
|
displayNoopSyncEvents: boolean;
|
||||||
|
minimumLogLevel: LogLevel;
|
||||||
|
maxFileSizeMB: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
const DEFAULT_SETTINGS: SyncSettings = {
|
||||||
|
remoteUri: "",
|
||||||
|
token: "",
|
||||||
|
vaultName: "default",
|
||||||
|
fetchChangesUpdateIntervalMs: 1000,
|
||||||
|
syncConcurrency: 1,
|
||||||
|
isSyncEnabled: false,
|
||||||
|
displayNoopSyncEvents: false,
|
||||||
|
minimumLogLevel: LogLevel.INFO,
|
||||||
|
maxFileSizeMB: 10
|
||||||
|
};
|
||||||
|
|
||||||
|
export class Settings {
|
||||||
|
private settings: SyncSettings;
|
||||||
|
|
||||||
|
private readonly onSettingsChangeHandlers: ((
|
||||||
|
newSettings: SyncSettings,
|
||||||
|
oldSettings: SyncSettings
|
||||||
|
) => void)[] = [];
|
||||||
|
|
||||||
|
public constructor(
|
||||||
|
initialState: Partial<SyncSettings> | undefined,
|
||||||
|
private readonly saveData: (data: unknown) => Promise<void>
|
||||||
|
) {
|
||||||
|
this.settings = {
|
||||||
|
...DEFAULT_SETTINGS,
|
||||||
|
...(initialState ?? {})
|
||||||
|
};
|
||||||
|
|
||||||
|
Logger.getInstance().debug(
|
||||||
|
`Loaded settings: ${JSON.stringify(this.settings, null, 2)}`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public getSettings(): SyncSettings {
|
||||||
|
return this.settings;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async setSettings(value: SyncSettings): Promise<void> {
|
||||||
|
const oldSettings = this.settings;
|
||||||
|
this.settings = value;
|
||||||
|
this.onSettingsChangeHandlers.forEach((handler) => {
|
||||||
|
handler(value, oldSettings);
|
||||||
|
});
|
||||||
|
await this.save();
|
||||||
|
}
|
||||||
|
|
||||||
|
public addOnSettingsChangeHandlers(
|
||||||
|
handler: (settings: SyncSettings, oldSettings: SyncSettings) => void
|
||||||
|
): void {
|
||||||
|
this.onSettingsChangeHandlers.push(handler);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async setSetting<T extends keyof SyncSettings>(
|
||||||
|
key: T,
|
||||||
|
value: SyncSettings[T]
|
||||||
|
): Promise<void> {
|
||||||
|
const newSettings = { ...this.settings, [key]: value };
|
||||||
|
Logger.getInstance().debug(
|
||||||
|
`Setting ${key} to ${value}, new settings: ${JSON.stringify(
|
||||||
|
newSettings,
|
||||||
|
null,
|
||||||
|
2
|
||||||
|
)}`
|
||||||
|
);
|
||||||
|
await this.setSettings(newSettings);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async save(): Promise<void> {
|
||||||
|
await this.saveData(this.settings);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,15 +1,15 @@
|
||||||
import type { Client } from "openapi-fetch";
|
import type { Client } from "openapi-fetch";
|
||||||
import createClient from "openapi-fetch";
|
import createClient from "openapi-fetch";
|
||||||
import type { components, paths } from "./types"; // Generated by openapi-typescript
|
import type { components, paths } from "./types"; // Generated by openapi-typescript
|
||||||
import type { Database } from "../database/database";
|
|
||||||
import type { SyncSettings } from "../database/sync-settings";
|
|
||||||
import type {
|
import type {
|
||||||
DocumentId,
|
DocumentId,
|
||||||
RelativePath,
|
RelativePath,
|
||||||
VaultUpdateId
|
VaultUpdateId
|
||||||
} from "src/database/document-metadata";
|
} from "../persistence/database";
|
||||||
import { Logger } from "src/tracing/logger";
|
import { Logger } from "src/tracing/logger";
|
||||||
import { retriedFetch } from "src/utils/retried-fetch";
|
import { retriedFetch } from "src/utils/retried-fetch";
|
||||||
|
import type { SyncSettings } from "dist/types";
|
||||||
|
import type { Settings } from "src/persistence/settings";
|
||||||
|
|
||||||
export interface CheckConnectionResult {
|
export interface CheckConnectionResult {
|
||||||
isSuccessful: boolean;
|
isSuccessful: boolean;
|
||||||
|
|
@ -19,10 +19,10 @@ export class SyncService {
|
||||||
private client: Client<paths>;
|
private client: Client<paths>;
|
||||||
private clientWithoutRetries: Client<paths>;
|
private clientWithoutRetries: Client<paths>;
|
||||||
|
|
||||||
public constructor(private readonly database: Database) {
|
public constructor(private readonly settings: Settings) {
|
||||||
this.createClient(database.getSettings());
|
this.createClient(settings.getSettings());
|
||||||
|
|
||||||
database.addOnSettingsChangeHandlers((s) => {
|
settings.addOnSettingsChangeHandlers((s) => {
|
||||||
this.createClient(s);
|
this.createClient(s);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
@ -43,7 +43,7 @@ export class SyncService {
|
||||||
const response = await this.clientWithoutRetries.GET("/ping", {
|
const response = await this.clientWithoutRetries.GET("/ping", {
|
||||||
params: {
|
params: {
|
||||||
header: {
|
header: {
|
||||||
authorization: `Bearer ${this.database.getSettings().token}`
|
authorization: `Bearer ${this.settings.getSettings().token}`
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
@ -80,10 +80,10 @@ export class SyncService {
|
||||||
{
|
{
|
||||||
params: {
|
params: {
|
||||||
path: {
|
path: {
|
||||||
vault_id: this.database.getSettings().vaultName
|
vault_id: this.settings.getSettings().vaultName
|
||||||
},
|
},
|
||||||
header: {
|
header: {
|
||||||
authorization: `Bearer ${this.database.getSettings().token}`
|
authorization: `Bearer ${this.settings.getSettings().token}`
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
// eslint-disable-next-line
|
// eslint-disable-next-line
|
||||||
|
|
@ -130,11 +130,11 @@ export class SyncService {
|
||||||
{
|
{
|
||||||
params: {
|
params: {
|
||||||
path: {
|
path: {
|
||||||
vault_id: this.database.getSettings().vaultName,
|
vault_id: this.settings.getSettings().vaultName,
|
||||||
document_id: documentId
|
document_id: documentId
|
||||||
},
|
},
|
||||||
header: {
|
header: {
|
||||||
authorization: `Bearer ${this.database.getSettings().token}`
|
authorization: `Bearer ${this.settings.getSettings().token}`
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
// eslint-disable-next-line
|
// eslint-disable-next-line
|
||||||
|
|
@ -171,11 +171,11 @@ export class SyncService {
|
||||||
{
|
{
|
||||||
params: {
|
params: {
|
||||||
path: {
|
path: {
|
||||||
vault_id: this.database.getSettings().vaultName,
|
vault_id: this.settings.getSettings().vaultName,
|
||||||
document_id: documentId
|
document_id: documentId
|
||||||
},
|
},
|
||||||
header: {
|
header: {
|
||||||
authorization: `Bearer ${this.database.getSettings().token}`
|
authorization: `Bearer ${this.settings.getSettings().token}`
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
body: {
|
body: {
|
||||||
|
|
@ -206,11 +206,11 @@ export class SyncService {
|
||||||
{
|
{
|
||||||
params: {
|
params: {
|
||||||
path: {
|
path: {
|
||||||
vault_id: this.database.getSettings().vaultName,
|
vault_id: this.settings.getSettings().vaultName,
|
||||||
document_id: documentId
|
document_id: documentId
|
||||||
},
|
},
|
||||||
header: {
|
header: {
|
||||||
authorization: `Bearer ${this.database.getSettings().token}`
|
authorization: `Bearer ${this.settings.getSettings().token}`
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -235,10 +235,10 @@ export class SyncService {
|
||||||
const response = await this.client.GET("/vaults/{vault_id}/documents", {
|
const response = await this.client.GET("/vaults/{vault_id}/documents", {
|
||||||
params: {
|
params: {
|
||||||
path: {
|
path: {
|
||||||
vault_id: this.database.getSettings().vaultName
|
vault_id: this.settings.getSettings().vaultName
|
||||||
},
|
},
|
||||||
header: {
|
header: {
|
||||||
authorization: `Bearer ${this.database.getSettings().token}`
|
authorization: `Bearer ${this.settings.getSettings().token}`
|
||||||
},
|
},
|
||||||
query: {
|
query: {
|
||||||
since_update_id: since
|
since_update_id: since
|
||||||
|
|
|
||||||
|
|
@ -1,20 +1,23 @@
|
||||||
import type { Database } from "../database/database";
|
import type { Database } from "../persistence/database";
|
||||||
import type { SyncService } from "src/services/sync-service";
|
import type { SyncService } from "src/services/sync-service";
|
||||||
import { Logger } from "src/tracing/logger";
|
import { Logger } from "src/tracing/logger";
|
||||||
import type { Syncer } from "./syncer";
|
import type { Syncer } from "./syncer";
|
||||||
|
import type { Settings } from "src/persistence/settings";
|
||||||
|
|
||||||
let isRunning = false;
|
let isRunning = false;
|
||||||
|
|
||||||
export async function applyRemoteChangesLocally({
|
export async function applyRemoteChangesLocally({
|
||||||
|
settings,
|
||||||
database,
|
database,
|
||||||
syncService,
|
syncService,
|
||||||
syncer
|
syncer
|
||||||
}: {
|
}: {
|
||||||
|
settings: Settings;
|
||||||
database: Database;
|
database: Database;
|
||||||
syncService: SyncService;
|
syncService: SyncService;
|
||||||
syncer: Syncer;
|
syncer: Syncer;
|
||||||
}): Promise<void> {
|
}): Promise<void> {
|
||||||
if (!database.getSettings().isSyncEnabled) {
|
if (!settings.getSettings().isSyncEnabled) {
|
||||||
Logger.getInstance().debug(
|
Logger.getInstance().debug(
|
||||||
`Syncing is disabled, not fetching remote changes`
|
`Syncing is disabled, not fetching remote changes`
|
||||||
);
|
);
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
import { RelativePath } from "../database/document-metadata";
|
import { RelativePath } from "../persistence/database";
|
||||||
import {
|
import {
|
||||||
tryLockDocument,
|
tryLockDocument,
|
||||||
waitForDocumentLock,
|
waitForDocumentLock,
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
import type { RelativePath } from "../database/document-metadata";
|
import type { RelativePath } from "../persistence/database";
|
||||||
|
|
||||||
const locked = new Set<RelativePath>();
|
const locked = new Set<RelativePath>();
|
||||||
const waiters = new Map<RelativePath, (() => void)[]>();
|
const waiters = new Map<RelativePath, (() => void)[]>();
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,9 @@
|
||||||
import type { Database } from "../database/database";
|
|
||||||
import type {
|
import type {
|
||||||
|
Database,
|
||||||
DocumentMetadata,
|
DocumentMetadata,
|
||||||
RelativePath
|
RelativePath
|
||||||
} from "src/database/document-metadata";
|
} from "../persistence/database";
|
||||||
|
|
||||||
import type { FileOperations } from "src/file-operations";
|
import type { FileOperations } from "src/file-operations";
|
||||||
import type { SyncService } from "src/services/sync-service";
|
import type { SyncService } from "src/services/sync-service";
|
||||||
import { Logger } from "src/tracing/logger";
|
import { Logger } from "src/tracing/logger";
|
||||||
|
|
@ -13,6 +14,7 @@ import PQueue from "p-queue";
|
||||||
import { EMPTY_HASH, hash } from "src/utils/hash";
|
import { EMPTY_HASH, hash } from "src/utils/hash";
|
||||||
import type { components } from "src/services/types";
|
import type { components } from "src/services/types";
|
||||||
import { deserialize } from "src/utils/deserialize";
|
import { deserialize } from "src/utils/deserialize";
|
||||||
|
import type { Settings } from "src/persistence/settings";
|
||||||
|
|
||||||
export class Syncer {
|
export class Syncer {
|
||||||
private readonly remainingOperationsListeners: ((
|
private readonly remainingOperationsListeners: ((
|
||||||
|
|
@ -25,16 +27,17 @@ export class Syncer {
|
||||||
|
|
||||||
public constructor(
|
public constructor(
|
||||||
private readonly database: Database,
|
private readonly database: Database,
|
||||||
|
private readonly settings: Settings,
|
||||||
private readonly syncService: SyncService,
|
private readonly syncService: SyncService,
|
||||||
private readonly operations: FileOperations,
|
private readonly operations: FileOperations,
|
||||||
private readonly history: SyncHistory
|
private readonly history: SyncHistory
|
||||||
) {
|
) {
|
||||||
this.syncQueue = new PQueue({
|
this.syncQueue = new PQueue({
|
||||||
concurrency: database.getSettings().syncConcurrency
|
concurrency: settings.getSettings().syncConcurrency
|
||||||
});
|
});
|
||||||
|
|
||||||
database.addOnSettingsChangeHandlers((settings) => {
|
settings.addOnSettingsChangeHandlers((newSettings) => {
|
||||||
this.syncQueue.concurrency = settings.syncConcurrency;
|
this.syncQueue.concurrency = newSettings.syncConcurrency;
|
||||||
});
|
});
|
||||||
|
|
||||||
this.syncQueue.on("active", () => {
|
this.syncQueue.on("active", () => {
|
||||||
|
|
@ -91,7 +94,7 @@ export class Syncer {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!this.database.getSettings().isSyncEnabled) {
|
if (!this.settings.getSettings().isSyncEnabled) {
|
||||||
Logger.getInstance().debug(
|
Logger.getInstance().debug(
|
||||||
`Syncing is disabled, not uploading local changes`
|
`Syncing is disabled, not uploading local changes`
|
||||||
);
|
);
|
||||||
|
|
@ -229,13 +232,13 @@ export class Syncer {
|
||||||
(await this.operations.getFileSize(relativePath)) /
|
(await this.operations.getFileSize(relativePath)) /
|
||||||
1024 /
|
1024 /
|
||||||
1024 >
|
1024 >
|
||||||
this.database.getSettings().maxFileSizeMB
|
this.settings.getSettings().maxFileSizeMB
|
||||||
) {
|
) {
|
||||||
this.history.addHistoryEntry({
|
this.history.addHistoryEntry({
|
||||||
status: SyncStatus.ERROR,
|
status: SyncStatus.ERROR,
|
||||||
relativePath,
|
relativePath,
|
||||||
message: `File size exceeds the maximum file size limit of ${
|
message: `File size exceeds the maximum file size limit of ${
|
||||||
this.database.getSettings().maxFileSizeMB
|
this.settings.getSettings().maxFileSizeMB
|
||||||
}MB`,
|
}MB`,
|
||||||
type: SyncType.CREATE
|
type: SyncType.CREATE
|
||||||
});
|
});
|
||||||
|
|
@ -332,13 +335,13 @@ export class Syncer {
|
||||||
(await this.operations.getFileSize(relativePath)) /
|
(await this.operations.getFileSize(relativePath)) /
|
||||||
1024 /
|
1024 /
|
||||||
1024 >
|
1024 >
|
||||||
this.database.getSettings().maxFileSizeMB
|
this.settings.getSettings().maxFileSizeMB
|
||||||
) {
|
) {
|
||||||
this.history.addHistoryEntry({
|
this.history.addHistoryEntry({
|
||||||
status: SyncStatus.ERROR,
|
status: SyncStatus.ERROR,
|
||||||
relativePath,
|
relativePath,
|
||||||
message: `File size exceeds the maximum file size limit of ${
|
message: `File size exceeds the maximum file size limit of ${
|
||||||
this.database.getSettings().maxFileSizeMB
|
this.settings.getSettings().maxFileSizeMB
|
||||||
}MB`,
|
}MB`,
|
||||||
type: SyncType.CREATE
|
type: SyncType.CREATE
|
||||||
});
|
});
|
||||||
|
|
@ -648,7 +651,7 @@ export class Syncer {
|
||||||
syncSource: SyncSource,
|
syncSource: SyncSource,
|
||||||
fn: () => Promise<void>
|
fn: () => Promise<void>
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
if (!this.database.getSettings().isSyncEnabled) {
|
if (!this.settings.getSettings().isSyncEnabled) {
|
||||||
Logger.getInstance().info(
|
Logger.getInstance().info(
|
||||||
`Syncing is disabled, not syncing ${relativePath}`
|
`Syncing is disabled, not syncing ${relativePath}`
|
||||||
);
|
);
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,7 @@ const LOG_LEVEL_ORDER = {
|
||||||
[LogLevel.ERROR]: 3
|
[LogLevel.ERROR]: 3
|
||||||
};
|
};
|
||||||
|
|
||||||
class LogLine {
|
export class LogLine {
|
||||||
public timestamp = new Date();
|
public timestamp = new Date();
|
||||||
public constructor(
|
public constructor(
|
||||||
public level: LogLevel,
|
public level: LogLevel,
|
||||||
|
|
@ -46,19 +46,16 @@ export class Logger {
|
||||||
|
|
||||||
public info(message: string): void {
|
public info(message: string): void {
|
||||||
console.info(message);
|
console.info(message);
|
||||||
|
|
||||||
this.pushMessage(message, LogLevel.INFO);
|
this.pushMessage(message, LogLevel.INFO);
|
||||||
}
|
}
|
||||||
|
|
||||||
public warn(message: string): void {
|
public warn(message: string): void {
|
||||||
console.warn(message);
|
console.warn(message);
|
||||||
|
|
||||||
this.pushMessage(message, LogLevel.WARNING);
|
this.pushMessage(message, LogLevel.WARNING);
|
||||||
}
|
}
|
||||||
|
|
||||||
public error(message: string): void {
|
public error(message: string): void {
|
||||||
console.error(message);
|
console.error(message);
|
||||||
|
|
||||||
this.pushMessage(message, LogLevel.ERROR);
|
this.pushMessage(message, LogLevel.ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
import type { RelativePath } from "src/database/document-metadata";
|
import { RelativePath } from "src/persistence/database";
|
||||||
import { Logger } from "./logger";
|
import { Logger } from "./logger";
|
||||||
|
|
||||||
export interface CommonHistoryEntry {
|
export interface CommonHistoryEntry {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue