diff --git a/lib/matrix.js b/lib/matrix.js index fbe0bc801..bc7cf8f1f 100644 --- a/lib/matrix.js +++ b/lib/matrix.js @@ -57,7 +57,9 @@ module.exports.createClient = function(opts) { } opts.request = request; opts.store = new module.exports.MatrixInMemoryStore(); - opts.scheduler = new module.exports.MatrixScheduler(); + opts.scheduler = new module.exports.MatrixScheduler( + function() {} // TODO + ); return new module.exports.MatrixClient(opts); }; diff --git a/lib/scheduler.js b/lib/scheduler.js index a15c59d0f..da5f3c181 100644 --- a/lib/scheduler.js +++ b/lib/scheduler.js @@ -9,18 +9,20 @@ var utils = require("./utils"); /** * Construct a scheduler for Matrix. * @constructor + * @param {module:scheduler~processFn} processFn Required. The function that can + * process events in the queue. * @param {module:scheduler~retryAlgorithm} retryAlgorithm Optional. The retry - * algorithm to use. + * algorithm to apply when determining when to try to send an event again. + * Defaults to {@link module:scheduler~MatrixScheduler.RETRY_BACKOFF_RATELIMIT}. * @param {module:scheduler~queueAlgorithm} queueAlgorithm Optional. The queuing - * algorithm to use. - * @prop {module:scheduler~retryAlgorithm} retryAlgorithm The retry algorithm to - * apply when determining when to try to send an event again. Defaults to - * {@link module:scheduler~MatrixScheduler.RETRY_BACKOFF_RATELIMIT}. - * @prop {module:scheduler~queueAlgorithm} queueAlgorithm The queuing algorithm - * to apply when determining which events should be sent before the given event. - * Defaults to {@link module:scheduler~MatrixScheduler.QUEUE_MESSAGES}. + * algorithm to apply when determining which events should be sent before the + * given event. Defaults to {@link module:scheduler~MatrixScheduler.QUEUE_MESSAGES}. */ -function MatrixScheduler(retryAlgorithm, queueAlgorithm) { +function MatrixScheduler(processFn, retryAlgorithm, queueAlgorithm) { + if (!utils.isFunction(processFn)) { + throw new Error("processFn must be a function."); + } + this.processFn = processFn; this.retryAlgorithm = retryAlgorithm || MatrixScheduler.RETRY_BACKOFF_RATELIMIT; this.queueAlgorithm = queueAlgorithm || MatrixScheduler.QUEUE_MESSAGES; this._queues = { @@ -29,7 +31,14 @@ function MatrixScheduler(retryAlgorithm, queueAlgorithm) { } /** - * Remove the head of the queue. + * Makes the scheduler start processing events in the queues if it isn't already. + */ +MatrixScheduler.prototype.checkQueuesRunning = function() { + // TODO +}; + +/** + * Remove the head of the queue. Reduces the length of the queue by 1. * @param {string} queueName The name of the queue to get the event from. * @return {MatrixEvent} The head of the queue or null. */ @@ -38,7 +47,7 @@ MatrixScheduler.prototype.removeNextEvent = function(queueName) { if (!utils.isArray(queue)) { return null; } - return queue[0]; + return queue.shift(); }; /** @@ -56,16 +65,15 @@ MatrixScheduler.prototype.addEventToQueue = function(queueName, event) { /** * Queue an event if it is required. * @param {MatrixEvent} event The event that may be queued. - * @return {Promise} A promise which will be resolved when the event is sent, if - * it has been added to a queue, else null. + * @return {boolean} True if the event was queued, else false. */ MatrixScheduler.prototype.queueEvent = function(event) { var queueName = this.queueAlgorithm(event); if (!queueName) { - return null; + return false; } this.addEventToQueue(queueName, event); - // TODO: Return a promise + return true; }; /** @@ -124,7 +132,7 @@ MatrixScheduler.QUEUE_MESSAGES = function(event) { * {@link module:models/event.EventStatus.NOT_SENT} and will not be retried. */ - /** +/** * The queuing algorithm to apply to events. All queues created are serviced in * a FIFO manner. To send the event ASAP, return null which will * not put this event in a queue. Events that fail to send that form part of @@ -137,6 +145,13 @@ MatrixScheduler.QUEUE_MESSAGES = function(event) { * the event is not put into a queue and will be sent concurrently. */ + /** + * The function to invoke to process (send) events in the queue. + * @callback processFn + * @param {MatrixEvent} event The event to send. + * @return {Promise} Resolves to the HTTP response, rejects with an HTTP error. + */ + /** * The MatrixScheduler class. */