Ejemplo n.º 1
0
// DISTRIBUTION //

/**
* FUNCTION: Distribution( lambda, k )
*	Distribution constructor.
*
* @constructor
* @param {Number} [lambda] - shape parameter
* @param {Number} [k] - scale prameter
* @returns {Distribution} Distribution instance
*/
function Distribution( lambda, k ) {
	if ( lambda !== undefined && k !== undefined ) {
		if ( !isPositive( lambda ) ) {
			throw new TypeError( 'constructor()::invalid input argument. Shape parameter `lambda` must be a positive number. Value: `' + lambda + '`' );
		}
		if ( !isPositive( k ) ) {
			throw new TypeError( 'constructor()::invalid input argument. Scale parameter `k` must be a positive number. Value: `' + k + '`' );
		}
	}

	this._lambda = lambda || 1 ; // shape
	this._k = k || 1; // scale
	return this;
} // end FUNCTION Distribution()
Ejemplo n.º 2
0
// DELAYED //

/**
* FUNCTION: delayed( fcn, delay )
*	Returns a function that fires after the end of an event. A `delay` parameter specifies the interval between when the event was last triggered and when the end listener should be fired (i.e., a form of throttling).
*
* @param {Function} fcn - function to fire at the end of an event
* @param {Number} delay - millisecond delay to impose for function invocation
* @returns {Function} delayed function
*/
function delayed( fcn, delay ) {
	var timeout;
	if ( typeof fcn !== 'function' ) {
		throw new TypeError( 'delayed()::invalid input argument. First argument must be a function. Value: `' + fcn + '`.' );
	}
	if ( !isPositive( delay ) ) {
		throw new TypeError( 'delayed()::invalid input argument. Second argument must be a positive number. Value: `' + delay + '`.' );
	}
	return function delayed() {
		var args;
		if ( timeout ) {
			clearTimeout( timeout );
			timeout = setTimeout( later, delay );
		}
		args = arguments;

		// Set a timeout for when the function can be invoked:
		timeout = setTimeout( later, delay );

		function later() {
			timeout = null;
			fcn.apply( null, args );
		}
	};
} // end FUNCTION delayed()
Ejemplo n.º 3
0
		'set': function set( val ) {
			var err;
			if ( !isPositive( val ) ) {
				err = new TypeError( 'invalid value. `interval` must be a positive number. Value: `' + val + '`.' );
				return this.emit( 'error', err );
			}
			this._interval = val;

			// TODO: can setting an interval resume streaming?
		},
Ejemplo n.º 4
0
Distribution.prototype.k = function( value ) {
	if ( !arguments.length ) {
		return this._k;
	}
	if ( !isPositive( value ) ) {
		throw new TypeError( 'k()::invalid input argument. Scale parameter `k` must be a positive number. Value: `' + value + '`' );
	}
	this._k = value;
	return this;
}; // end METHOD k()
Ejemplo n.º 5
0
Distribution.prototype.lambda = function( value ) {
	if ( !arguments.length ) {
		return this._lambda;
	}
	if ( !isPositive( value ) ) {
		throw new TypeError( 'lambda()::invalid input argument. Shape parameter `lambda` must be a positive number. Value: `' + value + '`' );
	}
	this._lambda = value;
	return this;
}; // end METHOD lambda()
Ejemplo n.º 6
0
// GET REPOS //

