const { listen } = require('../lib/db/notify');
const channels = require('../lib/db/channels');
const handlers = require('./handlers');
const { ActionsQueue } = require('../lib/queue');
const { ERROR, INFO, DEBUG } = require('DOUGAL_ROOT/debug')(__filename);

function start () {

	const queue = new ActionsQueue();
	const ctx = {}; // Context object

	const { prepare, despatch } = handlers.init(ctx);

	listen(channels, function (data) {
		DEBUG("Incoming data", data);

		// We don't bother awaiting
		queue.enqueue(() => despatch(data, ctx));
		DEBUG("Queue size", queue.length());
	});

	INFO("Events manager started");
}

module.exports = { start }

if (require.main === module) {
	start();
}
