Replacing Q promises with ES6 promises

This commit is contained in:
Jeff Avallone 2015-03-14 17:11:14 -04:00
parent 3970224302
commit 0093db8e20
24 changed files with 212 additions and 254 deletions

View File

@ -27,7 +27,6 @@
"karma-jasmine": "^0.3.1", "karma-jasmine": "^0.3.1",
"karma-notify-reporter": "^0.1.1", "karma-notify-reporter": "^0.1.1",
"lodash": "^2.4.1", "lodash": "^2.4.1",
"q": "^1.1.2",
"snapsvg": "git://github.com/adobe-webplatform/Snap.svg#dev", "snapsvg": "git://github.com/adobe-webplatform/Snap.svg#dev",
"through": "^2.3.6" "through": "^2.3.6"
} }

View File

@ -1,6 +1,5 @@
import javascript from 'src/js/parser/javascript/parser.js'; import javascript from 'src/js/parser/javascript/parser.js';
import util from 'src/js/util.js'; import util from 'src/js/util.js';
import Q from 'q';
import _ from 'lodash'; import _ from 'lodash';
describe('parser/javascript/charset_range.js', function() { describe('parser/javascript/charset_range.js', function() {
@ -71,8 +70,8 @@ describe('parser/javascript/charset_range.js', function() {
this.node.container = jasmine.createSpyObj('cotnainer', ['addClass', 'text', 'group']); this.node.container = jasmine.createSpyObj('cotnainer', ['addClass', 'text', 'group']);
this.node.container.text.and.returnValue('hyphen'); this.node.container.text.and.returnValue('hyphen');
this.firstDeferred = Q.defer(); this.firstDeferred = this.testablePromise();
this.lastDeferred = Q.defer(); this.lastDeferred = this.testablePromise();
spyOn(this.node.first, 'render').and.returnValue(this.firstDeferred.promise); spyOn(this.node.first, 'render').and.returnValue(this.firstDeferred.promise);
spyOn(this.node.last, 'render').and.returnValue(this.lastDeferred.promise); spyOn(this.node.last, 'render').and.returnValue(this.lastDeferred.promise);
@ -95,9 +94,8 @@ describe('parser/javascript/charset_range.js', function() {
'hyphen', 'hyphen',
this.node.last this.node.last
], { padding: 5 }); ], { padding: 5 });
}, fail) done();
.finally(done) });
.done();
}); });
}); });

View File

@ -3,7 +3,6 @@ import Node from 'src/js/parser/javascript/node.js';
import util from 'src/js/util.js'; import util from 'src/js/util.js';
import _ from 'lodash'; import _ from 'lodash';
import Snap from 'snapsvg'; import Snap from 'snapsvg';
import Q from 'q';
describe('parser/javascript/charset.js', function() { describe('parser/javascript/charset.js', function() {
@ -100,9 +99,9 @@ describe('parser/javascript/charset.js', function() {
jasmine.createSpyObj('item', ['render']) jasmine.createSpyObj('item', ['render'])
]; ];
this.elementDeferred = [ this.elementDeferred = [
Q.defer(), this.testablePromise(),
Q.defer(), this.testablePromise(),
Q.defer() this.testablePromise()
]; ];
this.node.elements[0].render.and.returnValue(this.elementDeferred[0].promise); this.node.elements[0].render.and.returnValue(this.elementDeferred[0].promise);
this.node.elements[1].render.and.returnValue(this.elementDeferred[1].promise); this.node.elements[1].render.and.returnValue(this.elementDeferred[1].promise);
@ -143,9 +142,8 @@ describe('parser/javascript/charset.js', function() {
this.node._render() this.node._render()
.then(() => { .then(() => {
expect(util.spaceVertically).toHaveBeenCalledWith(this.node.elements, { padding: 5 }); expect(util.spaceVertically).toHaveBeenCalledWith(this.node.elements, { padding: 5 });
}, fail) done();
.finally(done) });
.done();
}); });
it('renders a labeled box', function(done) { it('renders a labeled box', function(done) {
@ -153,9 +151,8 @@ describe('parser/javascript/charset.js', function() {
.then(result => { .then(result => {
expect(this.node.renderLabeledBox).toHaveBeenCalledWith('example label', this.partContainer, { padding: 5 }); expect(this.node.renderLabeledBox).toHaveBeenCalledWith('example label', this.partContainer, { padding: 5 });
expect(result).toEqual('labeled box promise'); expect(result).toEqual('labeled box promise');
}, fail) done();
.finally(done) });
.done();
}); });
}); });

View File

@ -63,9 +63,8 @@ describe('parser/javascript/escape.js', function() {
rx: '3', rx: '3',
ry: '3' ry: '3'
})); }));
}, fail) done();
.finally(done) });
.done();
}); });
}); });

View File

@ -43,9 +43,8 @@ describe('parser/javascript/literal.js', function() {
.then(label => { .then(label => {
expect(label.selectAll('tspan')[0].hasClass('quote')).toBeTruthy(); expect(label.selectAll('tspan')[0].hasClass('quote')).toBeTruthy();
expect(label.selectAll('tspan')[2].hasClass('quote')).toBeTruthy(); expect(label.selectAll('tspan')[2].hasClass('quote')).toBeTruthy();
}, fail) done();
.finally(done) });
.done();
}); });
it('sets the edge radius of the rect', function(done) { it('sets the edge radius of the rect', function(done) {
@ -55,9 +54,8 @@ describe('parser/javascript/literal.js', function() {
rx: '3', rx: '3',
ry: '3' ry: '3'
})); }));
}, fail) done();
.finally(done) });
.done();
}); });
}); });

