Skip to content

Commit a677568

Browse files
committed
feat: improve state machine context methods types
1 parent 8ec1b83 commit a677568

File tree

2 files changed

+19
-15
lines changed

2 files changed

+19
-15
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ export class MachineContext {
3030
this.context = initialContext ?? {};
3131
}
3232

33-
public get(path?: string | string[]): unknown {
34-
return getFromObject(this.context, path);
33+
public get<T = unknown>(path?: string | string[]): T {
34+
return getFromObject(this.context, path) as T;
3535
}
3636

3737
public set(path: string | string[], value: unknown = undefined): void {

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

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -637,8 +637,21 @@ export class StateMachine {
637637
* If value or path do not exist it returns undefined
638638
* @param path the context path to read
639639
*/
640-
public getFromContext(path?: string | string[]): any {
641-
return this.context.get(path);
640+
public getFromContext<T>(path?: string | string[]): T {
641+
return this.context.get<T>(path);
642+
}
643+
644+
/**
645+
* Resolves a value from the context if it specifies a path or returns it as a literal
646+
* @param value the literal value or read context object
647+
*/
648+
public resolveContextPathOrLiteral<T = unknown>(
649+
value: ContextOrLiteral<T> | T
650+
): T {
651+
if (value && typeof value === 'object' && 'contextPath' in value) {
652+
return this.context.get<T>(value.contextPath);
653+
}
654+
return value;
642655
}
643656

644657
/**
@@ -647,7 +660,7 @@ export class StateMachine {
647660
* @param path the context path to write
648661
* @param value the value to write in the context path
649662
*/
650-
public setToContext(path: string | string[], value: any): void {
663+
public setToContext(path: string | string[], value: unknown): void {
651664
this.context.set(path, value);
652665
}
653666

@@ -657,7 +670,7 @@ export class StateMachine {
657670
* @param path the context path to write
658671
* @param value the value to write in the context path
659672
*/
660-
public pushToContext(path: string | string[], value: any): void {
673+
public pushToContext(path: string | string[], value: unknown): void {
661674
this.context.push(path, value);
662675
}
663676

@@ -674,15 +687,6 @@ export class StateMachine {
674687
this.debug && console.log('State machine stopped');
675688
}
676689

677-
private resolveContextPathOrLiteral<T = unknown>(
678-
value: ContextOrLiteral<T> | T
679-
): T {
680-
if (value && typeof value === 'object' && 'contextPath' in value) {
681-
return this.context.get(value.contextPath) as T;
682-
}
683-
return value;
684-
}
685-
686690
/**
687691
* Stops listening on the current state's transitions and exits the current state.
688692
*/

0 commit comments

Comments
 (0)