From f3fa90bc74589d0301472d3a7895ef216b6d19e0 Mon Sep 17 00:00:00 2001 From: Vladislav Kurkin Date: Thu, 22 Jun 2017 19:31:28 +0300 Subject: [PATCH] Fixed a subscription of the queue on a failed item --- Response.js | 15 +++++++- package.json | 2 +- spec/Queue.spec.js | 86 +++++++++++++++++++++++++++++++++++++++------- 3 files changed, 89 insertions(+), 14 deletions(-) diff --git a/Response.js b/Response.js index ba4ab48..f3c5a1b 100644 --- a/Response.js +++ b/Response.js @@ -1327,7 +1327,20 @@ function checkResponse(queue, item) { } if (isCompatible(item) && !isItemResolved(item)) { - then(item, onResolveItem, onRejectItem, null, queue); + if (item.onChangeState && !isItemRejected(item)) { + item.once(EVENT_CHANGE_STATE, function listener(state) { + switch (state) { + case STATE_RESOLVED: + onResolveItem.call(queue); + break; + case STATE_REJECTED: + onRejectItem.call(queue, this.getReason()); + break; + } + }); + } else { + then(item, onResolveItem, onRejectItem, null, queue); + } return true; } } diff --git a/package.json b/package.json index 5999a3c..3f92cda 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "Response", "author": "Vladislav Kurkin (https://github.com/B-Vladi)", - "version": "0.1.10", + "version": "0.1.11", "main": "Response.js", "license": "MIT", "repository": { diff --git a/spec/Queue.spec.js b/spec/Queue.spec.js index 2197b08..2ef7700 100644 --- a/spec/Queue.spec.js +++ b/spec/Queue.spec.js @@ -267,7 +267,7 @@ describe('Queue:', function () { queue = new Queue() .push(1, 'a') - .push(function b () { + .push(function b() { expect(this.keys).toEqual(['a', 'b', 'c']); this.push(q, 'd'); @@ -287,12 +287,12 @@ describe('Queue:', function () { it('dynamic push in items', function () { queue = new Queue([{ - name: 'key0' - }]) - .push(function key1 () { + name: 'key0' + }]) + .push(function key1() { this.push(listener, 'key3'); }) - .push(function key2 () { + .push(function key2() { this .push(listener, 'key4') .push(listener, 'key5'); @@ -383,7 +383,8 @@ describe('Queue:', function () { it('on "itemRejected" event should not be fired if all items has resolved', function () { queue .push(1) - .push(function () {}) + .push(function () { + }) .push((new Response()).resolve()) .onItemRejected(listener) .start(); @@ -434,7 +435,7 @@ describe('Queue:', function () { }); it('on "itemRejected" event should fire once for each rejected item', function () { - function failingFn () { + function failingFn() { throw new Error(); } @@ -616,7 +617,7 @@ describe('Queue:', function () { it('start should accept arguments for first task', function () { var testArg1 = {}; - var testArg2= {}; + var testArg2 = {}; queue .push(function (arg1, arg2) { @@ -628,9 +629,9 @@ describe('Queue:', function () { it('start arguments should not propagate to second task', function () { queue - .push(function task1 () { + .push(function task1() { }) - .push(function task2 () { + .push(function task2() { expect(arguments.length).toBe(0); }) .start([1, 2]); @@ -797,10 +798,10 @@ describe('Queue:', function () { it('should not pass undefined to next task', function () { queue - .push(function task1 () { + .push(function task1() { }) - .push(function task2 () { + .push(function task2() { expect(arguments.length).toBe(0); }); }); @@ -841,4 +842,65 @@ describe('Queue:', function () { it('destroy for empty Queue don`t throw expection', function () { new Queue().destroy().destroy(true); }); + + describe('External errors:', function () { + var faultyListener; + var goodListener; + + beforeEach(function () { + faultyListener = jasmine + .createSpy('faultyListener').and + .callFake(function throwsError() { + throw new Error('handler throws'); + }); + + goodListener = jasmine.createSpy('goodListener'); + }); + + it('resolves when .onResolve for an item throws error', function () { + expect(faultyListener).toThrow(); + + var item = new Response().onResolve(faultyListener); + + queue + .push(item) + .onResolve(goodListener) + .start(); + + item.resolve(1); + + expect(goodListener).toHaveBeenCalled(); + }); + + it('resolves when .onReject for an item throws error', function () { + expect(faultyListener).toThrow(); + + var item = new Response().onReject(faultyListener); + + queue + .push(item) + .onResolve(goodListener) + .start(); + + item.reject(new Error('item fails')); + + expect(goodListener).toHaveBeenCalled(); + }); + + it('rejects if .strict and .onReject for an item throws an error', function () { + expect(faultyListener).toThrow(); + + var item = new Response().onReject(faultyListener); + + queue + .push(item) + .onReject(goodListener) + .strict() + .start(); + + item.reject(new Error('item fails')); + + expect(goodListener).toHaveBeenCalled(); + }); + }); });