1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2026-01-03 23:22:30 +03:00

Add _queues to MatrixScheduler.

This commit is contained in:
Kegan Dougal
2015-06-18 11:49:34 +01:00
parent ca414d1611
commit 18a3ce415c
3 changed files with 34 additions and 5 deletions

View File

@@ -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;

View File

@@ -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);
};

View File

@@ -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 <code>null</code>.
*/
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") {