<commit_before>import Ember from 'ember';
import createContext from './utils/context';
import Result from './result';
import Queue from './observer-queue';
import State from './validators/state';

var EachProxy = Ember.__loader.require('ember-runtime/system/each_proxy')['EachProxy'] || Ember.__loader.require('ember-runtime/system/each_proxy').default;

/**
 * Observer for automatic object validation
 * 
 * @namespace Furnace.Validation
 * @class Observer
 */
var Observer = Ember.Object.extend({
	
	_debugLogging : false,
	
	_validator : null,
	
	_target : null,
	
	_key : null,
	
	_keys : null,
	
	_callback : null,
	
	_name : null,
	
	_context : null,
	
	_chain : null,
	
	_getValue : function() {		
		Ember.warn('Validation observer received an unobservable object '+this._target+':'+this._key,Ember.Observable.detect(this._target),{id:'furnace-validation:observer-target-not-observable'});		
		var value = this._key ? Ember.get(this._target,this._key) : this._target;
		if(Ember.PromiseProxyMixin.detect(value)) {
			value=value.content;
		} 
		return value;
	},
	
	_orgValue : null,
	
	_children : null,
	
	_queue : null,
	
	init : function() {
		
		if(!this._result) {
			this._result=Result.create();
		}
		if(!this._context) {
			this._context=createContext(this._getValue(),this._name || this._key, this._result);
		}
		if(this._chain===null) {
			this._chain=Ember.A();
		}
		if(this._queue===null) {
			this._queue=Queue.create({callback: this._callback});
		}
		this._keys=[];

		this._children=Ember.A();
		
		if(this._chain.length>100) {
			console.log(this._chain);
			throw "Too much recursion?";
		}
		var chain =this._chain.filterBy('target',this._target).filterBy('key',this._key);
		if(chain.length!==0) {			
			if(chain.filterBy('validator',this._validator).length===0) {
				this._validator._observe(this);
			}
		}
		else {
			this._chain.push({target : this._target,key: this._key,validator:this._validator});		
			this._orgValue=this._getValue();
			this._validator._observe(this);
			this._attach();	
		}
		
		
	},
	
	_detach:function() {		
		Ember.removeObserver(this._target,this._key,this,this._fn);
		if(Ember.Array.detect(this._orgValue)) {
			this._orgValue.removeArrayObserver(this);
		}
		this._detachKeys();
	},
	
	_detachKeys:function() {
//		if(Ember.Observable.detect(this._orgValue)) {
			for(var i=0;i<this._keys.length;i++) {
				Ember.removeObserver(this._orgValue,this._keys[i],this,this._fn);
			}
//		}
	},
	
	_attach: function() {
		if(this._debugLogging) {
			this._logEvent('Observing',this._target.toString(),this._key);
		}
		if(this._key) {
			Ember.addObserver(this._target,this._key,this,this._fn);
		}
		if(Ember.Array.detect(this._getValue())) {
			this._getValue().addArrayObserver(this);
		}
		this._attachKeys();
	},
	
	_attachKeys: function() {
		if(Ember.Observable.detect(this._getValue())) {
			for(var i=0;i<this._keys.length;i++) {
				Ember.addObserver(this._getValue(),this._keys[i],this,this._fn);
			}
		}
	},
	
	arrayWillChange: function() {
	},
	
	arrayDidChange: function(arr) {
		this._fnOnce(arr,this._key);
	},
	
	_observeKey: function(key) {
		this._keys.push(key);
//		if(Ember.Observable.detect(this._getValue())) {
			Ember.addObserver(this._getValue(),key,this,this._fn);
//		}
	},
	
	_observe : function(key,validator,keepContext) {
		if(!validator) {
			validator=key;<commit_msg>Fixed regression in observer <commit_after>import Ember from 'ember';
import createContext from './utils/context';
import Result from './result';
import Queue from './observer-queue';
import State from './validators/state';

var EachProxy = Ember.__loader.require('ember-runtime/system/each_proxy')['EachProxy'] || Ember.__loader.require('ember-runtime/system/each_proxy').default;

/**
 * Observer for automatic object validation
 * 
 * @namespace Furnace.Validation
 * @class Observer
 */
var Observer = Ember.Object.extend({
	
	_debugLogging : false,
	
	_validator : null,
	
	_target : null,
	
	_key : null,
	
	_keys : null,
	
	_callback : null,
	
	_name : null,
	
	_context : null,
	
	_chain : null,
	
	_getValue : function() {		
		Ember.warn('Validation observer received an unobservable object '+this._target+':'+this._key,Ember.Observable.detect(this._target),{id:'furnace-validation:observer-target-not-observable'});		
		var value = this._key ? Ember.get(this._target,this._key) : this._target;
		if(Ember.PromiseProxyMixin.detect(value)) {
			value=value.content;
		} 
		return value;
	},
	
	_orgValue : null,
	
	_children : null,
	
	_queue : null,
	
	init : function() {
		
		if(!this._result) {
			this._result=Result.create();
		}
		if(!this._context) {
			this._context=createContext(this._getValue(),this._name || this._key, this._result);
		}
		if(this._chain===null) {
			this._chain=Ember.A();
		}
		if(this._queue===null) {
			this._queue=Queue.create({callback: this._callback});
		}
		this._keys=[];

		this._children=Ember.A();
		
		if(this._chain.length>100) {
			console.log(this._chain);
			throw "Too much recursion?";
		}
		var chain =this._chain.filterBy('target',this._target).filterBy('key',this._key);
		if(chain.length!==0) {			
			if(chain.filterBy('validator',this._validator).length===0) {
				this._validator._observe(this);
			}
		}
		else {
			this._chain.push({target : this._target,key: this._key,validator:this._validator});		
			this._orgValue=this._getValue();
			this._validator._observe(this);
			this._attach();	
		}
		
		
	},
	
	_detach:function() {		
		Ember.removeObserver(this._target,this._key,this,this._fn);
		if(Ember.Array.detect(this._orgValue)) {
			this._orgValue.removeArrayObserver(this);
		}
		this._detachKeys();
	},
	
	_detachKeys:function() {
		if(Ember.Observable.detect(this._orgValue)) {
			for(var i=0;i<this._keys.length;i++) {
				Ember.removeObserver(this._orgValue,this._keys[i],this,this._fn);
			}
		}
	},
	
	_attach: function() {
		if(this._debugLogging) {
			this._logEvent('Observing',this._target.toString(),this._key);
		}
		if(this._key) {
			Ember.addObserver(this._target,this._key,this,this._fn);
		}
		if(Ember.Array.detect(this._getValue())) {
			this._getValue().addArrayObserver(this);
		}
		this._attachKeys();
	},
	
	_attachKeys: function() {
		if(Ember.Observable.detect(this._getValue())) {
			for(var i=0;i<this._keys.length;i++) {
				Ember.addObserver(this._getValue(),this._keys[i],this,this._fn);
			}
		}
	},
	
	arrayWillChange: function() {
	},
	
	arrayDidChange: function(arr) {
		this._fnOnce(arr,this._key);
	},
	
	_observeKey: function(key) {
		this._keys.push(key);
		if(Ember.Observable.detect(this._getValue())) {
			Ember.addObserver(this._getValue(),key,this,this._fn);
		}
	},
	
	_observe : function(key,validator,keepContext) {
		if(!validator) {
			validator=key;
{'fraction_non_alphanumeric': 0.17126743840902345, 'fraction_numerical': 0.0020777678836449986, 'mean_word_length': 4.116932422171602, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, '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': '183882', 'n_tokens_mistral': 2669, 'n_tokens_neox': 2350, 'n_words': 305}