View File

@ -1,7 +1,6 @@
import javascript from 'src/js/parser/javascript/parser.js'; import javascript from 'src/js/parser/javascript/parser.js';
import _ from 'lodash'; import _ from 'lodash';
import Snap from 'snapsvg'; import Snap from 'snapsvg';
import Q from 'q';
describe('parser/javascript/match_fragment.js', function() { describe('parser/javascript/match_fragment.js', function() {
@ -66,7 +65,7 @@ describe('parser/javascript/match_fragment.js', function() {
]); ]);
this.node.container.group.and.returnValue('example group'); this.node.container.group.and.returnValue('example group');
this.renderDeferred = Q.defer(); this.renderDeferred = this.testablePromise();
this.node.content = jasmine.createSpyObj('content', [ this.node.content = jasmine.createSpyObj('content', [
'render', 'render',
'transform', 'transform',
@ -99,9 +98,8 @@ describe('parser/javascript/match_fragment.js', function() {
this.node._render() this.node._render()
.then(() => { .then(() => {
expect(this.node.content.transform).toHaveBeenCalledWith('example position'); expect(this.node.content.transform).toHaveBeenCalledWith('example position');
}, fail) done();
.finally(done) });
.done();
}); });
it('renders a skip path and loop path', function(done) { it('renders a skip path and loop path', function(done) {
@ -110,18 +108,16 @@ describe('parser/javascript/match_fragment.js', function() {
expect(this.node.skipPath).toHaveBeenCalledWith('content bbox'); expect(this.node.skipPath).toHaveBeenCalledWith('content bbox');
expect(this.node.loopPath).toHaveBeenCalledWith('content bbox'); expect(this.node.loopPath).toHaveBeenCalledWith('content bbox');
expect(this.node.container.path).toHaveBeenCalledWith('skip pathloop path'); expect(this.node.container.path).toHaveBeenCalledWith('skip pathloop path');
}, fail) done();
.finally(done) });
.done();
}); });
it('renders a loop label', function(done) { it('renders a loop label', function(done) {
this.node._render() this.node._render()
.then(() => { .then(() => {
expect(this.node.loopLabel).toHaveBeenCalled(); expect(this.node.loopLabel).toHaveBeenCalled();
}, fail) done();
.finally(done) });
.done();
}); });
}); });

View File

@ -2,7 +2,6 @@ import javascript from 'src/js/parser/javascript/parser.js';
import util from 'src/js/util.js'; import util from 'src/js/util.js';
import _ from 'lodash'; import _ from 'lodash';
import Snap from 'snapsvg'; import Snap from 'snapsvg';
import Q from 'q';
describe('parser/javascript/match.js', function() { describe('parser/javascript/match.js', function() {
@ -112,8 +111,8 @@ describe('parser/javascript/match.js', function() {
this.node.container.group.and.returnValue('example group'); this.node.container.group.and.returnValue('example group');
this.labelDeferreds = { this.labelDeferreds = {
'Start of line': Q.defer(), 'Start of line': this.testablePromise(),
'End of line': Q.defer() 'End of line': this.testablePromise()
}; };
spyOn(this.node, 'renderLabel').and.callFake(label => { spyOn(this.node, 'renderLabel').and.callFake(label => {
return this.labelDeferreds[label].promise; return this.labelDeferreds[label].promise;
@ -126,9 +125,9 @@ describe('parser/javascript/match.js', function() {
]; ];
this.partDeferreds = [ this.partDeferreds = [
Q.defer(), this.testablePromise(),
Q.defer(), this.testablePromise(),
Q.defer() this.testablePromise()
]; ];
this.node.parts[0].render.and.returnValue(this.partDeferreds[0].promise); this.node.parts[0].render.and.returnValue(this.partDeferreds[0].promise);
@ -217,9 +216,8 @@ describe('parser/javascript/match.js', function() {
.then(() => { .then(() => {
expect(this.node.start).toEqual('start label'); expect(this.node.start).toEqual('start label');
expect(this.node.end).toEqual('end label'); expect(this.node.end).toEqual('end label');
}, fail) done();
.finally(done) });
.done();
}); });
it('spaces the items horizontally', function(done) { it('spaces the items horizontally', function(done) {
@ -232,9 +230,8 @@ describe('parser/javascript/match.js', function() {
'part 2', 'part 2',
'end label' 'end label'
], { padding: 10 }); ], { padding: 10 });
}, fail) done();
.finally(done) });
.done();
}); });
it('renders the connector paths', function(done) { it('renders the connector paths', function(done) {
@ -248,9 +245,8 @@ describe('parser/javascript/match.js', function() {
'end label' 'end label'
]); ]);
expect(this.node.container.path).toHaveBeenCalledWith('connector paths'); expect(this.node.container.path).toHaveBeenCalledWith('connector paths');
}, fail) done();
.finally(done) });
.done();
}); });
}); });

View File

