Merge lp:~sidnei/lazr-js/prefetch into lp:lazr-js

Proposed by Sidnei da Silva
Status: Merged
Approved by: Māris Fogels
Approved revision: 188
Merged at revision: 188
Proposed branch: lp:~sidnei/lazr-js/prefetch
Merge into: lp:lazr-js
Diff against target: 92 lines (+87/-0)
1 file modified
src-js/lazrjs/loader/prefetch.js (+87/-0)
To merge this branch: bzr merge lp:~sidnei/lazr-js/prefetch
Reviewer Review Type Date Requested Status
Māris Fogels (community) Approve
Review via email: mp+36710@code.launchpad.net

Description of the change

This is a copy of the refactored prefetch code from Landscape. It can be used to defer calls to use() until one or more combo files containing YUI modules are loaded (in parallel!) so that the YUI loader doesn't try to re-fetch them.

To post a comment you must log in.
Revision history for this message
Māris Fogels (mars) wrote :

Hi Sidnei,

This branch looks good. I think you have an indentation error on line 21, and another error on line 24. Instead of this:

15 +YUI.prototype.prefetch = function prefetch() {
16 + var Y = this,
17 + deferred = [],
18 + preload = arguments,
19 + pending = arguments.length;
20 +
21 + var YArray = Y.Array,
22 + YGet_script = Y.Get.script;
23 +
24 + /**

It should be this:

15 +YUI.prototype.prefetch = function prefetch() {
16 + var Y = this,
17 + deferred = [],
18 + preload = arguments,
19 + pending = arguments.length;
20 +
21 + var YArray = Y.Array, <== Indented
22 + YGet_script = Y.Get.script;
23 +
24 + /** <== Indented

Besides that, this code looks good. r=mars

review: Approve
lp:~sidnei/lazr-js/prefetch updated
189. By Sidnei da Silva

- Fix indentation.

190. By Sidnei da Silva

- Minify even more

191. By Sidnei da Silva

- Fix style of the copyright notice

Preview Diff

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

Subscribers

People subscribed via source and target branches