Skip to content
This repository was archived by the owner on Jul 28, 2025. It is now read-only.

Commit d1429ff

Browse files
committed
Merge branch 'wait_dataview_until_is_mapped_at_start'
2 parents e53eadb + b354471 commit d1429ff

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

src/main.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import { DatabaseHelperCreationModal } from 'commands/addDatabaseHelper/database
3737
import { generateDbConfiguration, generateNewDatabase } from 'helpers/CommandsHelper';
3838
import { t } from 'lang/helpers';
3939
import ProjectAPI from 'api/obsidian-projects-api';
40+
import { DataviewService } from 'services/DataviewService';
4041
interface WindowRegistry {
4142
viewMap: Map<string, DatabaseView>;
4243
viewStateReceivers: Array<(views: DatabaseView[]) => void>;
@@ -397,7 +398,15 @@ export default class DBFolderPlugin extends Plugin {
397398
}
398399
})
399400
);
401+
402+
this.registerEvent(
403+
// @ts-ignore
404+
this.app.metadataCache.on("dataview:index-ready", () => {
405+
DataviewService.setIndexIsLoaded(true);
406+
})
407+
);
400408
}
409+
401410
registerCommands() {
402411
// Creator Helper Command
403412
this.addCommand({

src/services/DataviewService.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,24 @@ import { Literal, WrappedLiteral } from "obsidian-dataview/lib/data-model/value"
44
class DataviewProxy {
55

66
private static instance: DataviewProxy;
7+
private indexIsLoaded: boolean = false;
8+
79
/**
810
* Check if dataview plugin is installed
911
* @returns true if installed, false otherwise
1012
* @throws Error if plugin is not installed
1113
*/
1214
getDataviewAPI(): DataviewApi {
13-
if (isPluginEnabled(app)) {
14-
return getAPI(app);
15-
} else {
15+
if (!this.isDataviewEnabled()) {
1616
new Notice(`Dataview plugin is not installed. Please install it to load Databases.`);
1717
throw new Error('Dataview plugin is not installed');
1818
}
19+
if (!this.indexIsLoaded) {
20+
new Notice(`Dataview plugin is not loaded yet. Please wait a few seconds and try again.`);
21+
throw new Error("Dataview index is not loaded");
22+
}
23+
24+
return getAPI(app);
1925
}
2026

2127
wrapLiteral(literal: Literal): WrappedLiteral {
@@ -26,6 +32,17 @@ class DataviewProxy {
2632
return this.getDataviewAPI().value.isTruthy(literal?.toString());
2733
}
2834

35+
isDataviewEnabled(): boolean {
36+
return isPluginEnabled(app);
37+
}
38+
39+
isIndexLoaded(): boolean {
40+
return this.indexIsLoaded;
41+
}
42+
43+
setIndexIsLoaded(value: boolean) {
44+
this.indexIsLoaded = value;
45+
}
2946
/**
3047
* Singleton instance
3148
* @returns {VaultManager}

0 commit comments

Comments
 (0)