CoolFace
Datasetpublic

basant307/AI_Governance_Project

sourceHugging Faceapache-2.0updated 2mo agoView on Hugging Face
0likes48downloads
modify-in-emit.js91 linesDownload Raw Back to tests
1// Copyright Joyent, Inc. and other Node contributors.2//3// Permission is hereby granted, free of charge, to any person obtaining a4// copy of this software and associated documentation files (the5// "Software"), to deal in the Software without restriction, including6// without limitation the rights to use, copy, modify, merge, publish,7// distribute, sublicense, and/or sell copies of the Software, and to permit8// persons to whom the Software is furnished to do so, subject to the9// following conditions:10//11// The above copyright notice and this permission notice shall be included12// in all copies or substantial portions of the Software.13//14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN17// 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 OR19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE20// USE OR OTHER DEALINGS IN THE SOFTWARE.21 22require('./common');23var assert = require('assert');24var events = require('../');25 26var callbacks_called = [];27 28var e = new events.EventEmitter();29 30function callback1() {31  callbacks_called.push('callback1');32  e.on('foo', callback2);33  e.on('foo', callback3);34  e.removeListener('foo', callback1);35}36 37function callback2() {38  callbacks_called.push('callback2');39  e.removeListener('foo', callback2);40}41 42function callback3() {43  callbacks_called.push('callback3');44  e.removeListener('foo', callback3);45}46 47e.on('foo', callback1);48assert.strictEqual(e.listeners('foo').length, 1);49 50e.emit('foo');51assert.strictEqual(e.listeners('foo').length, 2);52assert.ok(Array.isArray(callbacks_called));53assert.strictEqual(callbacks_called.length, 1);54assert.strictEqual(callbacks_called[0], 'callback1');55 56e.emit('foo');57assert.strictEqual(e.listeners('foo').length, 0);58assert.ok(Array.isArray(callbacks_called));59assert.strictEqual(callbacks_called.length, 3);60assert.strictEqual(callbacks_called[0], 'callback1');61assert.strictEqual(callbacks_called[1], 'callback2');62assert.strictEqual(callbacks_called[2], 'callback3');63 64e.emit('foo');65assert.strictEqual(e.listeners('foo').length, 0);66assert.ok(Array.isArray(callbacks_called));67assert.strictEqual(callbacks_called.length, 3);68assert.strictEqual(callbacks_called[0], 'callback1');69assert.strictEqual(callbacks_called[1], 'callback2');70assert.strictEqual(callbacks_called[2], 'callback3');71 72e.on('foo', callback1);73e.on('foo', callback2);74assert.strictEqual(e.listeners('foo').length, 2);75e.removeAllListeners('foo');76assert.strictEqual(e.listeners('foo').length, 0);77 78// Verify that removing callbacks while in emit allows emits to propagate to79// all listeners80callbacks_called = [];81 82e.on('foo', callback2);83e.on('foo', callback3);84assert.strictEqual(2, e.listeners('foo').length);85e.emit('foo');86assert.ok(Array.isArray(callbacks_called));87assert.strictEqual(callbacks_called.length, 2);88assert.strictEqual(callbacks_called[0], 'callback2');89assert.strictEqual(callbacks_called[1], 'callback3');90assert.strictEqual(0, e.listeners('foo').length);91 
basant307/AI_Governance_Project · CoolFace