@ -1,5 +1,4 @@
import Node from 'src/js/parser/javascript/node.js'; import Node from 'src/js/parser/javascript/node.js';
import Q from 'q';
import Snap from 'snapsvg'; import Snap from 'snapsvg';
describe('parser/javascript/node.js', function() { describe('parser/javascript/node.js', function() {
@ -126,9 +125,8 @@ describe('parser/javascript/node.js', function() {
.then(() => { .then(() => {
expect(resolve).toHaveBeenCalledWith('result'); expect(resolve).toHaveBeenCalledWith('result');
expect(reject).not.toHaveBeenCalled(); expect(reject).not.toHaveBeenCalled();
}, fail) done();
.finally(done) });
.done();
}); });
it('rejects the returned promise when the render is canceled', function(done) { it('rejects the returned promise when the render is canceled', function(done) {
@ -141,9 +139,8 @@ describe('parser/javascript/node.js', function() {
.then(() => { .then(() => {
expect(resolve).not.toHaveBeenCalled(); expect(resolve).not.toHaveBeenCalled();
expect(reject).toHaveBeenCalledWith('Render cancelled'); expect(reject).toHaveBeenCalledWith('Render cancelled');
}, fail) done();
.finally(done) });
.done();
}); });
}); });
@ -193,9 +190,8 @@ describe('parser/javascript/node.js', function() {
.then(() => { .then(() => {
expect(this.text.transform).toHaveBeenCalledWith(Snap.matrix() expect(this.text.transform).toHaveBeenCalledWith(Snap.matrix()
.translate(5, 22)); .translate(5, 22));
}, fail) done();
.finally(done) });
.done();
}); });
it('sets the dimensions of the rect element', function(done) { it('sets the dimensions of the rect element', function(done) {
@ -205,18 +201,16 @@ describe('parser/javascript/node.js', function() {
width: 52, width: 52,
height: 34 height: 34
}); });
}, fail) done();
.finally(done) });
.done();
}); });
it('resolves with the group element', function(done) { it('resolves with the group element', function(done) {
this.node.renderLabel('example label') this.node.renderLabel('example label')
.then(group => { .then(group => {
expect(group).toEqual(this.group); expect(group).toEqual(this.group);
}, fail) done();
.finally(done) });
.done();
}); });
}); });
@ -271,7 +265,7 @@ describe('parser/javascript/node.js', function() {
describe('when a proxy node is not used', function() { describe('when a proxy node is not used', function() {
beforeEach(function() { beforeEach(function() {
this.deferred = Q.defer(); this.deferred = this.testablePromise();
this.node._render = jasmine.createSpy('_render').and.returnValue(this.deferred.promise); this.node._render = jasmine.createSpy('_render').and.returnValue(this.deferred.promise);
spyOn(this.node, 'startRender'); spyOn(this.node, 'startRender');
spyOn(this.node, 'doneRender'); spyOn(this.node, 'doneRender');
@ -302,18 +296,16 @@ describe('parser/javascript/node.js', function() {
this.node.render(this.container) this.node.render(this.container)
.then(() => { .then(() => {
expect(this.node.doneRender).toHaveBeenCalled(); expect(this.node.doneRender).toHaveBeenCalled();
}, fail) done();
.finally(done) });
.done();
}); });
it('ultimately resolves with the node instance', function(done) { it('ultimately resolves with the node instance', function(done) {
this.node.render(this.container) this.node.render(this.container)
.then(result => { .then(result => {
expect(result).toEqual(this.node); expect(result).toEqual(this.node);
}, fail) done();
.finally(done) });
.done();
}); });
}); });
@ -389,9 +381,8 @@ describe('parser/javascript/node.js', function() {
.then(() => { .then(() => {
expect(this.text.transform).toHaveBeenCalledWith(Snap.matrix() expect(this.text.transform).toHaveBeenCalledWith(Snap.matrix()
.translate(0, 20)); .translate(0, 20));
}, fail) done();
.finally(done) });
.done();
}); });
it('positions the rect element', function(done) { it('positions the rect element', function(done) {
@ -400,9 +391,8 @@ describe('parser/javascript/node.js', function() {
.then(() => { .then(() => {
expect(this.rect.transform).toHaveBeenCalledWith(Snap.matrix() expect(this.rect.transform).toHaveBeenCalledWith(Snap.matrix()
.translate(0, 20)); .translate(0, 20));
}, fail) done();
.finally(done) });
.done();
}); });
it('sets the dimensions of the rect element', function(done) { it('sets the dimensions of the rect element', function(done) {
@ -412,10 +402,9 @@ describe('parser/javascript/node.js', function() {
expect(this.rect.attr).toHaveBeenCalledWith({ expect(this.rect.attr).toHaveBeenCalledWith({
width: 210, width: 210,
height: 110 height: 110
}) });
}, fail) done();
.finally(done) });
.done();
}); });
it('sets the dimensions of the rect element (based on the text element)', function(done) { it('sets the dimensions of the rect element (based on the text element)', function(done) {
@ -430,10 +419,9 @@ describe('parser/javascript/node.js', function() {
expect(this.rect.attr).toHaveBeenCalledWith({ expect(this.rect.attr).toHaveBeenCalledWith({
width: 100, width: 100,
height: 110 height: 110
}) });
}, fail) done();
.finally(done) });
.done();
}); });
it('positions the content element', function(done) { it('positions the content element', function(done) {
@ -442,9 +430,8 @@ describe('parser/javascript/node.js', function() {
.then(() => { .then(() => {
expect(this.content.transform).toHaveBeenCalledWith(Snap.matrix() expect(this.content.transform).toHaveBeenCalledWith(Snap.matrix()
.translate(5, 25)); .translate(5, 25));
}, fail) done();
.finally(done) });
.done();
}); });
}); });

View File

