Supplementary

A JavaScript Library for Managing Race Conditions

While JavaScript functions are called by other functions, they are also invoked by user actions, timers and incoming server-events. However, where such calls occur in an unpredictable order and where the effect of one depends on the action of another, then race conditions can arise, yielding obscure and baffling errors.

This section presents a small library that enables the ordered management of such asynchronous calls through the use of 'EventMarshaller' objects. These act as prioritised queues, to which incoming events can be posted, and which will dispatch responses to those events subsequently and according to their priority.

Using this technology, applications can ensure that a set of actions unfold in a particular order, and only when all other pre-requisites have been satisfied. Event marshallers thereby obviate the inefficient and often complex conditional-logic that is employed conventionally to resolve the pitfalls of asynchronicity. They can also be used in conjunction with the other libraries supported on this site.

As with the AspectJS AJS-object, and those other libraries, the event marshaller library does not perform argument checking intrinsically on its various methods. Instead, an optional adjunct called EventMarshaller_DbC is provided that uses the AJS object to perform this role.

Only one call to this function is required, and such a call leaves all client code unchanged and operating normally. Argument-checking can therefore be enabled and disabled trivially, thus giving equally trivial control over the overheads it incurs. Programmers can thereby enjoy all of the benefits of a design-by-contract approach during development, yet avoid all of its costs when the system is deployed.