* Wait for Obsidian's metadata cache to have fresh data for a file. * This is necessary after creating/modifying files because the metadata cache * updates asynchronously.
(
pathOrFile: string | { path: string },
maxRetries = 10
)
| 1073 | * updates asynchronously. |
| 1074 | */ |
| 1075 | async waitForFreshTaskData( |
| 1076 | pathOrFile: string | { path: string }, |
| 1077 | maxRetries = 10 |
| 1078 | ): Promise<void> { |
| 1079 | const path = typeof pathOrFile === "string" ? pathOrFile : pathOrFile.path; |
| 1080 | const file = |
| 1081 | typeof pathOrFile === "string" |
| 1082 | ? this.app.vault.getAbstractFileByPath(path) |
| 1083 | : pathOrFile; |
| 1084 | |
| 1085 | if (!(file instanceof TFile)) { |
| 1086 | // File doesn't exist yet, just wait a bit |
| 1087 | await new Promise((resolve) => window.setTimeout(resolve, 100)); |
| 1088 | return; |
| 1089 | } |
| 1090 | |
| 1091 | // Poll the metadata cache until it has the file's frontmatter |
| 1092 | for (let i = 0; i < maxRetries; i++) { |
| 1093 | const metadata = this.app.metadataCache.getFileCache(file); |
| 1094 | if (metadata?.frontmatter) { |
| 1095 | // Metadata cache has the file indexed |
| 1096 | return; |
| 1097 | } |
| 1098 | // Wait before retrying (50ms, 100ms, 150ms, etc.) |
| 1099 | await new Promise((resolve) => window.setTimeout(resolve, 50 * (i + 1))); |
| 1100 | } |
| 1101 | |
| 1102 | // If we still don't have metadata after retries, log a warning but continue |
| 1103 | tasknotesLogger.warn( |
| 1104 | `TaskManager: Metadata cache not ready for ${path} after ${maxRetries} retries`, |
| 1105 | { category: "stale-data", operation: "taskmanager-metadata-cache-not-ready" } |
| 1106 | ); |
| 1107 | } |
| 1108 | |
| 1109 | updateConfig(settings: TaskNotesSettings): void { |
| 1110 | // Update settings |
no test coverage detected