2.0.0 • Published 6 years ago

@lgslabs/bits-auto-discovery v2.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
6 years ago

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();
    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, topic: 'numbers'});
  }

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

How to use a LocalResource

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

class ModuleApp {
  constructor() {
    this._resource = new LocalResource();
    this._interval = null;
  }

  load(messageCenter) {
    return Promise.resolve()
      .then(() => this._resource.load({messageCenter, topic: 'numbers', value: -1}))
      .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);
  }
}
2.0.0

6 years ago

1.0.0

6 years ago