<commit_before>"use strict";
var Base = require( '../../base' )
var set = Base.prototype.set
var remove = Base.prototype.remove
var exec = require( '../exec/method.js' )
var Event = require( '../../event' )
var isIncluded = function( arr, bind, event ) {
  for( var i in arr ) {
    if( arr[ i ].bind === bind && ( arr[ i ].event.$stamp === event.$stamp || ( event.$inherits && event.$inherits.$stamp === arr[ i ].event.$stamp ) ) ) {
      console.error( 'block' )
      return true
    }
  }
}
Event.prototype.inject( require( '../../event/toString.js' ) )
module.exports = new Base( {
  $key: '$defer',
  $flags: {
    cancel: function( val ) {
      this._cancel = val
    }
  },
  $define: {
    remove: function() {
      if( this.$inProgress ) {
        this.cancel( true )
      }
      return remove.apply( this, arguments )
    },
    set: function() {
      return set.apply( this, arguments )
    },
    cancel: function( forAllInstances ) {
      var emitter = this._$parent
      var observable = emitter.$parent.$parent
      var inProgress = this.$inProgress
      var length = inProgress.length
      var cancel = this._cancel
      var i = length - 1
      var bind
      var item

      var iShouldCancel = forAllInstances
        ? function( bind ){
            return bind instanceof observable.$Constructor || observable === bind
          }
        : function( bind ){
            return observable === bind
          }

      for (; i >= 0; i--) {
        item = inProgress[i]
        bind = item.bind
        if( iShouldCancel( bind ) ){
          if( cancel ) {
            cancel.call( bind, item.event, this )
          }
          inProgress.splice(i,1)
          length--
        }
      }

      if(!length){
        
        this.$inProgress = null

        emitter.$isPostponed = null
        emitter._$emitting = null
        emitter._$meta = null
        
      }

    },
    $exec: function( event ) {
      // console.error('hey exec!', event.toString(), this._$parent.$lastStamp)
      var emitter = this._$parent
      var stamp = event.$stamp
      var inProgress
      var $binds
      var binds

      if( emitter.$lastStamp !== stamp ) {
        if( inProgress = this.$inProgress ){
          for (var i = inProgress.length - 1; i >= 0; i--) {
            var item = inProgress[i]
            if( event.$deferred !== item ){
              this.cancel()
            }
          }
        }

        $binds = this._$parent.$binds
        if( binds = $binds && $binds[ event.$stamp ] ) {
          for( var i = 0, length = binds.length; i < length; i++ ) {
            this.$execInternal( binds[ i ], event )
          }
        }
        emitter.$lastStamp = stamp
      }
    },
    $execInternal: function( bind, event ) {
      var emitter = this._$parent
      var inProgress = this.$inProgress

      if( !inProgress ) {
        inProgress = this.$inProgress = []
      } else if( isIncluded( inProgress, bind, event ) ) {
        return
      }

      var origin = event.$origin
      var defer = this
      var oldevent

      var progress = {
        bind: bind,
        event: event
      }
      
      inProgress.push( progress )

      if( this._$val ) {
        //how to exec emitter????
        //dont do this for every instances -- they can share -- find origin
        oldevent = event
        event = new Event( bind, event.$type )
        event.$inherits = oldevent
        event.$deferred = progress
        event.$origin = bind

        var ret = this._$val.call( bind, function execEmitter() {
          var inProgress = defer.$inProgress
          if( inProgress ) {
            
            var length = inProgress.length
            var i = length - 1

            for( ; i >= 0; i-- ) {
              if( inProgress[ i ] === progress ) {
                inProgress.splice( i, 1 )
                length--
              }
            }
            if( !length ) {
              defer.$inProgress = null
            }
          }

          emitter.$execInternal( bind, event )
        }, event, this )

        if( ret === true ) {
          //if this happens dont rly need to make a new event
          this.cancel()
        }
      }
    }
  }
} )
<commit_msg>cleaned up defer<commit_after>"use strict";
var Base = require( '../../base' )
var set = Base.prototype.set
var remove = Base.prototype.remove
var exec = require( '../exec/method.js' )
var Event = require( '../../event' )

