Skip to content

Commit

Permalink
♻️ (TypeScript) fix strictFunctionTypes violations (pt 1) (#2065)
Browse files Browse the repository at this point in the history
  • Loading branch information
MatissJanis authored Dec 15, 2023
1 parent af9efa0 commit 8a15caf
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 36 deletions.
44 changes: 32 additions & 12 deletions packages/loot-core/src/server/budget/types/handlers.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ export interface BudgetHandlers {
category: string /* category id */;
month: string;
amount: number;
}) => Promise<unknown>;
}) => Promise<void>;

'budget/copy-previous-month': (...args: unknown[]) => Promise<unknown>;
'budget/copy-previous-month': (arg: { month: string }) => Promise<void>;

'budget/set-zero': (...args: unknown[]) => Promise<unknown>;
'budget/set-zero': (arg: { month: string }) => Promise<void>;

'budget/set-3month-avg': (...args: unknown[]) => Promise<unknown>;
'budget/set-3month-avg': (arg: { month: string }) => Promise<void>;

'budget/check-templates': () => Promise<Notification>;

Expand All @@ -27,17 +27,37 @@ export interface BudgetHandlers {
month: string;
}) => Promise<Notification>;

'budget/hold-for-next-month': (...args: unknown[]) => Promise<unknown>;
'budget/hold-for-next-month': (arg: {
month: string;
amount: number;
}) => Promise<boolean>;

'budget/reset-hold': (...args: unknown[]) => Promise<unknown>;
'budget/reset-hold': (arg: { month: string }) => Promise<void>;

'budget/cover-overspending': (...args: unknown[]) => Promise<unknown>;
'budget/cover-overspending': (arg: {
month: string;
to: string;
from: string;
}) => Promise<void>;

'budget/transfer-available': (...args: unknown[]) => Promise<unknown>;
'budget/transfer-available': (arg: {
month: string;
amount: number;
category: string;
}) => Promise<void>;

'budget/transfer-category': (...args: unknown[]) => Promise<unknown>;
'budget/transfer-category': (arg: {
month: string;
amount: number;
to: string;
from: string;
}) => Promise<void>;

'budget/set-carryover': (...args: unknown[]) => Promise<unknown>;
'budget/set-carryover': (arg: {
startMonth: string;
category: string;
flag: boolean;
}) => Promise<void>;

'budget/apply-single-template': (arg: {
month: string;
Expand All @@ -48,10 +68,10 @@ export interface BudgetHandlers {
month: string;
N: number;
category: string; //category id
}) => Promise<unknown>;
}) => Promise<void>;

'budget/copy-single-month': (arg: {
month: string;
category: string; //category id
}) => Promise<unknown>;
}) => Promise<void>;
}
5 changes: 2 additions & 3 deletions packages/loot-core/src/server/mutators.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { captureException, captureBreadcrumb } from '../platform/exceptions';
import { sequential } from '../shared/async';
import { type HandlerFunctions } from '../types/handlers';

const runningMethods = new Set();

Expand All @@ -9,9 +10,7 @@ let globalMutationsEnabled = false;

let _latestHandlerNames = [];

export function mutator<T extends (...args: unknown[]) => unknown>(
handler: T,
): T {
export function mutator<T extends HandlerFunctions>(handler: T): T {
mutatingMethods.set(handler, true);
return handler;
}
Expand Down
17 changes: 5 additions & 12 deletions packages/loot-core/src/server/undo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Timestamp } from '@actual-app/crdt';

import * as connection from '../platform/server/connection';
import { getIn } from '../shared/util';
import { type HandlerFunctions } from '../types/handlers';

import { withMutatorContext, getMutatorContext } from './mutators';
import { Message, sendMessages } from './sync';
Expand Down Expand Up @@ -89,18 +90,10 @@ export function withUndo<T>(
);
}

// for some reason `void` is not inferred properly without this overload
export function undoable<Args extends unknown[]>(
func: (...args: Args) => Promise<void>,
): (...args: Args) => Promise<void>;
export function undoable<
Args extends unknown[],
Return extends Promise<unknown>,
>(func: (...args: Args) => Return): (...args: Args) => Return;
export function undoable(func: (...args: unknown[]) => Promise<unknown>) {
return (...args: unknown[]) => {
return withUndo(() => {
return func(...args);
export function undoable<T extends HandlerFunctions>(func: T) {
return (...args: Parameters<T>) => {
return withUndo<Awaited<ReturnType<T>>>(() => {
return func.apply(null, args);
});
};
}
Expand Down
14 changes: 7 additions & 7 deletions packages/loot-core/src/shared/async.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export function sequential<T extends (...args: unknown[]) => unknown>(
import { type HandlerFunctions } from '../types/handlers';

export function sequential<T extends HandlerFunctions>(
fn: T,
): (...args: Parameters<T>) => Promise<Awaited<ReturnType<T>>> {
const sequenceState = {
Expand All @@ -16,7 +18,7 @@ export function sequential<T extends (...args: unknown[]) => unknown>(
}

function run(args, resolve, reject) {
sequenceState.running = fn(...args);
sequenceState.running = fn.apply(null, args);

sequenceState.running.then(
val => {
Expand All @@ -43,20 +45,18 @@ export function sequential<T extends (...args: unknown[]) => unknown>(
};
}

export function once<T extends (...args: unknown[]) => Promise<unknown>>(
export function once<T extends HandlerFunctions>(
fn: T,
): (...args: Parameters<T>) => Promise<Awaited<ReturnType<T>>> {
let promise = null;
const onceFn = (...args: Parameters<T>): Promise<Awaited<ReturnType<T>>> => {
return (...args) => {
if (!promise) {
promise = fn(...args).finally(() => {
promise = fn.apply(null, args).finally(() => {
promise = null;
});
return promise;
}

return promise;
};

return onceFn;
}
4 changes: 3 additions & 1 deletion packages/loot-core/src/types/api-handlers.d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { type ServerHandlers } from './server-handlers';

export interface ApiHandlers {
'api/batch-budget-start': () => Promise<unknown>;

'api/batch-budget-end': () => Promise<unknown>;

'api/load-budget': (
...args: Parameters<ServerHandlers['load-budget']>
) => Promise<unknown>;
) => Promise<void>;

'api/download-budget': (arg: { syncId; password }) => Promise<unknown>;

Expand Down
2 changes: 2 additions & 0 deletions packages/loot-core/src/types/handlers.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ export interface Handlers
RulesHandlers,
SchedulesHandlers,
ToolsHandlers {}

export type HandlerFunctions = Handlers[keyof Handlers];
2 changes: 1 addition & 1 deletion packages/loot-core/src/types/server-handlers.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ export interface ServerHandlers {
error?: { message: string; reason: string; meta: unknown };
}>;

'load-budget': (arg: { id }) => Promise<{ error }>;
'load-budget': (arg: { id: string }) => Promise<{ error }>;

'create-demo-budget': () => Promise<unknown>;

Expand Down
2 changes: 2 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
"downlevelIteration": true,
// TODO: enable once every file is ts
// "strict": true,
// TODO: enable once the violations are fixed
// "strictFunctionTypes": true,
"noFallthroughCasesInSwitch": true,
"skipLibCheck": true,
"jsx": "preserve",
Expand Down
6 changes: 6 additions & 0 deletions upcoming-release-notes/2065.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
category: Maintenance
authors: [MatissJanis]
---

Fixing TypeScript issues when enabling `strictFunctionTypes`.

0 comments on commit 8a15caf

Please sign in to comment.