Miss any of our Open RFC calls?Watch the recordings here! »

esrol-initializer

0.1.1 • Public • Published

NPM version Build Status Test coverage

esrol-initializer

An Initializer Class used for adding and initializing components. Priority must exist between them.

Part of Esrol

Installation

$ npm install --save esrol-initializer

Usage

'use strict';
let Initializer = require('../lib/initializer');
let initializer = new Initializer();
 
let foo = {
  priority: 1,
  component: function() {
    console.log('foo component is resolved');
  }
};
 
let promise = new Promise((resolve, reject) => {
  // some async stuff
  setTimeout(() => {
    resolve();
  }, 500);
});
 
promise.then(() => {
  console.log('promisified component is resolved');
});
 
let promisifiedComponent = {
  priority: 2,
  component: function() {
    console.log('promisified component is instantiated');
    return promise;
  }
};
 
let bar = {
  priority: 3,
  component: function() {
    console.log('bar component is resolved');
  }
};
 
let onResolvedComponents = function() {
  console.log('all components are resolved');
};
 
initializer.setCallback(onResolvedComponents);
initializer.registerComponent(promisifiedComponent);
initializer.registerComponent(foo);
initializer.registerComponent(bar);
initializer.instantiateComponents();

Methods

setCallback(fn)boolean

Set a callback which will be called when all components are resolved.

registerComponent(component)boolean

Register a new component object. Re-sort (re-prioritize) components with the new one.

instantiateComponents()

Instantiate all components.

## setCallback(fn) ⇒ boolean Set a callback which will be called when all components are resolved.

Returns: boolean - true - on success Throws:

  • error error - if parameter is not a function
Param Type Description
fn function the callback function that needs to be executed when all components are resolved

registerComponent(component) ⇒ boolean

Register a new component object. Re-sort (re-prioritize) components with the new one.

Returns: boolean - true - on success Throws:

  • error error - if component object isn't as expected
Param Type Description
component object holds two properties - { priority: 1, component: function() {} }

instantiateComponents()

Instantiate all components.

Contriubtion

Any contribution will be highly appreciated. Just make sure that:

  1. Your code works.
  2. You have 100% successful tests coverage.
  3. You have comments in your code.
  4. Follows eslint config. Exceptions are possible where that make sense.

Tests

To run the test suite, first install the dependencies, then run npm test:

$ npm install
$ npm test

License

MIT

Install

npm i esrol-initializer

DownloadsWeekly Downloads

2

Version

0.1.1

License

MIT

Last publish

Collaborators

  • avatar