Event Loop and EventEmitter
Last updated
Last updated
Node.js에선 Event를 매우 많이 사용하고, 이 때문에 다른 기술들보다 훨씬 속도가 빠름
Node.js 기반으로 만들어진 server가 가동되면,
변수들을 initialize하고,
함수를 선언하고,
Event가 일어날 때 까지 기다림
Event-Driven application
에서는 event를 대기하는 main loop가 있음
그리고 event가 감지되었을 시 Callback 함수 를 호출함
Callback function
은 비동기식 함수에서 결과를 반환할 때 호출
Event Handling
은 observer pattern에 의해 작동됨
Observer pattern
object의 상태 변화를 관찰하는 관찰자들, 즉 observer들의 목록을 object에 등록하여 상태 변화가 있을 때마다 method 등을 통해 객체가 직접 목록의 각 observer에게 통지하도록 하는 design pattern
주로 Distributed event handling system 을 구현하는 데 사용됨
이벤트를 대기하는 EventListeners
함수들이 observer 역할을 함!
-> Observer들이 event를 기다리다가, event가 실행되면 event를 처리하는 함수가 실행됨!
: Node.js의 event module
과 EventEmitter Class
를 사용하여 event와 event handler를 bind 할 수 있다
ex)
event_handle.js
Result
Many objects in a Node emit events
Server emits an event each time a peer connects to it
An fs.readStream
emits an event when the file is opened
All objects which emit events are instance of events.EventEmitter
1
addListener(event, listener)
Adds a listener at the end of the listeners array for the specified event. No checks are made to see if the listener has already been added.
Multiple calls passing the same combination of event and listener will result in the listener being added multiple times.
Returns emitter, so calls can be chained.
2
on(event, listener)
Adds a listener at the end of the listeners array for the specified event. No checks are made to see if the listener has already been added.
Multiple calls passing the same combination of event and listener will result in the listener being added multiple times. Returns emitter, so calls can be chained.
3
once(event, listener)
Adds a one time listener to the event.
This listener is invoked only the next time the event is fired, after which it is removed.
Returns emitter, so calls can be chained.
4
removeListener(event, listener)
Removes a listener from the listener array for the specified event.
Caution − It changes the array indices in the listener array behind the listener. removeListener will remove, at most, one instance of a listener from the listener array.
If any single listener has been added multiple times to the listener array for the specified event, then removeListener must be called multiple times to remove each instance.
Returns emitter, so calls can be chained.
5
removeAllListeners([event])
Removes all listeners, or those of the specified event.
It's not a good idea to remove listeners that were added elsewhere in the code, especially when it's on an emitter that you didn't create (e.g. sockets or file streams).
Returns emitter, so calls can be chained.
6
setMaxListeners(n)
By default, EventEmitters will print a warning if more than 10 listeners are added for a particular event.
This is a useful default which helps finding memory leaks.
Obviously not all Emitters should be limited to 10.
This function allows that to be increased. Set to zero for unlimited.
7
listeners(event)
Returns an array of listeners for the specified event.
8
emit(event, [arg1], [arg2], [...])
Execute each of the listeners in order with the supplied arguments.
Returns true if the event had listeners, false otherwise.
1
listenerCount(emitter, event)
Returns the number of listeners for a given event.
1
newListener
event − String
: the event name
listener − Function
: the event handler functionThis event is emitted any time a listener is added.
When this event is triggered, the listener may not yet have been added to the array of listeners for the event.
2
removeListener
event − String The event name
listener − Function The event handler function
This event is emitted any time someone removes a listener.
When this event is triggered, the listener may not yet have been removed from the array of listeners for the event.
ex)
EventEmitter.js
Result