UNPKG

scientist

Version:

Carefully refactor critical paths in production

38 lines (27 loc) 1.23 kB
var EventEmitter, Experiment, Scientist, _, extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }, hasProp = {}.hasOwnProperty; _ = require('underscore'); EventEmitter = require('events').EventEmitter; Experiment = require('./experiment'); Scientist = (function(superClass) { extend(Scientist, superClass); function Scientist() { this._sampler = _.constant(true); } Scientist.prototype.sample = function(sampler) { return this._sampler = sampler; }; Scientist.prototype.science = function(name, setup) { var experiment; experiment = new Experiment(name); setup(experiment); this.emit('experiment', experiment); experiment.on('skip', EventEmitter.prototype.emit.bind(this, 'skip')); experiment.on('result', EventEmitter.prototype.emit.bind(this, 'result')); experiment.on('error', EventEmitter.prototype.emit.bind(this, 'error')); return experiment.run(this._sampler); }; return Scientist; })(EventEmitter); module.exports = Scientist;