DevPrep
  • Interview Prep
  • Projects
  • Resources
  • Pricing
  • About Us
Submit Question
DevPrep
  • Pricing
  • About Us
Submit Question

Practice

  • JavaScript
  • DSA
  • Machine Coding
  • System Design

Resources

  • Learning Tracks
  • Articles
  • Roadmaps
  • Compare Concepts
  • Glossary
  • Developer Tools
  • All Questions

Company

  • About
  • Pricing

Legal

  • Privacy Policy
  • Terms of Service
DevPrep

© 2026 DevPrep. All rights reserved.

← Back to Questions
EasyJavaScript

Wrap DOM Event Listeners Into a Subscribable Stream With Automatic Cleanup

6 views

We are building a High-Frequency Trading Terminal. The interface has hundreds of buttons, inputs, and real-time data toggles. Using raw addEventListener across the app has led to memory leaks because developers frequently forget to call removeEventListener when components are destroyed.

The Task: Implement a simplified Observable.fromEvent utility. This utility must encapsulate the event listener logic so that the lifecycle of the event is tied directly to the subscription. When a user subscribes, the listener is attached; when they unsubscribe, it is removed. How do you ensure this is memory-safe and supports multiple independent observers?

Sample Test Cases

Case 1
Input
subscribe to click, trigger click
Expected Output
next receives event
Case 2
Input
["click", "#btn1", 1]
Expected Output
["click-btn1-1"]
Case 3
Input
unsubscribe, trigger click
Expected Output
no callback
Case 4
Input
["mouseover", "#div1", 2]
Expected Output
["mouseover-div1-1", "mouseover-div1-2"]
Case 5
Input
multiple subscribers
Expected Output
all fire on event
Case 6
Input
["keydown", "#input1", 1, {"key": "Enter"}]
Expected Output
["keydown-input1-1-Enter"]

No solutions yet

Be the first to share a solution for this question.

Comments (0)

Sign in to leave a comment.

No comments yet. Be the first to comment.

Stats

Views
6
Likes
0
Solutions
0
Comments
0

Category

Frontend Engineering

Languages

JavaScript