{"size": 1495, "ext": "js", "max_stars_repo_path": "plugins/redisBeacon.js", "max_stars_repo_name": "winnipegcompsci/futuresapp", "max_stars_repo_stars_event_min_datetime": "2021-02-20T04:59:44.000Z", "max_stars_repo_stars_event_max_datetime": "2021-03-09T20:03:54.000Z", "max_issues_repo_path": "plugins/redisBeacon.js", "max_issues_repo_name": "winnipegcompsci/futuresapp", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "plugins/redisBeacon.js", "max_forks_repo_name": "winnipegcompsci/futuresapp", "max_forks_repo_forks_event_min_datetime": "2021-02-20T04:59:42.000Z", "max_forks_repo_forks_event_max_datetime": "2021-02-20T04:59:42.000Z"}
{"max_stars_count": 4, "max_issues_count": null, "max_forks_count": 1, "avg_line_length": 23.359375, "max_line_length": 81, "alphanum_fraction": 0.6822742475}
var log = require('../core/log.js');
var util = require('../core/util');
var config = util.getConfig();
var redisBeacon = config.redisBeacon;
var watch = config.watch;

var subscriptions = require('../subscriptions');
var _ = require('lodash');

var redis = require("redis");

var Actor = function(done) {
  _.bindAll(this);

  this.market = [
    watch.exchange,
    watch.currency,
    watch.asset
  ].join('-');

  this.init(done);
}

// This actor is dynamically build based on
// what the config specifies it should emit.
// 
// This way we limit overhead because Gekko
// only binds to events redis is going to
// emit.

var proto = {};
_.each(redisBeacon.broadcast, function(e) {
  // grab the corresponding subscription 
  var subscription = _.find(subscriptions, function(s) { return s.event === e });

  if(!subscription)
    util.die('Gekko does not know this event:' + e);

  var channel = redisBeacon.channelPrefix + subscription.event

  proto[subscription.handler] = function(message) {
    this.emit(channel, {
      market: this.market,
      data: message
    });
  };

}, this)

Actor.prototype = proto;

Actor.prototype.init = function(done) {
  this.client = redis.createClient(redisBeacon.port, redisBeacon.host);
  this.client.on('ready', _.once(done));
}

Actor.prototype.emit = function(channel, message) {
  log.debug('Going to publish to redis channel:', channel);

  var data = JSON.stringify(message);
  this.client.publish(channel, data);
}

module.exports = Actor;