{"size": 3920, "ext": "js", "max_stars_repo_path": "windows/backend/node_modules/livereload-core/node_modules/reactive/lib/model.js", "max_stars_repo_name": "Acidburn0zzz/LiveReload", "max_stars_repo_stars_event_min_datetime": "2015-01-03T09:33:55.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-28T07:25:17.000Z", "max_issues_repo_path": "windows/backend/node_modules/livereload-core/node_modules/reactive/lib/model.js", "max_issues_repo_name": "Acidburn0zzz/LiveReload", "max_issues_repo_issues_event_min_datetime": "2015-01-15T06:56:15.000Z", "max_issues_repo_issues_event_max_datetime": "2020-03-09T02:09:10.000Z", "max_forks_repo_path": "windows/backend/node_modules/livereload-core/node_modules/reactive/lib/model.js", "max_forks_repo_name": "Acidburn0zzz/LiveReload", "max_forks_repo_forks_event_min_datetime": "2015-02-04T15:02:59.000Z", "max_forks_repo_forks_event_max_datetime": "2021-03-26T11:16:39.000Z"}
{"max_stars_count": 723, "max_issues_count": 39, "max_forks_count": 102, "avg_line_length": 30.3875968992, "max_line_length": 292, "alphanum_fraction": 0.6160714286}
// Generated by IcedCoffeeScript 1.3.3f
(function() {
  var EventEmitter, RBlock, RModel,
    __hasProp = {}.hasOwnProperty,
    __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };

  EventEmitter = require('events').EventEmitter;

  RBlock = require('./block');

  RModel = (function(_super) {

    __extends(RModel, _super);

    function RModel(options) {
      var k, schema, v;
      if (!this.constructor.name) throw new Error("R.Model must have a name");
      if (!this.constructor.isSingletonClass) {
        throw new Error("R.Model subclass " + this.constructor.name + " must be instantiated via R.Universe.create()");
      }
      this._id = this.universe.uniqueId(this.constructor.name);
      this.attributes = {};
      this._changedAttrs = {};
      this._changePending = false;
      this._blocks = [];
      this._subscribers = [];
      schema = this.constructor.schemaObj;
      schema.initializeInstance(this);
      for (k in options) {
        if (!__hasProp.call(options, k)) continue;
        v = options[k];
        if (schema.attributes.hasOwnProperty(k)) this.set(k, v);
      }
      this.initialize(options);
    }

    RModel.prototype.toString = function() {
      return this._id;
    };

    RModel.prototype.initialize = function(options) {};

    RModel.prototype.dispose = function() {
      var block, _i, _len, _ref;
      _ref = this._blocks;
      for (_i = 0, _len = _ref.length; _i < _len; _i++) {
        block = _ref[_i];
        block.dispose();
      }
      return this._blocks = [];
    };

    RModel.prototype.pleasedo = function(name, func) {
      return new RBlock(this, name, func);
    };

    RModel.prototype.get = function(attr) {
      this.universe.dependency(this, attr);
      return this.attributes[attr];
    };

    RModel.prototype.has = function(attr) {
      return this.attributes[attr] != null;
    };

    RModel.prototype.set = function(attr, value) {
      var attrSchema;
      if (!(attrSchema = this.constructor.schemaObj.attributes[attr])) {
        throw new Error("Unknown attribute " + this.constructor.name + "." + attr);
      }
      value = attrSchema.preSet(this, value);
      if (this.attributes[attr] !== value) {
        this.attributes[attr] = value;
        return this._changed(attr);
      }
    };

    RModel.prototype._changed = function(attr) {
      if (!this._changedAttrs[attr]) {
        this._changedAttrs[attr] = true;
        if (!this._changePending) {
          this._changePending = true;
          return this.universe._internal_modelChanged(this);
        }
      }
    };

    RModel.prototype.subscribe = function(subscriber, attribute) {
      return this._subscribers.push(subscriber, attribute);
    };

    RModel.prototype.unsubscribe = function(subscriber) {
      var index, subscribers, _results;
      subscribers = this._subscribers;
      index = 0;
      _results = [];
      while ((index = subscribers.indexOf(subscriber, index)) >= 0) {
        _results.push(subscribers.splice(index, 2));
      }
      return _results;
    };

    RModel.prototype.subscribersTo = function(attribute) {
      var index, result, subscribers;
      subscribers = this._subscribers;
      result = [];
      index = -1;
      while ((index = subscribers.indexOf(attribute, index + 1)) >= 0) {
        result.push(subscribers[index - 1]);
      }
      return result;
    };

    RModel.prototype._internal_startProcessingChanges = function() {
      var attrs;
      this._changePending = false;
      attrs = this._changedAttrs;
      this._changedAttrs = {};
      return attrs;
    };

    return RModel;

  })(EventEmitter);

  module.exports = RModel;

}).call(this);
