Skip to content

Commit

Permalink
fixes the issue with dataview indexing
Browse files Browse the repository at this point in the history
dataview only triggers indexing, if something changed. but the plugin does not know, if it is ready. So we nee to trigger it on our own.
  • Loading branch information
Heiss committed Aug 8, 2024
1 parent f6f2884 commit 13aefad
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 63 deletions.
21 changes: 16 additions & 5 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ export default class VikunjaPlugin extends Plugin {
}

private async handleEditorChange(data: any) {
if (!this.settings.updateOnCursorMovement) {
return;
}

if (this.settings.debugging) console.log("Editor changed", data);
const currentFile = this.app.workspace.getActiveFile();
if (!currentFile) {
Expand Down Expand Up @@ -154,6 +158,7 @@ export default class VikunjaPlugin extends Plugin {
if (!this.settings.updateOnCursorMovement) {
return;
}

const target = evt.target as HTMLInputElement;
if (this.app.workspace.activeEditor?.editor?.hasFocus()) {
await this.checkLastLineForUpdate();
Expand All @@ -165,18 +170,24 @@ export default class VikunjaPlugin extends Plugin {
if (this.settings.debugging) console.log("No task element found for checkbox");
return;
}
if (this.settings.debugging) console.log("Task element found for checkbox", taskElement.textContent);

if (this.settings.debugging) console.log("Task element found for checkbox",);
const regex = /\svikunja_id(\d+)\s*/; // this ugly stuff is needed, because textContent remove all markdown related stuff
const match = taskElement.textContent?.match(regex) || false;
if (match) {
const taskId = parseInt(match[1]);
if (this.settings.debugging) console.log("Checkbox clicked for task", taskId);
const task = await this.tasksApi.getTaskById(taskId);
const cachedTask = this.cache.get(taskId);
const task = await this.processor.getTaskParser().parse(`- [${target.checked ? "X" : " "}] ${taskElement.textContent || ""} [vikunja_id:: ${taskId}]`);
if (task.id === undefined) {
if (this.settings.debugging) console.log("No task id found in task");
return;
}
if (this.settings.debugging) console.log("Checkbox clicked for task", taskId, "cached task", task, "found task", task);
const cachedTask = this.cache.get(task.id);

if (cachedTask !== undefined) {
cachedTask.task = task;
cachedTask.task.done = target.checked;
await this.tasksApi.updateTask(cachedTask);
// FIXME update the checked status in vikunja. add a settings option for it
}
} else {
if (this.settings.debugging) console.log("No task id found for checkbox");
Expand Down
2 changes: 0 additions & 2 deletions src/processing/automaton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import UpdateTasks from "./updateTasks";
import CreateTasks from "./createTasks";
import {Processor} from "./processor";
import {SyncLabels} from "./syncLabels";
import CheckCache from "./checkCache";

interface StepsOutput {
localTasks: PluginTask[];
Expand Down Expand Up @@ -42,7 +41,6 @@ class Automaton {

this.steps = [
new GetTasks(app, plugin, processor),
new CheckCache(plugin, app, processor),
new SyncLabels(app, plugin),
new RemoveTasks(app, plugin),
new CreateTasks(app, plugin, processor),
Expand Down
41 changes: 0 additions & 41 deletions src/processing/checkCache.ts

This file was deleted.

48 changes: 39 additions & 9 deletions src/processing/getTasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import VikunjaPlugin from "../../main";
import {App} from "obsidian";
import {TaskParser} from "../taskFormats/taskFormats";
import {Processor} from "./processor";
import {backendToFindTasks} from "../enums";

class GetTasks implements IAutomatonSteps {
app: App;
Expand All @@ -23,14 +24,7 @@ class GetTasks implements IAutomatonSteps {
}

async step(_1: PluginTask[], _2: ModelsTask[]): Promise<StepsOutput> {
// Get all tasks in vikunja and vault
if (this.plugin.settings.debugging) console.log("Step GetTask: Pulling tasks from vault");
const localTasks = await this.getTasksFromVault();
if (this.plugin.settings.debugging) console.log("Step GetTask: Got tasks from vault", localTasks);

if (this.plugin.settings.debugging) console.log("Step GetTask: Pulling tasks from Vikunja");
let vikunjaTasks = await this.getTasksFromVikunja();
if (this.plugin.settings.debugging) console.log("Step GetTask: Got tasks from Vikunja", vikunjaTasks);
let [localTasks, vikunjaTasks] = await Promise.all([this.getTasksFromVault(), this.getTasksFromVikunja()]);

if (this.plugin.settings.pullTasksOnlyFromDefaultProject) {
if (this.plugin.settings.debugging) console.log("Step GetTask: Filtering tasks to only default project");
Expand All @@ -42,12 +36,48 @@ class GetTasks implements IAutomatonSteps {
return {localTasks, vikunjaTasks};
}

/*
* This function is used to wait for the dataview index to be ready.
* Because dataview only indexing, if something changed, but sync does not know if anything changed recently,
* we need to trigger it manually.
*/
private async handleDataviewIndex() {
const currentFile = this.app.workspace.getActiveFile();
if (!currentFile) {
throw new Error("No active file");
}
let dataViewIndexReady = false;
// @ts-ignore
this.plugin.registerEvent(this.plugin.app.metadataCache.on("dataview:metadata-change", () => {
dataViewIndexReady = true;
}));

do {
if (this.plugin.settings.debugging) console.log("Step GetTask: Waiting for dataview index to be ready");
await new Promise(resolve => setTimeout(resolve, 1000));
this.app.metadataCache.trigger("resolve", currentFile);
} while (!dataViewIndexReady)

}

private async getTasksFromVault(): Promise<PluginTask[]> {
if (this.plugin.settings.backendToFindTasks === backendToFindTasks.Dataview) {
await this.handleDataviewIndex();
}

if (this.plugin.settings.updateOnCursorMovement) {
const tasks = this.plugin.cache.getCachedTasks();
if (this.plugin.settings.debugging) console.log("GetTasks: Using cache", tasks);
return tasks;
}
if (this.plugin.settings.debugging) console.log("Step GetTask: Pulling tasks from vault");
return await this.vaultSearcher.getTasks(this.taskParser);
}

private async getTasksFromVikunja(): Promise<ModelsTask[]> {
return await this.plugin.tasksApi.getAllTasks();
const tasks = await this.plugin.tasksApi.getAllTasks();
if (this.plugin.settings.debugging) console.log("Step GetTask: Got tasks from Vikunja", tasks);
return tasks;
}

}
Expand Down
7 changes: 6 additions & 1 deletion src/processing/updateTasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,12 @@ class UpdateTasks implements IAutomatonSteps {
private async updateTasksInVikunja(updateTasks: PluginTask[]) {
if (this.plugin.settings.debugging) console.log("Step UpdateTask: Update tasks in vikunja");

await Promise.all(updateTasks.map(task => this.plugin.tasksApi.updateTask(task)));
await Promise.all(updateTasks.map(async task => {
const answer = await this.plugin.tasksApi.updateTask(task)
if (this.plugin.settings.debugging) console.log("Step UpdateTask: Updated task in Vikunja", task, answer);
task.task = answer;
await this.updateTasksInVault([task]);
}));
}

private async updateTasksInVault(updateTasks: PluginTask[]) {
Expand Down
7 changes: 2 additions & 5 deletions src/settings/VaultTaskCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ export default class VaultTaskCache {

async saveCacheToDisk() {
if (this.changesMade) {
this.plugin.settings.cache = this.getCachedTasks().map(task => task.toJson());
if (this.plugin.settings.debugging) console.log("VaultTaskCache: Updated cache in settings", this.plugin.settings.cache);
if (this.plugin.settings.debugging) console.log("VaultTaskCache: Saving cache to disk");
await this.plugin.saveSettings();
}
Expand Down Expand Up @@ -76,8 +78,6 @@ export default class VaultTaskCache {
this.cache.set(local.task.id, local);

console.log("VaultTaskCache: Updated cache", this.cache);
this.plugin.settings.cache = this.getCachedTasks().map(task => task.toJson());
console.log("VaultTaskCache: Updated cache in settings", this.plugin.settings.cache);
this.changesMade = true;
}

Expand All @@ -93,9 +93,6 @@ export default class VaultTaskCache {
this.changesMade = true;
}

/*
* Do not forget to call delete after processing the tasks.
*/
getCachedTasks(): PluginTask[] {
return Array.from(this.cache.values());
}
Expand Down

0 comments on commit 13aefad

Please sign in to comment.