README
applyq
A JavaScript library for applying asynchronous calls to an API.
Installation
npm install --save applyq
How the Asynchronous Syntax Works
Lets say there is a JavaScript object on the page but it is unknown where and when it will be loaded. However, you would like to have the possibility to interact with it at any place on the page. The asynchronous syntax makes it possible to call the API before its loaded.
The asynchronous syntax uses a queue, which is a first-in, first-out
data structure
that collects API calls until the target object is ready to execute them.
To push an API call onto the queue, you must convert it from the traditional
JavaScript syntax into a command array
. Command arrays are simply JavaScript
arrays that conform to a certain format. The first element in a command array is
the name of the object method you want to call. It must be a string. The rest of
the elements are the arguments you want to pass to the object method. These can
be any JavaScript value.
The following code calls logger.log
using the traditional syntax:
logger.log('Hello world!');
The equivalent code in the asynchronous syntax:
window._loggerq = window._loggerq || [];
_loggerq.push(['log', 'Hello world!']);
Usage
Once the object is loaded and initialized, it has to execute the queued command
arrays. This can be done by calling applyq
:
var logger = new Logger();
applyq(logger, _loggerq);
After the object had been initialized, there is no need to store the commands
anymore. The commands can be executed right away. That's why after calling applyq
the push
method of the commands queue is overridden. The overridden version of
push is executing the command arrays immediately instead of storing them.
Whitelist of methods
You might want to process a subset of commands only. In that case you can path the list of commands to process:
/* In this case only commands passed to the register() method will be processed */
applyq(server, _serverq, ['register']);
P.S.
Some of the explanations of the Asynchronous Syntax were taken from Google Analytics Tracking Basics (Asynchronous Syntax).
License
MIT