5 kyu

Simple Events

4,560 of 6,033akoptsov

Description:

Your goal is to write an Event constructor function, which can be used to make event objects.

An event object should work like this:

  • it has a .subscribe() method, which takes a function and stores it as its handler
  • it has an .unsubscribe() method, which takes a function and removes it from its handlers
  • it has an .emit() method, which takes an arbitrary number of arguments and calls all the stored functions with these arguments

As this is an elementary example of events, there are some simplifications:

  • all functions are called with correct arguments (e.g. only functions will be passed to unsubscribe)
  • you should not worry about the order of handlers' execution
  • the handlers will not attempt to modify an event object (e.g. add or remove handlers)
  • the context of handlers' execution is not important
  • each handler will be subscribed at most once at any given moment of time. It can still be unsubscribed and then subscribed again

Also see an example test fixture for suggested usage

Design Patterns
Event Handling
Fundamentals

Stats:

CreatedJan 13, 2014
PublishedJan 13, 2014
Warriors Trained14328
Total Skips3656
Total Code Submissions28832
Total Times Completed6033
JavaScript Completions4560
Ruby Completions203
Python Completions1314
Total Stars354
% of votes with a positive feedback rating85% of 586
Total "Very Satisfied" Votes439
Total "Somewhat Satisfied" Votes116
Total "Not Satisfied" Votes24
Ad
Contributors
  • akoptsov Avatar
  • GiacomoSorbi Avatar
  • sid114 Avatar
Ad