var isIncluded = function( arr, bind, event ) {
  for( var i in arr ) {
    var item = arr[i]
    var stamp = item.event.$stamp
    var inherits
    if( item.bind === bind
      && stamp === event.$stamp
        || ( inherits = event.$inherits )
          && inherits.$stamp === stamp
      ) {
      return true
    }
  }
}

Event.prototype.inject( require( '../../event/toString.js' ) )

module.exports = new Base( {
  $key: '$defer',
  $flags: {
    cancel: function( val ) {
      this._cancel = val
    }
  },
  $define: {
    remove: function() {
      if( this.$inProgress ) {
        this.cancel( true )
      }
      return remove.apply( this, arguments )
    },
    set: function() {
      return set.apply( this, arguments )
    },
    cancel: function( forAllInstances ) {
      var emitter = this._$parent
      var observable = emitter.$parent.$parent
      var inProgress = this.$inProgress
      var length = inProgress.length
      var cancel = this._cancel
      var i = length - 1
      var bind
      var item

      var iShouldCancel = forAllInstances
        ? function( bind ){
            return bind instanceof observable.$Constructor || observable === bind
          }
        : function( bind ){
            return observable === bind
          }

      for (; i >= 0; i--) {
        item = inProgress[i]
        bind = item.bind
        if( iShouldCancel( bind ) ){
          if( cancel ) {
            cancel.call( bind, item.event, this )
          }
          inProgress.splice(i,1)
          length--
        }
      }

      if(!length){
        
        this.$inProgress = null

        emitter.$isPostponed = null
        emitter._$emitting = null
        emitter._$meta = null
        
      }

    },
    $exec: function( event ) {
      // console.error('hey exec!', event.toString(), this._$parent.$lastStamp)
      var emitter = this._$parent
      var stamp = event.$stamp
      var inProgress
      var $binds
      var binds

      if( emitter.$lastStamp !== stamp ) {
        if( inProgress = this.$inProgress ){
          for (var i = inProgress.length - 1; i >= 0; i--) {
            var item = inProgress[i]
            if( event.$deferred !== item ){
              this.cancel()
            }
          }
        }

        $binds = this._$parent.$binds
        if( binds = $binds && $binds[ event.$stamp ] ) {
          for( var i = 0, length = binds.length; i < length; i++ ) {
            this.$execInternal( binds[ i ], event )
          }
        }
        emitter.$lastStamp = stamp
      }
    },
    $execInternal: function( bind, event ) {
      var emitter = this._$parent
      var inProgress = this.$inProgress

      if( !inProgress ) {
        inProgress = this.$inProgress = []
      } else if( isIncluded( inProgress, bind, event ) ) {
        return
      }

      var origin = event.$origin
      var defer = this
      var oldevent

      var progress = {
        bind: bind,
        event: event
      }
      
      inProgress.push( progress )

      if( this._$val ) {
        //how to exec emitter????
        //dont do this for every instances -- they can share -- find origin
        oldevent = event
        event = new Event( bind, event.$type )
        event.$inherits = oldevent
        event.$deferred = progress
        event.$origin = bind

        var ret = this._$val.call( bind, function execEmitter() {
          var inProgress = defer.$inProgress
          if( inProgress ) {
            
            var length = inProgress.length
            var i = length - 1

            for( ; i >= 0; i-- ) {
              if( inProgress[ i ] === progress ) {
                inProgress.splice( i, 1 )
                length--
              }
            }
            if( !length ) {
              defer.$inProgress = null
            }
          }

          emitter.$execInternal( bind, event )
        }, event, this )

        if( ret === true ) {
          //if this happens dont rly need to make a new event
          this.cancel()
        }
      }
    }
  }
} )

{'fraction_non_alphanumeric': 0.12676553672316385, 'fraction_numerical': 0.00211864406779661, 'mean_word_length': 1.505750516071955, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, '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': '2209003', 'n_tokens_mistral': 2493, 'n_tokens_neox': 2403, 'n_words': 669}