sqmicro-commons

0.0.7 • Public • Published

Общие инструменты для компонентов sqmicro: обзор

Инсталляция

npm install --save sqmicro-commons

bindEvents(target, eventTable, context, args)

Связать обработчики событий из таблицы с целью.

const { bindEvents, log } = require('sqmicro-commons');
 
const EVENT_TABLE = {
    close: function(name) { log.info(`Stream '${name}' was closed`); },
    error: function(name, error) { log.info(`Error in stream '${name}': `, error); }
};
 
// ...
 
bindEvents(stream, EVENT_TABLE, stream, ['my stream'])

log

Логирование.

const { log } = require('sqmicro-commons');
log.error('Error');
log.warn('Warning');
log.info('Information');
log.debug('Debugging the code');
log.trace('Tracing the code');
log.log('Useless gossip');

Retrier

Повторные попытки асинхронных вызовов.

const { log, Retrier } = require('sqmicro-commons');
const retrier = new Retrier();
 
retrier.retry(() => getConnectionPromise()).then(
    console.log,
    console.error
)

Throttled

Дросселирование вызовов методов дочерних классов.

const { log, Throttled } = require('sqmicro-commons');
 
class MyClass extende Throttled(Object) {
    constructor() {
        super();
        this.throttleLevel = 75;
        this.throttledMethods = ['sometimes'];
    }
 
    sometimes() {
        return 'value';
    }
}
 
const my = new My();
const max = 100000;
const actualCalls = 0;
for (let i = 0; i < max; i++) {
    let res = my.sometimes();
    if (res.isThrottled) {
        continue;
    }
    
    actualCalls++;
    if (actualCalls % 1000 === 0) {
        log.info(`Actual return value is ${res.value}`);
    }
}
 
log.info(`Only ${100 * actualCalls / max}% of calls were performed`);

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 0.0.7
    1
    • latest

Version History

Package Sidebar

Install

npm i sqmicro-commons

Weekly Downloads

1

Version

0.0.7

License

UNLICENSED

Last publish

Collaborators

  • tutukin