From e7605e4d7affd1bec69e52e94fd041d8c658b798 Mon Sep 17 00:00:00 2001 From: Clare Macrae Date: Fri, 17 Jan 2025 16:34:50 +0000 Subject: [PATCH] vault: Write up learnings on startup-behaviour with custom searches This allows Co-Authored-By: Michael Naumov --- .../Reusable Code in Custom Filters.md | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/resources/sample_vaults/Tasks-Demo/Functions/Reusable Code in Custom Filters.md b/resources/sample_vaults/Tasks-Demo/Functions/Reusable Code in Custom Filters.md index d4a406fe4f..afceb6e25e 100644 --- a/resources/sample_vaults/Tasks-Demo/Functions/Reusable Code in Custom Filters.md +++ b/resources/sample_vaults/Tasks-Demo/Functions/Reusable Code in Custom Filters.md @@ -2,6 +2,8 @@ ## Custom JS way +These depend on the `customJS` variable being initialised, which is not always true if this file is loaded in Reading mode during startup. + ```tasks group by function \ const {Tasks} = customJS; \ @@ -14,8 +16,14 @@ group by function customJS.Tasks.byParentItemDescription(task); limit 1 ``` +See earlier discussion: + +See . + ## CodeScript Toolkit way +### Searches which always work if this file is loaded in Reading mode during startup + ```tasks group by function \ const {parentDescription} = require('/Tasks.js'); \ @@ -28,6 +36,8 @@ group by function require('/Tasks.js').parentDescription(task) limit 1 ``` +### Searches usually work if this file is loaded in Reading mode during startup, and Tasks has been patched + ```tasks group by function Tasks.parentDescription(task) limit 1 @@ -37,3 +47,38 @@ limit 1 group by function TasksNew.parentDescription(task) limit 1 ``` + +These did not initially work if this file is loaded in Reading mode during startup - because they depend on `Tasks` and `TasksNew` being initialised by the "CodeScript Toolkit" [startup script](https://github.com/mnaoumov/obsidian-codescript-toolkit?tab=readme-ov-file#startup-script), which is not always true if this code block is loaded in Reading mode during startup. + +By use of [onLayoutReady()](https://docs.obsidian.md/Reference/TypeScript+API/Workspace/onLayoutReady) in Tasks main, it made this code even during startup... + +This is the diff: + +```diff +Index: src/main.ts +IDEA additional info: +Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP +<+>UTF-8 +=================================================================== +diff --git a/src/main.ts b/src/main.ts +--- a/src/main.ts (revision 2493c6635db11d94379429fbed88c14323c5a7ff) ++++ b/src/main.ts (revision 00edbe4eb1283fd22a3226ea9587ad3d8ec0da98) +@@ -55,8 +55,14 @@ + workspace: this.app.workspace, + events, + }); +- this.inlineRenderer = new InlineRenderer({ plugin: this }); +- this.queryRenderer = new QueryRenderer({ plugin: this, events }); ++ ++ this.app.workspace.onLayoutReady(() => { ++ // Only execute searches once all plugins are loaded. ++ // This fixed use of a "CodeScript Toolkit" startup script inside ++ // notes that were already open in Reading mode when Obsdian was starting up. ++ this.inlineRenderer = new InlineRenderer({ plugin: this }); ++ this.queryRenderer = new QueryRenderer({ plugin: this, events }); ++ }); + + this.registerEditorExtension(newLivePreviewExtension()); + this.registerEditorSuggest(new EditorSuggestor(this.app, getSettings(), this)); + +```