コード例 #1
0
test("will auto-process pushed functions sequentially by default", function() {
  expect(4);

  var op1 = {op: 'add', path: ['planets', '123'], value: 'Mercury'},
      op2 = {op: 'add', path: ['planets', '234'], value: 'Venus'},
      transformCount = 0;

  var _transform = function(op) {
    transformCount++;
    if (transformCount === 1) {
      deepEqual(op, op1, 'op1 passed as argument');
    } else if (transformCount === 2) {
      deepEqual(op, op2, 'op2 passed as argument');
    }
  };

  var queue = new ActionQueue(_transform);

  queue.on('didComplete', function() {
    if (transformCount === 1) {
      ok(true, 'queue completed after op1');
    } else if (transformCount === 2) {
      ok(true, 'queue completed after op2');
    }
  });

  queue.push(op1);
  queue.push(op2);
});
コード例 #2
0
test("with `autoProcess` disabled, will process pushed functions sequentially when `process` is called", function() {
  expect(3);

  var op1 = {op: 'add', path: ['planets', '123'], value: 'Mercury'},
      op2 = {op: 'add', path: ['planets', '234'], value: 'Venus'},
      transformCount = 0;

  var _transform = function(op) {
    transformCount++;
    if (transformCount === 1) {
      deepEqual(op, op1, 'op1 passed as argument');
    } else if (transformCount === 2) {
      deepEqual(op, op2, 'op2 passed as argument');
    }
  };

  var queue = new ActionQueue(_transform, this, {autoProcess: false});

  queue.on('didComplete', function() {
    if (transformCount === 1) {
      ok(false, 'queue SHOULD NOT be completed after op1');
    } else if (transformCount === 2) {
      ok(true, 'queue completed after op2');
    }
  });

  queue.push(op1);
  queue.push(op2);
  queue.process();
});
コード例 #3
0
  queue.then(function() {
    ok(!queue.processing, 'queue is not processing, so it resolves immediately');

    queue.push(op1);
    queue.push(op2);
    ok(queue.processing, 'queue is processing');

    queue.then(function() {
      start();
      ok(!queue.processing, 'queue resolves when it is done processing');
    });

    trigger.emit('start1');
  });
コード例 #4
0
test("with `autoProcess` disabled, will process pushed functions sequentially when `process` is called", function() {
  expect(5);
  stop();

  var queue = new ActionQueue();

  var op1 = {op: 'add', path: ['planets', '123'], value: 'Mercury'},
      op2 = {op: 'add', path: ['planets', '234'], value: 'Venus'},
      transformCount = 0;

  queue.on('didProcessAction', function(action) {
    if (transformCount === 1) {
      deepEqual(action.data, op1, 'op1 processed');
    } else if (transformCount === 2) {
      deepEqual(action.data, op2, 'op2 processed');
    }
  });

  queue.on('didProcess', function() {
    start();
    ok(true, 'queue completed');
  });

  var _transform = function(op) {
    transformCount++;
    if (transformCount === 1) {
      deepEqual(op, op1, 'op1 passed as argument');
    } else if (transformCount === 2) {
      deepEqual(op, op2, 'op2 passed as argument');
    }
  };

  queue.push({
    id: 1,
    process: function() {
      _transform.call(this, this.data);
    },
    data: op1
  });

  queue.push({
    id: 2,
    process: function() {
      _transform.call(this, this.data);
    },
    data: op2
  });

  queue.process();
});
コード例 #5
0
ファイル: jsonapi-source.js プロジェクト: kodiua/orbit.js
  _transform: function(ops) {
    var fullResult = new TransformResult();
    var queue = new ActionQueue({autoProcess: false});
    var operation;
    var method;
    var action;

    for (var i = 0, len = ops.length; i < len; i++) {
      operation = ops[i];

      if (action && this._mergeOperationWithAction(action, operation)) {
        // Operation merged with previous action.
      } else {
        // Not able to merge operation with previous action, so we need to
        // queue a new action.
        method = this._operationMethod(operation);
        action = this._createTransformAction(method, operation, fullResult);
        queue.push(action);
      }
    }

    return queue.process().then(function() {
      return fullResult;
    });
  },
