@@ -2,6 +2,7 @@ import { ActionContext, API_ACTION_PREFIX, takeEvery } from "../action.ts";
22import { compose } from "../compose.ts" ;
33import { Callable , ensure , Ok , Operation , Signal } from "../deps.ts" ;
44import { keepAlive , supervise } from "../fx/mod.ts" ;
5+ import { IdContext } from "../store/store.ts" ;
56import { createKey } from "./create-key.ts" ;
67import { isFn , isObject } from "./util.ts" ;
78
@@ -15,7 +16,6 @@ import type {
1516 Supervisor ,
1617 ThunkCtx ,
1718} from "./types.ts" ;
18-
1919export interface ThunksApi < Ctx extends ThunkCtx > {
2020 use : ( fn : Middleware < Ctx > ) => void ;
2121 routes : ( ) => Middleware < Ctx > ;
@@ -82,6 +82,8 @@ export interface ThunksApi<Ctx extends ThunkCtx> {
8282 ) : CreateActionWithPayload < Gtx , P > ;
8383}
8484
85+ let id = 0 ;
86+
8587/**
8688 * Creates a middleware pipeline.
8789 *
@@ -124,17 +126,17 @@ export function createThunks<Ctx extends ThunkCtx = ThunkCtx<any>>(
124126 } = { supervisor : takeEvery } ,
125127) : ThunksApi < Ctx > {
126128 let signal : Signal < AnyAction , void > | undefined = undefined ;
129+ let storeId : number | undefined = undefined ;
127130 const middleware : Middleware < Ctx > [ ] = [ ] ;
128131 const visors : { [ key : string ] : Callable < unknown > } = { } ;
129132 const middlewareMap : { [ key : string ] : Middleware < Ctx > } = { } ;
130133 let dynamicMiddlewareMap : { [ key : string ] : Middleware < Ctx > } = { } ;
131134 const actionMap : {
132135 [ key : string ] : CreateActionWithPayload < Ctx , any > ;
133136 } = { } ;
134- const thunkId = `${ Date . now ( ) . toString ( 36 ) } -${
135- Math . random ( ) . toString ( 36 ) . substring ( 2 , 11 )
136- } `;
137- let hasRegistered = false ;
137+ const thunkId = id ++ ;
138+
139+ const storeMap = new Map < number , Signal < AnyAction , void > > ( ) ;
138140
139141 function * defaultMiddleware ( _ : Ctx , next : Next ) {
140142 yield * next ( ) ;
@@ -207,10 +209,10 @@ export function createThunks<Ctx extends ThunkCtx = ThunkCtx<any>>(
207209
208210 visors [ name ] = curVisor ;
209211
210- // If signal is available , register immediately, otherwise defer
211- if ( signal ) {
212- signal . send ( {
213- type : `${ API_ACTION_PREFIX } REGISTER_THUNK_${ thunkId } ` ,
212+ // If signal is already referenced , register immediately, otherwise defer
213+ for ( const [ storeId , storeSignal ] of storeMap . entries ( ) ) {
214+ storeSignal . send ( {
215+ type : `${ API_ACTION_PREFIX } REGISTER_THUNK_${ storeId } _ ${ thunkId } ` ,
214216 payload : curVisor ,
215217 } ) ;
216218 }
@@ -253,23 +255,27 @@ export function createThunks<Ctx extends ThunkCtx = ThunkCtx<any>>(
253255 }
254256
255257 function * register ( ) {
256- if ( hasRegistered ) {
258+ storeId = yield * IdContext ;
259+ if ( storeId && storeMap . has ( storeId ) ) {
257260 console . warn ( "This thunk instance is already registered." ) ;
258261 return ;
259262 }
260- hasRegistered = true ;
263+
261264 signal = yield * ActionContext ;
265+ storeMap . set ( storeId , signal ) ;
262266
263267 yield * ensure ( function * ( ) {
264- hasRegistered = false ;
268+ if ( storeId ) {
269+ storeMap . delete ( storeId ) ;
270+ }
265271 } ) ;
266272
267273 // Register any thunks created after signal is available
268274 yield * keepAlive ( Object . values ( visors ) ) ;
269275
270276 // Spawn a watcher for further thunk matchingPairs
271277 yield * takeEvery (
272- `${ API_ACTION_PREFIX } REGISTER_THUNK_${ thunkId } ` ,
278+ `${ API_ACTION_PREFIX } REGISTER_THUNK_${ storeId } _ ${ thunkId } ` ,
273279 watcher as any ,
274280 ) ;
275281 }
0 commit comments