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

View File

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

View File

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

View File

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

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 { export default {
customEvent, customEvent,
normalizeBBox, normalizeBBox,
spaceHorizontally, spaceHorizontally,
spaceVertically spaceVertically,
wait,
tick,
exposeError
}; };