import { Class } from './lib/objects';

/**
 The `Notifier` class can emit messages to an array of subscribed listeners.
 Here's a simple example:

 ```javascript
 var notifier = new Orbit.Notifier();
 notifier.addListener(function(message) {
   console.log("I heard " + message);
 });
 notifier.addListener(function(message) {
   console.log("I also heard " + message);
 });

 notifier.emit('hello'); // logs "I heard hello" and "I also heard hello"
 ```

 Notifiers can also poll listeners with an event and return their responses:

 ```javascript
 var dailyQuestion = new Orbit.Notifier();
 dailyQuestion.addListener(function(question) {
   if (question === 'favorite food?') return 'beer';
 });
 dailyQuestion.addListener(function(question) {
   if (question === 'favorite food?') return 'wasabi almonds';
 });
 dailyQuestion.addListener(function(question) {
   // this listener doesn't return anything, and therefore won't participate
   // in the poll
 });

 dailyQuestion.poll('favorite food?'); // returns ['beer', 'wasabi almonds']
 ```

 Calls to `emit` and `poll` will send along all of their arguments.

 @class Notifier
 @namespace Orbit
 @constructor
 */
var Notifier = Class.extend({
  init: function() {
    this.listeners = [];
  },

  /**
   Add a callback as a listener, which will be triggered when sending
   notifications.

   @method addListener
   @param {Function} callback Function to call as a notification
   @param {Object} binding Context in which to call `callback`
   */
  addListener: function(callback, binding) {
    binding = binding || this;
    this.listeners.push([callback, binding]);
  },

  /**
   Remove a listener so that it will no longer receive notifications.

   @method removeListener
   @param {Function} callback Function registered as a callback
   @param {Object} binding Context in which `callback` was registered
   */
  removeListener: function(callback, binding) {
    let listeners = this.listeners;
    let listener;

    binding = binding || this;
    for (var i = 0, len = listeners.length; i < len; i++) {
      listener = listeners[i];
      if (listener && listener[0] === callback && listener[1] === binding) {
        listeners.splice(i, 1);
        return;
      }
    }
  },

  /**
   Notify registered listeners.

   Any responses from listeners will be ignored.

   @method emit
   @param {*} Any number of parameters to be sent to listeners
   */
  emit: function() {
    var args = arguments;
    this.listeners.slice(0).forEach(function(listener) {
      listener[0].apply(listener[1], args);
    });
  },

  /**
   Poll registered listeners.

   Any responses from listeners will be returned in an array.

   @method poll
   @param {*} Any number of parameters to be sent to listeners
   @returns {Array} Array of responses
   */
  poll: function() {
    let args = arguments;
    let allResponses = [];
    let response;

    this.listeners.slice(0).forEach(function(listener) {
      response = listener[0].apply(listener[1], args);
      if (response !== undefined) { allResponses.push(response); }
    });

    return allResponses;
  }
});

export default Notifier;

@@ -1,44 +1,4 @@
-import %7B Class %7D from './lib/objects';%0A%0A
 /**%0A
@@ -1126,54 +1126,53 @@
  */%0A
-var Notifier = Class.extend(%7B%0A  init: function
+export default class Notifier %7B%0A  constructor
 () %7B
@@ -1196,25 +1196,24 @@
 rs = %5B%5D;%0A  %7D
-,
 %0A%0A  /**%0A   A
@@ -1463,34 +1463,24 @@
  addListener
-: function
 (callback, b
@@ -1565,25 +1565,24 @@
 nding%5D);%0A  %7D
-,
 %0A%0A  /**%0A   R
@@ -1830,26 +1830,16 @@
 Listener
-: function
 (callbac
@@ -2167,25 +2167,24 @@
  %7D%0A    %7D%0A  %7D
-,
 %0A%0A  /**%0A   N
@@ -2357,48 +2357,19 @@
 emit
-: function() %7B%0A    var args = arguments;
+(...args) %7B
 %0A   
@@ -2397,32 +2397,24 @@
 (0).forEach(
-function
 (listener) %7B
@@ -2403,32 +2403,35 @@
 rEach((listener)
+ =%3E
  %7B%0A      listene
@@ -2472,17 +2472,16 @@
  %7D);%0A  %7D
-,
 %0A%0A  /**%0A
@@ -2708,48 +2708,19 @@
 poll
-: function() %7B%0A    let args = arguments;
+(...args) %7B
 %0A   
@@ -2746,26 +2746,8 @@
  %5B%5D;
-%0A    let response;
 %0A%0A  
@@ -2780,24 +2780,16 @@
 forEach(
-function
 (listene
@@ -2790,16 +2790,19 @@
 istener)
+ =%3E
  %7B%0A     
@@ -2802,16 +2802,20 @@
 %7B%0A      
+let 
 response
@@ -2965,33 +2965,5 @@
  %7D%0A%7D
-);%0A%0Aexport default Notifier;
 %0A

Convert Notifier to ES class.