#C8725. Efficient Event Emitter Simulation

    ID: 52739 Type: Default 1000ms 256MiB

Efficient Event Emitter Simulation

Efficient Event Emitter Simulation

You are tasked with simulating an Event Emitter system. The system supports the following commands:

  • on <event> <listener_id> — Register a persistent listener with id listener_id for the event named event.
  • once <event> <listener_id> — Register a one-time listener with id listener_id for the event. When the event is emitted, the listener will be invoked and then automatically removed.
  • off <event> <listener_id> — Remove all occurrences of the listener with id listener_id for the event.
  • emit <event> [args...] — Emit an event with optional arguments. All listeners registered for that event are invoked in the order they were added. Each listener prints a line to stdout in the following format: listener<listener_id> arg1 arg2 ....

The system starts with no registered listeners. Your task is to process a series of commands read from standard input (stdin) and output the results (if any) to standard output (stdout). Ensure that one-time listeners are properly removed after being called.

Note: The commands are given one per line. The first line of the input contains an integer n indicating the number of commands that follow.

inputFormat

The input begins with an integer n (1 ≤ n ≤ 10^3) on the first line, representing the number of commands that follow. Each of the next n lines contains a command in one of the following formats:

  • on <event> <listener_id>
  • once <event> <listener_id>
  • off <event> <listener_id>
  • emit <event> [args...]

outputFormat

For each emit command, output the result of all listener invocations in the order they are called. Each invocation should be printed as a separate line in the following format:

listener<listener_id> arg1 arg2 ...

If an emit command triggers no listeners, nothing is printed for that command.

## sample
5
on event1 1
on event1 2
emit event1 hello world
off event1 1
emit event1 test case
listener1 hello world

listener2 hello world listener2 test case

</p>