Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Exercício de event emitter em Javascript usando TDD

License

NotificationsYou must be signed in to change notification settings

srmeneses/JS-exercise-event-emitter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 

Repository files navigation

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.

Running tests

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.

Example:

emitter.on('click',()=>console.log('Click 1'))emitter.on('click',()=>console.log('Click 2'))emitter.on('click',()=>console.log('Click 3'))emitter.emit('click')// Prints:// Click 1// Click 2// Click 3

Every listener callback receives adata object which contains thetype of the event and any other property passed on.emit():

Example:

emitter.on('loaded',e=>console.log(e))emitter.emit('loaded',{foo:'potato'})// Prints:// { type: 'loaded', foo: 'potato' }

once

once(event: string, callback: function): function

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

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp