'use strict';
let Q = require('q');
let errorWithMetadata = require('./util/error-with-metadata');
let commandHandlers = {};
let errorHandler;
let interval;
let queue = [];
exports.setErrorHandler = function(handler) {
	errorHandler = handler;
};
exports.registerCommandHandler = function(name, handler) {
	commandHandlers[name] = handler;
};
exports.removeCommandHandler = function(name) {
	delete commandHandlers[name];
};
exports.registerCommandHandler('@function', function(fn, command) {
	return fn(command);
});
exports.push = function() {
	let commands = [].slice.call(arguments);
	[].push.apply(queue, commands);
	startIntervalIfNotActive();
};
function startIntervalIfNotActive() {
	if(interval) {
		return;
	}
	let commandState = {};
	interval = setInterval(function() {
		if(queue.length === 0) {
			clearInterval(interval);
			interval = null;
			return;
		}
		let command = queue[0];
		if(typeof command !== 'object') {
			let commandObject = {};
			commandObject['@' + typeof command] = command;
			queue[0] = command = commandObject;
		}
		let commandName = Object.keys(command)[0];
		let mainCommandValue = command[commandName];
		let commandHandler = commandHandlers[commandName];
		let result = commandHandler.call (
			commandState,
			mainCommandValue,
			command
		);
		switch(result) {
			case 'yield':
				break;
			case 'done':
				commandState = {};
				queue.shift();
				break;
			default:
				commandState = {};
				dispatchOrThrow (
					errorWithMetadata (
						new Error (
							"Unexpected command handler result: '" + result + "'"
							+ " (command: '" + command + "')"
						), {
							command,
							commandHandler,
							queue,
						}
					)
				);
				break;
		}
	}, 0);
}
function dispatchOrThrow(error) {
	if(errorHandler) {
		errorHandler(error);
	}
	else {
		throw error;
	}
}

@@ -1187,24 +1187,159 @@
 mmandName%5D;%0A
+%09%09if(!commandHandler) %7B%0A%09%09%09throw errorWithMetadata (%0A%09%09%09%09new Error(%22Unknown command: %22 + commandName), %7B%0A%09%09%09%09%09command,%0A%09%09%09%09%7D%0A%09%09%09);%0A%09%09%7D%0A
 %09%09let result

Throw an exception when a command handler is not found.