1// Copyright Joyent, Inc. and other Node contributors. 2// 3// Permission is hereby granted, free of charge, to any person obtaining a 4// copy of this software and associated documentation files (the 5// "Software"), to deal in the Software without restriction, including 6// without limitation the rights to use, copy, modify, merge, publish, 7// distribute, sublicense, and/or sell copies of the Software, and to permit 8// persons to whom the Software is furnished to do so, subject to the 9// following conditions: 10// 11// The above copyright notice and this permission notice shall be included 12// in all copies or substantial portions of the Software. 13// 14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 17// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 18// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 20// USE OR OTHER DEALINGS IN THE SOFTWARE. 21 22'use strict'; 23const common = require('../common'); 24const assert = require('assert'); 25const EventEmitter = require('events'); 26 27const e = new EventEmitter(); 28 29e.once('hello', common.mustCall()); 30 31e.emit('hello', 'a', 'b'); 32e.emit('hello', 'a', 'b'); 33e.emit('hello', 'a', 'b'); 34e.emit('hello', 'a', 'b'); 35 36function remove() { 37 assert.fail('once->foo should not be emitted'); 38} 39 40e.once('foo', remove); 41e.removeListener('foo', remove); 42e.emit('foo'); 43 44e.once('e', common.mustCall(function() { 45 e.emit('e'); 46})); 47 48e.once('e', common.mustCall()); 49 50e.emit('e'); 51 52{ 53 // once() has different code paths based on the number of arguments being 54 // emitted. Verify that all of the cases are covered. 55 const maxArgs = 4; 56 57 for (let i = 0; i <= maxArgs; ++i) { 58 const ee = new EventEmitter(); 59 const args = ['foo']; 60 61 for (let j = 0; j < i; ++j) 62 args.push(j); 63 64 ee.once('foo', common.mustCall((...params) => { 65 assert.deepStrictEqual(params, args.slice(1)); 66 })); 67 68 EventEmitter.prototype.emit.apply(ee, args); 69 } 70} 71