1- import type { Future , Operation , Result , Scope } from "effection" ;
2- import { Err , Ok , run , suspend , useScope , withResolvers } from "effection" ;
1+ import type {
2+ Future ,
3+ Operation ,
4+ Result ,
5+ Scope ,
6+ WithResolvers ,
7+ } from "effection" ;
8+ import {
9+ createScope ,
10+ Ok ,
11+ run ,
12+ suspend ,
13+ useScope ,
14+ withResolvers ,
15+ } from "effection" ;
16+ import { box } from "./box.ts" ;
317
418export interface TestOperation {
519 ( ) : Operation < void > ;
@@ -63,7 +77,7 @@ export interface TestAdapter {
6377 *
6478 * @ignore
6579 */
66- [ "@@init@@" ] ( ) : Operation < Scope > ;
80+ [ "@@init@@" ] ( ) : Operation < Result < Scope > > ;
6781}
6882
6983export interface TestAdapterOptions {
@@ -100,7 +114,8 @@ export function createTestAdapter(
100114 } ;
101115 const { parent, name = anonymousNames . next ( ) . value } = options ;
102116
103- let scope : Scope | undefined = undefined ;
117+ let scope : WithResolvers < Result < Scope > > | undefined = undefined ;
118+ let destroy : ( ) => Operation < void > = function * ( ) { } ;
104119
105120 const adapter : TestAdapter = {
106121 parent,
@@ -123,63 +138,65 @@ export function createTestAdapter(
123138 setup . all . push ( op ) ;
124139 } ,
125140 runTest ( op ) {
126- return run ( ( ) =>
127- box ( function * ( ) {
128- const setups = adapter . lineage . reduce (
129- ( all , adapter ) => all . concat ( adapter . setup . each ) ,
130- [ ] as TestOperation [ ] ,
131- ) ;
132-
133- let scope = yield * adapter [ "@@init@@" ] ( ) ;
134-
135- let test = yield * scope . spawn ( function * ( ) {
136- for ( const setup of setups ) {
137- yield * setup ( ) ;
141+ return run ( function * ( ) {
142+ let init = yield * adapter [ "@@init@@" ] ( ) ;
143+ if ( ! init . ok ) {
144+ return init ;
145+ }
146+ let scope = init . value ;
147+
148+ const setups = adapter . lineage . reduce (
149+ ( all , adapter ) => all . concat ( adapter . setup . each ) ,
150+ [ ] as TestOperation [ ] ,
151+ ) ;
152+
153+ let test = yield * scope . spawn ( ( ) =>
154+ box ( function * ( ) {
155+ for ( let fn of setups ) {
156+ yield * fn ( ) ;
138157 }
139158 yield * op ( ) ;
140- } ) ;
141-
142- yield * test ;
143- } ( ) )
144- ) ;
159+ } )
160+ ) ;
161+ return yield * test ;
162+ } ) ;
145163 } ,
146164
147- // no-op that will be replaced once initialze
148- destroy : ( ) => run ( function * ( ) { } ) ,
149-
150165 * [ "@@init@@" ] ( ) {
151166 if ( scope ) {
152- return scope ;
167+ return yield * scope . operation ;
153168 }
169+ scope = withResolvers < Result < Scope > > ( ) ;
154170
155- let parentScope = parent
156- ? yield * parent [ "@@init@@" ] ( )
157- : yield * useScope ( ) ;
171+ let parent = adapter . parent
172+ ? yield * adapter . parent [ "@@init@@" ] ( )
173+ : Ok ( createScope ( ) [ 0 ] ) ;
158174
159- let initialized = withResolvers < Scope > ( ) ;
175+ if ( ! parent . ok ) {
176+ scope . resolve ( parent ) ;
177+ return yield * scope . operation ;
178+ }
160179
161- let task = yield * parentScope . spawn ( function * ( ) {
162- scope = yield * useScope ( ) ;
163- for ( let op of setup . all ) {
164- yield * op ( ) ;
180+ let task = yield * parent . value . spawn ( function * ( ) {
181+ let init = yield * box ( function * ( ) {
182+ for ( let initializer of adapter . setup . all ) {
183+ yield * initializer ( ) ;
184+ }
185+ } ) ;
186+ if ( ! init . ok ) {
187+ scope ! . resolve ( init ) ;
188+ } else {
189+ scope ! . resolve ( Ok ( yield * useScope ( ) ) ) ;
190+ yield * suspend ( ) ;
165191 }
166- initialized . resolve ( scope ) ;
167- yield * suspend ( ) ;
168192 } ) ;
169193
170- adapter . destroy = ( ) => run ( task . halt ) ;
194+ destroy = task . halt ;
171195
172- return yield * initialized . operation ;
196+ return yield * scope . operation ;
173197 } ,
198+ destroy : ( ) => run ( destroy ) ,
174199 } ;
175200
176201 return adapter ;
177202}
178-
179- function * box < T > ( op : Operation < T > ) : Operation < Result < T > > {
180- try {
181- return Ok ( yield * op ) ;
182- } catch ( error ) {
183- return Err ( error as Error ) ;
184- }
185- }
0 commit comments