core-stack
v0.2.0
Published
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.
Downloads
4
Maintainers
Readme
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 Talle |