Skip to content
This repository was archived by the owner on Dec 31, 2022. It is now read-only.

Commit 30cadd2

Browse files
committed
more refactoring work
1 parent c3c3de8 commit 30cadd2

File tree

1 file changed

+54
-56
lines changed

1 file changed

+54
-56
lines changed

LAB.src.js

Lines changed: 54 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,8 @@
5252

5353
// **************************************
5454

55-
// test for function
56-
function is_func(func) { return Object.prototype.toString.call(func) == "[object Function]"; }
57-
58-
// test for array
59-
function is_array(arr) { return Object.prototype.toString.call(arr) == "[object Array]"; }
60-
6155
// make script URL absolute/canonical
62-
function canonical_uri(src,base_path) {
56+
function canonicalURI(src,base_path) {
6357
var absolute_regex = /^\w+\:\/\//;
6458

6559
// is `src` is protocol-relative (begins with // or ///), prepend protocol
@@ -71,15 +65,20 @@
7165
// prepend `base_path`, if any
7266
src = (base_path || "") + src;
7367
}
68+
7469
// make sure to return `src` as absolute
75-
return absolute_regex.test(src) ? src : ((src.charAt(0) == "/" ? root_domain : root_page) + src);
70+
return absolute_regex.test(src) ?
71+
src :
72+
(
73+
(src.charAt(0) == "/" ? root_domain : root_page) + src
74+
);
7675
}
7776

