diff --git a/lib/index.js b/lib/index.js index 5622f64..e75f95f 100644 --- a/lib/index.js +++ b/lib/index.js @@ -232,6 +232,8 @@ const Channel = function (bufferLength = 0) { order.reject(new TypeError( `Can't push 'undefined' to channel, use close instead.` )) + } else if (arguments.length > 1) { + order.reject(new Error(`Can't push more than one value at a time.`)) } else { pushes.push(order) setImmediate(processOrders) diff --git a/test/index.js b/test/index.js index 2876ffb..a4b8184 100644 --- a/test/index.js +++ b/test/index.js @@ -38,7 +38,9 @@ describe(`Channel`, function () { assert.equal(await channel.shift(), 0) })() - await channel.push(0, 1, 2) + await channel.push(0) + await channel.push(1) + await channel.push(2) }) describe(`from`, function () { @@ -188,10 +190,9 @@ describe(`Channel object`, function () { })().then(() => { assert(false) }).catch((reason) => { - assert.deepEqual( - reason, - new TypeError(`Can't push 'undefined' to channel, use close instead.`) - ) + assert.deepEqual(reason, new TypeError( + `Can't push 'undefined' to channel, use close instead.` + )) }) }) @@ -203,14 +204,26 @@ describe(`Channel object`, function () { })().then(() => { assert(false) }).catch((reason) => { - assert.deepEqual( - reason, - new TypeError(`Can't push 'undefined' to channel, use close instead.`) - ) + assert.deepEqual(reason, new TypeError( + `Can't push 'undefined' to channel, use close instead.` + )) }) }) }) + it(`disallows multiple values`, function () { + const channel = Channel() + + return (async () => { + await channel.push(0, 1, 2) + })().then(() => { + assert(false) + }).catch((reason) => { + assert.deepEqual(reason, new Error( + `Can't push more than one value at a time.`)) + }) + }) + it(`returns a frozen promise`, function () { assert.throws(() => { Channel().push(0).frozen = false