You signed in with another tab or window.Reload to refresh your session.You signed out in another tab or window.Reload to refresh your session.You switched accounts on another tab or window.Reload to refresh your session.Dismiss alert
Exercício de event emitter em Javascript usando TDD
Esse é um exercício de uma entrevista técnica de emprego para vaga de Engenheiro de Software. Então você pode utilizá-la para treinar seus conhecimentos antes de uma entrevista javascript.
Para fazer o exercício você terá que instalar o Yarn em sua máquina e estar levemente familiarizado com o desenvolvimento de software orientado a testes (TDD).
Baixe a pasta “exercise-event-emitter-master” e tente solucioná-lo antes de ver a resposta.Procure se focar mais em solucionar o problema do que em eficiência.
Lembre-se: Sempre há mais de uma maneira de se chegar na resposta correta. E o mais importante se divirta!
Event Emitter
The goal of this exercise is to create an event emitter which can listen, unlisten and emit events.
The main module exports an object with acreate method which act as a factory and should return a new event emitter every time it is called. The implementation of the emitter itself is up to you.
By executing thepackage.jsontest script, ajest process will start and re-run the test suite after every file change you made.
Example
importEmitterfrom'./emitter.js'constemitter=Emitter.create()consthandleMyEvent=()=>{console.log('Hello!')}// Registers the callback `handleMyEvent` to be called when we call emitter.emit passing `myEvent` as parameteremitter.on('myEvent',handleMyEvent)// This will call the `handleMyEvent` functionemitter.emit('myEvent')// Will print "Hello!"// Unregisters the callback `handleMyEvent`emitter.off('myEvent',handleMyEvent)// No function will be called since `myEvent` does not have any callbacks assigned to it anymoreemitter.emit('myEvent')
Documentation
Creating a new Emitter
constEventEmitter=require('...')// Return a new event emitterconstemitter=EventEmitter.create()
Methods
on
on(event: string, callback: function): function
Registers acallback that runs whenevent is emitted.
Example:
emitter.on('click',()=>{console.log('Click!')})// register a new listener for the 'click' event
It returns a method which unregisters the listener:
Example:
constcancel=emitter.on('click',()=>{console.log('Click!')})cancel()// unregister the click listener
off
off(event: string, callback: function)
Unregisters acallback from theevent callback list.
Example:
constcallback=()=>{console.log('Click!')}// register the listeneremitter.on('click',callback)// unregister the listeneremitter.off('click',callback)
emit
emit(event: string, data: object)
Execute all callbacks registered forevent passingdata as a parameter.
Registers acallback tha runs only once whenevent is emitted.
Example:
emitter.once('click',()=>console.log('Single click!'))emitter.emit('click')emitter.emit('click')// Prints 'Single click!' only once
race
race(Array<[event: string, callback: function]>): function
Receives a list of[event, callback] and when any of the passed events is emitted, it unregisters all of them.
Example:
emitter.race([['success',()=>console.log('Success!')],['failure',()=>console.log('Failure :(')],])emitter.emit('success')// Prints 'Success!', `success` and `failure` are unregistered.emitter.emit('failure')// nothing happens
About
Exercício de event emitter em Javascript usando TDD