11import { 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 */
89export 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
1617export 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 ( [ ] ) ;
0 commit comments