Skip to content

Commit 9c9f08a

Browse files
committed
refactor: change hooks structure
1 parent a5e95c5 commit 9c9f08a

File tree

8 files changed

+42
-24
lines changed

8 files changed

+42
-24
lines changed

src/declarations.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@ import type { getRelativeDate } from "@/lib/helpers/get-relative-date.ts";
55
import type { getConfigFile } from "@/lib/main/get-config-file.ts";
66
import type { getDefaultConfig } from "@/lib/main/get-default-config.ts";
77
import type { initializeConfigFile } from "@/lib/main/initialize-config-file.ts";
8+
import type { HookReturnType } from "@/types/extensions/hook-return.type.ts";
89
import * as TauriApi from "@tauri-apps/api";
9-
import * as TauriLog from "@tauri-apps/plugin-log";
1010
import * as TauriFs from "@tauri-apps/plugin-fs";
1111

1212
declare global {
1313
interface Window {
1414
"__TAURI__": {
1515
"api": typeof TauriApi;
16-
"log": typeof TauriLog;
1716
"fs" : typeof TauriFs;
1817
};
1918
"__KAEDE__": {
@@ -29,11 +28,10 @@ declare global {
2928
};
3029
"hooks": {
3130
"getConfigFile": {
32-
"before": () => Promise<"stop" | void>;
31+
"before": HookReturnType<ConfigType>;
3332
};
3433
"getDefaultConfig": {
35-
"before" : () => Promise<"stop" | void>;
36-
"onAbort": () => Promise<ConfigType>;
34+
"before": HookReturnType<ConfigType>;
3735
};
3836
};
3937
};

src/lib/handlers/log.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { debug, info, warn, error } = window.__TAURI__.log;
1+
import { debug, info, warn, error } from "@tauri-apps/plugin-log";
22

33
// We do not care about promises here
44
export const log = {

src/lib/main/declare-window.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,10 @@ export function declareWindow() {
2020
},
2121
"hooks": {
2222
"getConfigFile": {
23-
"before": async () => {},
23+
"before": [],
2424
},
2525
"getDefaultConfig": {
26-
"before" : async () => {},
27-
"onAbort": async () => getDefaultConfig(),
26+
"before": [],
2827
},
2928
},
3029
};

src/lib/main/get-config-file.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,23 @@ import { log } from "@/lib/handlers/log.ts";
22
import { ApplicationNamespace, ConfigFilename } from "@/constants/application.ts";
33
import { getDefaultConfig } from "@/lib/main/get-default-config.ts";
44
import { initializeConfigFile } from "@/lib/main/initialize-config-file.ts";
5+
import { BaseDirectory, exists, readTextFile } from "@tauri-apps/plugin-fs";
56
import { type ConfigType, ConfigValidator } from "@/types/config/config.schema.ts";
6-
const { BaseDirectory, exists, readTextFile } = window.__TAURI__.fs;
77

88
export async function getConfigFile(): Promise<ConfigType> {
9-
log.debug("Executing the 'before' method on extensions' hook for 'getConfigFile'");
10-
const hookResponse = await window[ApplicationNamespace].hooks.getConfigFile.before();
9+
const hooksArray = window[ApplicationNamespace].hooks.getConfigFile.before;
1110

12-
if (hookResponse === "stop") {
13-
log.debug("'getConfigFile.before' hook has aborted execution");
11+
log.debug("Starting iterating through hooks for 'getConfigFile.before'");
12+
for (const [hookIndex, hookFunction] of hooksArray.entries()) {
13+
log.debug("Executing a hook with the next index:", hookIndex.toString());
14+
const hookResponse = await hookFunction();
1415

15-
// Awaiting here will just be an unnecessary action
16-
return getDefaultConfig();
16+
if (hookResponse.status === "stop") {
17+
log.debug(`A hook with the index of ${hookIndex} has aborted execution`);
18+
19+
// Awaiting here will just be an unnecessary action
20+
return hookResponse.response;
21+
}
1722
}
1823

1924
log.debug("Checking if config file exists");

src/lib/main/get-default-config.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
import type { ConfigType } from "@/types/config/config.schema.ts";
22
import { log } from "@/lib/handlers/log.ts";
33
import { ApplicationNamespace } from "@/constants/application.ts";
4-
const { getCurrentWindow } = window.__TAURI__.api.window;
4+
import { getCurrentWindow } from "@tauri-apps/api/window";
55

66
export async function getDefaultConfig(): Promise<ConfigType> {
7-
log.debug("Executing the 'before' method on extensions' hook for 'getDefaultConfig'");
8-
const hookResponse = await window[ApplicationNamespace].hooks.getDefaultConfig.before();
7+
const hooksArray = window[ApplicationNamespace].hooks.getDefaultConfig.before;
98

10-
if (hookResponse === "stop") {
11-
log.debug("'getConfigFile.before' hook has aborted execution");
9+
log.debug("Starting iterating through hooks for 'getDefaultConfig.before'");
10+
for (const [hookIndex, hookFunction] of hooksArray.entries()) {
11+
log.debug("Executing a hook with the next index:", hookIndex.toString());
12+
const hookResponse = await hookFunction();
1213

13-
return await window[ApplicationNamespace].hooks.getDefaultConfig.onAbort();
14+
if (hookResponse.status === "stop") {
15+
log.debug(`A hook with the index of ${hookIndex} has aborted execution`);
16+
17+
// Awaiting here will just be an unnecessary action
18+
return hookResponse.response;
19+
}
1420
}
1521

1622
log.debug("Getting current window theme");

src/lib/main/initialize-config-file.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { ConfigFilename } from "@/constants/application.ts";
22
import { getDefaultConfig } from "@/lib/main/get-default-config.ts";
33
import { log } from "@/lib/handlers/log.ts";
4-
const { BaseDirectory, writeFile } = window.__TAURI__.fs;
4+
import { BaseDirectory, writeFile } from "@tauri-apps/plugin-fs";
55

66
export async function initializeConfigFile(): Promise<void> {
77
log.debug("Getting default config");

src/lib/main/initialize-launcher.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { log } from "@/lib/handlers/log.ts";
22
import { getConfigFile } from "@/lib/main/get-config-file.ts";
33
import { getDefaultConfig } from "@/lib/main/get-default-config.ts";
4+
import { getCurrentWebviewWindow } from "@tauri-apps/api/webviewWindow";
45
import type { ConfigType } from "@/types/config/config.schema.ts";
5-
const { getCurrentWebviewWindow } = window.__TAURI__.api.webviewWindow;
66

77
export async function initializeLauncher(): Promise<void> {
88
let config: ConfigType;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* Used in '/src/declarations.ts'.
3+
* All hooks are promises and are stored in the array to handle multiple hooks
4+
*/
5+
export type HookReturnType<T> = Array<() => Promise<{
6+
// 'stop' aborts a function that executed current hook
7+
"status" : "stop" | "continue";
8+
// if 'status' is 'stop', function will return this field
9+
"response": T | Promise<T>;
10+
}>>;

0 commit comments

Comments
 (0)