143

Unlike the polling example you provided (where the button is checked every frame), an event listener does not check if the button is pushed at all. Instead, it gets called when the button is pushed. Perhaps the term "event listener" is throwing you. This term suggests that the "listener" is actively doing something to listen, when in fact, it's not doing ...


105

Nope. They're really handy for implementing Observers and making sure that classes are closed to modification. Let's say we have a method that registers new users. public void Register(user) { db.Save(user); } Then someone decides that an email should be sent. We could do this: public void Register(user) { db.Save(user); emailClient.Send(...


79

An event is a notification describing an occurrence from the recent past. A typical implementation of a event-driven system utilises an event dispatcher and handler functions (or subscribers). The dispatcher provides an API to wire handlers up to events (jQuery's bind), and a method to publish an event to its subscribers (trigger in jQuery). When you're ...


58

Most event loops will suspend if there are no events ready, which means the operating system will not give the task any execution time until an event happens. Say the event is a key being pressed. You might ask if there's a loop somewhere in the operating system checking for keypresses. The answer is no. Keys being pressed generate an interrupt, which is ...


54

An event listener akin to an e-mail newsletter subscription (you register yourself to receive updates, whose transmission is later initiated by the sender), rather than endlessly refreshing a web page (where you are the one initiating the transfer of information). An event system is implemented using event objects, which manage a list of subscribers. ...


53

Nope. A classic example of events being used in non-GUI logic are database triggers. Triggers are code that gets executed when a given event happen (INSERT,DELETE, etc). Seems like an event to me. This is the Wikipedia definition of event: In computing, an event is an action or occurrence recognized by software that may be handled by the software. ...


38

The short, unsatisfactory answer is that the application receives a signal (the event) and that the routine is only called at that point. The longer explanation is a bit more involved. Where do client events come from? Each modern application† has an inner, usually semi-hidden "event loop" that dispatches events to the correct components that ...


32

There are many ways to do this, but I prefer to keep a message-based system as decoupled as possible. This means the overall system cannot read the state of any component, nor any component read the state of any other (as that way lies spaghetti ties of dependancies). So, while the running system will look after itself, we need a way to tell each component ...


31

Follow the KISS principle: Keep It Simple, Stupid, or the YAGNI principle: You Ain't Going to Need It. You can write the code like: void updateSpecialData() { // do the update. backupData(); } Or you can write code like: void updateSpecialData() { // do the update. emit SpecialDataUpdated(); } void SpecialDataUpdatedHandler() { ...


27

Event-based programming is actually also used for highly performant server programming. At a typical server workload, much of the time processing a result actually comes from I/O. For example, pulling data off a (7200 RPM) hard disk drive can take up to 8.3 ms. For a modern GHz processor, that would equate to ~1 million clock cycles. If a CPU were ...


21

proto3 makes a number of changes aimed (as I understand it) at making it far more usable in cross-platform scenarios. Explicit tracking of "assigned" vs "not assigned but reporting the default value" can be very hard to implement on some of the target platforms, and can also be confusing to use. As such, proto3 adopts a much simpler approach: the implicit ...


20

That depends upon whether it is really an Event or State Notification (which is what ready is). If it is an event, then you don't tell the listener about all prior events. If it is State Notification, then you tell them immediately after they have subscribed and you are in the specified state. The tricky bit will be where it can be either a state or an ...


19

Yep, you are definitely missing something. Gotos would typically be used, like you said, to perform a one-way transfer of control. However, events do not do that. When the code fires the event, it knows full well that once event is published (or processed, queued, fired... etc) code execution will resume on the very next line in the code that generated ...


19

The main difference between a hook and event is loose coupling versus tight coupling. A hook is a generic way to broadcast that something has happened. You can add new hooks without having to recompile plugins, and all hooks follow a generic design pattern. Once the hook API is defined it doesn't change so the coupling between the app and plugin isn't ...


19

Terminology event: A type of thing that can happen. event firing: A specific occurrence of an event; an event happening. event listener: Something that looks out for event firings. event handler: Something that occurs when an event listener detects an event firing. event subscriber: A response that the event handler's supposed to call. These ...


18

In an Event-based application the concept of Event Listeners will give you the ability to write even more Loosely Coupled applications. For example a third-party module or plug-in can delete a record from the database and then trigger the receordDeleted event and leave the rest to the event listeners to do their job. Everything will work fine, even though ...


16

In principle, a command describes a request that is to be executed, whereas an event describes something that has happened: A command requires some action to be performed by a processor, and this action should be performed only once by this processor. An event is the notification of some action that was already executed or an external happening. Several ...


14

Event driven architecture simply means, that the main loop AKA event loop does not understand much about the data a program processes. It understands only enough, that it can dispatch the right events for the data. So for example, if event is triggered by data coming from a socket, all the event loop needs to know is, what callback to call, when data comes ...


13

I think of an event listener not as a function running its own loop, but as a relay race with the first runner waiting for the starting gun. A significant reason for using events instead of polling is that they are more efficient with CPU cycles. Why? Look at it from the hardware up (rather than the source code down). Consider a Web server. When your ...


13

The example you describe of a simple data, where the modification triggers some effect can perfectly be implemented with the observer design pattern: this is simpler to implement and maintain than full event driven code. the coupling between subject and observer can be abstract, which facilitates separation of concerns. it's ideal for one to many ...


13

How do I deal with side effects in Event Sourcing? Short version: the domain model doesn't perform side effects. It tracks them. Side effects are performed using a port that connects to the boundary; when the email is sent, you send the acknowledgement back to the domain model. This means that the email is sent outside of the transaction that updates the ...


10

Events are also heavily used in network programming (e.g. Nginx) to avoid expensive busy-wait loops and instead provide a clean interface to know exactly when a certain operation is available(I/O, urgent data etc). This is also a solution to the C10k problem. The basic idea is to provide the OS a set of sockets (i.e. network connections) to monitor for ...


9

No. It is not "optimized polling." An event-loop uses interrupt-driven I/O instead of polling. While, Until, For, etc. loops are polling loops. "Polling" is the process of repeatedly checking something. Since the loop code executes continuously, and because it is a small, "tight" loop, there is little time for the processor to switch tasks and do anything ...


9

Pull vs Push There are two main strategies to check if an event happened, or a specific state is reached. For example, imagine waiting for an important delivery: Pull: every 10 minute, go down to your mailbox and check if it was delivered, Push: tell the delivery guy to call you when they do the delivery. The pull approach (also called polling) is simpler:...


8

Typically the answer is hardware, the OS and background threads you don't control conspire to make it look effortless. The network card receives some data it raises an interrupt to let the CPU know. The OS's interrupt handler handles it. Then a background thread you don't control (that was created by registering for the event and has been sleeping ever ...


8

If you ever try to write something like: window.onClick = myHandler; You will get a compiler error about event handlers only allowed on the right side of a += or -=. The C# compiler team was smart enough to recognize that possibility and make it a compiler error. Events are special types of multi-cast delegates, so therefore you are adding to its ...


7

I am going to go against all of the other answers I see so far and say "yes". I think the other answers are complicating things too much. From a conceptual point of view, all event loops are essentially: while <the_program_is_running> { event=wait_for_next_event() process_event(event) } If you are trying to understand event loops for the ...


7

My preference is for defining a & attribute in the directive scope primarily because I view the scope: {} definition of a directive as its API. It's much easier to look at a scope attribute definition to see what information the directive needs to function properly than it is to scour link and controller functions for $emit'd events, inherited scope ...


7

You can: Use the features of Observer that you need, and ignore the other features; or Use some other software pattern that more closely matches what you need, or Don't use a pattern at all, and simply write code that solves the problem. Just because the Wikipedia article says that the Observer pattern is designed to notify the observer of state changes ...


Only top voted, non community-wiki answers of a minimum length are eligible