Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
21e8 committed Dec 7, 2024
1 parent 96c9b60 commit 920e2a3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
32 changes: 23 additions & 9 deletions src/batcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,19 @@ import {
type BatcherConfig,
type NotificationLevel,
type MessageProcessor,
type IMessageBatcher
type MessageBatcher,
} from './types';

let instance: MessageBatcher | null = null;

export function createMessageBatcher(
processors: MessageProcessor[],
config: Required<BatcherConfig>
): IMessageBatcher {
): MessageBatcher {
if (instance) {
return instance;
}

const queues: Map<string, Message[]> = new Map();
const timers: Map<string, NodeJS.Timeout> = new Map();
let processInterval: NodeJS.Timeout | null = null;
Expand Down Expand Up @@ -84,18 +90,26 @@ export function createMessageBatcher(
queues.clear();
}

// Start processing on creation
startProcessing();

// Return the public interface
return {
// Create the instance
instance = {
info,
warning,
error,
queueMessage,
flush,
destroy
destroy,
};
}

// Start processing on creation
startProcessing();

return instance;
}

// Add a way to reset the singleton (mainly for testing)
export function resetBatcher(): void {
if (instance) {
instance.destroy();
instance = null;
}
}
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ export type {
BatcherConfig,
NotificationLevel,
MessageProcessor,
IMessageBatcher,
MessageBatcher as IMessageBatcher,
} from './types';
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export interface MessageProcessor {
processBatch(messages: Message[]): Promise<void>;
}

export interface IMessageBatcher {
export interface MessageBatcher {
info(message: string): void;
warning(message: string): void;
error(message: string): void;
Expand Down

0 comments on commit 920e2a3

Please sign in to comment.