/**
* FUNCTION: getRepos( token[, opts] )
*	Returns a new query instance.
*
* @param {String} token - Github access token
* @param {Object} [opts] - function options
* @param {Number} [opts.interval] - defines a poll interval (in milliseconds) for repeatedly querying an endpoint
* @returns {Query} Query instance
*/
function getRepos( token, options ) {
	var opts = {};
	if ( !isString( token ) ) {
		throw new TypeError( 'getRepos()::invalid input argument. Github personal access token must be a string primitive. Value: `' + token + '`.' );
	}
	if ( arguments.length > 1 ) {
		if ( !isObject( options ) ) {
			throw new TypeError( 'getRepos()::invalid input argument. Options argument must be an object. Value: `' + options + '`.' );
		}
		if ( options.hasOwnProperty( 'interval' ) ) {
			opts.interval = options.interval;
			if ( !isPositive( opts.interval ) ) {
				throw new TypeError( 'getRepos()::invalid option. Interval option must be a positive number. Option: `' + opts.interval + '`.' );
			}
		}
	}
	opts = merge( {}, OPTS, opts );

	// Set the user's personal access token which is used for authentication and authorization:
	opts.headers.Authorization = 'token ' + token;

	// Return a new query instance:
	return createQuery( opts );
} // end FUNCTION getRepos()
Ejemplo n.º 7
0
// STREAM //

/**
* FUNCTION: Stream( opts )
*	Readable stream constructor.
*
* @constructor
* @param {Object} opts - Readable stream options
* @param {String} opts.uri - remote resource URI
* @param {Number} [opts.interval=3600000] - defines a poll interval (in milliseconds) for repeatedly querying a remote endpoint
* @returns {Stream} Readable stream
*/
function Stream( opts ) {
	var self;
	if ( !( this instanceof Stream ) ) {
		return new Stream( opts );
	}
	self = this;

	// [0] Make the stream instance a readable stream:
	Readable.call( this, {} );
	this._destroyed = false;

	// [1] Ensure that request options are provided...
	if ( !isObject( opts ) ) {
		throw new TypeError( 'invalid input argument. Options argument must be an object. Value: `' + opts + '`.' );
	}
	// [2] Merge the provided request options into the default options...
	opts = merge( {}, opts );

	// [3] Ensure a valid `interval` option...
	if ( opts.interval ) {
		if ( !isPositive( opts.interval ) ) {
			throw new TypeError( 'invalid option. `interval` option must be a positive number. Option: `' + opts.interval + '`.' );
		}
		this._poll = true;
		this._interval = opts.interval;
		delete opts.interval;
	} else {
		this._interal = 60 * 60 * 1000; // 1hr
	}
	// [4] Ensure that the request method is always `GET`:
	opts.method = 'GET';

	// [5] Cache the request options:
	this._opts = opts;

	// [6] Expose a property for setting and getting the interval...
	Object.defineProperty( this, 'interval', {
		'set': function set( val ) {
			var err;
			if ( !isPositive( val ) ) {
				err = new TypeError( 'invalid value. `interval` must be a positive number. Value: `' + val + '`.' );
				return this.emit( 'error', err );
			}
			this._interval = val;

			// TODO: can setting an interval resume streaming?
		},
		'get': function get() {
			return this._interval;
		},
		'configurable': false,
		'enumerable': true
	});

	// [7] Expose a property for determining if any requests are pending...
	Object.defineProperty( this, 'pending', {
		'get': function get() {
			return this._pending;
		},
		'configurable': false,
		'enumerable': true
	});
	this._pending = 0;

	// [8] Initialize a request cache which is used to track pending requests:
	this._cache = {};

	// [9] Initialize a request count (used for assigning query ids):
	this._rid = 0;

	// [10] Initialize an interval id:
	this._id = null;

	// [11] Add event listeners to keep track of pending queries...
	this.on( 'init', init );
	this.on( 'response', onResponse );

	return this;

	/**
	* FUNCTION: init( evt )
	*	Event listener invoked when initiating a remote endpoint request.
	*
	* @private
	* @param {Object} evt - init event object
	*/
	function init( evt ) {
		self._cache[ evt.rid ] = true;
		self._pending += 1;
		self.emit( 'pending', self._pending );
	} // end FUNCTION init()

	/**
	* FUNCTION: onResponse( evt )
	*	Event listener invoked when a request ends.
	*
	* @private
	* @param {Object} evt - end event object
	*/
	function onResponse( evt ) {
		delete self._cache[ evt.rid ];
		self._pending -= 1;
		self.emit( 'pending', self._pending );
	} // end FUNCTION onResponse()
} // end FUNCTION Stream()