Skip to content
This repository has been archived by the owner on Feb 1, 2022. It is now read-only.

Commit

Permalink
feat: added flush
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed May 13, 2018
1 parent c0bbca0 commit d27775f
Showing 1 changed file with 21 additions and 21 deletions.
42 changes: 21 additions & 21 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,27 @@ export const cli = {
this.log(text)
}
},

async flush() {
function timeout(p: Promise<any>, ms: number) {
function wait(ms: number, unref: boolean = false) {
return new Promise(resolve => {
let t: any = setTimeout(resolve, ms)
if (unref) t.unref()
})
}

return Promise.race([p, wait(ms, true).then(() => cli.warn('timed out'))])
}

async function flush() {
let p = new Promise(resolve => process.stdout.once('drain', resolve))
process.stdout.write('')
return p
}

await timeout(flush(), 10000)
}
}
export default cli

Expand All @@ -88,24 +109,3 @@ process.once('exit', async () => {
process.exitCode = 1
}
})

// async function flushStdout() {
// function timeout(p: Promise<any>, ms: number) {
// function wait(ms: number, unref: boolean = false) {
// return new Promise(resolve => {
// let t: any = setTimeout(resolve, ms)
// if (unref) t.unref()
// })
// }

// return Promise.race([p, wait(ms, true).then(() => cli.warn('timed out'))])
// }

// async function flush() {
// let p = new Promise(resolve => process.stdout.once('drain', resolve))
// process.stdout.write('')
// return p
// }

// await timeout(flush(), 10000)
// }

0 comments on commit d27775f

Please sign in to comment.