<commit_before>import log from 'utils/log';
import diff from 'viewmodel/Computation/diff';

var Computation = function ( ractive, key, signature ) {
	this.ractive = ractive;
	this.viewmodel = ractive.viewmodel;
	this.key = key;

	this.getter = signature.get;
	this.setter = signature.set;

	this.hardDeps = signature.deps;
	this.softDeps = [];

	if ( this.hardDeps ) {
		this.hardDeps.forEach( d => ractive.viewmodel.register( d, this, 'computed' ) );
	}

	this._dirty = true;
};

Computation.prototype = {
	constructor: Computation,

	init: function () {
		var initial;

		this.bypass = true;

		initial = this.ractive.viewmodel.get( this.key );
		this.ractive.viewmodel.mark( this.key ); // re-clear the cache

		this.bypass = false;

		if ( this.setter && initial !== undefined ) {
			this.set( initial );
		}
	},

	invalidate: function () {
		this._dirty = true;
	},

	get: function () {
		var ractive, newDeps, args;

		if ( this.getting ) {
			// prevent double-computation (e.g. caused by array mutation inside computation)
			return;
		}

		console.log( 'getting', this.key );

		this.getting = true;

		if ( this._dirty ) {
			ractive = this.ractive;
			ractive.viewmodel.capture();

			try {
				if ( this.hardDeps ) {
					args = this.hardDeps.map( keypath => this.viewmodel.get( keypath ) );
					this.value = this.getter.apply( ractive, args );
				} else {
					this.value = this.getter.call( ractive );
				}
			} catch ( err ) {
				log.warn({
					debug: ractive.debug,
					message: 'failedComputation',
					args: {
						key: this.key,
						err: err.message || err
					}
				});

				this.value = void 0;
			}

			newDeps = ractive.viewmodel.release();
			this.updateDependencies( newDeps );

			this._dirty = false;
		}

		this.getting = false;
		return this.value;
	},

	set: function ( value ) {
		if ( this.setting ) {
			this.value = value;
			return;
		}

		if ( !this.setter ) {
			throw new Error( 'Computed properties without setters are read-only. (This may change in a future version of Ractive!)' );
		}

		this.setter.call( this.ractive, value );
	},

	updateDependencies: function ( newDeps ) {
		var i, oldDeps, keypath;

		oldDeps = this.softDeps;

		// remove dependencies that are no longer used
		i = oldDeps.length;
		while ( i-- ) {
			keypath = oldDeps[i];

			if ( newDeps.indexOf( keypath ) === -1 ) {
				this.viewmodel.unregister( keypath, this, 'computed' );
			}
		}

		// create references for any new dependencies
		i = newDeps.length;
		while ( i-- ) {
			keypath = newDeps[i];

			if ( oldDeps.indexOf( keypath ) === -1 && ( !this.hardDeps || this.hardDeps.indexOf( keypath ) === -1 ) ) {
				this.viewmodel.register( keypath, this, 'computed' );
			}
		}

		this.softDeps = newDeps.slice();
	}
};

export default Computation;
<commit_msg>clear cache on computation init, dont mark<commit_after>import log from 'utils/log';
import diff from 'viewmodel/Computation/diff';

var Computation = function ( ractive, key, signature ) {
	this.ractive = ractive;
	this.viewmodel = ractive.viewmodel;
	this.key = key;

	this.getter = signature.get;
	this.setter = signature.set;

	this.hardDeps = signature.deps;
	this.softDeps = [];

	if ( this.hardDeps ) {
		this.hardDeps.forEach( d => ractive.viewmodel.register( d, this, 'computed' ) );
	}

	this._dirty = true;
};

Computation.prototype = {
	constructor: Computation,

	init: function () {
		var initial;

		this.bypass = true;

		initial = this.ractive.viewmodel.get( this.key );
		this.ractive.viewmodel.clearCache( this.key );

		this.bypass = false;

		if ( this.setter && initial !== undefined ) {
			this.set( initial );
		}
	},

	invalidate: function () {
		this._dirty = true;
	},

	get: function () {
		var ractive, newDeps, args;

		if ( this.getting ) {
			// prevent double-computation (e.g. caused by array mutation inside computation)
			return;
		}

		this.getting = true;

		if ( this._dirty ) {
			ractive = this.ractive;
			ractive.viewmodel.capture();

			try {
				if ( this.hardDeps ) {
					args = this.hardDeps.map( keypath => this.viewmodel.get( keypath ) );
					this.value = this.getter.apply( ractive, args );
				} else {
					this.value = this.getter.call( ractive );
				}
			} catch ( err ) {
				log.warn({
					debug: ractive.debug,
					message: 'failedComputation',
					args: {
						key: this.key,
						err: err.message || err
					}
				});

				this.value = void 0;
			}

			newDeps = ractive.viewmodel.release();
			this.updateDependencies( newDeps );

			this._dirty = false;
		}

		this.getting = false;
		return this.value;
	},

	set: function ( value ) {
		if ( this.setting ) {
			this.value = value;
			return;
		}

		if ( !this.setter ) {
			throw new Error( 'Computed properties without setters are read-only. (This may change in a future version of Ractive!)' );
		}

		this.setter.call( this.ractive, value );
	},

	updateDependencies: function ( newDeps ) {
		var i, oldDeps, keypath;

		oldDeps = this.softDeps;

		// remove dependencies that are no longer used
		i = oldDeps.length;
		while ( i-- ) {
			keypath = oldDeps[i];

			if ( newDeps.indexOf( keypath ) === -1 ) {
				this.viewmodel.unregister( keypath, this, 'computed' );
			}
		}

		// create references for any new dependencies
		i = newDeps.length;
		while ( i-- ) {
			keypath = newDeps[i];

			if ( oldDeps.indexOf( keypath ) === -1 && ( !this.hardDeps || this.hardDeps.indexOf( keypath ) === -1 ) ) {
				this.viewmodel.register( keypath, this, 'computed' );
			}
		}

		this.softDeps = newDeps.slice();
	}
};

export default Computation;

{'fraction_non_alphanumeric': 0.14316546762589927, 'fraction_numerical': 0.0014388489208633094, 'mean_word_length': 2.8062970568104038, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '1775336', 'n_tokens_mistral': 2291, 'n_tokens_neox': 1913, 'n_words': 452}