44 type Operation ,
55 type Scope ,
66 createContext ,
7- createSignal ,
87 each ,
98 ensure ,
109 useScope ,
@@ -15,6 +14,7 @@ import { supervise } from "../fx/index.js";
1514import { createKey } from "./create-key.js" ;
1615import { isFn , isObject } from "./util.js" ;
1716
17+ import { createReplaySignal } from "../fx/replay-signal.js" ;
1818import { IdContext } from "../store/store.js" ;
1919import type { ActionWithPayload , AnyAction , Next , Payload } from "../types.js" ;
2020import type {
@@ -139,10 +139,9 @@ export function createThunks<Ctx extends ThunkCtx = ThunkCtx<any>>(
139139 } = { supervisor : takeEvery } ,
140140) : ThunksApi < Ctx > {
141141 const storeRegistration = new Set ( ) ;
142- const watch = createSignal < Visors > ( ) ;
142+ const watch = createReplaySignal < Visors > ( ) ;
143143
144144 const middleware : Middleware < Ctx > [ ] = [ ] ;
145- const visors : { [ key : string ] : Visors } = { } ;
146145 const middlewareMap : { [ key : string ] : Middleware < Ctx > } = { } ;
147146 let dynamicMiddlewareMap : { [ key : string ] : Middleware < Ctx > } = { } ;
148147 const actionMap : {
@@ -174,7 +173,7 @@ export function createThunks<Ctx extends ThunkCtx = ThunkCtx<any>>(
174173 }
175174
176175 function create ( name : string , ...args : any [ ] ) {
177- if ( visors [ name ] ) {
176+ if ( actionMap [ name ] ) {
178177 const msg = `[${ name } ] already exists, do you have two thunks with the same name?` ;
179178 console . warn ( msg ) ;
180179 }
@@ -216,10 +215,6 @@ export function createThunks<Ctx extends ThunkCtx = ThunkCtx<any>>(
216215 function * curVisor ( ) : Operation < void > {
217216 yield * tt ( type , onApi ) ;
218217 }
219-
220- // maintains a history for any future registration
221- visors [ name ] = ( ) => supervise ( curVisor ) ;
222- // signals for any stores already listening
223218 watch . send ( ( ) => supervise ( curVisor ) ) ;
224219
225220 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.` ;
@@ -262,9 +257,6 @@ export function createThunks<Ctx extends ThunkCtx = ThunkCtx<any>>(
262257 return kickoff ;
263258 }
264259
265- // maintains a history for any future registration
266- visors [ name ] = curVisor ;
267- // signals for any stores already listening
268260 watch . send ( curVisor ) ;
269261
270262 // returns to the user can use this resource from
@@ -285,11 +277,6 @@ export function createThunks<Ctx extends ThunkCtx = ThunkCtx<any>>(
285277 storeRegistration . delete ( parentStoreId ) ;
286278 } ) ;
287279
288- // Register any thunks created before listening to signal
289- for ( const created of Object . values ( visors ) ) {
290- yield * scope . spawn ( created ( scope ) ) ;
291- }
292-
293280 // wait for further thunk create
294281 for ( const watched of yield * each ( watch ) ) {
295282 yield * scope . spawn ( watched ( scope ) ) ;
0 commit comments