~launchpad-pqm/lazr-js/toolchain

187.1.4 by Sidnei da Silva
- Fix style of the copyright notice
1
/* Copyright (c) 2010, Canonical Ltd. All rights reserved. */
187.1.1 by Sidnei da Silva
- Add prefetch extension from Landscape.
2
3
/**
4
 * Prefetch a set of files, deferring calls to 'use' until the
5
 * preloaded files are finished loading.
6
 *
7
 * @public
8
 */
9
10
YUI.prototype.prefetch = function prefetch() {
11
    var Y = this,
12
        deferred = [],
13
        preload = arguments,
14
        pending = arguments.length;
15
187.1.3 by Sidnei da Silva
- Minify even more
16
    var YArray_each = Y.Array.each,
187.1.1 by Sidnei da Silva
- Add prefetch extension from Landscape.
17
        YGet_script = Y.Get.script;
18
187.1.2 by Sidnei da Silva
- Fix indentation.
19
    /**
20
     * Wrap the native 'use' function to add some smarts around
21
     * our custom loading of the rolled-up minified files so that
22
     * we delay the loader from firing until the rolled-up files
23
     * are finished loading, in order to avoid loading the
24
     * modules twice.
25
     */
26
    var native_use = Y.use;
27
28
    /* Now replace the original 'use' function with our own. */
29
    Y.use = function use() {
187.1.1 by Sidnei da Silva
- Add prefetch extension from Landscape.
30
        /**
187.1.2 by Sidnei da Silva
- Fix indentation.
31
         * If all external dependencies have been loaded, just
32
         * call the native 'use' function directly.
187.1.1 by Sidnei da Silva
- Add prefetch extension from Landscape.
33
         */
187.1.2 by Sidnei da Silva
- Fix indentation.
34
        if (!pending) {
35
            native_use.apply(Y, arguments);
36
        } else {
37
            /**
38
             * If there are things still loading, queue calls to 'use'
39
             *  until they are finished.
40
             */
41
            var ridx = arguments.length,
42
            args = [];
43
            /* Make a copy of the original arguments. */
44
            while (--ridx >= 0) {
45
                args[ridx] = arguments[ridx];
46
            }
47
48
            /* Push copied arguments into the queue. */
49
            deferred.push(args);
50
        }
51
    };
52
53
    /**
54
     * For each item to be preloaded, use the Y.Get utility to
55
     * fetch the script (which might fetch them in parallel). When
56
     * all the scripts are finished loading, we'll process the
57
     * deferred calls to use with the native 'use' function.
58
     */
187.1.3 by Sidnei da Silva
- Minify even more
59
    YArray_each(preload, function(value) {
187.1.2 by Sidnei da Silva
- Fix indentation.
60
        YGet_script(value, {onEnd: function() {
61
            /**
62
             * Once an item has finished preloading, we decrement
63
             * the pending variable. Once it reaches zero, we
64
             * know all preload items have finished loading.
65
             */
66
            pending--;
67
68
            /**
69
             * Once we're done, restore the original 'use'
70
             * function and call all of the deferred callbacks in
71
             * their original order.
187.1.1 by Sidnei da Silva
- Add prefetch extension from Landscape.
72
             */
73
            if (!pending) {
187.1.2 by Sidnei da Silva
- Fix indentation.
74
                Y.use = native_use;
75
187.1.1 by Sidnei da Silva
- Add prefetch extension from Landscape.
76
                /**
187.1.2 by Sidnei da Silva
- Fix indentation.
77
                 * Attach the 'loader' module, which *should* be
78
                 * already loaded by now.
187.1.1 by Sidnei da Silva
- Add prefetch extension from Landscape.
79
                 */
187.1.2 by Sidnei da Silva
- Fix indentation.
80
                Y._attach(["loader"]);
187.1.3 by Sidnei da Silva
- Minify even more
81
                YArray_each(deferred, function(value) {
187.1.2 by Sidnei da Silva
- Fix indentation.
82
                    native_use.apply(this, value);
83
                });
187.1.1 by Sidnei da Silva
- Add prefetch extension from Landscape.
84
            }
187.1.2 by Sidnei da Silva
- Fix indentation.
85
        }, attributes: {defer: "defer"}});
86
    });
187.1.1 by Sidnei da Silva
- Add prefetch extension from Landscape.
87
};