From d997c74d7057f26038432d38ec544d3796db6727 Mon Sep 17 00:00:00 2001 From: Sami Samhuri Date: Mon, 8 Nov 2010 23:33:21 -0800 Subject: [PATCH] support multiple simultaneous requests, version bump --- lib/index.js | 118 ++++++++++++++++++++++++++++----------------------- 1 file changed, 64 insertions(+), 54 deletions(-) diff --git a/lib/index.js b/lib/index.js index 53d3222..b5a8fc4 100644 --- a/lib/index.js +++ b/lib/index.js @@ -11,7 +11,8 @@ (function() { var global = this , isBrowser = 'document' in global - , request = isBrowser ? xhr : require('request') + // when running in the browser request is set later + , request = isBrowser ? null : require('request') , Blob, Branch, Commit, Raw, Repo, Tree, User , api @@ -316,6 +317,7 @@ Object.defineProperty(obj, prop, opts) } + // util.inherits from node function inherits(ctor, superCtor) { ctor.super_ = superCtor ctor.prototype = Object.create(superCtor.prototype, { @@ -325,48 +327,6 @@ } }) } - - function xhr(options, cb) { - GITR._jsonpCallback = function(obj) { - cb(null, null, obj) - } - ;(function (global, oDOC, handler) { - options.uri += '?callback=GITR._jsonpCallback' - var head = oDOC.head || oDOC.getElementsByTagName("head") - - // loading code borrowed directly from LABjs itself - setTimeout(function () { - if ("item" in head) { // check if ref is still a live node list - if (!head[0]) { // append_to node not yet ready - setTimeout(arguments.callee, 25) - return - } - head = head[0]; // reassign from live node list ref to pure node ref -- avoids nasty IE bug where changes to DOM invalidate live node lists - } - var scriptElem = oDOC.createElement("script"), - scriptdone = false - scriptElem.onload = scriptElem.onreadystatechange = function () { - if ((scriptElem.readyState && scriptElem.readyState !== "complete" && scriptElem.readyState !== "loaded") || scriptdone) { - return false - } - scriptElem.onload = scriptElem.onreadystatechange = null - scriptdone = true - }; - scriptElem.src = options.uri - head.insertBefore(scriptElem, head.firstChild) - }, 0) - - // required: shim for FF <= 3.5 not having document.readyState - if (oDOC.readyState == null && oDOC.addEventListener) { - oDOC.readyState = "loading" - oDOC.addEventListener("DOMContentLoaded", handler = function () { - oDOC.removeEventListener("DOMContentLoaded", handler, false) - oDOC.readyState = "complete" - }, false) - } - })(window, document) - } - // get an only property, if any function onlyProp(obj) { if (obj && typeof obj === 'object') { @@ -386,19 +346,32 @@ function titleCaseFirst(s) { return s.charAt(0).toUpperCase() + s.slice(1) } - if (isBrowser) { - function update(array, args) { - var arrayLength = array.length, length = args.length; - while (length--) array[arrayLength + length] = args[length]; - return array; + + // Browser Utilities // + + if (isBrowser) (function() { + var update, merge, load, _jsonpCounter = 1 + request = function(options, cb) { // jsonp request + var jsonpCallbackName = '_jsonpCallback' + _jsonpCounter++ + , url = options.uri + '?callback=GITR.' + jsonpCallbackName + GITR[jsonpCallbackName] = function(obj) { + cb(null, null, obj) + setTimeout(function() { delete GITR[jsonpCallbackName] }, 0) + } + load(url) } - function merge(array, args) { - array = [].slice.call(array, 0); - return update(array, args); - } - + // bind from Prototype (for Safari 5) if (!Function.prototype.bind) { + update = function(array, args) { + var arrayLength = array.length, length = args.length + while (length--) array[arrayLength + length] = args[length] + return array + } + merge = function(array, args) { + array = [].slice.call(array, 0) + return update(array, args) + } Function.prototype.bind = function(context) { if (arguments.length < 2 && typeof arguments[0] === 'undefined') return this var __method = this, args = [].slice.call(arguments, 1) @@ -408,5 +381,42 @@ } } } - } + // bootstrap loader from LABjs + load = function(url) { + var oDOC = document + , handler + , head = oDOC.head || oDOC.getElementsByTagName("head") + + // loading code borrowed directly from LABjs itself + setTimeout(function () { + if ("item" in head) { // check if ref is still a live node list + if (!head[0]) { // append_to node not yet ready + setTimeout(arguments.callee, 25) + return + } + head = head[0]; // reassign from live node list ref to pure node ref -- avoids nasty IE bug where changes to DOM invalidate live node lists + } + var scriptElem = oDOC.createElement("script"), + scriptdone = false + scriptElem.onload = scriptElem.onreadystatechange = function () { + if ((scriptElem.readyState && scriptElem.readyState !== "complete" && scriptElem.readyState !== "loaded") || scriptdone) { + return false + } + scriptElem.onload = scriptElem.onreadystatechange = null + scriptdone = true + }; + scriptElem.src = url + head.insertBefore(scriptElem, head.firstChild) + }, 0) + + // required: shim for FF <= 3.5 not having document.readyState + if (oDOC.readyState == null && oDOC.addEventListener) { + oDOC.readyState = "loading" + oDOC.addEventListener("DOMContentLoaded", handler = function () { + oDOC.removeEventListener("DOMContentLoaded", handler, false) + oDOC.readyState = "complete" + }, false) + } + } + }()) }()) \ No newline at end of file