{"size": 1682, "ext": "js", "max_stars_repo_path": "js/registry.js", "max_stars_repo_name": "sudheerapte/popcornserver", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "js/registry.js", "max_issues_repo_name": "sudheerapte/popcornserver", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "js/registry.js", "max_forks_repo_name": "sudheerapte/popcornserver", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 24.0285714286, "max_line_length": 66, "alphanum_fraction": 0.6409036861}
/**
   Copyright 2019 Sudheer Apte
*/

"use strict";

/**
  registry.js - register and maintain machines. Singleton.

  Usage: const registry = require('./registry.js');

  Each machine has a record:
  {
    name: string, the alphanumeric name of the machine.
    dir: string, the absolute filesystem path to the assets.
  }

  The dir for each machine is set by the admin.

  The records are indexed in the registry by machine name.

  Events:
    newmachine - somebody has registered a new machine.
      name - alphanumeric name of the machine
      dir - absolute path to assets directory

    lostmachine - an app has deregistered a machine.
      name - alphanumeric name of the machine.

  Methods:

    getAllMachines() - returns iterator for machine names
    getMachineDir(m) - returns path to assets directory, or null
    addMachine(m,d) -  add a new machine with indicated directory.
                       Returns true on success, else false.
    removeMachine(m) - Returns true on success, else false.
*/

const EventEmitter = require('events');

class Registry extends EventEmitter {
  constructor() {
    super();
    this._map = new Map();
  }
  getAllMachines() { return this._map.keys(); }
  getMachineDir(m) {
    return this._map.get(m);
  }
  addMachine(m, d) {
    if (m && d && ! this._map.has(m)) {
      this._map.set(m, d);
      this.emit('newmachine', m, d);
      return true;
    } else {
      console.log(`addMachine(${m}, ${d}) failed`);
      return false;
    }
  }
  removeMachine(m) {
    const result = this._map.delete(m);
    if (result) {
      this.emit('lostmachine', m);
    }
    return result;
  }
}

module.exports = new Registry();
