<commit_before>(function(context){
  "use strict";

  function animationEnd(e){
    this.previousFloor = this.requestsStack.shift();

    this.transition('unloading');
  }

  function queuedNotice(){
  }

  /**
   * The expected flow of things is
   * 1. idle
   * 2. request for a floor number
   * 3. moving (and stacking calls)
   * 4. unloading
   * 4.1 idle if queue empty
   * 4.2 moving to first item if queue not empty
   */
  var Elevator = machina.Fsm.extend({
    initialState: "idle",
    el: null,
    goingToFloor: 1,
    previousFloor: 1,
    initialize: function(){
      var self = this;

      //we do it here otherwise the stack is shared among the various elevators (and we don't want it)
      this.requestsStack = [];

      this.el.addEventListener('transitionend', animationEnd.bind(self));
      this.el.addEventListener('webkitTransitionEnd', animationEnd.bind(self));
    },
    states: {
      /**
       * Actions done when the elevator has an empty state
       */
      'idle': {
        _onEnter: function(){
          if (!this.requestsStack.length){
            this.emit('idle', this.goingToFloor, this);
          }

          this.goingToFloor = null;
        },
        'move': function(floor_number){
          this.transition('moving');
          this.goingToFloor = floor_number;

          this.emit('moving', this);

          // using this.goingToFloor because the event might change the direction
          // hardcoding :-( related to CSS height & margin
          var floorDiff = Math.abs(this.goingToFloor - this.previousFloor);

          this.el.style.bottom = ((this.goingToFloor - 1)*100 + 2) + 'px';
          this.el.style.transitionDuration = (floorDiff - (0.1*floorDiff))+"s";
          this.el.setAttribute('data-at-floor', this.goingToFloor);
        }
      },
      /**
       * Actions done when the elevator is moving
       */
      'moving': {
        'move': queuedNotice
      },
      /**
       * Actions done when the elevator is unloading people
       */
      'unloading': {
        _onEnter: function(){
          var self = this;

          setTimeout(function(){
            self.emit('unload', self.goingToFloor);
            self.transition('idle');

            if (self.requestsStack.length){
              console.log('about to move to ', self.requestsStack[0]);
              self.handle('move', self.requestsStack[0]);
            }
          }, 1000);
        },
        'move': queuedNotice
      }
    },
    /**
     * Public API used to drag an elevator to a certain level
     *
     * @api
     * @param floor_number
     * @returns {*}
     */
    goToFloor: function(floor_number){
      if (~this.requestsStack.indexOf(floor_number) === 0){
        this.requestsStack.push(floor_number);
        this.handle('move', floor_number);
      }

      return this;
    }
  });

  context['Elevator'] = Elevator;
})(window);<commit_msg>If calling the same floor where we are at, the elevator should not queue anything.<commit_after>(function(context){
  "use strict";

  function animationEnd(e){
    this.previousFloor = this.requestsStack.shift();

    this.transition('unloading');
  }

  function queuedNotice(){
  }

  /**
   * The expected flow of things is
   * 1. idle
   * 2. request for a floor number
   * 3. moving (and stacking calls)
   * 4. unloading
   * 4.1 idle if queue empty
   * 4.2 moving to first item if queue not empty
   */
  var Elevator = machina.Fsm.extend({
    initialState: "idle",
    el: null,
    goingToFloor: 1,
    previousFloor: 1,
    initialize: function(){
      //we do it here otherwise the stack is shared among the various elevators (and we don't want it)
      this.requestsStack = [];

      this.el.addEventListener('transitionend', animationEnd.bind(this));
      this.el.addEventListener('webkitTransitionEnd', animationEnd.bind(this));
    },
    states: {
      /**
       * Actions done when the elevator has an empty state
       */
      'idle': {
        _onEnter: function(){
          if (!this.requestsStack.length){
            this.emit('idle', this.goingToFloor, this);
          }

          this.goingToFloor = null;
        },
        'move': function(floor_number){
          this.transition('moving');
          this.goingToFloor = floor_number;

          this.emit('moving', this);

          // using this.goingToFloor because the event might change the direction
          // hardcoding :-( related to CSS height & margin
          var floorDiff = Math.abs(this.goingToFloor - this.previousFloor);

          this.el.style.bottom = ((this.goingToFloor - 1)*100 + 2) + 'px';
          this.el.style.transitionDuration = (floorDiff - (0.1*floorDiff))+"s";
          this.el.setAttribute('data-at-floor', this.goingToFloor);
        }
      },
      /**
       * Actions done when the elevator is moving
       */
      'moving': {
        'move': queuedNotice
      },
      /**
       * Actions done when the elevator is unloading people
       */
      'unloading': {
        _onEnter: function(){
          var self = this;

          setTimeout(function(){
            self.emit('unload', self.goingToFloor);
            self.transition('idle');

            if (self.requestsStack.length){
              console.log('about to move to ', self.requestsStack[0]);
              self.handle('move', self.requestsStack[0]);
            }
          }, 1000);
        },
        'move': queuedNotice
      }
    },
    /**
     * Public API used to drag an elevator to a certain level
     *
     * @api
     * @param floor_number
     * @returns {Elevator}
     */
    goToFloor: function(floor_number){
      if (
        this.requestsStack.indexOf(floor_number) === -1
        && this.previousFloor !== floor_number
      ){
        this.requestsStack.push(floor_number);
        this.handle('move', floor_number);
      }

      return this;
    }
  });

  context['Elevator'] = Elevator;
})(window);
{'fraction_non_alphanumeric': 0.12640590901460466, 'fraction_numerical': 0.00805774718818197, 'mean_word_length': 2.0663921770458056, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '179737', 'n_tokens_mistral': 1774, 'n_tokens_neox': 1697, 'n_words': 429}