mirror of
https://github.com/samsonjs/gitter.git
synced 2026-04-27 15:07:41 +00:00
support multiple simultaneous requests, version bump
This commit is contained in:
parent
c83ab50b3c
commit
d997c74d70
1 changed files with 64 additions and 54 deletions
118
lib/index.js
118
lib/index.js
|
|
@ -11,7 +11,8 @@
|
||||||
(function() {
|
(function() {
|
||||||
var global = this
|
var global = this
|
||||||
, isBrowser = 'document' in global
|
, 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
|
, Blob, Branch, Commit, Raw, Repo, Tree, User
|
||||||
, api
|
, api
|
||||||
|
|
||||||
|
|
@ -316,6 +317,7 @@
|
||||||
Object.defineProperty(obj, prop, opts)
|
Object.defineProperty(obj, prop, opts)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// util.inherits from node
|
||||||
function inherits(ctor, superCtor) {
|
function inherits(ctor, superCtor) {
|
||||||
ctor.super_ = superCtor
|
ctor.super_ = superCtor
|
||||||
ctor.prototype = Object.create(superCtor.prototype, {
|
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
|
// get an only property, if any
|
||||||
function onlyProp(obj) {
|
function onlyProp(obj) {
|
||||||
if (obj && typeof obj === 'object') {
|
if (obj && typeof obj === 'object') {
|
||||||
|
|
@ -386,19 +346,32 @@
|
||||||
|
|
||||||
function titleCaseFirst(s) { return s.charAt(0).toUpperCase() + s.slice(1) }
|
function titleCaseFirst(s) { return s.charAt(0).toUpperCase() + s.slice(1) }
|
||||||
|
|
||||||
if (isBrowser) {
|
|
||||||
function update(array, args) {
|
// Browser Utilities //
|
||||||
var arrayLength = array.length, length = args.length;
|
|
||||||
while (length--) array[arrayLength + length] = args[length];
|
if (isBrowser) (function() {
|
||||||
return array;
|
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) {
|
// bind from Prototype (for Safari 5)
|
||||||
array = [].slice.call(array, 0);
|
|
||||||
return update(array, args);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!Function.prototype.bind) {
|
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) {
|
Function.prototype.bind = function(context) {
|
||||||
if (arguments.length < 2 && typeof arguments[0] === 'undefined') return this
|
if (arguments.length < 2 && typeof arguments[0] === 'undefined') return this
|
||||||
var __method = this, args = [].slice.call(arguments, 1)
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}())
|
||||||
}())
|
}())
|
||||||
Loading…
Reference in a new issue