New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

core-stack

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

core-stack

The base of a core object. Create an object implementing a plugin system, a stack handler and an event emitter + some useful methods for a core object.

  • 0.2.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

core-stack

Create an object implementing:

  • An event emitter (Evemit, only 1 kb).
  • A plugin system that handles the asynchronous loading.
  • A stack handler.
  • And some useful methods for handling a core and config object.

core-stack was implemented with performance and lightness in mind.

Install

npm install core-stack

or with Yarn:

yarn add core-stack

Usage

See the source code for the JS doc.

Create a core

import CoreStack from 'core-stack';

// or const CoreStack = require('core-stack');
const core = new CoreStack();

// adds what do you need in the `core`...
core.foo = {};
core.bar = 'bar';

export default core;
// or module.exports = core;

Plugins

Create a plugin
/**
 * My plugin
 *
 * @param  {CoreStack}   core
 * @param  {*}           [args]
 * @param  {function}    done
 */
export default function myPlugin(core, args, done) {
  // add some feature to the core
  // ...

  // `done` function indicate that the plugin is loaded
  done(args);
};
Use a plugin

On the fly:

core.use(plugin, pluginArgs, function(done /*, doneArgs */) {
  console.log('plugin loaded!');
  done(/* doneArgs */);
});

or a reusable plugin:

import myPlugin from './plug/myPlugin';

core.use(myPlugin);
// or core.use(require('./plug/myPlugin'));

Example, create a simple logger plugin (reusable):

export default function loggerPlugin(core, args, done) {
  core.log = function() {
    console.log(...arguments);
  };

  core.logWarn = function() {
    console.warn(...arguments);
  };

  core.logError = function() {
    console.error(...arguments);
  };

  done();
};

Load and use the logger plugin:

import logger from './plug/logger';

// load the logger plugin
core.use(logger);

// use the logger plugin when the core was booted
core.boot(function() {
  core.log('Hello');
  core.logWarn('Warning!');
  core.logError('Ooops! An error occurred.');
})

Full example

import CoreStack from 'core-stack';

// Plugins
import logger from './plug/logger/';
import config from './plug/config/';
import router from './plug/router/';
import react from './plug/react/'; // or another lib / framework to initialize

const app = new CoreStack();

app
  .use(logger)
  .use(config)
  .use(router)
  .use(react)
  .boot(function() {
    app.log('all plugins are loaded');

    // you can emit any events with the builtin event emitter
    // see evemit package
    app.emit('app.booted');

    // init the routes and a router (e.g: routux package)
    router.init();
  })
;

LICENSE

MIT (c) 2016, Nicolas Tallefourtane.

Author

Nicolas Tallefourtane - Nicolab.net
Nicolas Talle

Keywords

FAQs

Package last updated on 15 Aug 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc