1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-08-18 05:42:00 +03:00

Shelve scheduler queue work. Add processFn.

This commit is contained in:
Kegan Dougal
2015-06-18 13:56:46 +01:00
parent 7e12ea4273
commit 1e3162128a
2 changed files with 34 additions and 17 deletions

View File

@@ -57,7 +57,9 @@ module.exports.createClient = function(opts) {
} }
opts.request = request; opts.request = request;
opts.store = new module.exports.MatrixInMemoryStore(); 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); return new module.exports.MatrixClient(opts);
}; };

View File

@@ -9,18 +9,20 @@ var utils = require("./utils");
/** /**
* Construct a scheduler for Matrix. * Construct a scheduler for Matrix.
* @constructor * @constructor
* @param {module:scheduler~processFn} processFn Required. The function that can
* process events in the queue.
* @param {module:scheduler~retryAlgorithm} retryAlgorithm Optional. The retry * @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 * @param {module:scheduler~queueAlgorithm} queueAlgorithm Optional. The queuing
* algorithm to use. * algorithm to apply when determining which events should be sent before the
* @prop {module:scheduler~retryAlgorithm} retryAlgorithm The retry algorithm to * given event. Defaults to {@link module:scheduler~MatrixScheduler.QUEUE_MESSAGES}.
* 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}.
*/ */
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.retryAlgorithm = retryAlgorithm || MatrixScheduler.RETRY_BACKOFF_RATELIMIT;
this.queueAlgorithm = queueAlgorithm || MatrixScheduler.QUEUE_MESSAGES; this.queueAlgorithm = queueAlgorithm || MatrixScheduler.QUEUE_MESSAGES;
this._queues = { 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. * @param {string} queueName The name of the queue to get the event from.
* @return {MatrixEvent} The head of the queue or <code>null</code>. * @return {MatrixEvent} The head of the queue or <code>null</code>.
*/ */
@@ -38,7 +47,7 @@ MatrixScheduler.prototype.removeNextEvent = function(queueName) {
if (!utils.isArray(queue)) { if (!utils.isArray(queue)) {
return null; 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. * Queue an event if it is required.
* @param {MatrixEvent} event The event that may be queued. * @param {MatrixEvent} event The event that may be queued.
* @return {Promise} A promise which will be resolved when the event is sent, if * @return {boolean} True if the event was queued, else false.
* it has been added to a queue, else <code>null</code>.
*/ */
MatrixScheduler.prototype.queueEvent = function(event) { MatrixScheduler.prototype.queueEvent = function(event) {
var queueName = this.queueAlgorithm(event); var queueName = this.queueAlgorithm(event);
if (!queueName) { if (!queueName) {
return null; return false;
} }
this.addEventToQueue(queueName, event); this.addEventToQueue(queueName, event);
// TODO: Return a promise return true;
}; };
/** /**
@@ -137,6 +145,13 @@ MatrixScheduler.QUEUE_MESSAGES = function(event) {
* the event is not put into a queue and will be sent concurrently. * 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. * The MatrixScheduler class.
*/ */