refactor: Enhance GenerationJobManager with improved subscriber handling

- Updated RuntimeJobState to include allSubscribersLeftHandlers for managing client disconnections without affecting subscriber count.
- Refined createJob and subscribe methods to ensure generation starts only when the first real client connects.
- Added detailed documentation for methods and properties to clarify the synchronization of job generation with client readiness.
- Improved logging for subscriber checks and event handling to facilitate debugging and monitoring.
This commit is contained in:
Danny Avila 2025-12-12 02:36:44 -05:00
parent 3a0c1476da
commit bf9f2a3082
No known key found for this signature in database
GPG key ID: BF31EEB2C5CA0956
3 changed files with 107 additions and 21 deletions

View file

@ -43,6 +43,10 @@ export class InMemoryEventTransport implements IEventTransport {
state.emitter.on('done', doneHandler);
state.emitter.on('error', errorHandler);
logger.debug(
`[InMemoryEventTransport] subscribe ${streamId}: listeners=${state.emitter.listenerCount('chunk')}`,
);
return {
unsubscribe: () => {
const currentState = this.streams.get(streamId);
@ -90,7 +94,9 @@ export class InMemoryEventTransport implements IEventTransport {
*/
isFirstSubscriber(streamId: string): boolean {
const state = this.streams.get(streamId);
return state?.emitter.listenerCount('chunk') === 1;
const count = state?.emitter.listenerCount('chunk') ?? 0;
logger.debug(`[InMemoryEventTransport] isFirstSubscriber ${streamId}: count=${count}`);
return count === 1;
}
/**