11/* eslint-disable @typescript-eslint/no-explicit-any */
2- import { type Operation , type Task , run , Symbol } from '@effection/core' ;
3- import { type InteractionOptions as SerializedInteractionOptions , globals , type InteractionType } from '@interactors/globals' ;
2+ import type { InteractionOptions as SerializedInteractionOptions , InteractionType } from '@interactors/globals' ;
43import type { Interactor , FilterObject , FilterFn , FilterParams } from './specification.ts' ;
54import { serializeInteractionOptions } from './serialize.ts' ;
65
@@ -25,7 +24,7 @@ export interface Interaction<E extends Element, T = void> extends Promise<T> {
2524 type : InteractionType ;
2625
2726 interactor : Interactor < E , any > ;
28- run : ( interactor : Interactor < E , any > ) => Operation < T > ;
27+ run : ( interactor : Interactor < E , any > ) => Promise < T > ;
2928 /**
3029 * Return a description of the interaction
3130 */
@@ -41,11 +40,9 @@ export interface Interaction<E extends Element, T = void> extends Promise<T> {
4140 /**
4241 * Perform the interaction
4342 */
44- action : ( ) => Task < T > ;
43+ action : ( ) => Promise < T > ;
4544
46- check ?: ( ) => Task < T > ;
47-
48- halt : ( ) => Promise < void > ;
45+ check ?: ( ) => Promise < T > ;
4946
5047 [ interactionSymbol ] : true ;
5148}
@@ -65,7 +62,7 @@ export interface AssertionInteraction<E extends Element, T = void> extends Inter
6562 /**
6663 * Perform the check
6764 */
68- check : ( ) => Task < T > ;
65+ check : ( ) => Promise < T > ;
6966}
7067
7168export type InteractionOptions < E extends Element , T > = {
@@ -74,70 +71,47 @@ export type InteractionOptions<E extends Element, T> = {
7471 filters ?: FilterParams < any , any > ;
7572 args : unknown [ ] ;
7673 interactor : Interactor < E , any > ;
77- run : ( interactor : Interactor < E , any > ) => Operation < T > ;
74+ run : ( interactor : Interactor < E , any > ) => Promise < T > ;
7875}
7976
8077export function createInteraction < E extends Element , T > ( type : 'action' , options : InteractionOptions < E , T > ) : ActionInteraction < E , T >
8178export function createInteraction < E extends Element , T > ( type : 'assertion' , options : InteractionOptions < E , T > ) : AssertionInteraction < E , T >
8279export function createInteraction < E extends Element , T , Q > ( type : 'action' , options : InteractionOptions < E , T > , apply : FilterFn < Q , Element > ) : ActionInteraction < E , T > & FilterObject < Q , Element >
8380export function createInteraction < E extends Element , T , Q > ( type : 'assertion' , options : InteractionOptions < E , T > , apply : FilterFn < Q , Element > ) : AssertionInteraction < E , T > & FilterObject < Q , Element >
84- export function createInteraction < E extends Element , T , Q > ( type : InteractionType , options : InteractionOptions < E , T > , apply ?: FilterFn < Q , Element > ) : Interaction < E , T > & Operation < T > {
85- let task : Task < T > ;
86- let shouldCatchHalt = false
87-
88- function operation ( scope : Task ) : Operation < T > {
89- let run = ( ) => options . run ( options . interactor ) ;
90-
91- return ( globals . wrapInteraction
92- ? globals . wrapInteraction ( ( ) => scope . run ( run ) , interaction )
93- : globals . wrapAction ( interaction . description , ( ) => scope . run ( run ) , type ) ) as Operation < T > ;
94- } ;
95-
96- function action ( ) : Task < T > {
97- if ( ! task ) {
98- task = run ( operation ) ;
81+ export function createInteraction < E extends Element , T , Q > ( type : InteractionType , options : InteractionOptions < E , T > , apply ?: FilterFn < Q , Element > ) : Interaction < E , T > {
82+ let promise : Promise < T > ;
83+
84+ let operation = ( ) => options . run ( options . interactor ) ;
85+
86+ function action ( ) : Promise < T > {
87+ if ( ! promise ) {
88+ promise = operation ( ) ;
9989 }
100- return task ;
90+ return promise ;
10191 } ;
10292
103- function catchHalt < T extends ( reason : any ) => any > ( onReject ?: T | null ) {
104- return ( reason : any ) => {
105- if ( shouldCatchHalt && reason instanceof Error && reason . message == 'halted' ) {
106- return reason
107- } else {
108- onReject ?.( reason )
109- return reason
110- }
111- }
112- }
113-
11493 let serializedOptions = serializeInteractionOptions ( type , options )
115- let interaction : Interaction < E , T > & Operation < T > = {
94+ let interaction : Interaction < E , T > = {
11695 type,
11796 options : serializedOptions ,
11897 description : options . description ,
11998 interactor : options . interactor ,
12099 run : options . run ,
121- args : options . args ,
100+ // args: options.args,
122101 action,
123102 check : type === 'assertion' ? action : undefined ,
124103 code : ( ) => serializedOptions . code ( ) ,
125- halt : ( ) => {
126- shouldCatchHalt = true
127- return action ( ) . halt ( )
128- } ,
129104 [ interactionSymbol ] : true ,
130105 [ Symbol . toStringTag ] : `[interaction ${ options . description } ]` ,
131- [ Symbol . operation ] : operation ,
132106 then ( onFulfill , onReject ) {
133- return action ( ) . then ( onFulfill , catchHalt ( onReject ) ) ;
107+ return action ( ) . then ( onFulfill , onReject ) ;
134108 } ,
135109 catch ( onReject ) {
136- return action ( ) . catch ( catchHalt ( onReject ) ) ;
110+ return action ( ) . catch ( onReject ) ;
137111 } ,
138112 finally ( handler ) {
139113 return action ( ) . finally ( handler ) ;
140- }
114+ } ,
141115 }
142116 if ( apply ) {
143117 return Object . assign ( interaction , { apply } ) ;
0 commit comments