Skip to content

Commit acbb929

Browse files
committed
wip
1 parent b650d4f commit acbb929

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

packages/signals/signals/src/core/emitter/__tests__/signal-emitter.test.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,4 +298,47 @@ describe(SignalEmitter, () => {
298298
expect.objectContaining({ foo: mockCtx })
299299
)
300300
})
301+
302+
it('should not block the processing of signals for multiple subscribers .load method', async () => {
303+
class MockSubscriber1 implements SignalsSubscriber {
304+
process(signal: Signal): Signal {
305+
return signal
306+
}
307+
ctx!: SignalsMiddlewareContext
308+
async load(ctx: SignalsMiddlewareContext): Promise<void> {
309+
this.ctx = ctx
310+
}
311+
}
312+
const mockSubscriber1 = new MockSubscriber1()
313+
const processSpy1 = jest.spyOn(mockSubscriber1, 'process')
314+
const loadSpy1 = jest.spyOn(mockSubscriber1, 'load')
315+
316+
class MockSubscriber2 extends MockSubscriber1 {
317+
async load(): Promise<void> {
318+
await sleep(50)
319+
}
320+
}
321+
322+
const mockSubscriber2 = new MockSubscriber2()
323+
const processSpy2 = jest.spyOn(mockSubscriber2, 'process')
324+
const loadSpy2 = jest.spyOn(mockSubscriber2, 'load')
325+
326+
emitter = new SignalEmitter().subscribe(mockSubscriber1, mockSubscriber2)
327+
328+
emitter.emit(mockSignal)
329+
330+
void emitter.start(mockCtx)
331+
332+
await sleep(0)
333+
334+
expect(loadSpy1).toHaveBeenCalledTimes(1)
335+
expect(processSpy1).toHaveBeenCalledWith(mockSignal)
336+
337+
// Subscriber 2 has an async load method, but it should not block the processing of signals for subscriber 1
338+
expect(loadSpy2).toHaveBeenCalledTimes(1)
339+
expect(processSpy2).not.toHaveBeenCalled()
340+
341+
await sleep(50)
342+
expect(processSpy2).toHaveBeenCalledTimes(1)
343+
})
301344
})

0 commit comments

Comments
 (0)