@ -2,7 +2,6 @@ import javascript from 'src/js/parser/javascript/parser.js';
import util from 'src/js/util.js'; import util from 'src/js/util.js';
import _ from 'lodash'; import _ from 'lodash';
import Snap from 'snapsvg'; import Snap from 'snapsvg';
import Q from 'q';
describe('parser/javascript/regexp.js', function() { describe('parser/javascript/regexp.js', function() {
@ -60,9 +59,9 @@ describe('parser/javascript/regexp.js', function() {
]; ];
this.matchDeferred = [ this.matchDeferred = [
Q.defer(), this.testablePromise(),
Q.defer(), this.testablePromise(),
Q.defer() this.testablePromise()
]; ];
this.node.matches[0].render.and.returnValue(this.matchDeferred[0].promise); this.node.matches[0].render.and.returnValue(this.matchDeferred[0].promise);
@ -104,9 +103,8 @@ describe('parser/javascript/regexp.js', function() {
this.node._render() this.node._render()
.then(() => { .then(() => {
expect(util.spaceVertically).toHaveBeenCalledWith(this.node.matches, { padding: 5 }); expect(util.spaceVertically).toHaveBeenCalledWith(this.node.matches, { padding: 5 });
}, fail) done();
.finally(done) });
.done();
}); });
it('renders the sides and curves into the container', function(done) { it('renders the sides and curves into the container', function(done) {
@ -118,9 +116,8 @@ describe('parser/javascript/regexp.js', function() {
expect(this.node.makeSide).toHaveBeenCalledWith('container bbox', this.node.matches[0]); expect(this.node.makeSide).toHaveBeenCalledWith('container bbox', this.node.matches[0]);
expect(this.node.makeSide).toHaveBeenCalledWith('container bbox', this.node.matches[2]); expect(this.node.makeSide).toHaveBeenCalledWith('container bbox', this.node.matches[2]);
expect(this.node.container.path).toHaveBeenCalledWith('curvecurvecurvesideside'); expect(this.node.container.path).toHaveBeenCalledWith('curvecurvecurvesideside');
}, fail) done();
.finally(done) });
.done();
}); });
it('renders the connectors into the match container', function(done) { it('renders the connectors into the match container', function(done) {
@ -130,9 +127,8 @@ describe('parser/javascript/regexp.js', function() {
expect(this.node.makeConnector).toHaveBeenCalledWith('group bbox', this.node.matches[1]); expect(this.node.makeConnector).toHaveBeenCalledWith('group bbox', this.node.matches[1]);
expect(this.node.makeConnector).toHaveBeenCalledWith('group bbox', this.node.matches[2]); expect(this.node.makeConnector).toHaveBeenCalledWith('group bbox', this.node.matches[2]);
expect(this.group.path).toHaveBeenCalledWith('connectorconnectorconnector'); expect(this.group.path).toHaveBeenCalledWith('connectorconnectorconnector');
}, fail) done();
.finally(done) });
.done();
}); });
}); });

View File

@ -1,7 +1,6 @@
import javascript from 'src/js/parser/javascript/parser.js'; import javascript from 'src/js/parser/javascript/parser.js';
import Snap from 'snapsvg'; import Snap from 'snapsvg';
import _ from 'lodash'; import _ from 'lodash';
import Q from 'q';
describe('parser/javascript/root.js', function() { describe('parser/javascript/root.js', function() {
@ -62,7 +61,7 @@ describe('parser/javascript/root.js', function() {
'getBBox' 'getBBox'
]); ]);
this.renderDeferred = Q.defer(); this.renderDeferred = this.testablePromise();
this.node.regexp.render.and.returnValue(this.renderDeferred.promise); this.node.regexp.render.and.returnValue(this.renderDeferred.promise);
}); });
@ -114,9 +113,8 @@ describe('parser/javascript/root.js', function() {
this.node._render() this.node._render()
.then(() => { .then(() => {
expect(this.node.container.path).toHaveBeenCalledWith('M1,2H0M3,2H14'); expect(this.node.container.path).toHaveBeenCalledWith('M1,2H0M3,2H14');
}, fail) done();
.finally(done) });
.done();
}); });
it('renders circle elements before and after the regexp', function(done) { it('renders circle elements before and after the regexp', function(done) {
@ -124,9 +122,8 @@ describe('parser/javascript/root.js', function() {
.then(() => { .then(() => {
expect(this.node.container.circle).toHaveBeenCalledWith(0, 2, 5); expect(this.node.container.circle).toHaveBeenCalledWith(0, 2, 5);
expect(this.node.container.circle).toHaveBeenCalledWith(14, 2, 5); expect(this.node.container.circle).toHaveBeenCalledWith(14, 2, 5);
}, fail) done();
.finally(done) });
.done();
}); });
describe('when there are flags', function() { describe('when there are flags', function() {
@ -140,9 +137,8 @@ describe('parser/javascript/root.js', function() {
.then(() => { .then(() => {
expect(this.node.regexp.transform).toHaveBeenCalledWith(Snap.matrix() expect(this.node.regexp.transform).toHaveBeenCalledWith(Snap.matrix()
.translate(10, 20)); .translate(10, 20));
}, fail) done();
.finally(done) });
.done();
}); });
}); });
@ -158,9 +154,8 @@ describe('parser/javascript/root.js', function() {
.then(() => { .then(() => {
expect(this.node.regexp.transform).toHaveBeenCalledWith(Snap.matrix() expect(this.node.regexp.transform).toHaveBeenCalledWith(Snap.matrix()
.translate(10, 0)); .translate(10, 0));
}, fail) done();
.finally(done) });
.done();
}); });
}); });

View File

