Skip to content

Commit c4786f9

Browse files
committed
feat: types improvement
1 parent 3889f78 commit c4786f9

File tree

5 files changed

+26
-22
lines changed

5 files changed

+26
-22
lines changed

packages/automation/src/lib/context/machine-context.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ function parsePath(path?: string | string[]): string[] {
77
return path.split(/\.|\[|\]/).filter(Boolean);
88
}
99

10-
function getFromObject(object: Record<string, any>, path?: string | string[]) {
10+
function getFromObject(
11+
object: Record<string, unknown>,
12+
path?: string | string[]
13+
) {
1114
if (!path) return object;
1215

1316
const parts = parsePath(path);
@@ -85,7 +88,7 @@ export class MachineContext {
8588

8689
public setFromData(
8790
location: string | string[],
88-
data?: Record<string, any>,
91+
data?: Record<string, unknown>,
8992
path?: string | string[]
9093
) {
9194
if (!data) return;

packages/automation/src/lib/state-machine.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
} from './listeners';
1414
import { signWithLitActionCode, executeLitAction } from './litActions';
1515
import { State, StateParams } from './states';
16-
import { Check, Transition } from './transitions';
16+
import { CheckFn, Transition } from './transitions';
1717
import { getEvmChain } from './utils/chain';
1818
import { getBalanceTransitionCheck, getERC20Balance } from './utils/erc20';
1919

@@ -33,7 +33,7 @@ export type MachineStatus = 'running' | 'stopped';
3333
* A StateMachine class that manages states and transitions between them.
3434
*/
3535
export class StateMachine {
36-
private debug = false;
36+
private readonly debug;
3737
private context: MachineContext;
3838

3939
private litNodeClient: LitNodeClient;
@@ -273,7 +273,7 @@ export class StateMachine {
273273
};
274274

275275
const listeners: Listener<any>[] = [];
276-
const checks: Check[] = [];
276+
const checks: CheckFn[] = [];
277277

278278
if (timer) {
279279
const transitionIndex = checks.length;

packages/automation/src/lib/states/state.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ export type StateParams = BaseStateParams;
1111
* A State class that represents a state with optional entry and exit actions.
1212
*/
1313
export class State {
14-
private debug = false;
14+
private readonly debug;
1515
public readonly key: string;
1616
public readonly onEnter: (() => Promise<void>) | undefined;
1717
public readonly onExit: (() => Promise<void>) | undefined;
1818

19-
constructor(private params: BaseStateParams) {
19+
constructor(params: BaseStateParams) {
2020
this.key = params.key;
2121
this.onEnter = params.onEnter;
2222
this.onExit = params.onExit;

packages/automation/src/lib/transitions/transition.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
11
import { Listener } from '../listeners';
22

3-
export type Check = (values: (any | undefined)[]) => Promise<boolean>;
3+
export type CheckFn = (values: (unknown | undefined)[]) => Promise<boolean>;
4+
export type resultFn = (values: (unknown | undefined)[]) => Promise<void>;
45

56
/**
67
* A Transition class that manages state transitions based on listeners and conditions.
78
*/
89
export interface BaseTransitionParams {
910
debug?: boolean;
10-
listeners?: Listener<any>[];
11-
check?: (values: (any | undefined)[]) => Promise<boolean>;
12-
onMatch: (values: (any | undefined)[]) => Promise<void>;
13-
onMismatch?: (values: (any | undefined)[]) => Promise<void>;
11+
listeners?: Listener<any>[]; // should be unknown but that makes callers to cast listeners
12+
check?: CheckFn;
13+
onMatch: resultFn;
14+
onMismatch?: resultFn;
1415
}
1516

1617
export class Transition {
1718
private readonly debug: boolean;
18-
private readonly listeners: Listener<any>[];
19-
private readonly values: (any | undefined)[];
20-
private readonly check?: Check;
21-
private readonly onMatch: (values: (any | undefined)[]) => Promise<void>;
22-
private readonly onMismatch?: (values: (any | undefined)[]) => Promise<void>;
19+
private readonly listeners: Listener<unknown>[];
20+
private readonly values: (unknown | undefined)[];
21+
private readonly check?: CheckFn;
22+
private readonly onMatch: resultFn;
23+
private readonly onMismatch?: resultFn;
2324

2425
/**
2526
* Creates a new Transition instance. If no listeners are provided, the transition will automatically match on the next event loop.
@@ -47,7 +48,7 @@ export class Transition {
4748
*/
4849
private setupListeners() {
4950
this.listeners.forEach((listener, index) => {
50-
listener.onStateChange(async (value: any) => {
51+
listener.onStateChange(async (value: unknown) => {
5152
this.values[index] = value;
5253
const isMatch = this.check ? await this.check(this.values) : true;
5354
if (isMatch) {
@@ -69,7 +70,7 @@ export class Transition {
6970
await Promise.all(this.listeners.map((listener) => listener.start()));
7071

7172
if (!this.listeners.length) {
72-
// If the transition does not have any listeners it will never emit. Therefore, we "emit" automatically on next event loop
73+
// If the transition does not have any listeners it will never emit. Therefore, we "match" automatically on next event loop
7374
setTimeout(() => {
7475
this.debug && console.log('Transition without listeners: auto match');
7576
this.onMatch([]);

packages/automation/src/lib/types.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ export interface OnEvmChain {
1717
evmChainId: ContextOrLiteral<number>;
1818
}
1919

20-
export interface ReadsContext<T> {
20+
export interface ContextAccess {
2121
contextPath: string;
2222
}
2323

24-
export type ContextOrLiteral<T> = T | ReadsContext<T>;
24+
export type ContextOrLiteral<T> = T | ContextAccess;
2525

26-
interface ContextUpdate extends ReadsContext<unknown> {
26+
interface ContextUpdate extends ContextAccess {
2727
dataPath: string;
2828
}
2929

0 commit comments

Comments
 (0)