<commit_before>package ithril.component;

import ithril.Component;

typedef ComponentType = {
	public function new():Void;
}

class ComponentCache {
	static var componentInstances: Map<String, Component> = new Map();
	static var componentCount: Map<String, Int> = new Map();
	static var timeout: Null<Int> = null;

	static function createKey<S: Dynamic>(key: String, state: S) {
		var id = '', count = 0;
		
		if (state != null && Reflect.hasField(state, 'key'))
			id = key+Reflect.field(state, 'key');
		else
			id = key;
		
		if (componentCount.exists(id))
			count = componentCount.get(id);
		componentCount.set(id, count+1);
		id += '--'+Std.string(count);
		
		return id;
	}
	
	public static function invalidate() {
		for (component in componentInstances)
			component.dirty = true;
	}
	
	public static function collect() {
		componentCount = new Map();
		for (key in componentInstances.keys()) {
			var component = componentInstances.get(key);
			if (component.dirty) {
				component.unmount();
				componentInstances.remove(key);
			}
		}
	}

	static function getInstance<T>(
		key: String,
		children: Array<VirtualElement>,
		state: Dynamic,
		create: Void -> T): T 
	{
		key = createKey(key, state);
		if (!componentInstances.exists(key)) {
			var c: Component = cast create();
			//c.mount();
			componentInstances.set(key, c);
		}
		var component = componentInstances.get(key);
		component.dirty = false;
		return cast component;
	}

	@:generic
	public static function getComponent<T: ComponentType>(
		key: String,
		type: Class<T>,
		children: Array<VirtualElement>,
		state: Dynamic
	): T {
		return getInstance(key, children, state, function() return new T());
	}
}
<commit_msg>Update to newer haxe versions<commit_after>package ithril.component;

import ithril.Component;

typedef ComponentType = 
#if (haxe_ver > 3.201)
haxe.Constraints.Constructible<Dynamic>;
#else
{public function new():Void;}
#end

class ComponentCache {
	static var componentInstances: Map<String, Component> = new Map();
	static var componentCount: Map<String, Int> = new Map();
	static var timeout: Null<Int> = null;

	static function createKey<S: Dynamic>(key: String, state: S) {
		var id = '', count = 0;
		
		if (state != null && Reflect.hasField(state, 'key'))
			id = key+Reflect.field(state, 'key');
		else
			id = key;
		
		if (componentCount.exists(id))
			count = componentCount.get(id);
		componentCount.set(id, count+1);
		id += '--'+Std.string(count);
		
		return id;
	}
	
	public static function invalidate() {
		for (component in componentInstances)
			component.dirty = true;
	}
	
	public static function collect() {
		componentCount = new Map();
		for (key in componentInstances.keys()) {
			var component = componentInstances.get(key);
			if (component.dirty) {
				component.unmount();
				componentInstances.remove(key);
			}
		}
	}

	static function getInstance<T>(
		key: String,
		children: Array<VirtualElement>,
		state: Dynamic,
		create: Void -> T): T 
	{
		key = createKey(key, state);
		if (!componentInstances.exists(key)) {
			var c: Component = cast create();
			//c.mount();
			componentInstances.set(key, c);
		}
		var component = componentInstances.get(key);
		component.dirty = false;
		return cast component;
	}

	@:generic
	public static function getComponent<T: ComponentType>(
		key: String,
		type: Class<T>,
		children: Array<VirtualElement>,
		state: Dynamic
	): T {
		return getInstance(key, children, state, function() return new T());
	}
}

{'fraction_non_alphanumeric': 0.13182570325427467, 'fraction_numerical': 0.002206287920573635, 'mean_word_length': 3.317857142857143, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 25, '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': '4997022', 'n_tokens_mistral': 1306, 'n_tokens_neox': 1127, 'n_words': 304}