@ -2,7 +2,6 @@ import javascript from 'src/js/parser/javascript/parser.js';
import Node from 'src/js/parser/javascript/node.js'; import Node from 'src/js/parser/javascript/node.js';
import _ from 'lodash'; import _ from 'lodash';
import Snap from 'snapsvg'; import Snap from 'snapsvg';
import Q from 'q';
describe('parser/javascript/subexp.js', function() { describe('parser/javascript/subexp.js', function() {
@ -60,7 +59,7 @@ describe('parser/javascript/subexp.js', function() {
describe('#_render', function() { describe('#_render', function() {
beforeEach(function() { beforeEach(function() {
this.renderDeferred = Q.defer(); this.renderDeferred = this.testablePromise();
this.node = new javascript.Parser('(test)').__consume__subexp(); this.node = new javascript.Parser('(test)').__consume__subexp();
this.node.regexp = jasmine.createSpyObj('regexp', ['render']); this.node.regexp = jasmine.createSpyObj('regexp', ['render']);
@ -81,9 +80,8 @@ describe('parser/javascript/subexp.js', function() {
this.node._render() this.node._render()
.then(() => { .then(() => {
expect(this.node.renderLabeledBox).toHaveBeenCalledWith('example label', this.node.regexp, { padding: 10 }); expect(this.node.renderLabeledBox).toHaveBeenCalledWith('example label', this.node.regexp, { padding: 10 });
}, fail) done();
.finally(done) });
.done();
}); });
}); });

View File

@ -1,7 +1,6 @@
import Parser from 'src/js/parser/javascript.js'; import Parser from 'src/js/parser/javascript.js';
import regexpParser from 'src/js/parser/javascript/grammar.peg'; import regexpParser from 'src/js/parser/javascript/grammar.peg';
import Snap from 'snapsvg'; import Snap from 'snapsvg';
import Q from 'q';
describe('parser/javascript.js', function() { describe('parser/javascript.js', function() {
@ -54,36 +53,33 @@ describe('parser/javascript.js', function() {
this.parser.parse('example expression') this.parser.parse('example expression')
.then(() => { .then(() => {
expect(regexpParser.parse).toHaveBeenCalledWith('example expression'); expect(regexpParser.parse).toHaveBeenCalledWith('example expression');
}, fail) done();
.finally(done) });
.done();
}); });
it('replaces newlines with "\\n"', function(done) { it('replaces newlines with "\\n"', function(done) {
this.parser.parse('multiline\nexpression') this.parser.parse('multiline\nexpression')
.then(() => { .then(() => {
expect(regexpParser.parse).toHaveBeenCalledWith('multiline\\nexpression'); expect(regexpParser.parse).toHaveBeenCalledWith('multiline\\nexpression');
}, fail) done();
.finally(done) });
.done();
}); });
it('resolves the returned promise with the parser instance', function(done) { it('resolves the returned promise with the parser instance', function(done) {
this.parser.parse('example expression') this.parser.parse('example expression')
.then(result => { .then(result => {
expect(result).toEqual(this.parser); expect(result).toEqual(this.parser);
}, fail) done();
.finally(done) });
.done();
}); });
it('rejects the returned promise with the exception thrown', function(done) { it('rejects the returned promise with the exception thrown', function(done) {
this.parser.parse('/example') regexpParser.parse.and.throwError('fail');
this.parser.parse('(example')
.then(null, result => { .then(null, result => {
expect(result).toBeDefined(); expect(result).toBeDefined();
}, fail) done();
.finally(done) });
.done();
}); });
}); });
@ -91,7 +87,7 @@ describe('parser/javascript.js', function() {
describe('#render', function() { describe('#render', function() {
beforeEach(function() { beforeEach(function() {
this.renderPromise = Q.defer(); this.renderPromise = this.testablePromise();
this.parser.parsed = jasmine.createSpyObj('parsed', ['render']); this.parser.parsed = jasmine.createSpyObj('parsed', ['render']);
this.parser.parsed.render.and.returnValue(this.renderPromise.promise); this.parser.parsed.render.and.returnValue(this.renderPromise.promise);
}); });
@ -120,9 +116,8 @@ describe('parser/javascript.js', function() {
.then(() => { .then(() => {
expect(this.result.transform).toHaveBeenCalledWith(Snap.matrix() expect(this.result.transform).toHaveBeenCalledWith(Snap.matrix()
.translate(6, 8)); .translate(6, 8));
}, fail) done();
.finally(done) });
.done();
}); });
it('sets the dimensions of the image', function(done) { it('sets the dimensions of the image', function(done) {
@ -132,9 +127,8 @@ describe('parser/javascript.js', function() {
expect(svg.getAttribute('width')).toEqual('62'); expect(svg.getAttribute('width')).toEqual('62');
expect(svg.getAttribute('height')).toEqual('44'); expect(svg.getAttribute('height')).toEqual('44');
}, fail) done();
.finally(done) });
.done();
}); });
it('removes the "loading" class', function(done) { it('removes the "loading" class', function(done) {
@ -142,18 +136,16 @@ describe('parser/javascript.js', function() {
this.parser.render() this.parser.render()
.then(() => { .then(() => {
expect(this.parser._removeClass).toHaveBeenCalledWith('loading'); expect(this.parser._removeClass).toHaveBeenCalledWith('loading');
}) done();
.finally(done) });
.done();
}); });
it('removes the progress element', function(done) { it('removes the progress element', function(done) {
this.parser.render() this.parser.render()
.then(() => { .then(() => {
expect(this.container.querySelector('.loading')).toBeNull(); expect(this.container.querySelector('.loading')).toBeNull();
}) done();
.finally(done) });
.done();
}); });
}); });

View File

@ -2,7 +2,6 @@ import util from 'src/js/util.js';
import Regexper from 'src/js/regexper.js'; import Regexper from 'src/js/regexper.js';
import Parser from 'src/js/parser/javascript.js'; import Parser from 'src/js/parser/javascript.js';
import Snap from 'snapsvg'; import Snap from 'snapsvg';
import Q from 'q';
describe('regexper.js', function() { describe('regexper.js', function() {
@ -270,7 +269,7 @@ describe('regexper.js', function() {
describe('#showExpression', function() { describe('#showExpression', function() {
beforeEach(function() { beforeEach(function() {
spyOn(this.regexper, 'renderRegexp').and.returnValue(jasmine.createSpyObj('renderRegexp', ['done'])); spyOn(this.regexper, 'renderRegexp').and.returnValue(jasmine.createSpyObj('renderRegexp', ['catch']));
}); });
it('sets the text field value', function() { it('sets the text field value', function() {
@ -374,8 +373,8 @@ describe('regexper.js', function() {
describe('#renderRegexp', function() { describe('#renderRegexp', function() {
beforeEach(function() { beforeEach(function() {
this.parsePromise = Q.defer(); this.parsePromise = this.testablePromise();
this.renderPromise = Q.defer(); this.renderPromise = this.testablePromise();
spyOn(Parser.prototype, 'parse').and.returnValue(this.parsePromise.promise); spyOn(Parser.prototype, 'parse').and.returnValue(this.parsePromise.promise);
spyOn(Parser.prototype, 'render').and.returnValue(this.renderPromise.promise); spyOn(Parser.prototype, 'render').and.returnValue(this.renderPromise.promise);
spyOn(Parser.prototype, 'cancel'); spyOn(Parser.prototype, 'cancel');
@ -414,27 +413,24 @@ describe('regexper.js', function() {
this.regexper.renderRegexp('example expression') this.regexper.renderRegexp('example expression')
.then(() => { .then(() => {
expect(this.regexper.state).toEqual('has-error'); expect(this.regexper.state).toEqual('has-error');
}, fail) done();
.finally(done) });
.done();
}); });
it('displays the error message', function(done) { it('displays the error message', function(done) {
this.regexper.renderRegexp('example expression') this.regexper.renderRegexp('example expression')
.then(() => { .then(() => {
expect(this.regexper.error.innerHTML).toEqual('Error: example parse error'); expect(this.regexper.error.innerHTML).toEqual('Error: example parse error');
}, fail) done();
.finally(done) });
.done();
}); });
it('tracks the parse error', function(done) { it('tracks the parse error', function(done) {
this.regexper.renderRegexp('example expression') this.regexper.renderRegexp('example expression')
.then(() => { .then(() => {
expect(window._gaq.push).toHaveBeenCalledWith(['_trackEvent', 'visualization', 'parse error']); expect(window._gaq.push).toHaveBeenCalledWith(['_trackEvent', 'visualization', 'parse error']);
}, fail) done();
.finally(done) });
.done();
}); });
}); });
@ -451,9 +447,8 @@ describe('regexper.js', function() {
this.regexper.renderRegexp('example expression') this.regexper.renderRegexp('example expression')
.then(() => { .then(() => {
expect(this.parser.render).toHaveBeenCalled(); expect(this.parser.render).toHaveBeenCalled();
}, fail) done();
.finally(done) });
.done();
}); });
}); });
@ -470,54 +465,48 @@ describe('regexper.js', function() {
this.regexper.renderRegexp('example expression') this.regexper.renderRegexp('example expression')
.then(() => { .then(() => {
expect(this.regexper.state).toEqual('has-results'); expect(this.regexper.state).toEqual('has-results');
}, fail) done();
.finally(done) });
.done();
}); });
it('updates the links', function(done) { it('updates the links', function(done) {
this.regexper.renderRegexp('example expression') this.regexper.renderRegexp('example expression')
.then(() => { .then(() => {
expect(this.regexper.updateLinks).toHaveBeenCalled(); expect(this.regexper.updateLinks).toHaveBeenCalled();
}, fail) done();
.finally(done) });
.done();
}); });
it('displays the warnings', function(done) { it('displays the warnings', function(done) {
this.regexper.renderRegexp('example expression') this.regexper.renderRegexp('example expression')
.then(() => { .then(() => {
expect(this.regexper.displayWarnings).toHaveBeenCalled(); expect(this.regexper.displayWarnings).toHaveBeenCalled();
}, fail) done();
.finally(done) });
.done();
}); });
it('tracks the complete render', function(done) { it('tracks the complete render', function(done) {
this.regexper.renderRegexp('example expression') this.regexper.renderRegexp('example expression')
.then(() => { .then(() => {
expect(window._gaq.push).toHaveBeenCalledWith(['_trackEvent', 'visualization', 'complete']); expect(window._gaq.push).toHaveBeenCalledWith(['_trackEvent', 'visualization', 'complete']);
}, fail) done();
.finally(done) });
.done();
}); });
it('sets the running property to false', function(done) { it('sets the running property to false', function(done) {
this.regexper.renderRegexp('example expression') this.regexper.renderRegexp('example expression')
.then(() => { .then(() => {
expect(this.regexper.running).toBeFalsy(); expect(this.regexper.running).toBeFalsy();
}, fail) done();
.finally(done) });
.done();
}); });
it('tracks the total rendering time', function(done) { it('tracks the total rendering time', function(done) {
this.regexper.renderRegexp('example expression') this.regexper.renderRegexp('example expression')
.then(() => { .then(() => {
expect(window._gaq.push).toHaveBeenCalledWith(['_trackTiming', 'visualization', 'total time', jasmine.any(Number)]); expect(window._gaq.push).toHaveBeenCalledWith(['_trackTiming', 'visualization', 'total time', jasmine.any(Number)]);
}, fail) done();
.finally(done) });
.done();
}); });
}); });
@ -534,27 +523,24 @@ describe('regexper.js', function() {
this.regexper.renderRegexp('example expression') this.regexper.renderRegexp('example expression')
.then(() => { .then(() => {
expect(this.regexper.state).toEqual(''); expect(this.regexper.state).toEqual('');
}, fail) done();
.finally(done) });
.done();
}); });
it('tracks the cancelled render', function(done) { it('tracks the cancelled render', function(done) {
this.regexper.renderRegexp('example expression') this.regexper.renderRegexp('example expression')
.then(() => { .then(() => {
expect(window._gaq.push).toHaveBeenCalledWith(['_trackEvent', 'visualization', 'cancelled']); expect(window._gaq.push).toHaveBeenCalledWith(['_trackEvent', 'visualization', 'cancelled']);
}, fail) done();
.finally(done) });
.done();
}); });
it('sets the running property to false', function(done) { it('sets the running property to false', function(done) {
this.regexper.renderRegexp('example expression') this.regexper.renderRegexp('example expression')
.then(() => { .then(() => {
expect(this.regexper.running).toBeFalsy(); expect(this.regexper.running).toBeFalsy();
}, fail) done();
.finally(done) });
.done();
}); });
}); });
@ -571,9 +557,8 @@ describe('regexper.js', function() {
this.regexper.renderRegexp('example expression') this.regexper.renderRegexp('example expression')
.then(fail, () => { .then(fail, () => {
expect(this.regexper.running).toBeFalsy(); expect(this.regexper.running).toBeFalsy();
}) done();
.finally(done) });
.done();
}); });
}); });

