From 52a08634490f1ec39a51b2b9c8f23b2515192594 Mon Sep 17 00:00:00 2001 From: Jeff Avallone Date: Sun, 7 Dec 2014 18:19:00 -0500 Subject: [PATCH] Refactoring rendering code to allow the loader to show The loader is mostly an easter egg for people who like to try rendering that enormous email-matching regexp from the Mail::RFC822::Address perl module --- src/js/regexper.js | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/src/js/regexper.js b/src/js/regexper.js index 0bf62d5..500ba54 100644 --- a/src/js/regexper.js +++ b/src/js/regexper.js @@ -69,7 +69,7 @@ export default class Regexper { updateLinks() { var blob, url; - blob = new Blob([svg.outerHTML], { type: 'image/svg+xml' }); + blob = new Blob([this.svg.outerHTML], { type: 'image/svg+xml' }); url = URL.createObjectURL(blob); this.download.setAttribute('href', url); @@ -77,15 +77,27 @@ export default class Regexper { } renderSvg(snap, expression) { - var result; + var deferred = Q.defer(), + result; snap.selectAll('g').remove(); - result = parser.parse(expression); - result.container = snap.group(); - result.render(); + setTimeout(() => { + try { + result = parser.parse(expression); + } + catch(e) { + deferred.reject(e); + } - return result; + if (result) { + result.container = snap.group(); + result.render(); + deferred.resolve(result); + } + }); + + return deferred.promise; } positionSvg(snap, parsed) { @@ -104,15 +116,9 @@ export default class Regexper { renderRegexp(expression) { var padding = this.padding, - snap = Snap(this.svg), - promise; + snap = Snap(this.svg); - promise = Q.promise(((resolve, reject, notify) => { - resolve(this.renderSvg(snap, expression)); - }).bind(this)); - - promise.then(this.positionSvg.bind(this, snap)); - - return promise; + return this.renderSvg(snap, expression) + .then(this.positionSvg.bind(this, snap)); } }