Creating helper methods for the setTimeout uses

Abstracting out some of the setTimeout patterns that have popped up in
the code to make the intention clearer.
This commit is contained in:
Jeff Avallone 2015-03-14 17:27:59 -04:00
parent 0093db8e20
commit 06e66c0f24
5 changed files with 37 additions and 36 deletions

View File

@ -33,7 +33,7 @@ window._gaq = (typeof _gaq !== 'undefined') ? _gaq : {
regexper.bindListeners();
setTimeout(() => {
util.tick().then(() => {
window.dispatchEvent(util.customEvent('hashchange'));
});
}
@ -44,10 +44,6 @@ window._gaq = (typeof _gaq !== 'undefined') ? _gaq : {
.then(parser => {
parser.render();
})
.catch(error => {
setTimeout(() => {
throw error;
});
});
.catch(util.exposeError);
});
}());

View File

@ -1,6 +1,7 @@
import Snap from 'snapsvg';
import _ from 'lodash';
import util from '../util.js';
import javascript from './javascript/parser.js';
import ParserState from './javascript/parser_state.js';
@ -46,18 +47,11 @@ export default class Parser {
parse(expression) {
this._addClass('loading');
return new Promise((resolve, reject) => {
setTimeout(() => {
try {
javascript.Parser.SyntaxNode.state = this.state;
return util.tick().then(() => {
javascript.Parser.SyntaxNode.state = this.state;
this.parsed = javascript.parse(expression.replace(/\n/g, '\\n'));
resolve(this);
}
catch(e) {
reject(e);
}
});
this.parsed = javascript.parse(expression.replace(/\n/g, '\\n'));
return this;
});
}

View File

@ -57,15 +57,13 @@ export default class Node {
return this.container.transform(matrix);
}
deferredStep() {
return new Promise((resolve, reject) => {
setTimeout(() => {
if (this.state.cancelRender) {
reject('Render cancelled');
} else {
resolve.apply(this, arguments);
}
}, 1);
deferredStep(value) {
return util.tick().then(() => {
if (this.state.cancelRender) {
throw 'Render cancelled';
}
return value;
});
}

View File

@ -97,11 +97,7 @@ export default class Regexper {
this.state = '';
if (expression !== '') {
this.renderRegexp(expression).catch(message => {
setTimeout(() => {
throw message;
});
});
this.renderRegexp(expression).catch(util.exposeError);
}
}
@ -147,10 +143,8 @@ export default class Regexper {
if (this.running) {
this.running.cancel();
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(this.renderRegexp(expression));
}, 10);
util.wait(10).then(() => {
return this.renderRegexp(expression);
});
}

View File

@ -71,9 +71,28 @@ function spaceVertically(items, options) {
}
}
function wait(delay) {
return new Promise((resolve, reject) => {
setTimeout(resolve, delay);
});
}
function tick() {
return wait(0);
}
function exposeError(error) {
tick().then(() => {
throw error;
});
}
export default {
customEvent,
normalizeBBox,
spaceHorizontally,
spaceVertically
spaceVertically,
wait,
tick,
exposeError
};