From 5caab7ab4704102b9621d5989a6f7e9016789241 Mon Sep 17 00:00:00 2001 From: tophf Date: Mon, 22 Jun 2015 21:18:33 +0300 Subject: [PATCH] Editor: better async adding of sections in initWithStyle --- edit.js | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/edit.js b/edit.js index 492c4e1b..52cdd3bc 100644 --- a/edit.js +++ b/edit.js @@ -925,11 +925,20 @@ function initWithStyle(style) { document.querySelectorAll("#sections > div").forEach(function(div) { div.parentNode.removeChild(div); }); - (style.sections.length == 0 ? [{code: ""}] : style.sections).forEach(function(section) { - setTimeout(function() { - maximizeCodeHeight(addSection(null, section), editors.length == style.sections.length); + var queue = style.sections.length ? style.sections : [{code: ""}]; + var queueStart = new Date().getTime(); + // after 200ms the sections will be added asynchronously + while (new Date().getTime() - queueStart <= 200 && queue.length) { + maximizeCodeHeight(addSection(null, queue.shift()), !queue.length); + } + if (queue.length) { + setTimeout(function processQueue() { + maximizeCodeHeight(addSection(null, queue.shift()), !queue.length); + if (queue.length) { + setTimeout(processQueue, 0); + } }, 0); - }); + } initHooks(); }