{"size": 1561, "ext": "js", "max_stars_repo_path": "src/lib/Dependents.js", "max_stars_repo_name": "Rekord/neurosync", "max_stars_repo_stars_event_min_datetime": "2015-12-23T14:44:31.000Z", "max_stars_repo_stars_event_max_datetime": "2016-03-27T21:21:51.000Z", "max_issues_repo_path": "src/lib/Dependents.js", "max_issues_repo_name": "Rekord/neurosync", "max_issues_repo_issues_event_min_datetime": "2016-04-06T03:51:19.000Z", "max_issues_repo_issues_event_max_datetime": "2021-10-06T01:46:07.000Z", "max_forks_repo_path": "src/lib/Dependents.js", "max_forks_repo_name": "Rekord/neurosync", "max_forks_repo_forks_event_min_datetime": "2016-02-14T02:19:36.000Z", "max_forks_repo_forks_event_max_datetime": "2016-03-23T22:40:26.000Z"}
{"max_stars_count": 102, "max_issues_count": 247, "max_forks_count": 15, "avg_line_length": 18.5833333333, "max_line_length": 82, "alphanum_fraction": 0.5855221012}

function Dependents(subject)
{
  this.map = {};
  this.listeners = {};

  this.subject = subject;
}

Class.create( Dependents,
{

  add: function(model, relator)
  {
    var key = model.$uid();

    this.map[ key ] = model;

    if ( model.$db.keyChanges && !this.listeners[ key ] )
    {
      var listener = this.handleKeyChange( relator );

      this.listeners[ key ] = model.$on( Model.Events.KeyChange, listener, this );
    }
  },

  remove: function(model)
  {
    var key = model.$uid();

    evaluate( this.listeners[ key ] );

    delete this.listeners[ key ];
    delete this.map[ key ];
  },

  handleKeyChange: function(relator)
  {
    return function(model, oldKey, newKey)
    {
      var prefix = model.$db.name + '$';

      oldKey = prefix + oldKey;
      newKey = prefix + newKey;

      this.listeners[ newKey ] = this.listeners[ oldKey ];
      this.map[ newKey ] = this.map[ oldKey ];

      delete this.listeners[ oldKey ];
      delete this.map[ oldKey ];

      relator.updateForeignKey( this.subject, model, true );
    };
  },

  isSaved: function(callbackOnSaved, contextOnSaved)
  {
    var dependents = this.map;
    var off = noop;

    var onDependentSave = function()
    {
      callbackOnSaved.apply( contextOnSaved || this, arguments );

      off();
    };

    for (var uid in dependents)
    {
      var dependent = dependents[ uid ];

      if ( !dependent.$isSaved() )
      {
        off = dependent.$once( Model.Events.RemoteSaves, onDependentSave );

        return false;
      }
    }

    return true;
  }

});
