forked from maxkueng/node-trickle
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adds browser compatibility, avoid global namespace pollution in browsers
- add trickle to window namespace if module is not defined - this should make trickle usable in browsers
- Loading branch information
1 parent
45b3a0b
commit 27b990e
Showing
1 changed file
with
67 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,67 @@ | ||
var VERSION = '0.0.1'; | ||
|
||
var Trickle = function (interval, callback) { | ||
this._interval = interval; | ||
this._requests = []; | ||
this._lastRun = 0; | ||
this._callback = callback; | ||
}; | ||
|
||
Trickle.prototype = { | ||
|
||
trickle: function (callback) { | ||
var self = this; | ||
this._requests.push(callback); | ||
process.nextTick(function () { self._run(); }); | ||
}, | ||
|
||
isEmpty: function () { | ||
return this._requests.length === 0; | ||
}, | ||
|
||
_run: function () { | ||
var now = (new Date()).getTime(); | ||
|
||
if (this._isReady()) { | ||
this._requests.shift().call(); | ||
this._lastRun = now; | ||
|
||
if (!this.isEmpty()) { | ||
this._plan(); | ||
} | ||
|
||
} else if (!this._planed) { | ||
this._plan(this._lastRun + this._interval - now); | ||
} | ||
|
||
if (this.isEmpty() && this._callback) { | ||
this._callback(); | ||
} | ||
}, | ||
|
||
_plan: function (to) { | ||
var self = this; | ||
self._planed = true; | ||
setTimeout(function () { | ||
self._planed = false; self._run(); | ||
}, to || this._interval | ||
); | ||
}, | ||
|
||
_isReady: function () { | ||
return this._lastRun === null || this._lastRun + this._interval <= (new Date()).getTime(); | ||
} | ||
}; | ||
|
||
module.exports = { | ||
VERSION: VERSION, | ||
Trickle: Trickle | ||
}; | ||
;(function () { | ||
|
||
var VERSION = '0.0.1'; | ||
|
||
var Trickle = function (interval, callback) { | ||
this._interval = interval; | ||
this._requests = []; | ||
this._lastRun = 0; | ||
this._callback = callback; | ||
}; | ||
|
||
Trickle.prototype = { | ||
|
||
trickle: function (callback) { | ||
var self = this; | ||
this._requests.push(callback); | ||
process.nextTick(function () { self._run(); }); | ||
}, | ||
|
||
isEmpty: function () { | ||
return this._requests.length === 0; | ||
}, | ||
|
||
_run: function () { | ||
var now = (new Date()).getTime(); | ||
|
||
if (this._isReady()) { | ||
this._requests.shift().call(); | ||
this._lastRun = now; | ||
|
||
if (!this.isEmpty()) { | ||
this._plan(); | ||
} | ||
|
||
} else if (!this._planed) { | ||
this._plan(this._lastRun + this._interval - now); | ||
} | ||
|
||
if (this.isEmpty() && this._callback) { | ||
this._callback(); | ||
} | ||
}, | ||
|
||
_plan: function (to) { | ||
var self = this; | ||
self._planed = true; | ||
setTimeout(function () { | ||
self._planed = false; self._run(); | ||
}, to || this._interval | ||
); | ||
}, | ||
|
||
_isReady: function () { | ||
return this._lastRun === null || this._lastRun + this._interval <= (new Date()).getTime(); | ||
} | ||
}; | ||
|
||
if (typeof module != 'undefined') { | ||
module.exports = { | ||
VERSION: VERSION, | ||
Trickle: Trickle | ||
}; | ||
} else { | ||
window.Trickle = Trickle; | ||
} | ||
|
||
}).call(this); |