Commit 70041869 authored by Evgeny's avatar Evgeny

Add buffer as separate module

parent 8890cb17
......@@ -98,7 +98,7 @@ module.exports = (grunt) ->
specs: 'spec/*-spec.js'
helpers: 'spec/*-helper.js'
styles: 'c3.css'
vendor: 'bower_components/d3/d3.js'
vendor: ['bower_components/d3/d3.js', 'bower_components/async-buffer/index.js']
uglify:
c3:
......
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Jasmine Spec Runner</title>
<link rel="shortcut icon" type="image/png" href=".grunt/grunt-contrib-jasmine/jasmine_favicon.png">
<link rel="stylesheet" type="text/css" href=".grunt/grunt-contrib-jasmine/jasmine.css">
<link rel="stylesheet" type="text/css" href="c3.css">
</head>
<body>
<script src=".grunt/grunt-contrib-jasmine/es5-shim.js"></script>
<script src=".grunt/grunt-contrib-jasmine/jasmine.js"></script>
<script src=".grunt/grunt-contrib-jasmine/jasmine-html.js"></script>
<script src=".grunt/grunt-contrib-jasmine/json2.js"></script>
<script src=".grunt/grunt-contrib-jasmine/boot.js"></script>
<script src="bower_components/d3/d3.js"></script>
<script src="spec/c3-helper.js"></script>
<script src="c3.js"></script>
<script src="spec/api.chart-spec.js"></script>
<script src="spec/api.color-spec.js"></script>
<script src="spec/api.data-spec.js"></script>
<script src="spec/api.grid-spec.js"></script>
<script src="spec/api.load-spec.js"></script>
<script src="spec/api.zoom-spec.js"></script>
<script src="spec/arc-spec.js"></script>
<script src="spec/axis-spec.js"></script>
<script src="spec/c3-spec.js"></script>
<script src="spec/class-spec.js"></script>
<script src="spec/core-spec.js"></script>
<script src="spec/data-spec.js"></script>
<script src="spec/domain-spec.js"></script>
<script src="spec/grid-spec.js"></script>
<script src="spec/interaction-spec.js"></script>
<script src="spec/legend-spec.js"></script>
<script src="spec/shape.bar-spec.js"></script>
<script src="spec/shape.line-spec.js"></script>
<script src="spec/stacked-spec.js"></script>
<script src="spec/tooltip-spec.js"></script>
<script src="spec/type-spec.js"></script>
<script src="spec/zoom-spec.js"></script>
<script src=".grunt/grunt-contrib-jasmine/reporter.js"></script>
</body>
</html>
......@@ -27,6 +27,7 @@
"Gruntfile.*"
],
"dependencies": {
"d3": "3.5.15"
"d3": "3.5.15",
"async-buffer": "3.0.0"
}
}
......@@ -3,222 +3,6 @@
/*global define, module, exports, require */
/**
* Buffer class.
* @class Buffer
*/
function Buffer(){
this.waiting = false;
this.callbacks = [];
this.namedCallbacks = {};
this.lastCallbacks = [];
this.timeout = 100;
}
/**
* Starts buffer timer. After that timer fires, all changes applies to chart
* @function start
* @private
*/
Buffer.prototype.start = function(){
var self = this;
if(!self.waiting){
self.waiting = true;
setTimeout(function(){
// finish call be called manualy
if(self.waiting){
self.finish();
}
}, self.timeout);
}
};
/**
* If callback is function, calls it
* @function resolveCallbacks
* @private
* @param {Function} callback
*/
function resolveCallback(callback){
if(typeof callback === 'function'){
callback();
}
}
/**
* Resolves all callbacks in given array
* @function resolveCallbacks
* @param {Array} array of callbacks
*/
function resolveCallbacks(callbacks){
var length = callbacks.length;
for(var i = 0; i < length; i++){
var callback = callbacks[0];
resolveCallback(callback);
callbacks.shift();
}
}
/**
* Resolves all named callbacks
* @function resolveNamedCallbacks
* @private
*/
Buffer.prototype.resolveNamedCallbacks = function(){
var self = this;
for(var id in self.namedCallbacks){
var namedCallback = self.namedCallbacks[id];
self.namedCallbacks[id] = undefined;
resolveCallback(namedCallback);
}
};
/**
* Resolves all main callbacks
* @function resolveMainCallbacks
* @private
*/
Buffer.prototype.resolveMainCallbacks = function(){
var self = this;
resolveCallbacks(self.callbacks);
};
/**
* Resolves all callbacks that should be resolved last
* @function resolveLastCallbacks
* @private
*/
Buffer.prototype.resolveLastCallbacks = function(){
var self = this;
resolveCallbacks(self.lastCallbacks);
};
/**
* Resolves all callbacks
* @function finish
* @private
*/
Buffer.prototype.finish = function(){
var self = this;
self.waiting = false;
self.resolveNamedCallbacks();
self.resolveMainCallbacks();
self.resolveLastCallbacks();
};
/**
* Resolves while buffer is not empty, but not more than 10 times.
* @function finishAll
* @returns {int} How many times 'finish' was called
*/
Buffer.prototype.finishAll = function(){
var self = this;
var counter = 0;
while(!self.isEmpty() && counter<10){
self.finish();
counter++;
}
return counter;
};
/**
* Adds callback to main queue and starts timer
* @function onFinish
* @param {Function} callback
*/
Buffer.prototype.onFinish = function(callback){
var self = this;
self.start();
self.callbacks.push(callback);
};
/**
* Adds named callback to queue and starts timer. Only last callback with given id will be called
* @function onLastFinish
* @param {String} id of callback
* @param {Function} callback
*/
Buffer.prototype.onLastFinish = function(id, callback){
var self = this;
self.start();
self.namedCallbacks[id] = callback;
};
/**
* Adds callback to queue that resolves after all other callbacks were called and starts timer
* @function afterFinish
* @param {Function} callback
*/
Buffer.prototype.afterFinish = function(callback){
var self = this;
self.start();
self.lastCallbacks.push(callback);
};
/**
* Returns whether or not there is a callback for given event to be fired
* @function has
* @param {String} id
* @return {bool}
*/
Buffer.prototype.has = function(id){
var self = this;
return isUndefined(self.namedCallbacks[id]);
};
Buffer.prototype.isEmpty = function(){
var self = this;
for(var id in self.namedCallbacks){
if(self.has(id)){
return false;
}
}
var callbacks = self.callbacks.length == 0;
var lastCallbacks = self.lastCallbacks.length == 0;
return callbacks && lastCallbacks;
};
/**
* Sets timeout after which buffer should fire
* @function setTimeout
* @param {int} timeout
*/
Buffer.prototype.setTimeout = function(timeout){
var self = this;
if(isNaN(timeout)){
throw new Error("Buffer timeout should be a number");
}
self.timeout = timeout;
};
/**
* Returns current buffer timeout
* @function getTimeout
* @returns {int} timeout
*/
Buffer.prototype.getTimeout = function(){
var self = this;
return self.timeout;
};
var __filename; // used in Node.js to get css file using path
var c3_chart_fn, c3_chart_internal_fn;
var c3 = { version: "0.4.9" };
......@@ -234,16 +18,14 @@
var $$ = this.internal = new ChartInternal(this);
$$.ed3Config = config.ed3Config;
// We have to pass some methods from ed3
if(!config.inject) {
throw new Error('You have to set config.inject');
}
if(config.inject) {
injectedMethods.forEach(function(fn){
if(typeof config.inject[fn] !== 'function'){
throw new Error('You have to set '+fn+' function in config');
}
$$[fn] = config.inject[fn];
});
}
$$.loadConfig(config);
$$.init();
......@@ -262,7 +44,10 @@
function ChartInternal(api) {
var $$ = this;
$$.d3 = window.d3 ? window.d3 : typeof require !== 'undefined' ? require("d3") : undefined;
$$.buffer = new Buffer();
var AsyncBuffer = window.AsyncBuffer ? window.AsyncBuffer : typeof require !== 'undefined' ? require("AsyncBuffer").AsyncBuffer : undefined;
$$.buffer = new AsyncBuffer();
$$.api = api;
$$.config = $$.getDefaultConfig();
$$.data = {};
......@@ -8479,7 +8264,7 @@
};
if (typeof define === 'function' && define.amd) {
define("c3", ["module", "d3"], c3init);
define("c3", ["module", "d3", "async-buffer"], c3init);
} else if ('undefined' !== typeof exports && 'undefined' !== typeof module) {
module.exports = c3;
} else {
......
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -20,7 +20,8 @@
"gitHead": "84e03109d9a590f9c8ef687c03d751f666080c6f",
"readmeFilename": "README.md",
"dependencies": {
"d3": "3.5.15"
"d3": "3.5.15",
"async-buffer": "3.0.0"
},
"devDependencies": {
"grunt": "~0.4.1",
......
/**
* Buffer class.
* @class Buffer
*/
function Buffer(){
this.waiting = false;
this.callbacks = [];
this.namedCallbacks = {};
this.lastCallbacks = [];
this.timeout = 100;
}
/**
* Starts buffer timer. After that timer fires, all changes applies to chart
* @function start
* @private
*/
Buffer.prototype.start = function(){
var self = this;
if(!self.waiting){
self.waiting = true;
setTimeout(function(){
// finish call be called manualy
if(self.waiting){
self.finish();
}
}, self.timeout);
}
};
/**
* If callback is function, calls it
* @function resolveCallbacks
* @private
* @param {Function} callback
*/
function resolveCallback(callback){
if(typeof callback === 'function'){
callback();
}
}
/**
* Resolves all callbacks in given array
* @function resolveCallbacks
* @param {Array} array of callbacks
*/
function resolveCallbacks(callbacks){
var length = callbacks.length;
for(var i = 0; i < length; i++){
var callback = callbacks[0];
resolveCallback(callback);
callbacks.shift();
}
}
/**
* Resolves all named callbacks
* @function resolveNamedCallbacks
* @private
*/
Buffer.prototype.resolveNamedCallbacks = function(){
var self = this;
for(var id in self.namedCallbacks){
var namedCallback = self.namedCallbacks[id];
self.namedCallbacks[id] = undefined;
resolveCallback(namedCallback);
}
};
/**
* Resolves all main callbacks
* @function resolveMainCallbacks
* @private
*/
Buffer.prototype.resolveMainCallbacks = function(){
var self = this;
resolveCallbacks(self.callbacks);
};
/**
* Resolves all callbacks that should be resolved last
* @function resolveLastCallbacks
* @private
*/
Buffer.prototype.resolveLastCallbacks = function(){
var self = this;
resolveCallbacks(self.lastCallbacks);
};
/**
* Resolves all callbacks
* @function finish
* @private
*/
Buffer.prototype.finish = function(){
var self = this;
self.waiting = false;
self.resolveNamedCallbacks();
self.resolveMainCallbacks();
self.resolveLastCallbacks();
};
/**
* Resolves while buffer is not empty, but not more than 10 times.
* @function finishAll
* @returns {int} How many times 'finish' was called
*/
Buffer.prototype.finishAll = function(){
var self = this;
var counter = 0;
while(!self.isEmpty() && counter<10){
self.finish();
counter++;
}
return counter;
};
/**
* Adds callback to main queue and starts timer
* @function onFinish
* @param {Function} callback
*/
Buffer.prototype.onFinish = function(callback){
var self = this;
self.start();
self.callbacks.push(callback);
};
/**
* Adds named callback to queue and starts timer. Only last callback with given id will be called
* @function onLastFinish
* @param {String} id of callback
* @param {Function} callback
*/
Buffer.prototype.onLastFinish = function(id, callback){
var self = this;
self.start();
self.namedCallbacks[id] = callback;
};
/**
* Adds callback to queue that resolves after all other callbacks were called and starts timer
* @function afterFinish
* @param {Function} callback
*/
Buffer.prototype.afterFinish = function(callback){
var self = this;
self.start();
self.lastCallbacks.push(callback);
};
/**
* Returns whether or not there is a callback for given event to be fired
* @function has
* @param {String} id
* @return {bool}
*/
Buffer.prototype.has = function(id){
var self = this;
return isUndefined(self.namedCallbacks[id]);
};
Buffer.prototype.isEmpty = function(){
var self = this;
for(var id in self.namedCallbacks){
if(self.has(id)){
return false;
}
}
var callbacks = self.callbacks.length == 0;
var lastCallbacks = self.lastCallbacks.length == 0;
return callbacks && lastCallbacks;
};
/**
* Sets timeout after which buffer should fire
* @function setTimeout
* @param {int} timeout
*/
Buffer.prototype.setTimeout = function(timeout){
var self = this;
if(isNaN(timeout)){
throw new Error("Buffer timeout should be a number");
}
self.timeout = timeout;
};
/**
* Returns current buffer timeout
* @function getTimeout
* @returns {int} timeout
*/
Buffer.prototype.getTimeout = function(){
var self = this;
return self.timeout;
};
......@@ -13,16 +13,14 @@ function Chart(config) {
var $$ = this.internal = new ChartInternal(this);
$$.ed3Config = config.ed3Config;
// We have to pass some methods from ed3
if(!config.inject) {
throw new Error('You have to set config.inject');
}
if(config.inject) {
injectedMethods.forEach(function(fn){
if(typeof config.inject[fn] !== 'function'){
throw new Error('You have to set '+fn+' function in config');
}
$$[fn] = config.inject[fn];
});
}
$$.loadConfig(config);
$$.init();
......@@ -41,7 +39,10 @@ function Chart(config) {
function ChartInternal(api) {
var $$ = this;
$$.d3 = window.d3 ? window.d3 : typeof require !== 'undefined' ? require("d3") : undefined;
$$.buffer = new Buffer();
var AsyncBuffer = window.AsyncBuffer ? window.AsyncBuffer : typeof require !== 'undefined' ? require("AsyncBuffer").AsyncBuffer : undefined;
$$.buffer = new AsyncBuffer();
$$.api = api;
$$.config = $$.getDefaultConfig();
$$.data = {};
......
if (typeof define === 'function' && define.amd) {
define("c3", ["module", "d3"], c3init);
define("c3", ["module", "d3", "async-buffer"], c3init);
} else if ('undefined' !== typeof exports && 'undefined' !== typeof module) {
module.exports = c3;
} else {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment