4 kyu

Advanced Events

1,776 of 1,785akoptsov

Description:

This excercise is a more sophisticated version of Simple Events kata.

Your task is to implement an Event constructor function for creating event objects

var event = new Event();

which comply to the following:

  • an event object should have .subscribe() and .unsubscribe() methods to add and remove handlers

  • .subscribe() and .unsubscribe() should be able take an arbitrary number of arguments and tolerate invalid arguments (not functions, or for unsubscribe, functions which are not subscribed) by simply skipping them

  • multiple subscription of the same handler is allowed, and in this case unsubscription removes the last subscription of the same handler

  • an event object should have an .emit() method which must invoke all the handlers with the arguments provided

  • .emit() should use its own invocation context as handers' invocation context

  • the order of handlers invocation must match the order of subscription

  • handler functions can subscribe and unsubscribe handlers, but the changes should only apply to the next emit call - the handlers for an ongoing emit call should not be affected

  • subscribe, unsubscribe and emit are the only public properties that are allowed on event objects (apart from Object.prototype methods)

Check the test fixture for usage example

Design Patterns
Event Handling

Stats:

CreatedJan 13, 2014
PublishedJan 14, 2014
Warriors Trained6530
Total Skips2179
Total Code Submissions41250
Total Times Completed1785
JavaScript Completions1776
Total Stars270
% of votes with a positive feedback rating86% of 203
Total "Very Satisfied" Votes160
Total "Somewhat Satisfied" Votes30
Total "Not Satisfied" Votes13
Ad
Contributors
  • akoptsov Avatar
Ad