Skip to content

Commit 36d44e3

Browse files
committed
refactor: use generic "on" method rather than creating multiple functions
1 parent f6c5510 commit 36d44e3

File tree

4 files changed

+48
-10
lines changed

4 files changed

+48
-10
lines changed

src/lib/obsidian-dataview/adapter.ts

+30-8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
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";
34
import { Task } from "@/data/task";
45
import { mergeTaskParts } from "@/data/merge-task-parts";
56
import { parseTaskEmojis } from "@/data/parse-task-emojis";
67

8+
type MetadataCacheOnFunctionParameters = ParametersFrom4Overloads<MetadataCache["on"]>;
9+
710
export class Dataview {
811
private constructor(
912
private readonly plugin: Plugin,
@@ -22,20 +25,39 @@ export class Dataview {
2225
} else if (api.index.initialized) {
2326
resolve(new Dataview(plugin, api));
2427
} 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)));
2930
}
3031
}
3132
});
3233
}
3334

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);
3757
}
3858

59+
/* eslint-enable @typescript-eslint/no-explicit-any */
60+
3961
public getPages(query: string, originFile?: string): SMarkdownPage[] {
4062
return [...this.dv.pages(query, originFile)];
4163
}

src/lib/obsidian/types.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
export { Plugin } from "obsidian";
2-
export type { EventRef } from "obsidian";
2+
export type { EventRef, MetadataCache, TAbstractFile, TFile } from "obsidian";

src/main.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export class Objo extends Plugin {
66
this.app.workspace.onLayoutReady(async () => {
77
const dv = await Dataview.getReady(this);
88
this.log(dv);
9-
this.registerEvent(dv.onMetadataChange(() => this.log(dv)));
9+
this.registerEvent(dv.on("dataview:metadata-change", () => this.log(dv)));
1010
});
1111
}
1212

src/utils/type-utils.ts

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/* eslint-disable @typescript-eslint/no-explicit-any */
2+
3+
export type ParametersFrom4Overloads<T> =
4+
T extends (
5+
{
6+
(...args: infer A1): any;
7+
(...args: infer A2): any;
8+
(...args: infer A3): any;
9+
(...args: infer A4): any;
10+
}
11+
) ?
12+
A1 | A2 | A3 | A4
13+
: T extends { (...args: infer A1): any; (...args: infer A2): any; (...args: infer A3): any } ? A1 | A2 | A3
14+
: T extends { (...args: infer A1): any; (...args: infer A2): any } ? A1 | A2
15+
: T extends (...args: infer A) => any ? A
16+
: any;

0 commit comments

Comments
 (0)