Skip to content

Commit

Permalink
Fix up use of assertion
Browse files Browse the repository at this point in the history
  • Loading branch information
vectrixdevelops committed Jun 23, 2017
1 parent b5f1816 commit 41a7caa
Showing 1 changed file with 4 additions and 11 deletions.
15 changes: 4 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ function Speaker (opts) {
autoFlush: false
}, opts)

assert(options, 'Applied speaker options')

This comment has been minimized.

Copy link
@dy

dy Jun 23, 2017

Member

It will always be passing, so no sense in this assert.
Some examples: choo, web-audio-write


if (options.handler) {
throw new Error('_create() was called more than once. Only one handler should exist.')
}
Expand All @@ -53,17 +55,13 @@ function Speaker (opts) {
options.handler = binding.create((success) => {
if(!success) {
throw new Error('Failed to create the audio handler.')
} else {
assert(binding, 'Audio handler created')

This comment has been minimized.

Copy link
@dy

dy Jun 23, 2017

Member

👍

}
})

if (options.handler !== null) {
binding.open(options.handler, options.sampleRate, options.channels, format, function (success) {
if (!success) {
throw new Error('Could not start the audio output with these properties.')
} else {
assert(options, 'Audio handler options applied')
}
})
}
Expand All @@ -89,10 +87,9 @@ function Speaker (opts) {
function write (chunk, callback) {
if (!callback) callback = noop

if (options._closed) return assert(chunk, 'Write cannot occur after the speaker is closed')
if (options._closed) return callback(new Error('Write cannot occur after the speaker is closed.'))

if (chunk && options._busy) {
assert(chunk, 'Write cannot occur until the previous chunk is processed')
callback(new Error('Cannot write chunk as the buffer was busy.'), 0)
}

Expand Down Expand Up @@ -121,7 +118,6 @@ function Speaker (opts) {
options._busy = false
callback(new Error('Could not flush the audio output.'), written)
} else {
assert(chunk, 'Finished flushing chunk')
options._busy = false
callback(null, written)
}
Expand All @@ -133,7 +129,6 @@ function Speaker (opts) {
}
})
} else {
assert(chunk, 'Abandoning remaining chunks as the speaker has closed')
callback(new Error('Could not write remaining chunks as the speaker is closed.'), 0)
}
}
Expand All @@ -158,15 +153,14 @@ function Speaker (opts) {
* @api public
*/
function end (flush, callback) {
if (options._closed) return assert(flush, 'Closing the speaker cannot occur after the speaker is already closed')
if (options._closed) return callback(new Error('Closing the speaker cannot occur after the speaker is already closed.'))

if (options.handler) {
if (flush) {
binding.flush(options.handler, function (success) {
if (success != 1) {
callback(new Error('Could not flush the audio output.'))
} else {
assert(flush, 'Finished flushing chunk')
return close(callback)
}
})
Expand All @@ -183,7 +177,6 @@ function Speaker (opts) {
if (success != 1) {
callback(new Error('Failed to close speaker.'))
} else {
assert(options.handler, 'Closed speaker')
callback()
}
}
Expand Down

0 comments on commit 41a7caa

Please sign in to comment.