From 18a3ce415c4b47fbe146881e89f9806b453c2ab0 Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Thu, 18 Jun 2015 11:49:34 +0100 Subject: [PATCH] Add _queues to MatrixScheduler. --- lib/client.js | 2 +- lib/matrix.js | 7 +++---- lib/scheduler.js | 30 ++++++++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 5 deletions(-) diff --git a/lib/client.js b/lib/client.js index 2e9a22b11..a7da603e7 100644 --- a/lib/client.js +++ b/lib/client.js @@ -38,7 +38,7 @@ var utils = require("./utils"); function MatrixClient(opts) { utils.checkObjectHasKeys(opts, ["baseUrl", "request"]); utils.checkObjectHasNoAdditionalKeys(opts, - ["baseUrl", "request", "accessToken", "userId", "store"] + ["baseUrl", "request", "accessToken", "userId", "store", "scheduler"] ); this.store = opts.store; diff --git a/lib/matrix.js b/lib/matrix.js index de4a0c4ff..fbe0bc801 100644 --- a/lib/matrix.js +++ b/lib/matrix.js @@ -45,10 +45,8 @@ module.exports.request = function(r) { * @param {string} opts.baseUrl The base URL to the client-server HTTP API. * @param {string} opts.accessToken Optional. The access_token for this user. * @param {string} opts.userId Optional. The user ID for this user. - * @param {Object} opts.store Optional. The data store to use. Defaults to - * {@link module:store/memory.MatrixInMemoryStore}. - * @param {Object} opts.scheduler Optional. The scheduler to use. Defaults to - * {@link module:scheduler~MatrixScheduler}. + * @param {Object} opts.store Set to {@link module:store/memory.MatrixInMemoryStore}. + * @param {Object} opts.scheduler Set to {@link module:scheduler~MatrixScheduler}. * @return {MatrixClient} A new matrix client. */ module.exports.createClient = function(opts) { @@ -59,6 +57,7 @@ module.exports.createClient = function(opts) { } opts.request = request; opts.store = new module.exports.MatrixInMemoryStore(); + opts.scheduler = new module.exports.MatrixScheduler(); return new module.exports.MatrixClient(opts); }; diff --git a/lib/scheduler.js b/lib/scheduler.js index d20c55156..b56038f82 100644 --- a/lib/scheduler.js +++ b/lib/scheduler.js @@ -4,6 +4,7 @@ * of requests. * @module scheduler */ +var utils = require("./utils"); /** * Construct a scheduler for Matrix. @@ -22,8 +23,35 @@ function MatrixScheduler(retryAlgorithm, queueAlgorithm) { this.retryAlgorithm = retryAlgorithm || MatrixScheduler.RETRY_BACKOFF_RATELIMIT; this.queueAlgorithm = queueAlgorithm || MatrixScheduler.QUEUE_MESSAGES; + this._queues = { + // queueName: [MatrixEvent, ...] + }; } +/** + * Remove the head of the queue. + * @param {string} queueName The name of the queue to get the event from. + * @return {MatrixEvent} The head of the queue or null. + */ +MatrixScheduler.prototype.removeNextEvent = function(queueName) { + var queue = this._queues[queueName]; + if (!utils.isArray(queue)) { + return null; + } + return queue[0]; +}; + +/** + * Add an event to the end of the queue. + * @param {string} queueName The name of the queue to add the event to. + * @param {MatrixEvent} event The event to queue. + */ +MatrixScheduler.prototype.queueEvent = function(queueName, event) { + if (!this._queues[queueName]) { + this._queues[queueName] = []; + } + this._queues[queueName].push(event); +}; /** * Retries events up to 4 times using exponential backoff. This produces wait @@ -34,6 +62,7 @@ function MatrixScheduler(retryAlgorithm, queueAlgorithm) { * @param {Number} attempts * @param {MatrixError} err * @return {Number} + * @see module:scheduler~retryAlgorithm */ MatrixScheduler.RETRY_BACKOFF_RATELIMIT = function(event, attempts, err) { if (err.name === "M_LIMIT_EXCEEDED") { @@ -53,6 +82,7 @@ MatrixScheduler.RETRY_BACKOFF_RATELIMIT = function(event, attempts, err) { * concurrently. * @param {MatrixEvent} event * @return {string} + * @see module:scheduler~queueAlgorithm */ MatrixScheduler.QUEUE_MESSAGES = function(event) { if (event.getType() === "m.room.message") {