mirror of
https://github.com/monkeytypegame/monkeytype.git
synced 2025-11-09 21:51:29 +08:00
checking performance.now once, and passing it around
checking performance.now before a setTimeout setting test end as soon as possible
This commit is contained in:
parent
ac4158986c
commit
7835cac0f5
3 changed files with 31 additions and 21 deletions
|
|
@ -394,6 +394,9 @@ function handleChar(
|
|||
if (TestUI.resultCalculating || TestUI.resultVisible) {
|
||||
return;
|
||||
}
|
||||
|
||||
const now = performance.now();
|
||||
|
||||
const isCharKorean: boolean = TestInput.input.getKoreanStatus();
|
||||
if (char === "…") {
|
||||
for (let i = 0; i < 3; i++) {
|
||||
|
|
@ -442,7 +445,7 @@ function handleChar(
|
|||
}
|
||||
|
||||
//start the test
|
||||
if (!TestState.isActive && !TestLogic.startTest()) {
|
||||
if (!TestState.isActive && !TestLogic.startTest(now)) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -464,7 +467,7 @@ function handleChar(
|
|||
}
|
||||
|
||||
if (TestInput.input.current === "") {
|
||||
TestInput.setBurstStart(performance.now());
|
||||
TestInput.setBurstStart(now);
|
||||
}
|
||||
|
||||
if (!isCharKorean && !Config.language.startsWith("korean")) {
|
||||
|
|
@ -958,21 +961,21 @@ $(document).keydown(async (event) => {
|
|||
|
||||
$("#wordsInput").keydown((event) => {
|
||||
if (event.originalEvent?.repeat) return;
|
||||
|
||||
const now = performance.now();
|
||||
setTimeout(() => {
|
||||
const isAndroid =
|
||||
event.key === "Unidentified" && event.code === "" && event.which === 229;
|
||||
TestInput.recordKeydownTime(isAndroid ? "Android" : event.code);
|
||||
TestInput.recordKeydownTime(now, isAndroid ? "Android" : event.code);
|
||||
}, 0);
|
||||
});
|
||||
|
||||
$("#wordsInput").keyup((event) => {
|
||||
if (event.originalEvent?.repeat) return;
|
||||
|
||||
const now = performance.now();
|
||||
setTimeout(() => {
|
||||
const isAndroid =
|
||||
event.key === "Unidentified" && event.code === "" && event.which === 229;
|
||||
TestInput.recordKeyupTime(isAndroid ? "Android" : event.code);
|
||||
TestInput.recordKeyupTime(now, isAndroid ? "Android" : event.code);
|
||||
}, 0);
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -285,7 +285,7 @@ export function incrementAccuracy(correctincorrect: boolean): void {
|
|||
}
|
||||
}
|
||||
|
||||
export function forceKeyup(): void {
|
||||
export function forceKeyup(now: number): void {
|
||||
//using mean here because for words mode, the last keypress ends the test.
|
||||
//if we then force keyup on that last keypress, it will record a duration of 0
|
||||
//skewing the average and standard deviation
|
||||
|
|
@ -293,7 +293,7 @@ export function forceKeyup(): void {
|
|||
const keysOrder = Object.entries(keyDownData);
|
||||
keysOrder.sort((a, b) => a[1].timestamp - b[1].timestamp);
|
||||
for (let i = 0; i < keysOrder.length - 1; i++) {
|
||||
recordKeyupTime(keysOrder[i][0]);
|
||||
recordKeyupTime(now, keysOrder[i][0]);
|
||||
}
|
||||
const last = keysOrder[keysOrder.length - 1];
|
||||
if (last !== undefined) {
|
||||
|
|
@ -303,7 +303,7 @@ export function forceKeyup(): void {
|
|||
|
||||
let androidIndex = 0;
|
||||
|
||||
export function recordKeyupTime(key: string): void {
|
||||
export function recordKeyupTime(now: number, key: string): void {
|
||||
if (!keysToTrack.includes(key)) return;
|
||||
|
||||
if (key === "Android") {
|
||||
|
|
@ -313,7 +313,6 @@ export function recordKeyupTime(key: string): void {
|
|||
|
||||
if (keyDownData[key] === undefined) return;
|
||||
|
||||
const now = performance.now();
|
||||
const diff = Math.abs(keyDownData[key].timestamp - now);
|
||||
keypressTimings.duration.array[keyDownData[key].index] = diff;
|
||||
delete keyDownData[key];
|
||||
|
|
@ -321,7 +320,7 @@ export function recordKeyupTime(key: string): void {
|
|||
updateOverlap(now);
|
||||
}
|
||||
|
||||
export function recordKeydownTime(key: string): void {
|
||||
export function recordKeydownTime(now: number, key: string): void {
|
||||
if (!keysToTrack.includes(key)) {
|
||||
if (spacingDebug) {
|
||||
console.log(
|
||||
|
|
@ -353,8 +352,6 @@ export function recordKeydownTime(key: string): void {
|
|||
return;
|
||||
}
|
||||
|
||||
const now = performance.now();
|
||||
|
||||
keyDownData[key] = {
|
||||
timestamp: now,
|
||||
index: keypressTimings.duration.array.length,
|
||||
|
|
|
|||
|
|
@ -307,7 +307,7 @@ async function applyEnglishPunctuationToWord(word: string): Promise<string> {
|
|||
return EnglishPunctuation.replace(word);
|
||||
}
|
||||
|
||||
export function startTest(): boolean {
|
||||
export function startTest(now: number): boolean {
|
||||
if (PageTransition.get()) {
|
||||
return false;
|
||||
}
|
||||
|
|
@ -348,7 +348,7 @@ export function startTest(): boolean {
|
|||
}
|
||||
} catch (e) {}
|
||||
//use a recursive self-adjusting timer to avoid time drift
|
||||
TestStats.setStart(performance.now());
|
||||
TestStats.setStart(now);
|
||||
TestTimer.start();
|
||||
return true;
|
||||
}
|
||||
|
|
@ -1386,9 +1386,7 @@ function buildCompletedEvent(difficultyFailed: boolean): CompletedEvent {
|
|||
Config.mode === "zen"
|
||||
? 0
|
||||
: Misc.roundTo2(TestStats.end - TestInput.keypressTimings.spacing.last),
|
||||
startToFirstKey: Misc.roundTo2(
|
||||
TestInput.keypressTimings.spacing.first - TestStats.start
|
||||
),
|
||||
startToFirstKey: undefined,
|
||||
consistency: undefined,
|
||||
keyConsistency: undefined,
|
||||
funbox: Config.funbox,
|
||||
|
|
@ -1403,6 +1401,16 @@ function buildCompletedEvent(difficultyFailed: boolean): CompletedEvent {
|
|||
afkDuration: undefined,
|
||||
};
|
||||
|
||||
const stfk = Misc.roundTo2(
|
||||
TestInput.keypressTimings.spacing.first - TestStats.start
|
||||
);
|
||||
|
||||
if (stfk > 0) {
|
||||
completedEvent.startToFirstKey = stfk;
|
||||
} else {
|
||||
completedEvent.startToFirstKey = 0;
|
||||
}
|
||||
|
||||
// stats
|
||||
const stats = TestStats.calculateStats();
|
||||
if (stats.time % 1 != 0 && Config.mode !== "time") {
|
||||
|
|
@ -1527,19 +1535,21 @@ function buildCompletedEvent(difficultyFailed: boolean): CompletedEvent {
|
|||
}
|
||||
|
||||
export async function finish(difficultyFailed = false): Promise<void> {
|
||||
await Misc.sleep(1); //this is needed to make sure the last keypress is registered
|
||||
if (!TestState.isActive) return;
|
||||
const now = performance.now();
|
||||
TestStats.setEnd(now);
|
||||
|
||||
await Misc.sleep(1); //this is needed to make sure the last keypress is registered
|
||||
if (TestInput.input.current.length != 0) {
|
||||
TestInput.input.pushHistory();
|
||||
TestInput.corrected.pushHistory();
|
||||
Replay.replayGetWordsList(TestInput.input.history);
|
||||
}
|
||||
|
||||
TestInput.forceKeyup(); //this ensures that the last keypress(es) are registered
|
||||
TestInput.forceKeyup(now); //this ensures that the last keypress(es) are registered
|
||||
|
||||
TestUI.setResultCalculating(true);
|
||||
TestUI.setResultVisible(true);
|
||||
TestStats.setEnd(performance.now());
|
||||
TestState.setActive(false);
|
||||
Replay.stopReplayRecording();
|
||||
Focus.set(false);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue