Browse Source

Integrated queue in DataSet

v3_develop
jos 10 years ago
parent
commit
41bbb30b8c
4 changed files with 102 additions and 7 deletions
  1. +15
    -0
      lib/DataSet.js
  2. +3
    -3
      lib/Queue.js
  3. +63
    -1
      test/DataSet.test.js
  4. +21
    -3
      test/Queue.test.js

+ 15
- 0
lib/DataSet.js View File

@ -1,4 +1,5 @@
var util = require('./util');
var Queue = require('./Queue');
/**
* DataSet
@ -35,6 +36,11 @@ var util = require('./util');
* {Object.<String, String} type
* A map with field names as key,
* and the field type as value.
* {Object} queue Queue changes to the DataSet,
* flush them all at once.
* Queue options:
* - {number} delay Delay in ms, null by default
* - {number} max Maximum number of entries in the queue, Infinity by default
* @constructor DataSet
*/
// TODO: add a DataSet constructor DataSet(data, options)
@ -77,6 +83,15 @@ function DataSet (data, options) {
if (data) {
this.add(data);
}
// change the dataset in a queued one
if (this._options.queue) {
var queue = {
replace: ['add', 'update', 'remove']
};
if (typeof this._options.queue === 'object') util.extend(queue, this._options.queue);
Queue.extend(this, queue);
}
}
/**

+ 3
- 3
lib/Queue.js View File

@ -5,7 +5,7 @@
* - delay: number When a number, the queue will be flushed
* automatically after an inactivity of this delay
* in milliseconds.
* When false (default), the queue is not flushed
* Default value is null.
* - max: number When the queue exceeds the given maximum number
* of entries, the queue is flushed automatically.
* Default value of max is Infinity.
@ -13,8 +13,8 @@
*/
function Queue(options) {
// options
this.delay = options && typeof options.delay === 'number' ? options.delay : false;
this.max = options && typeof options.max === 'number' ? options.max : Infinity;
this.delay = options && typeof options.delay === 'number' ? options.delay : null;
this.max = options && typeof options.max === 'number' ? options.max : Infinity;
// properties
this._queue = [];

+ 63
- 1
test/DataSet.test.js View File

@ -1,12 +1,13 @@
var assert = require('assert');
var moment = require('moment');
var DataSet = require('../lib/DataSet');
var Queue = require('../lib/Queue');
var now = new Date();
// TODO: improve DataSet tests, split up in one test per function
describe('DataSet', function () {
it('should work', function () {
// TODO: improve DataSet tests, split up in one test per function
var data = new DataSet({
type: {
@ -174,4 +175,65 @@ describe('DataSet', function () {
// TODO: test subscribing to events
});
it('should queue and flush changes', function () {
var options = {queue: true};
var dataset = new DataSet([
{id: 1, content: 'Item 1'},
{id: 2, content: 'Item 2'}
], options);
assert.deepEqual(dataset.get(), [
{id: 1, content: 'Item 1'},
{id: 2, content: 'Item 2'}
]);
dataset.add({id: 3, content: 'Item 3'});
dataset.update({id: 1, content: 'Item 1 (updated)'});
dataset.remove(2);
assert.deepEqual(dataset.get(), [
{id: 1, content: 'Item 1'},
{id: 2, content: 'Item 2'}
]);
dataset.flush();
assert.deepEqual(dataset.get(), [
{id: 1, content: 'Item 1 (updated)'},
{id: 3, content: 'Item 3'}
]);
});
it('should queue and flush changes after a timeout', function (done) {
var options = {queue: {delay: 100}};
var dataset = new DataSet([
{id: 1, content: 'Item 1'},
{id: 2, content: 'Item 2'}
], options);
assert.deepEqual(dataset.get(), [
{id: 1, content: 'Item 1'},
{id: 2, content: 'Item 2'}
]);
dataset.add({id: 3, content: 'Item 3'});
dataset.update({id: 1, content: 'Item 1 (updated)'});
dataset.remove(2);
assert.deepEqual(dataset.get(), [
{id: 1, content: 'Item 1'},
{id: 2, content: 'Item 2'}
]);
setTimeout(function () {
assert.deepEqual(dataset.get(), [
{id: 1, content: 'Item 1 (updated)'},
{id: 3, content: 'Item 3'}
]);
done();
}, 200)
});
});

+ 21
- 3
test/Queue.test.js View File

@ -2,7 +2,7 @@ var assert = require('assert');
var Queue = require('../lib/Queue');
describe('Queue', function () {
it('queue, queue actions', function () {
it('queue actions', function () {
var queue = new Queue();
var count = 0;
@ -17,7 +17,7 @@ describe('Queue', function () {
assert.equal(count, 1);
});
it('queue, queue actions with a delay', function (done) {
it('queue actions with a delay', function (done) {
var queue = new Queue({delay: 25});
var count = 0;
@ -35,7 +35,7 @@ describe('Queue', function () {
}, 50);
});
it('queue, queue multiple actions with a delay', function (done) {
it('queue multiple actions with a delay', function (done) {
var queue = new Queue({delay: 100});
var count = 0;
@ -63,6 +63,24 @@ describe('Queue', function () {
}, 50);
});
it('flush when the configured maximum is exceeded', function () {
var queue = new Queue({max: 4});
var count = 0;
function inc() {
count++;
}
queue.queue(inc);
queue.queue(inc);
assert.equal(count, 0);
queue.queue(inc);
queue.queue(inc);
queue.queue(inc);
assert.equal(count, 5);
});
it('queue actions with args', function () {
var queue = new Queue();

Loading…
Cancel
Save