Skip to content

Commit 839fceb

Browse files
committed
fix: sequence console.log() calls
1 parent b524822 commit 839fceb

File tree

3 files changed

+41
-8
lines changed

3 files changed

+41
-8
lines changed

lib/log.js

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import colors from 'ansi-colors'
33
import stripAnsi from 'strip-ansi'
44

55
const tty = process.stdout.isTTY && process.stderr.isTTY
6+
let logQueue = []
7+
let logSeq = -1
68

79
function brand (error) {
810
const brand = 'polendina ☞'
@@ -20,11 +22,40 @@ export function error (msg) {
2022
console.log(` ${brand(true)} ${tty ? colors.gray.italic(msg) : msg}`)
2123
}
2224

23-
export function logRaw (type, args) {
25+
export function logRaw (type, seq, args) {
2426
if (!tty) {
2527
args = args.map((a) => typeof a === 'string' ? stripAnsi(a) : a)
2628
}
27-
console[type === 'info' ? 'log' : type].apply(null, args)
29+
logQueue.push({
30+
type: type === 'info' ? 'log' : type,
31+
args,
32+
seq
33+
})
34+
flushLogs(false)
35+
}
36+
37+
export function flushLogs (force) {
38+
while (true) {
39+
const lastLen = logQueue.length
40+
logQueue = logQueue.filter((le) => {
41+
const next = le.seq === -1 || le.seq === logSeq + 1
42+
if (force || next) {
43+
console[le.type].apply(null, le.args)
44+
if (le.seq !== -1) {
45+
logSeq++
46+
}
47+
} else if (le.seq < logSeq) {
48+
throw new Error(`Unexpected log output sequencing (${le.seq}<${logSeq})`)
49+
}
50+
return !next
51+
})
52+
if (lastLen === logQueue.length) {
53+
break
54+
}
55+
}
56+
if (force) {
57+
logSeq = -1
58+
}
2859
}
2960

3061
export function logWrite (args) {

lib/puppeteer.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import puppeteer from 'puppeteer'
22
// TODO
33
// const pti = require('puppeteer-to-istanbul')
44
// const { copyFile } = require('fs').promises
5-
import { log, error, logRaw, logWrite } from './log.js'
5+
import { log, error, logRaw, logWrite, flushLogs } from './log.js'
66

77
// wrap our convoluted _run() function in a pure Promise that can handle
88
// both standard throws and the callback that ends it. _run() needs to handle
@@ -42,6 +42,7 @@ async function _run (outputDir, port, timeout, mode, runner, coverage, callback)
4242
return
4343
}
4444
executionQueue.then(async () => {
45+
flushLogs(true)
4546
executionQueue = null
4647
/* TODO
4748
if (coverage) {
@@ -71,7 +72,7 @@ async function _run (outputDir, port, timeout, mode, runner, coverage, callback)
7172
}
7273
const args = []
7374
executionQueue = executionQueue.then(async () => {
74-
logRaw('info', await Promise.all(args))
75+
logRaw('info', -1, await Promise.all(args))
7576
})
7677
for (const arg of msg.args()) {
7778
args.push(arg.evaluate(n => n))
@@ -86,7 +87,7 @@ async function _run (outputDir, port, timeout, mode, runner, coverage, callback)
8687
end(errors)
8788
})
8889
await page.exposeFunction('polendinaLog', (args) => {
89-
logRaw(args.shift(), args)
90+
logRaw(args.shift(), args.shift(), args)
9091
maybeEnd()
9192
})
9293
await page.exposeFunction('polendinaWrite', (args) => {

resources/common-run.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const inWorker = !inPage && !inServiceWorker &&
1313
typeof WorkerGlobalScope !== 'undefined' &&
1414
globalThis instanceof WorkerGlobalScope
1515
let _executionQueue = Promise.resolve()
16+
let logSeq = 0
1617

1718
export { registry }
1819
export function executionQueue (fn) {
@@ -22,17 +23,17 @@ export function executionQueue (fn) {
2223

2324
export const log = {
2425
info: (...args) => {
25-
executionQueue(() => globalThis.polendinaLog(['info'].concat(args)))
26+
executionQueue(() => globalThis.polendinaLog(['info', logSeq++].concat(args)))
2627
return executionQueue
2728
},
2829
// TODO
2930
warn: (...args) => {
30-
executionQueue(() => globalThis.polendinaLog(['warn'].concat(args)))
31+
executionQueue(() => globalThis.polendinaLog(['warn', logSeq++].concat(args)))
3132
return executionQueue
3233
},
3334
// TODO
3435
error: (...args) => {
35-
executionQueue(() => globalThis.polendinaLog(['error'].concat(args)))
36+
executionQueue(() => globalThis.polendinaLog(['error', logSeq++].concat(args)))
3637
return executionQueue
3738
}
3839
}

0 commit comments

Comments
 (0)