Skip to content

Commit e7605e4

Browse files
claremacraemnaoumov
andcommitted
vault: Write up learnings on startup-behaviour with custom searches
This allows Co-Authored-By: Michael Naumov <[email protected]>
1 parent 00edbe4 commit e7605e4

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

resources/sample_vaults/Tasks-Demo/Functions/Reusable Code in Custom Filters.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Custom JS way
44

5+
These depend on the `customJS` variable being initialised, which is not always true if this file is loaded in Reading mode during startup.
6+
57
```tasks
68
group by function \
79
const {Tasks} = customJS; \
@@ -14,8 +16,14 @@ group by function customJS.Tasks.byParentItemDescription(task);
1416
limit 1
1517
```
1618

19+
See earlier discussion:
20+
21+
See <https://github.com/obsidian-tasks-group/obsidian-tasks/issues/3024#issuecomment-2285073838>.
22+
1723
## CodeScript Toolkit way
1824

25+
### Searches which always work if this file is loaded in Reading mode during startup
26+
1927
```tasks
2028
group by function \
2129
const {parentDescription} = require('/Tasks.js'); \
@@ -28,6 +36,8 @@ group by function require('/Tasks.js').parentDescription(task)
2836
limit 1
2937
```
3038

39+
### Searches usually work if this file is loaded in Reading mode during startup, and Tasks has been patched
40+
3141
```tasks
3242
group by function Tasks.parentDescription(task)
3343
limit 1
@@ -37,3 +47,38 @@ limit 1
3747
group by function TasksNew.parentDescription(task)
3848
limit 1
3949
```
50+
51+
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.
52+
53+
By use of [onLayoutReady()](https://docs.obsidian.md/Reference/TypeScript+API/Workspace/onLayoutReady) in Tasks main, it made this code even during startup...
54+
55+
This is the diff:
56+
57+
```diff
58+
Index: src/main.ts
59+
IDEA additional info:
60+
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
61+
<+>UTF-8
62+
===================================================================
63+
diff --git a/src/main.ts b/src/main.ts
64+
--- a/src/main.ts (revision 2493c6635db11d94379429fbed88c14323c5a7ff)
65+
+++ b/src/main.ts (revision 00edbe4eb1283fd22a3226ea9587ad3d8ec0da98)
66+
@@ -55,8 +55,14 @@
67+
workspace: this.app.workspace,
68+
events,
69+
});
70+
- this.inlineRenderer = new InlineRenderer({ plugin: this });
71+
- this.queryRenderer = new QueryRenderer({ plugin: this, events });
72+
+
73+
+ this.app.workspace.onLayoutReady(() => {
74+
+ // Only execute searches once all plugins are loaded.
75+
+ // This fixed use of a "CodeScript Toolkit" startup script inside
76+
+ // notes that were already open in Reading mode when Obsdian was starting up.
77+
+ this.inlineRenderer = new InlineRenderer({ plugin: this });
78+
+ this.queryRenderer = new QueryRenderer({ plugin: this, events });
79+
+ });
80+
81+
this.registerEditorExtension(newLivePreviewExtension());
82+
this.registerEditorSuggest(new EditorSuggestor(this.app, getSettings(), this));
83+
84+
```

0 commit comments

Comments
 (0)