7877
// merge `source` into `target`
79-
function merge_objs(source,target) {
80-
for (var k in source) { if (source.hasOwnProperty(k)) {
78+
function mergeObjs(source,target) {
79+
for (var k in source) {
8180
target[k] = source[k]; // TODO: does this need to be recursive for our purposes?
82-
}}
81+
}
8382
return target;
8483
}
8584

@@ -159,7 +158,7 @@
159158
instanceAPI = {
160159
// main API functions
161160
setGlobalDefaults: function setGlobalDefaults(opts){
162-
merge_objs(opts,global_defaults);
161+
mergeObjs(opts,global_defaults);
163162
return instanceAPI;
164163
},
165164
setOptions: function setOptions(){
@@ -217,16 +216,9 @@
217216
// **************************************
218217

219218
// execute a script that has been preloaded already
220-
function execute_preloaded_script(chain_opts,script_obj,registry_item) {
219+
function executePreloadedScript(chain_opts,script_obj,registry_item) {
221220
var script;
222221

223-
function preload_execute_finished() {
224-
if (script != null) { // make sure this only ever fires once
225-
script = null;
226-
script_executed(registry_item);
227-
}
228-
}
229-
230222
if (registry[script_obj.src].finished) return;
231223
if (!chain_opts[_AllowDuplicates]) registry[script_obj.src].finished = true;
232224

@@ -235,51 +227,46 @@
235227
if (script_obj.charset) script.charset = script_obj.charset;
236228
create_script_load_listener(script,registry_item,"finished",preload_execute_finished);
237229

238-
// script elem was real-preloaded
239-
if (registry_item.elem) {
240-
registry_item.elem = null;
241-
}
242-
// script was XHR preloaded
243-
else if (registry_item.text) {
244-
script.onload = script.onreadystatechange = null; // script injection doesn't fire these events
245-
script.text = registry_item.text;
246-
}
247-
// script was cache-preloaded
248-
else {
249-
script.src = script_obj.real_src;
250-
}
230+
script.src = script_obj.real_src;
231+
251232
append_to.insertBefore(script,append_to.firstChild);
252233

253-
// manually fire execution callback for injected scripts, since events don't fire
254-
if (registry_item.text) {
255-
preload_execute_finished();
234+
// **************************************
235+
236+
function preload_execute_finished() {
237+
if (script != null) { // make sure this only ever fires once
238+
script = null;
239+
script_executed(registry_item);
240+
}
256241
}
257242
}
258243

259244
// process the script request setup
260-
function do_script(chain_opts,script_obj,chain_group,preload_this_script) {
261-
var registry_item,
262-
registry_items,
263-
ready_cb = function(){ script_obj.ready_cb(script_obj,function(){ execute_preloaded_script(chain_opts,script_obj,registry_item); }); },
264-
finished_cb = function(){ script_obj.finished_cb(script_obj,chain_group); }
265-
;
245+
function setupScript(chain_opts,script_obj,chain_group,preload_this_script) {
246+
var registry_item;
247+
var registry_items;
266248

267-
script_obj.src = canonical_uri(script_obj.src,chain_opts[_BasePath]);
249+
script_obj.src = canonicalURI(script_obj.src,chain_opts[_BasePath]);
268250
script_obj.real_src = script_obj.src +
269251
// append cache-bust param to URL?
270252
(chain_opts[_CacheBust] ? ((/\?.*$/.test(script_obj.src) ? "&_" : "?_") + ~~(Math.random()*1E9) + "=") : "")
271253
;
272254

273-
if (!registry[script_obj.src]) registry[script_obj.src] = {items:[],finished:false};
255+
if (!registry[script_obj.src]) {
256+
registry[script_obj.src] = {
257+
items: [],
258+
finished: false
259+
};
260+
}
274261
registry_items = registry[script_obj.src].items;
275262

276263
// allowing duplicates, or is this the first recorded load of this script?
277264
if (chain_opts[_AllowDuplicates] || registry_items.length == 0) {
278265
registry_item = registry_items[registry_items.length] = {
279-
ready:false,
280-
finished:false,
281-
ready_listeners:[ready_cb],
282-
finished_listeners:[finished_cb]
266+
ready: false,
267+
finished: false,
268+
ready_listeners: [ready_cb],
269+
finished_listeners: [finished_cb]
283270
};
284271

285272
requestScript(chain_opts,script_obj,registry_item,
@@ -308,12 +295,23 @@
308295
registry_item.finished_listeners.push(finished_cb);
309296
}
310297
}
298+
299+
300+
function ready_cb() {
301+
script_obj.ready_cb(script_obj,function done(){
302+
executePreloadedScript(chain_opts,script_obj,registry_item);
303+
});
304+
}
305+
306+
function finished_cb() {
307+
script_obj.finished_cb(script_obj,chain_group);
308+
}
311309
}
312310

313311
// creates a closure for each separate chain spawned from this $LAB instance, to keep state cleanly separated between chains
314312
function createChainInstance() {
315313
var chainedAPI,
316-
chain_opts = merge_objs(global_defaults,{}),
314+
chain_opts = mergeObjs(global_defaults,{}),
317315
chain = [],
318316
exec_cursor = 0,
319317
scripts_currently_loading = false,
@@ -331,7 +329,7 @@
331329
script: chainedAPI.script,
332330
wait: chainedAPI.wait,
333331
setOptions: function setOptions(opts){
334-
merge_objs(opts,chain_opts);
332+
mergeObjs(opts,chain_opts);
335333
return chainedAPI;
336334
}
337335
};
@@ -364,7 +362,7 @@
364362
// main driver for executing each part of the chain
365363
function advance_exec_cursor() {
366364
while (exec_cursor < chain.length) {
367-
if (is_func(chain[exec_cursor])) {
365+
if (typeof chain[exec_cursor] == "function") {
368366
/*!START_DEBUG*/if (chain_opts[_Debug]) log_msg("$LAB.wait() executing: "+chain[exec_cursor]);/*!END_DEBUG*/
369367
try { chain[exec_cursor++](); } catch (err) {
370368
/*!START_DEBUG*/if (chain_opts[_Debug]) log_error("$LAB.wait() error caught: ",err);/*!END_DEBUG*/
@@ -397,16 +395,16 @@
397395
(function(script_obj,script_list){
398396
var splice_args;
399397

400-
if (!is_array(script_obj)) {
398+
if (!Array.isArray(script_obj)) {
401399
script_list = [script_obj];
402400
}
403401
for (var j=0; j<script_list.length; j++) {
404402
init_script_chain_group();
405403
script_obj = script_list[j];
406404

407-
if (is_func(script_obj)) script_obj = script_obj();
405+
if (typeof script_obj == "function") script_obj = script_obj();
408406
if (!script_obj) continue;
409-
if (is_array(script_obj)) {
407+
if (Array.isArray(script_obj)) {
410408
// set up an array of arguments to pass to splice()
411409
splice_args = [].slice.call(script_obj); // first include the actual array elements we want to splice in
412410
splice_args.unshift(j,1); // next, put the `index` and `howMany` parameters onto the beginning of the splice-arguments array
@@ -415,7 +413,7 @@
415413
continue;
416414
}
417415
if (typeof script_obj == "string") script_obj = {src:script_obj};
418-
script_obj = merge_objs(script_obj,{
416+
script_obj = mergeObjs(script_obj,{
419417
ready:false,
420418
ready_cb:chain_script_ready,
421419
finished:false,
@@ -424,7 +422,7 @@
424422
group.finished = false;
425423
group.scripts.push(script_obj);
426424

427-
do_script(chain_opts,script_obj,group,(can_use_preloading && scripts_currently_loading));
425+
setupScript(chain_opts,script_obj,group,(can_use_preloading && scripts_currently_loading));
428426
scripts_currently_loading = true;
429427

430428
if (chain_opts[_AlwaysPreserveOrder]) chainedAPI.wait();

0 commit comments

Comments
 (0)