All You Need to Know About Event Handler and WebAssembly

All You Need to Know About Event Handler and WebAssembly

What is an event handler?

With an event handler, a software developer can control exactly what should happen in the program when a certain event occurs. The triggering events can have different origins, but they are often triggered by the interaction of the user.

Events occur in a wide variety of places in software, e.g. For example, when a user clicks a button, moves the mouse pointer or writes something in a text field. Event handlers have the task of recognizing these events and then executing a predetermined action, e.g. B. to temporarily store the content of a text field as soon as the user changes it.

Event oriented software

Programs that do not always run linearly according to the same scheme and react to inputs and behavior of the user – or, more generally, to changes in state – work with events. This means that at some points in the program it is expected that a certain event could occur.

Accordingly, developers must provide a way in which the program code can deal with these events. For this purpose, it must first be monitored in the program code whether and when a predetermined event occurs. If it is determined that such an event has been triggered, the associated functionality or routine can then be executed.

Interface between code and events

Event handlers are most commonly used to create a connection between elements from the graphical user interface and the code in the background. So programmers have the possibility to react directly to the input of the user or to other events. In addition, working with event handlers allows programs that can react spontaneously and dynamically to events instead of actively waiting for a specific event and thus blocking resources.

Examples

  • The validation for a password field in a form is only triggered when the user has entered something in the associated text field and then left the field again.
  • Only when a user clicks the “Select Date” button should the program display a control for selecting the date.
  • While the user is already entering text in a text field, each new character is checked to determine whether the content exceeds the maximum number of characters allowed.
  • When a user enters a predetermined key or key combination, a predetermined event is to be triggered, e.g. B. the change to a certain view.

What is WebAssembly?

WebAssembly is intended as a supplement to Javascript in the web browser. It’s a byte code that is supposed to help increase the speed of web applications. However, the system still has certain weaknesses.

WebAssembly, or Wasm for short, has the task of eliminating some of the shortcomings of Javascript . This language is actually too messy and has too soft a syntax for the central role it has played in web development for quite some time. It is not sufficient for modern performance requirements.

On a small scale, the developers have with emscripten resorted. C programs are compiled and executed in a virtual machine of the browser. On the whole, however, the whole thing does not work because Javascript cannot offer the necessary performance.

WebAssembly is designed to solve exactly these problems. It is a bytecode that constitutes an alternate virtual machine that has sufficient performance.

WebAssembly simply explained

The easiest way to explain what Wasm does is with a metaphor: A hybrid vehicle has an electric motor and a petrol unit. The electric motor is the normal Javascript environment of the browser. The engine is completely sufficient for everyday tasks. However, he has performance limits. In such moments, the gasoline engine (the Wasm machine) must be switched on in order to eliminate this. The following things must be taken into account:

  • WebAssembly and the Javascript environment need to be able to communicate with each other to determine when the additional power is needed.
  • Wasm provides so-called “performance islands” and does not continuously provide additional services. In practice, the code compiled for WebAssembly is loaded and executed for this in the form of modules.
  • So that the modules can be generated, compilers are required that understand and can process the Wasm format.
  • Such compilers are available for C, C ++, Rust, Blazor and Clang, for example.
  • The compilers must ensure that handovers can take place in both directions. To stay in the picture: the petrol engine must be able to indicate when its power is no longer needed and not just receive commands.

The advantages of WebAssembly

All major browser developers (Microsoft, Google, Apple, Mozilla, Opera) are driving the development of WebAssembly as part of the “Open Web” platform. Since 2017, all popular browsers have supported the associated modules, with the exception of Internet Explorer, which has now been discontinued. The following advantages have been shown:

  • Web applications load faster.
  • The corresponding apps work more efficiently.
  • Wasm environments are safe because they run in a sandbox.
  • The bytecode is openly accessible and can be used accordingly for development and debugging.
  • In theory, it is even possible to use WebAssembly as a virtual machine outside of browsers. An interface called WASI should also make this possible in practice.

WebAssembly problems

One major problem WebAssembly struggles with is the difficulty of implementation . At the moment the Wasm modules can only be loaded in the browser by either manually programming a separate JavaScript loader or by using the “shell page” generated by Emscripts.

The latter, however, is difficult and sometimes impossible: Many of the commands in the standard library require a supporting infrastructure in runtime , which, however, is often not available. To take up the above metaphor again: The user currently still has to ensure that the electric motor explains to the gasoline engine what is required every time. Unfortunately, he does not understand some commands, which therefore have to be explained again and again (or saved using a new library that you can create yourself).

The developers definitely have a solution for this problem. script tags should help to load the modules in the future. So far, however, this approach has not had any noticeable success.

What is WebAssembly

Comments are closed.