@@ -4,11 +4,11 @@ import {
44 Ok ,
55 type Operation ,
66 createContext ,
7- createScope ,
87 createSignal ,
98 each ,
109 ensure ,
1110 useScope ,
11+ type Scope ,
1212} from "effection" ;
1313import { API_ACTION_PREFIX , takeEvery } from "../action.js" ;
1414import { compose } from "../compose.js" ;
@@ -140,9 +140,10 @@ export function createThunks<Ctx extends ThunkCtx = ThunkCtx<any>>(
140140 } = { supervisor : takeEvery } ,
141141) : ThunksApi < Ctx > {
142142 const storeRegistration = new Set ( ) ;
143- const watch = createSignal < Callable < unknown > > ( ) ;
143+ const watch = createSignal < any > ( ) ;
144+
144145 const middleware : Middleware < Ctx > [ ] = [ ] ;
145- const visors : { [ key : string ] : Callable < unknown > } = { } ;
146+ const visors : { [ key : string ] : any } = { } ;
146147 const middlewareMap : { [ key : string ] : Middleware < Ctx > } = { } ;
147148 let dynamicMiddlewareMap : { [ key : string ] : Middleware < Ctx > } = { } ;
148149 const actionMap : {
@@ -218,9 +219,9 @@ export function createThunks<Ctx extends ThunkCtx = ThunkCtx<any>>(
218219 }
219220
220221 // maintains a history for any future registration
221- visors [ name ] = curVisor ;
222+ visors [ name ] = ( ) => supervise ( curVisor ) ;
222223 // signals for any stores already listening
223- watch . send ( curVisor ) ;
224+ watch . send ( ( ) => supervise ( curVisor ) ) ;
224225
225226 const errMsg = `[${ name } ] is being called before its thunk has been registered. Run \`store.run(thunks.register)\` where \`thunks\` is the name of your \`createThunks\` or \`createApi\` variable.` ;
226227 const actionFn = ( options ?: Ctx [ "payload" ] ) => {
@@ -254,9 +255,12 @@ export function createThunks<Ctx extends ThunkCtx = ThunkCtx<any>>(
254255
255256 function manage < Resource > ( name : string , resource : Operation < Resource > ) {
256257 const CustomContext = createContext < Resource > ( name ) ;
257- function * curVisor ( ) {
258- const providedResource = yield * resource ;
259- CustomContext . set ( providedResource ) ;
258+ function curVisor ( scope : Scope ) {
259+ function * kickoff ( ) {
260+ const providedResource = yield * resource ;
261+ scope . set ( CustomContext , providedResource ) ;
262+ }
263+ return kickoff ;
260264 }
261265
262266 // maintains a history for any future registration
@@ -270,29 +274,26 @@ export function createThunks<Ctx extends ThunkCtx = ThunkCtx<any>>(
270274 }
271275
272276 function * register ( ) {
273- const parent = yield * useScope ( ) ;
274- const parentStoreId = parent . get ( IdContext ) ;
275- if ( storeRegistration . has ( parentStoreId ) ) {
277+ const scope = yield * useScope ( ) ;
278+ const parentStoreId = scope . get ( IdContext ) ;
279+ if ( parentStoreId && storeRegistration . has ( parentStoreId ) ) {
276280 console . warn ( "This thunk instance is already registered." ) ;
277281 return ;
278282 }
279283 storeRegistration . add ( parentStoreId ) ;
280284
281- const [ thunkScope , destroy ] = createScope ( parent ) ;
282-
283285 yield * ensure ( function * ( ) {
284286 storeRegistration . delete ( parentStoreId ) ;
285- yield * destroy ( ) ;
286287 } ) ;
287288
288289 // Register any thunks created before listening to signal
289290 for ( const created of Object . values ( visors ) ) {
290- yield * thunkScope . spawn ( supervise ( created ) ) ;
291+ yield * scope . spawn ( created ( scope ) ) ;
291292 }
292293
293294 // wait for further thunk create
294295 for ( const watched of yield * each ( watch ) ) {
295- yield * thunkScope . spawn ( supervise ( watched ) ) ;
296+ yield * scope . spawn ( watched ( scope ) ) ;
296297 yield * each . next ( ) ;
297298 }
298299 }
0 commit comments