コード例 #6
0
test("will auto-process pushed async functions sequentially by default", function() {
  expect(4);

  var op1 = {op: 'add', path: ['planets', '123'], value: 'Mercury'},
      op2 = {op: 'add', path: ['planets', '234'], value: 'Venus'};

  var trigger = {};
  Evented.extend(trigger);

  var _transform = function(op) {
    var promise;
    if (op === op1) {
      promise = new Promise(function(resolve) {
        trigger.on('start1', function() {
          ok(true, '_transform with op1 resolved');
          resolve();
        });
      });

    } else if (op === op2) {
      promise = new Promise(function(resolve) {
        ok(true, '_transform with op2 resolved');
        resolve();
      });
    }
    return promise;
  };

  var queue = new ActionQueue(_transform);

  queue.on('didComplete', function() {
    start();
    ok(!queue.processing, 'queue is done processing');
  });

  stop();
  queue.push(op1);
  queue.push(op2);
  ok(queue.processing, 'queue is processing');
  trigger.emit('start1');
});
コード例 #7
0
test("will stop processing when an action errors", function() {
  expect(8);
  stop();

  var queue = new ActionQueue({autoProcess: false});

  var op1 = {op: 'add', path: ['planets', '123'], value: 'Mercury'},
      op2 = {op: 'add', path: ['planets', '234'], value: 'Venus'},
      transformCount = 0;

  queue.on('didProcessAction', function(action) {
    if (transformCount === 1) {
      deepEqual(action.data, op1, 'didProcessAction - op1 processed');
    } else if (transformCount === 2) {
      ok(false, 'op2 could not be processed');
    }
  });

  queue.on('didNotProcessAction', function(action, err) {
    deepEqual(action.data, op2, 'didNotProcessAction - op2 failed processing');
    equal(err.message, ':(', 'didNotProcessAction - error matches expectation');
  });

  queue.on('didProcess', function() {
    ok(false, 'queue should not complete');
  });

  queue.on('didNotProcess', function(errData, err) {
    ok(true, 'didNotProcess - queue could not process');
    deepEqual(errData.action.data, op2, 'didNotProcess - op2 failed processing');
    equal(err.message, ':(', 'didNotProcess - error matches expectation');
  });

  var _transform = function(op) {
    transformCount++;
    if (transformCount === 1) {
      deepEqual(op, op1, 'op1 passed as argument');
    } else if (transformCount === 2) {
      deepEqual(op, op2, 'op2 passed as argument');
    }
  };

  queue.push({
    id: 1,
    process: function() {
      _transform.call(this, this.data);
    },
    data: op1
  });

  queue.push({
    id: 2,
    process: function() {
      throw new Error(':(');
    },
    data: op2
  });

  queue.process().then(function() {
    ok(false, 'process should not resolve successfully');
  }, function(err) {
    start();
    equal(err.message, ':(', 'process rejection - error matches expectation');
  });
});
コード例 #8
0
test("will auto-process pushed async functions sequentially by default", function() {
  expect(8);
  stop();

  var queue = new ActionQueue();

  var op1 = {op: 'add', path: ['planets', '123'], value: 'Mercury'},
      op2 = {op: 'add', path: ['planets', '234'], value: 'Venus'},
      order = 0;

  queue.on('didProcessAction', function(action) {
    if (action.data === op1) {
      equal(++order, 3, 'op1 completed');

    } else if (action.data === op2) {
      equal(++order, 6, 'op2 completed');
    }
  });

  queue.on('didProcess', function() {
    equal(++order, 7, 'queue completed');
  });

  var trigger = {};
  Evented.extend(trigger);

  var _transform = function(op) {
    var promise;
    if (op === op1) {
      equal(++order, 1, '_transform with op1');
      promise = new Promise(function(resolve) {
        trigger.on('start1', function() {
          equal(++order, 2, '_transform with op1 resolved');
          resolve();
        });
      });

    } else if (op === op2) {
      equal(++order, 4, '_transform with op1');
      promise = new Promise(function(resolve) {
        equal(++order, 5, '_transform with op1 resolved');
        resolve();
      });
    }
    return promise;
  };

  queue.push({
    id: 1,
    process: function() {
      _transform.call(this, this.data);
    },
    data: op1
  });

  queue.push({
    id: 2,
    process: function() {
      _transform.call(this, this.data);
    },
    data: op2
  });

  queue.process().then(function() {
    start();
    equal(++order, 8, 'queue resolves last');
  });

  trigger.emit('start1');
});