1
1
import { DataviewApi , SMarkdownPage , STask , getAPI , isPluginEnabled } from "@/lib/obsidian-dataview/types" ;
2
- import { EventRef , Plugin } from "@/lib/obsidian/types" ;
2
+ import { EventRef , MetadataCache , Plugin , TAbstractFile , TFile } from "@/lib/obsidian/types" ;
3
+ import { ParametersFrom4Overloads } from "@/utils/type-utils" ;
3
4
import { Task } from "@/data/task" ;
4
5
import { mergeTaskParts } from "@/data/merge-task-parts" ;
5
6
import { parseTaskEmojis } from "@/data/parse-task-emojis" ;
6
7
8
+ type MetadataCacheOnFunctionParameters = ParametersFrom4Overloads < MetadataCache [ "on" ] > ;
9
+
7
10
export class Dataview {
8
11
private constructor (
9
12
private readonly plugin : Plugin ,
@@ -22,20 +25,39 @@ export class Dataview {
22
25
} else if ( api . index . initialized ) {
23
26
resolve ( new Dataview ( plugin , api ) ) ;
24
27
} else {
25
- plugin . registerEvent (
26
- // @ts -expect-error - obsidian doesn't define types for third-party events.
27
- plugin . app . metadataCache . on ( "dataview:index-ready" , ( ) => resolve ( new Dataview ( plugin , api ) ) ) ,
28
- ) ;
28
+ const dv = new Dataview ( plugin , api ) ;
29
+ plugin . registerEvent ( dv . on ( "dataview:index-ready" , ( ) => resolve ( dv ) ) ) ;
29
30
}
30
31
}
31
32
} ) ;
32
33
}
33
34
34
- public onMetadataChange ( callback : ( ) => void ) : EventRef {
35
- // @ts -expect-error - obsidian doesn't define overloads for third-party events.
36
- return this . plugin . app . metadataCache . on ( "dataview:metadata-change" , callback ) ;
35
+ /* eslint-disable @typescript-eslint/no-explicit-any */
36
+
37
+ public on ( name : "dataview:index-ready" , callback : ( ) => void , ctx ?: any ) : EventRef ;
38
+
39
+ public on (
40
+ name : "dataview:metadata-change" ,
41
+ callback :
42
+ | ( ( name : "delete" , file : TFile ) => void )
43
+ | ( ( name : "rename" , file : TAbstractFile , oldPath : string ) => void )
44
+ | ( ( name : "update" , file : TFile ) => void ) ,
45
+ ctx ?: any ,
46
+ ) : EventRef ;
47
+
48
+ public on < Name extends MetadataCacheOnFunctionParameters [ 0 ] > (
49
+ name : Name ,
50
+ callback : MetadataCache [ "on" ] extends ( name : Name , callback : infer F , ctx ?: any ) => EventRef ? F : never ,
51
+ ctx ?: any ,
52
+ ) : EventRef ;
53
+
54
+ public on ( name : string , callback : ( ...args : any [ ] ) => any , ctx ?: any ) : EventRef {
55
+ // @ts -expect-error - Rely on overloads for errors.
56
+ return this . plugin . app . metadataCache . on ( name , callback , ctx ) ;
37
57
}
38
58
59
+ /* eslint-enable @typescript-eslint/no-explicit-any */
60
+
39
61
public getPages ( query : string , originFile ?: string ) : SMarkdownPage [ ] {
40
62
return [ ...this . dv . pages ( query , originFile ) ] ;
41
63
}
0 commit comments