<commit_before>namespace $ {

	export let $mol_task_current : $mol_task_state< any >
	
	export class $mol_task_state< Result > {
		
		constructor(
			public handler : ( ... args : any[] )=> any ,
			public slave : $mol_task_state< Result > ,
		) { }

		masters = [] as $mol_task_state< any >[]
		cursor = -1
		result : Result

		done( res : Result ) {
			this.result = res
			if( this.slave ) this.slave.notify()
		}

		error( error : any ) : Result {

			if( typeof error.then === 'function' ) {
				error.then(
					( val: any )=> this.done( val ) ,
					( error : any )=> this.done( this.error( error ) ) ,
				)
				error = new Error( 'Wait...' )
			}
	
			return new Proxy<Error>( error , {
				get() { throw error } ,
				apply() { throw error } ,
			} ) as any as Result
	
		}

		notify() {
			if( this.cursor === -1 ) return
			this.cursor = -1
			
			if( this.slave ) this.slave.notify()
			else new $mol_defer( ()=> this.run() )
		}

		run() {
			if( this.cursor !== -1 ) return this.result
			
			const slave = $mol_task_current

			$mol_task_current = this
			this.cursor = 0
			
			try { this.result = this.handler() }
			catch( error ) { this.result = this.error( error ) }

			$mol_task_current = slave

			return this.result
		}

	}

	export function $mol_task_wrap< Result >( handler : ( ... args : any[] )=> Result ) {
		
		return function $mol_task_wrapper( ... args : any[] ) : Result {

			const slave = $mol_task_current

			if( slave ) {
				const master = slave.masters[ slave.cursor ++ ]
				if( master ) return master.run()
			}

			const master = new $mol_task_state( ()=> handler( ... args ) , slave )

			if( slave ) slave.masters[ slave.cursor - 1 ] = master

			return master.run()
		} as typeof handler

	}

}<commit_msg>$mol_task: better typing.<commit_after>namespace $ {

	export let $mol_task_current : $mol_task_state< any >
	
	export class $mol_task_state< Result > {
		
		constructor(
			public handler : ( ... args : any[] )=> any ,
			public slave : $mol_task_state< Result > ,
		) { }

		masters = [] as $mol_task_state< any >[]
		cursor = -1
		result : Result

		done( res : Result ) {
			this.result = res
			if( this.slave ) this.slave.notify()
		}

		error( error : any ) : Result {

			if( typeof error.then === 'function' ) {
				error.then(
					( val: any )=> this.done( val ) ,
					( error : any )=> this.done( this.error( error ) ) ,
				)
				error = new Error( 'Wait...' )
			}
	
			return new Proxy<Error>( error , {
				get() { throw error } ,
				apply() { throw error } ,
			} ) as any as Result
	
		}

		notify() {
			if( this.cursor === -1 ) return
			this.cursor = -1
			
			if( this.slave ) this.slave.notify()
			else new $mol_defer( ()=> this.run() )
		}

		run() {
			if( this.cursor !== -1 ) return this.result
			
			const slave = $mol_task_current

			$mol_task_current = this
			this.cursor = 0
			
			try { this.result = this.handler() }
			catch( error ) { this.result = this.error( error ) }

			$mol_task_current = slave

			return this.result
		}

	}

	export function $mol_task_wrap< Handler extends ( ... args : any[] )=> Result , Result = void >( handler : Handler ) {
		
		return function $mol_task_wrapper( ... args : any[] ) : Result {

			const slave = $mol_task_current

			if( slave ) {
				const master = slave.masters[ slave.cursor ++ ]
				if( master ) return master.run()
			}

			const master = new $mol_task_state< Result >( ()=> handler( ... args ) , slave )

			if( slave ) slave.masters[ slave.cursor - 1 ] = master

			return master.run()

		} as Handler

	}

}
{'fraction_non_alphanumeric': 0.15274261603375527, 'fraction_numerical': 0.0033755274261603376, 'mean_word_length': 2.3931297709923665, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 28, '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': '5122567', 'n_tokens_mistral': 1492, 'n_tokens_neox': 1192, 'n_words': 318}