<commit_before>"use import";

import std.js as JS;

exports = Class(function() {
	this.init = function() {
		this._fired = false;
		this._run = [];
		this._id = 0;
		this._pending = 0;
		this._stat = {};
	}
	
	this.fired = function() { return this._fired; } 
	this.reset = function() { this._args = []; this._fired = false; }
	this.clear = function() { this.reset(); this._run = []; }
	this.forward = function(args) { this.run.apply(this, args); }
	this.run = function(ctx, method) {
		var f = method ? bind.apply(this, arguments) : ctx;
		if (f) {
			if (this._fired) {
				f.apply(this, this._args);
			} else {
				this._run.push(f);
			}
		}
		return this;
	}
	
	this.runOrTimeout = function(onFire, onTimeout, duration) {
		if (!onFire && !onTimeout) { return; }
		
		if (this._fired) {
			onFire.apply(this, this._args);
		} else {
			var f = bind(this, function() {
				clearTimeout(timeout);
				onFire.apply(this, this._args);
			});
			
			this.run(f);
			
			var timeout = setTimeout(bind(this, function() {
				for (var i = 0, n = this._run.length; i < n; ++i) {
					if (this._run[i] == f) {
						this._run.splice(i, 1);
						break;
					}
				}
				
				onTimeout();
			}), duration);
		}
	}
	
	this.fire = function() {
		if (this._fired) { return; }
		this._fired = true;
		
		var cbs = this._run;
		this._run = [];
		this._args = arguments;
		for(var i = 0, len = cbs.length; i < len; ++i) {
			if (cbs[i]) { cbs[i].apply(this, arguments); }
		}
	}
	
	this.chain = function(id) {
		++this._pending;
		this.reset();
		return bind(this, '_deferred', id || (this._id++));
	}

	this._deferred = function(id) {
		if (this._stat.hasOwnProperty(id)) { return; }

		this._stat[id] = JS.vargs(arguments, 1);
		if (this._pending) { --this._pending; }<commit_msg>move primitives to the prototype and only create the _stat object if the callback is used for chaining<commit_after>"use import";

import std.js as JS;

exports = Class(function() {

	this._fired = false;
	this._id = 0;
	this._pending = 0;
	
	this.init = function() { this._run = []; }
	this.fired = function() { return this._fired; } 
	this.reset = function() { this._args = []; this._fired = false; }
	this.clear = function() { this.reset(); this._run = []; }
	this.forward = function(args) { this.run.apply(this, args); }
	this.run = function(ctx, method) {
		var f = method ? bind.apply(this, arguments) : ctx;
		if (f) {
			if (this._fired) {
				f.apply(this, this._args);
			} else {
				this._run.push(f);
			}
		}
		return this;
	}
	
	this.runOrTimeout = function(onFire, onTimeout, duration) {
		if (!onFire && !onTimeout) { return; }
		
		if (this._fired) {
			onFire.apply(this, this._args);
		} else {
			var f = bind(this, function() {
				clearTimeout(timeout);
				onFire.apply(this, this._args);
			});
			
			this.run(f);
			
			var timeout = setTimeout(bind(this, function() {
				for (var i = 0, n = this._run.length; i < n; ++i) {
					if (this._run[i] == f) {
						this._run.splice(i, 1);
						break;
					}
				}
				
				onTimeout();
			}), duration);
		}
	}
	
	this.fire = function() {
		if (this._fired) { return; }
		this._fired = true;
		
		var cbs = this._run;
		this._run = [];
		this._args = arguments;
		for(var i = 0, len = cbs.length; i < len; ++i) {
			if (cbs[i]) { cbs[i].apply(this, arguments); }
		}
	}
	
	this.chain = function(id) {
		++this._pending;
		this.reset();
		return bind(this, '_deferred', id || (this._id++));
	}

	this._deferred = function(id) {
		if (!this._stat) { this._stat = {}; }
		if (this._stat.hasOwnProperty(id)) { return; }

		this._stat[id] = JS.vargs(arguments, 1);
		if (this._pending) { --this._pending; }
{'fraction_non_alphanumeric': 0.1849108367626886, 'fraction_numerical': 0.0032921810699588477, 'mean_word_length': 2.805845511482255, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, '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': '2098762', 'n_tokens_mistral': 1589, 'n_tokens_neox': 1342, 'n_words': 301}