Skip to content

Commit

Permalink
feat: pause/resume
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Oct 31, 2018
1 parent 3962a1f commit f217cc1
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ Create a new `Consola` instance with that tag.

Globally redirect all `console.log`, etc calls to consola handlers.

#### `pause()`
#### `resume()`

**Globally** pause and resume logs.

Consola will enqueue all logs when paused and then sends them to the reported when resumed.

## Fields

#### `reporters`
Expand Down
18 changes: 18 additions & 0 deletions examples/pause.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env node -r esm

import { consola } from './utils'

const c1 = consola.withTag('foo')
const c2 = consola.withTag('bar')

consola.log('before pause')

c2.pause()

c1.log('C1 is ready')
c2.log('C2 is ready')

setTimeout(() => {
consola.resume()
consola.log('Yo!')
}, 1000)
22 changes: 22 additions & 0 deletions src/consola.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import { isLogObj } from './utils/index.js'

const originalConsole = Object.assign({}, console)

let paused = false
const queue = []

export default class Consola {
constructor (options = {}) {
this._reporters = options.reporters || []
Expand Down Expand Up @@ -102,8 +105,27 @@ export default class Consola {
Object.assign(console, originalConsole)
}

pause () {
paused = true
}

resume () {
paused = false

// Process queue
const _queue = queue.splice(0)
for (const item of _queue) {
item[0]._logFn(item[1], item[2])
}
}

_wrapLogFn (defaults) {
function logFn () {
if (paused) {
queue.push([this, defaults, arguments])
return
}

return this._logFn(defaults, arguments)
}
return logFn.bind(this)
Expand Down

0 comments on commit f217cc1

Please sign in to comment.