Skip to content

Commit

Permalink
add response time
Browse files Browse the repository at this point in the history
  • Loading branch information
UziTech committed Feb 13, 2019
1 parent acf9c82 commit ab42f86
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
29 changes: 27 additions & 2 deletions docs/demo/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ var $markedVer = document.querySelector('#markedCdn');
var $optionsElem = document.querySelector('#options');
var $outputTypeElem = document.querySelector('#outputType');
var $inputTypeElem = document.querySelector('#inputType');
var $responseTimeElem = document.querySelector('#responseTime');
var $previewElem = document.querySelector('#preview');
var $previewIframe = document.querySelector('#preview iframe');
var $permalinkElem = document.querySelector('#permalink');
Expand Down Expand Up @@ -298,6 +299,7 @@ function checkForChanges() {
setScrollPercent(scrollPercent);
var endTime = new Date();
delayTime = endTime - startTime;
setResponseTime(delayTime);
if (delayTime < 50) {
delayTime = 50;
} else if (delayTime > 500) {
Expand All @@ -309,6 +311,22 @@ function checkForChanges() {
checkChangeTimeout = window.setTimeout(checkForChanges, delayTime);
};

function setResponseTime(ms) {
var amount = ms;
var suffix = 'ms';
if (ms > 1000 * 60 * 60) {
amount = 'Too Long';
suffix = '';
} else if (ms > 1000 * 60) {
amount = '>' + Math.floor(ms / (1000 * 60));
suffix = 'm';
} else if (ms > 1000) {
amount = '>' + Math.floor(ms / 1000);
suffix = 's';
}
$responseTimeElem.textContent = amount + suffix;
}

function setParsed(parsed, lexed) {
if (iframeLoaded) {
$previewIframe.contentDocument.body.innerHTML = parsed;
Expand Down Expand Up @@ -339,6 +357,7 @@ function messageWorker(message) {
var scrollPercent = getScrollPercent();
setParsed(e.data.parsed, e.data.lexed);
setScrollPercent(scrollPercent);
setResponseTime(e.data.time);
break;
}
clearTimeout(checkChangeTimeout);
Expand All @@ -363,10 +382,16 @@ function messageWorker(message) {
};
}
markedWorker.working = true;
workerTimeout(0);
markedWorker.postMessage(message);
}

function workerTimeout(seconds) {
markedWorker.timeout = setTimeout(function () {
markedWorker.onerror('Marked is taking a while...');
seconds++;
markedWorker.onerror('Marked has taken longer than ' + seconds + ' second' + (seconds > 1 ? 's' : '') + ' to respond...');
workerTimeout(seconds);
}, 1000);
markedWorker.postMessage(message);
}
checkForChanges();
setScrollPercent(0);
4 changes: 3 additions & 1 deletion docs/demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ <h1>Marked Demo</h1>
<option value="html">HTML Source</option>
<option value="lexer">Lexer Data</option>
<option value="quickref">Quick Reference</option>
</select>
</select> ·
Response Time:
<span id="responseTime"></span>
</div>

<div id="preview" class="pane">
Expand Down
8 changes: 6 additions & 2 deletions docs/demo/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ function parse(e) {
});
break;
case 'parse':
var startTime = new Date();
var lexed = marked.lexer(e.data.markdown, e.data.options);
var lexedList = [];
for (var i = 0; i < lexed.length; i++) {
Expand All @@ -40,12 +41,15 @@ function parse(e) {
lexedList.push('{' + lexedLine.join(', ') + '}');
}
var parsed = marked.parser(lexed, e.data.options);

var endTime = new Date();
// setTimeout(function () {
postMessage({
task: e.data.task,
lexed: lexedList.join('\n'),
parsed: parsed
parsed: parsed,
time: endTime - startTime
});
// }, 10000);
break;
}
}
Expand Down

0 comments on commit ab42f86

Please sign in to comment.