View File

@ -8,6 +8,17 @@ beforeEach(function() {
'<div class="progress"><div></div></div>' '<div class="progress"><div></div></div>'
].join(''); ].join('');
document.body.appendChild(template); document.body.appendChild(template);
this.testablePromise = function() {
var result = {};
result.promise = new Promise((resolve, reject) => {
result.resolve = resolve;
result.reject = reject;
});
return result;
};
}); });
afterEach(function() { afterEach(function() {

View File

@ -41,7 +41,13 @@ window._gaq = (typeof _gaq !== 'undefined') ? _gaq : {
_.each(document.querySelectorAll('[data-expr]'), element => { _.each(document.querySelectorAll('[data-expr]'), element => {
new Parser(element, { keepContent: true }) new Parser(element, { keepContent: true })
.parse(element.getAttribute('data-expr')) .parse(element.getAttribute('data-expr'))
.invoke('render') .then(parser => {
.done(); parser.render();
})
.catch(error => {
setTimeout(() => {
throw error;
});
});
}); });
}()); }());

View File

@ -1,4 +1,3 @@
import Q from 'q';
import Snap from 'snapsvg'; import Snap from 'snapsvg';
import _ from 'lodash'; import _ from 'lodash';
@ -45,23 +44,21 @@ export default class Parser {
} }
parse(expression) { parse(expression) {
var deferred = Q.defer();
this._addClass('loading'); this._addClass('loading');
setTimeout(() => { return new Promise((resolve, reject) => {
try { setTimeout(() => {
javascript.Parser.SyntaxNode.state = this.state; try {
javascript.Parser.SyntaxNode.state = this.state;
this.parsed = javascript.parse(expression.replace(/\n/g, '\\n')); this.parsed = javascript.parse(expression.replace(/\n/g, '\\n'));
deferred.resolve(this); resolve(this);
} }
catch(e) { catch(e) {
deferred.reject(e); reject(e);
} }
});
}); });
return deferred.promise;
} }
render() { render() {
@ -78,7 +75,7 @@ export default class Parser {
height: box.height + 20 height: box.height + 20
}); });
}) })
.finally(() => { .then(() => {
this._removeClass('loading'); this._removeClass('loading');
this.container.removeChild(this.container.querySelector('.progress')); this.container.removeChild(this.container.querySelector('.progress'));
}); });

