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

theatre-kernel

v1.0.3

Published

A singular kernel for runing application process on top of Inversify

Downloads

4

Readme

Theatre Kernel

Circle CI npm version dependencies WTFPL Licence

This library is a part of the Theatre module. You can use it ase is or with a complete Theatre installation.

About

Theatre Kernel is a very short implementation of an application runner. It introduces a simple way to run application on top of inversify.

Installation

npm install theatre-kernel --save

Quick Start

This little module introduce a single class allowing you to run any kind of application. To do so you have two components: An application and some container extensions.

  1. The application: The application run a process allowing your domain to be implementated. It can be a singular function or a given class.
  2. The container extension(s): It bind services inside the containers. Allowing your application to respect the IOC principles.

A basic example: Hello World

This is a simple and dumb example but it shows the main functionality.

import {EventDrivenKernel} from 'theatre-kernel';

const kernel = new EventDrivenKernel();

// Register a user inside the container
kernel.addExtension(container => {
    container.bind('user').toConstantValue({
        firstname: 'John',
        lastname: 'Doe'
    });
});

// Finally run our hello world application
kernel.run(container => {
    const user = container.get('user');

    console.log(`Hello ${user.firstname} ${user.lastname}`);
}).then(() => {
    console.log('Application ended well');
}).catch(error => {
    console.log('Oh crap! An error!');
    console.error(error);
});

Working with Classes

Let's take our previous example but with classes implementation:

// src/application.js
class Application
{
    run(container)
    {
        const user = container.get('user');

        console.log(`Hello ${user.firstname} ${user.lastname}`);
    }

    // Application can brings there own extensions:
    get extensions()
    {
        return [container => {
            container.bind('user').toConstantValue({
                firstname: 'John',
                lastname: 'Doe'
            });
        }];
    }
}
// src/index.js
import {Application} from './application';
import {EventDrivenKernel} from 'theatre-kernel';

const kernel = new EventDrivenKernel();

kernel.run(new Application());

Hooking the running process with events

Theatre kernel is using theatre events to dispatch usefull events for any hook process. Basically you can declare your own EventDispatcher or use an extension and retrieve the default EventDispatcher instance.

With an extension

import {EventDrivenKernel} from 'theatre-kernel';

const kernel = new EventDrivenKernel();

kernel.addExtension(container => {
    const dispatcher = container.get(EventDrivenKernel.EVENT_DISPATCHER);

    // Listen to the `BEFORE_RUN` and `AFTER_RUN` events wich is launch at the start and the end of your application.
    dispatcher.addEventListener(EventDrivenKernel.BEFORE_RUN, (event) => {
        // You can retrieve the application and the container with `event.application` and `event.container

        console.log('Application will run');
    });
    dispatcher.addEventListener(EventDrivenKernel.AFTER_RUN, (event) => {
        // You can retrieve the application and the container with `event.application` and `event.container

        console.log('Application has runned');
    });
});

kernel.run(container => console.log('Application'));

With your own event dispatcher

import {Container} from 'inversify';
import {AsynchronousEventDispatcher} from 'theatre-events';
import {EventDrivenKernel} from 'theatre-kernel';

// Create your own container and dispatcher
const container = new Container();
const dispatcher = new AsynchronousEventDispatcher();

// Listen to the `BEFORE_RUN` and `AFTER_RUN` events wich is launch at the start and the end of your application.
dispatcher.addEventListener(EventDrivenKernel.BEFORE_RUN, (event) => {
    // You can retrieve the application and the container with `event.application` and `event.container

    console.log('Application will run');
});
dispatcher.addEventListener(EventDrivenKernel.AFTER_RUN, (event) => {
    // You can retrieve the application and the container with `event.application` and `event.container

    console.log('Application has runned');
});

// Bind your dispatcher:
container.bind(EventDrivenKernel.EVENT_DISPATCHER).toConstantValue(dispatcher);

// Create your kernel
const kernel = new EventDrivenKernel(container);

// Run your application
kernel.run(container => console.log('Application'));

Complete list of events

/**
 * Trigger before ANY extensions. This event is blocking
 *
 * @see ContainerEvent
 */
static readonly BEFORE_EXTENSIONS = Symbol('BeforeExtensions');

/**
 * Trigger before each extension. This event is blocking
 *
 * @see ExtensionEvent
 */
static readonly BEFORE_EXTENSION = Symbol('BeforeExtension');

/**
 * Trigger after each extension. This event is not blocking
 *
 * @see ExtensionEvent
 */
static readonly AFTER_EXTENSION = Symbol('AfterExtensions');

/**
 * Trigger after ALL the extensions. This event is not blocking
 *
 * @see ContainerEvent
 */
static readonly AFTER_EXTENSIONS = Symbol('AfterExtensions');

/**
 * Trigger just before the application. This event is blocking
 *
 * @see ApplicationEvent
 */
static readonly BEFORE_RUN = Symbol('BeforeRun');

/**
 * Trigger after the application. This event is not blocking
 *
 * @see ApplicationEvent
 */
static readonly AFTER_RUN = Symbol('AfterRun');

/**
 * Trigger when an error occured. This event is blocking
 *
 * @see ErrorEvent
 */
static readonly ON_ERROR = Symbol('OnError');

/**
 * Trigger when the kernel stop. This event is blocking
 *
 * @see ApplicationEvent
 */
static readonly ON_STOP = Symbol('OnStop');