Skip to content

nanpx/node-bits-auto-discovery

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

BITS auto-discovery

A BITS library to facilitate auto discovery of resources.

How to use a ResourceManager

const {ResourceManager} = require('../..');

class ModuleApp {
  constructor() {
    this._numbersResources = new ResourceManager({topic: 'numbers'});
    this._numbersResources.on('add', (res) => {
      console.log(`New 'numbers' resource: ${res.getUuid()}`);
      res.on('value', (value) => console.log(`Resource ${res.getUuid()} changed value:`, value));
    });
  }

  load(messageCenter) {
    return this._numbersResources.load(messageCenter);
  }

  unload() {
    return this._numbersResources.unload();
  }
}

How to use a LocalResource

const {LocalResource} = require('../..');

class ModuleApp {
  constructor() {
    this._resource = new LocalResource({topic: 'numbers', value: -1});
    this._interval = null;
  }

  load(messageCenter) {
    return Promise.resolve()
      .then(() => this._resource.load(messageCenter))
      .then(() => {
        this._interval = setInterval(() => {
          const num = this._getRandomInt();
          this._resource.setValue(num);
        }, 2000);
      });
  }

  unload() {
    return Promise.resolve()
      .then(() => {
        clearInterval(this._interval);
        this._interval = null;
      })
      .then(() => this._resource.unload());
  }

  _getRandomInt({min=0, max=100}={}) {
    return Math.floor(Math.random() * (max - min) + min);
  }
}

About

A bits library to enable easy auto loading capabilities

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%