View File

@ -1,6 +1,5 @@
import util from '../../util.js'; import util from '../../util.js';
import _ from 'lodash'; import _ from 'lodash';
import Q from 'q';
export default { export default {
type: 'charset', type: 'charset',
@ -22,7 +21,7 @@ export default {
_render() { _render() {
this.partContainer = this.container.group(); this.partContainer = this.container.group();
return Q.all(_.map(this.elements, part => { return Promise.all(_.map(this.elements, part => {
return part.render(this.partContainer.group()); return part.render(this.partContainer.group());
})) }))
.then(() => { .then(() => {

View File

@ -1,6 +1,5 @@
import util from '../../util.js'; import util from '../../util.js';
import _ from 'lodash'; import _ from 'lodash';
import Q from 'q';
export default { export default {
type: 'charset-range', type: 'charset-range',
@ -12,7 +11,7 @@ export default {
this.last this.last
]; ];
return Q.all([ return Promise.all([
this.first.render(this.container.group()), this.first.render(this.container.group()),
this.last.render(this.container.group()) this.last.render(this.container.group())
]) ])

View File

@ -15,11 +15,12 @@ export default {
_render() { _render() {
return this.renderLabel(this.label) return this.renderLabel(this.label)
.tap(label => { .then(label => {
label.select('rect').attr({ label.select('rect').attr({
rx: 3, rx: 3,
ry: 3 ry: 3
}); });
return label;
}); });
}, },

View File

@ -5,7 +5,7 @@ export default {
_render() { _render() {
return this.renderLabel(['\u201c', this.literal, '\u201d']) return this.renderLabel(['\u201c', this.literal, '\u201d'])
.tap(label => { .then(label => {
var spans = label.selectAll('tspan'); var spans = label.selectAll('tspan');
spans[0].addClass('quote'); spans[0].addClass('quote');
@ -15,6 +15,8 @@ export default {
rx: 3, rx: 3,
ry: 3 ry: 3
}); });
return label;
}); });
}, },

View File

@ -1,6 +1,5 @@
import util from '../../util.js'; import util from '../../util.js';
import _ from 'lodash'; import _ from 'lodash';
import Q from 'q';
export default { export default {
type: 'match', type: 'match',
@ -28,12 +27,16 @@ export default {
if (this.anchorStart) { if (this.anchorStart) {
start = this.renderLabel('Start of line') start = this.renderLabel('Start of line')
.invoke('addClass', 'anchor'); .then(label => {
return label.addClass('anchor');
});
} }
if (this.anchorEnd) { if (this.anchorEnd) {
end = this.renderLabel('End of line') end = this.renderLabel('End of line')
.invoke('addClass', 'anchor'); .then(label => {
return label.addClass('anchor');
});
} }
partPromises = _.map(this.parts, part => { partPromises = _.map(this.parts, part => {
@ -46,7 +49,7 @@ export default {
items = [this.container.group()]; items = [this.container.group()];
} }
return Q.all(items) return Promise.all(items)
.then(items => { .then(items => {
this.start = _.first(items); this.start = _.first(items);
this.end = _.last(items); this.end = _.last(items);

View File

@ -1,6 +1,5 @@
import util from '../../util.js'; import util from '../../util.js';
import _ from 'lodash'; import _ from 'lodash';
import Q from 'q';
export default class Node { export default class Node {
constructor(textValue, offset, elements, properties) { constructor(textValue, offset, elements, properties) {
@ -59,18 +58,15 @@ export default class Node {
} }
deferredStep() { deferredStep() {
var deferred = Q.defer(), return new Promise((resolve, reject) => {
result = arguments; setTimeout(() => {
if (this.state.cancelRender) {
setTimeout(() => { reject('Render cancelled');
if (this.state.cancelRender) { } else {
deferred.reject('Render cancelled'); resolve.apply(this, arguments);
} else { }
deferred.resolve.apply(this, result); }, 1);
} });
}, 1);
return deferred.promise;
} }
renderLabel(text) { renderLabel(text) {

View File

@ -1,6 +1,5 @@
import util from '../../util.js'; import util from '../../util.js';
import _ from 'lodash'; import _ from 'lodash';
import Q from 'q';
export default { export default {
type: 'regexp', type: 'regexp',
@ -11,7 +10,7 @@ export default {
.transform(Snap.matrix() .transform(Snap.matrix()
.translate(20, 0)); .translate(20, 0));
return Q.all(_.map(this.matches, match => { return Promise.all(_.map(this.matches, match => {
return match.render(matchContainer.group()); return match.render(matchContainer.group());
})) }))
.then(() => { .then(() => {

View File

@ -1,6 +1,5 @@
import util from './util.js'; import util from './util.js';
import Parser from './parser/javascript.js'; import Parser from './parser/javascript.js';
import Q from 'q';
import _ from 'lodash'; import _ from 'lodash';
export default class Regexper { export default class Regexper {
@ -98,7 +97,11 @@ export default class Regexper {
this.state = ''; this.state = '';
if (expression !== '') { if (expression !== '') {
this.renderRegexp(expression).done(); this.renderRegexp(expression).catch(message => {
setTimeout(() => {
throw message;
});
});
} }
} }
@ -142,15 +145,13 @@ export default class Regexper {
startTime, endTime; startTime, endTime;
if (this.running) { if (this.running) {
let deferred = Q.defer();
this.running.cancel(); this.running.cancel();
setTimeout(() => { return new Promise((resolve, reject) => {
deferred.resolve(this.renderRegexp(expression)); setTimeout(() => {
}, 10); resolve(this.renderRegexp(expression));
}, 10);
return deferred.promise; });
} }
this.state = 'is-loading'; this.state = 'is-loading';
@ -161,7 +162,7 @@ export default class Regexper {
return this.running return this.running
.parse(expression) .parse(expression)
.then(null, message => { .catch(message => {
this.state = 'has-error'; this.state = 'has-error';
this.error.innerHTML = ''; this.error.innerHTML = '';
this.error.appendChild(document.createTextNode(message)); this.error.appendChild(document.createTextNode(message));
@ -170,7 +171,9 @@ export default class Regexper {
throw message; throw message;
}) })
.invoke('render') .then(parser => {
return parser.render();
})
.then(() => { .then(() => {
this.state = 'has-results'; this.state = 'has-results';
this.updateLinks(); this.updateLinks();
@ -180,7 +183,7 @@ export default class Regexper {
endTime = new Date().getTime(); endTime = new Date().getTime();
window._gaq.push(['_trackTiming', 'visualization', 'total time', endTime - startTime]); window._gaq.push(['_trackTiming', 'visualization', 'total time', endTime - startTime]);
}) })
.then(null, message => { .catch(message => {
if (message === 'Render cancelled') { if (message === 'Render cancelled') {
window._gaq.push(['_trackEvent', 'visualization', 'cancelled']); window._gaq.push(['_trackEvent', 'visualization', 'cancelled']);
this.state = ''; this.state = '';
@ -190,8 +193,14 @@ export default class Regexper {
throw message; throw message;
} }
}) })
.finally(() => { .then(
this.running = false; () => {
}); this.running = false;
},
message => {
this.running = false;
throw message;
}
);
} }
} }