npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

centralize-js

v1.1.4

Published

<h1 align="center">Centralize</h1> <p align="center"> A simple JS message hub </p> <p align="center"> <img src="https://api.travis-ci.org/davinche/centralize.svg?branch=master" alt="build status"/> </p>

Downloads

577

Readme

Installation

npm install centralize-js --save

Messages

Messages have the following form:

const message = {
  logLevel: 10,
  labels: {
    app: 'my-app',
    category: 'analytics',
    metric: 'load-time'
  },
  timestamp: new Date(),
  value: 10000 // in milliseconds
}

A message sent by the hub will be dispatched to all receivers. To send a message, you can use the library provided logger or manually create and send a message.

Creating Messages

You can use the utility function CreateMessage to create an empty message.

import {CreateMessage} from 'centralize-js';
const myAnalyticsMessage = CreateMessage(10, {type: 'analytics'});
myAnalyticsMessage.value = 'val';

Sending Messages

Using the logger

import Logger from 'centralize-js';

Logger.info('this is an info log');
Logger.debug('this is a debug log');

Note: The logger contains the info(), debug(), warn()... methods because it is created using the DEFAULT_LOG_LEVELS.

You can use your own custom methods by creating a new logger with your own log levels:

import {Hub, LoggerClass} from 'centralize-js';
const myLogger = new LoggerClass(Hub, {foo: 10, bar: 20});
// myLogger.foo('my message');
// myLogger.bar('my other message');

Or you can change the log levels using the setLogLevels() method.

import Logger from 'centralize-js';
Logger.setLogLevels({foo: 10, bar: 20});

// Logger.foo('my message');
// Logger.bar('my other message');

Manually

import { Hub } from 'centralize-js';

// Create the message manually
const message = {
  logLevel: 1000000,
  labels: {},
  timestamp: new Date(),
  value: 'This is a very important message due to the high logLevel'
}

// alternatively use the CreateMessage function
// const message = CreateMessage(1000000);
// message.value = 'This is a very important message due to the high logLevel';

Hub.send(message);

Receiving Messages

A receiver is any function that can receive a message.

To receive all messages, you can attach to the global stream:

import { Hub, Logger } from 'centralize-js';

const myReceiver = function(message) {
  console.log(message.value);
};

Hub.messages.addReceiver(myReceiver);
Logger.info('this is an info log');
// console.log outputs 'this is an info log'

Any configuration to the global stream will apply to all messages. There are certain scenarios where you would want to leave the global stream intact, but add customizations while still receiving all messages (ie: set log levels but not applied globally).

To do so, you can use the matchAll() function to create a new substream that receives all of the messages from the global stream.

const stream = Hub.messages.matchAll().addReceiver(myreceiver);

Filter received messages by labels

You can filter received messages for matching labels by doing the following:

import { Hub, Logger } from 'centralize-js';

const myAppMessagesReceiver = function(message) {
  console.log(message.value);
};

Hub.messages.matchLabels({app: 'my-app'}).addReceiver(myAppMessagesReceiver);

Logger.debug('my-app debug message', {app: 'my-app'});
Logger.debug('my-other-app message', {app: 'not-my-app'});

// console.log only prints 'my-app debug message'

Filtering received messages by match conditions

import { Hub, Logger } from 'centralize-js';

// Scenario: 3 environments (development, staging, production)
// receiver should only be triggered for staging and production
const stagingAndProductionReceiver = function(message) {
  console.log(message.value);
};

Hub.messages
  .matchConditions('env', 'IN', ['staging', 'production'])
  .addReceiver(stagingAndProductionReceiver);
...

Logger.debug('my log in development', {env: 'development'});
Logger.debug('staging log', {env: 'staging'});
Logger.debug('production log', {env: 'production'});

// console.log only prints out 'staging log' and 'production log'

matchConditions accepts the following operators:

  1. 'IN'
  2. 'NOT_IN'
  3. 'NOT'

Interceptors

Interceptors allow you to intercept a message and mutate it before passing it down the stream.

An interceptor must either return a message, or null to stop the message from propogating further.

Adding an interceptor

const myInterceptor = (message) => {
  message.value = 'bar';
  return message;
};

const myReceiver = (message) => {
  console.log(message.value);
};

Hub.messages.addInterceptor(myInterceptor);
Hub.messages.addReceiver(myReceiver);


Hub.messages.send({
  value: 'foo';
});

// console.log outputs 'bar'

Removing an interceptor

const myInterceptor = (message) => {
// interceptor code
};

Hub.messages.removeInterceptor(myInterceptor);

Log Levels

To set global log level for messages:

import { Hub, LOG_LEVELS } from 'centralize-js';
Hub.messages.setLogLevel(LOG_LEVELS.error);

// only messages that are 'errors' and higher will be dispatched.

You can also set a loglevels for individually matched messages:

import { Hub, LOG_LEVELS } from 'centralize-js';

const myAppReceiver = function(message) {
  console.log(message.value);
};

const messages = Hub.messages.matchLabels({app: 'my-app'});
messages.setLogLevel(LOG_LEVELS.error);
messages.addReceiver(myAppReceiver);

Note: that the global log level precedes everything, so setting a 'lower' log level for matched messages will have no effect if the global log level is 'higher'.