Collects functions that should be called in the future
JavaScript
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
docs
src
.editorconfig
.eslintignore
.eslintrc
.gitattributes
.gitignore
LICENSE
LICENSE-BSD
LICENSE-GPL
LICENSE-MIT
README.md
package.json

README.md

CollectCalls javascript library.

Donate $5 now Donate $25 now Donate $100 now More options

Collects function () { } blocks that should be called in the future. The script collectcalls.joelpurra.js does not need to load until the future is nigh.

While executing an inline script, it might have pending dependencies that prevents it from completing right away. CollectCalls can collect functions and call them at a later time, when the time is right.

Use CollectCalls when...

The dynamic script tags might be generated by templates, page parts, included files or scoped functions. They might contain logic based on data from the model/database that will be used by other scripts, when they have loaded.

Get it

To include dependencies, make sure to get the submodules too.

git clone --recursive git://github.com/joelpurra/collectcalls.git

Demos

Basic example with jQuery

jQuery is loaded at the bottom of the page for efficiency, but <script> tags are generated by page parts. Use CollectCalls(...) to let the <script> tags enqueue functionality until jQuery has loaded, then execute it with .join().

<!-- Code that is written to the HTML now, but needs to be executed later -->
<!-- This can be anywhere on the page, even after CollectCalls(...), and multiple times -->
<script>
    // Set up, or reuse, a queue built as an array object
    var runWithJQuery = runWithJQuery || [];

    runWithJQuery.push(function() {
        // Do some heavy lifting with jQuery
        var scriptCount = $("script").length;

        // Then show alert box - it won't block the page load, since it's already done
        var msg = "jQuery has loaded, the page is ready and the queue is invoked." + "\n\n"
            + "Found " + scriptCount + " script tags on the page, using jQuery " + $.fn.jquery + ".";
        alert(msg);
    });
</script>

<!-- Load jQuery (and plugins) at the end of the page for efficiency -->
<script src="https://code.jquery.com/jquery-latest.js"></script>

<!-- Load collectcalls.joelpurra.js at any point before calling JoelPurra.CollectCalls(...) -->
<script src="https://raw.github.com/joelpurra/collectcalls/master/src/collectcalls.joelpurra.js"></script>

<script>
    // This code won't be executed until jQuery has been loaded.
    $(function ()
    {
        // Wrap the queue with CollectCalls
        runWithJQuery = new JoelPurra.CollectCalls(runWithJQuery);

        // Invoke the entire queue in the order the functions were added
        runWithJQuery.join();
    });
</script>

Usage

CollectCalls was inspired by the command queue in Google Analytic's Asynchronous Syntax.

// Set up, or reuse, a queue built as an array object.
// This can be done as many times you like, even after CollectCalls(...).
var collectCallQueue = collectCallQueue || [];

// Push any function.
// This can be done as many times you like, even after CollectCalls(...).
collectCallQueue.push(fnc);

// When the time is right, wrap the queue with CollectCalls(...).
// This can only be done once per queue.
collectCallQueue = new JoelPurra.CollectCalls(collectCallQueue);

// Then invoke the entire queue in the order the functions were added.
// This is only necessary to do once per queue.
collectCallQueue.join();

// Pass true as the second argument, and the queue will be
// invoked right away without calling .join()
// This can only be done once per queue.
collectCallQueue = new JoelPurra.CollectCalls(collectCallQueue, true);

Dependencies

CollectCalls does not have any dependencies. (jQuery is only used in the examples.)

Browser compatibility

Should be compatible with all javascript-enabled browsers. You are encouraged to run the CollectCalls test suite and then report any issues.


CollectCalls copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, Joel Purra. All rights reserved. When using CollectCalls, comply to at least one of the three available licenses: BSD, MIT, GPL. Please see the LICENSE file for details. Your donations are appreciated!