Implementation of the Observer pattern with priority and filtering support

TypeScript Hard classes

Task «Implementation of the Observer pattern with priority and filtering support»

Create a class `EventBus` that implements the Observer pattern. It should allow subscribing to events with a specified priority (an integer, where smaller values are called first) and an optional filter (a function that accepts data and returns true if the handler should be invoked). When an event is published, handlers are called in order of priority, only if the filter (if specified) returns true. A handler can be unsubscribed by its identifier. Implement the methods: `subscribe(event: string, handler: Function, priority: number = 0, filter?: (data: any) => boolean): string` (returns the subscription id), `unsubscribe(id: string): void`, `publish(event: string, data: any): void`. If an exception occurs when calling a handler, it should be caught and logged to the console, but should not interrupt the execution of other handlers. Input: a sequence of commands (subscribe, unsubscribe, publish). Output: the result of executing the commands (for publish — via the console).

Input Format

A sequence of commands, each on a separate line. Commands: subscribe <event> <priority> [filter], unsubscribe <id>, publish <event> <data>. For subscribe: event — string, priority — integer, filter — optional string with filter code (e.g., 'data>5'). For publish: event — string, data — JSON value (number, string, object).

Output Format

For each publish command, the result of the handlers' execution is displayed in the order they are called: a line in the format 'Handler <id> called with <data>', each on a new line. If there are no handlers — 'No handlers'. Handler errors are ignored, but their messages are printed to the console (not checked in tests).

Examples

Example 1

INPUT
2 Circle 0.0 0.0 5.0 Rectangle 3.0 2.0 7.0 0.0
OUTPUT

Example 2

INPUT
2 Point 1.0 2.0 Circle 3.0 4.0 1.0
OUTPUT
main.ts