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

mainframe-core

v0.2.3

Published

A hive-mind shaped event system.

Downloads

15

Readme

mainframe

mainframe is a hive-mind approach to event systems.

install

mainframe is available on npm as 'mainframe-core'.

$ npm install mainframe-core

## principle mainframe is a hive-mind. You can think about it as a central computer, or even as the Borg Collective. Its point is to have a flock of agents available at all time, each one of them fulfilling a different function. The mainframe centralizes the decisions and can give different order to agents according to the data returned by all agents.

First, the mainframe has a brain. The brain have layers, which are essentially finite space machines. Each layer can propagate thoughts inside itself, which changes the current state of the layer.

The mainframe also have agents. Think of it as the endpoints of the central computer, or as a borg drone. They can be registered to different mainframes at the same time. They receive orders from the mainframe and obey, according to their function.

The mainframe communicate with the agents by sending signals. The mainframe can create groups of agents, taskforces, to manage tasks more efficiently when broadcasting signals to only a group of agents.

If you need to extend the abilities of the mainframe (add a database, or an evolutive grammar parser), you can by adding modules. Modules are simple objects that can be created on the fly or imported, that can extend the capacities of the mainframe. Adding the abilities to the mainframe itself means that all the agents will be able to use them.

documentation

To create a mainframe :

var Mainframe = require('mainframe-core')

var mainframe = new Mainframe()

brain

The brain of the mainframe is accessible by the following property :

var brain = mainframe.brain

You can manage layers with the following methods :

mainframe.brain.bud(id)        // create a layer at given id
mainframe.brain.id(layer)      // get the id of a layer
mainframe.brain.layer(id)      // get the layer associated to an id
mainframe.brain.ted(layerorid)  // delete a layer (by object or id)

agents

Agents can be registered to the mainframe. The organ manging the agents is named the commander, and it is accessible by the following property :

var commander = mainframe.agents

An agent is a particular type of object created specially to interface with a mainframe. It is an object of the form :

var agent = {
  // REQUIRED
  name: '[agent name]',
  // REQUIRED
  slots: {
    // called when the agent is registered.
    // adding this slot is optional.
    // NB : the args param always contains the keys
    // 'id', 'taskforce', and 'self' (a reference to the agent itself)
    register: function (mainframe, args) { }
    // called when the agent is unregistered
    // adding this slot is optional.
    // NB : the args param always contains the keys
    // 'id', 'taskforce', and 'self' (a reference to the agent itself)
    unregister: function (mainframe, args) { }

    // add your own slots to respond to the mainframe signals.
    // the name of the function is the content of the signal
    // send by the mainframe.
  }
}

You can manage agents with the following methods :

mainframe.agents.register(agent, taskforce, args) // register an agent
mainframe.agents.unregister(agent, args)          // unregister an agent
mainframe.agents.id(agent)                        // return the id of the agent

You can communicate with agents with the following methods (the taskforce argument is always facultative):

// send a signal to a precise agent
mainframe.agents.unicast(receiver, signal, args)
// send a signal to agents that match a given predicate. you can specify
// the taskforce where the search will be made
mainframe.agents.multicast(predicate, signal, args, taskforce)
// send a signal to all the agents. you can specify the taskforce
// where the search will be made
mainframe.agents.broadcast(signal, args, taskforce)

You can manage taskforces with the following methods :

// create a layer at given id
mainframe.agents.demote(agent)        
// get the id of a layer
mainframe.agents.promote(agent, taskforce)
// get the layer associated to an id   
mainframe.agents.taskforce(agent)      

logging

The logger of the mainframe is accessible by the following property :

var logger = mainframe.logs

You can manage logs with the following methods :

// create a new log that'll be returned
mainframe.logs.make(title, loglevel)     
// erase a log
// CAUTION : all data will be lost
mainframe.logs.erase(title)
// get a log by title
// (all syntaxes are equivalent)
mainframe.logs.get('title')
mainframe.logs['title']
mainframe.logs.title

A log has a logging threshold set at its creation. Only the messages that have a higher level of importance will be indeed logged.

The different levels are here shown sorted from more important to less important.

'EMERGENCY' // most important
'ALERT'
'CRITICAL'
'ERROR'
'WARNING'
'NOTICE'
'INFO'
'DEBUG'     // less important

By default, a log titled 'main' with logging level 'INFO' is created. All organs have their own log (respectively 'brain', 'commander', and 'dock') with logging level set at 'INFO'.

modules

The modules added to the mainframe are simple objects of the form :

var module = {
  // the name of the module
  // must match /[a-zA-Z_][a-zA-Z0-9_-]*/
  name: "[mymodule]",

  // called when the module is docked.
  // REQUIRED
  dock: function() { },
  // called when the module is undocked
  // REQUIRED
  undock: function() {}

  // add your own methods
  myMethod: function() { /*...*/ }
}

The name of the module must match the regex :

/[a-zA-Z_][a-zA-Z0-9_-]*/

... which means that a module name is composed of letters, numbers, and underscores, and must begin by a letter or an underscore

To add or remove a module from the dock of modules, simply call :

mainframe.dock(module) // dock the module
// ... use the module...
mainframe.undock(module) // undock the module, optional

When the module is docked, you can access the properties of the module you created by doing :

module === mainframe.mymodule // the module is now accessible here
mainframe.mymodule.myMethod() // access the module