<commit_before>
import { Stream } from '../../fn/module.js';
import Event from './event.js';
import features from './features.js';

const assign  = Object.assign;
const rspaces = /\s+/;

function prefixType(type) {
	return features.events[type] || type ;
}


/* Handle event types */

var clickTimeStamp = 0;

function listen(source, type) {
    // DOM click events may be simulated on inputs when their labels are
    // clicked. The tell-tale is they have the same timeStamp. Ignore
    // simulated clicks with the same timeStamp as previous clicks.
    if (type === 'click') {
        source.clickUpdate = function click(e) {
            if (e.timeStamp <= clickTimeStamp) { return; }
            clickTimeStamp = e.timeStamp;
            source.update(e);
        };

        source.node.addEventListener(type, source.clickUpdate, source.options);
        return source;
    }

    source.node.addEventListener(type, source.update, source.options);
    return source;
}

function unlisten(source, type) {
    source.node.removeEventListener(type, type === 'click' ?
        source.clickUpdate :
        source.update
    );

    return source;
}

window.addEventListener('click', function(e) {
    clickTimeStamp = e.timeStamp;
});



/* Stream of events */

function Source(notify, stop, type, options, node) {
	const types  = type.split(rspaces).map(prefixType);
	const buffer = [];

	function update(value) {
		buffer.push(value);
		notify('push');
	}

	this._stop    = stop;
	this.types   = types;
	this.node    = node;
	this.buffer  = buffer;
	this.update  = update;
	this.options = options;

	// Potential hard-to-find error here if type has repeats, ie 'click click'.
	// Lets assume nobody is dumb enough to do this, I dont want to have to
	// check for that every time.
	types.reduce(listen, this);
}

assign(Source.prototype, {
	shift: function shiftEvent() {
		const buffer = this.buffer;
		return buffer.shift();
	},

	stop: function stopEvent() {
console.log('STOP');
		types.reduce(unlisten, this);
		this._stop(buffer.length);
	}
});

export default function events(type, node) {
	let options;

	if (typeof type === 'object') {
		options = type;
		type    = options.type;
	}

	return new Stream(function setup(notify, stop) {
		return new Source(notify, stop, type, options, node);
	});
}

// -----------------

export function isPrimaryButton(e) {
	// Ignore mousedowns on any button other than the left (or primary)<commit_msg>Stoip<commit_after>
import { Stream } from '../../fn/module.js';
import Event from './event.js';
import features from './features.js';

const assign  = Object.assign;
const rspaces = /\s+/;

function prefixType(type) {
	return features.events[type] || type ;
}


/* Handle event types */

var clickTimeStamp = 0;

function listen(source, type) {
    // DOM click events may be simulated on inputs when their labels are
    // clicked. The tell-tale is they have the same timeStamp. Ignore
    // simulated clicks with the same timeStamp as previous clicks.
    if (type === 'click') {
        source.clickUpdate = function click(e) {
            if (e.timeStamp <= clickTimeStamp) { return; }
            clickTimeStamp = e.timeStamp;
            source.update(e);
        };

        source.node.addEventListener(type, source.clickUpdate, source.options);
        return source;
    }

    source.node.addEventListener(type, source.update, source.options);
    return source;
}

function unlisten(source, type) {
    source.node.removeEventListener(type, type === 'click' ?
        source.clickUpdate :
        source.update
    );

    return source;
}

window.addEventListener('click', function(e) {
    clickTimeStamp = e.timeStamp;
});



/* Stream of events */

function Source(notify, stop, type, options, node) {
	const types  = type.split(rspaces).map(prefixType);
	const buffer = [];

	function update(value) {
		buffer.push(value);
		notify('push');
	}

	this._stop    = stop;
	this.types   = types;
	this.node    = node;
	this.buffer  = buffer;
	this.update  = update;
	this.options = options;

	// Potential hard-to-find error here if type has repeats, ie 'click click'.
	// Lets assume nobody is dumb enough to do this, I dont want to have to
	// check for that every time.
	types.reduce(listen, this);
}

assign(Source.prototype, {
	shift: function shiftEvent() {
		const buffer = this.buffer;
		return buffer.shift();
	},

	stop: function stopEvent() {
console.log('STOP');
		this.types.reduce(unlisten, this);
		this._stop(buffer.length);
	}
});

export default function events(type, node) {
	let options;

	if (typeof type === 'object') {
		options = type;
		type    = options.type;
	}

	return new Stream(function setup(notify, stop) {
		return new Source(notify, stop, type, options, node);
	});
}

// -----------------

export function isPrimaryButton(e) {
	// Ignore mousedowns on any button other than the left (or primary)
{'fraction_non_alphanumeric': 0.13503575076608784, 'fraction_numerical': 0.00040858018386108274, 'mean_word_length': 3.046280991735537, 'pattern_counts': {'":': 0, '<': 5, '<?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': '2834288', 'n_tokens_mistral': 1543, 'n_tokens_neox': 1478, 'n_words': 441}