Giter Site home page Giter Site logo

jab's Introduction

aysnc dependency injections and components on steroids for es2015+ javascript applications!

Build Status bitHound Overall Score

Install

jab can be installed using npm:

npm install spearwolf/jab

Usage Example:

    import { App } from '@spearwolf/jab';

    class Foo {

        constructor (plah, data) {
            this.plah = plah;
            this.data = data;
        }

    }

    App.Component(Foo, {  // A Component is like an ordinary class, you can create multiple entities from it
                          // (an Entity is an instance of a Component)

        construct: ['plah', 'data!'], // Define the services which shall be used as arguments for Foo constructor
                                      // data has an exclamation mark, so the construction of Foo will be delayed
                                      // until 'data' is resolved

        inject: ['bar', 'fooBar!'],  // After object creation, add (create) these Components
                                     // as properties to our object

        // Remember: construct services & inject components

        provider: {  // our Component has some extra providers which are not defined in the App
                     // providers are hierachical so they can override providers with same name from the App

            data: fetch('https://example.com/123.json')   // Every Promise can be used as provider!

        }

    });


    class Plah { }
    // no annotations here!
    // without any annotations a class will be act as Service (which is a Compoment singelton)

    class Bar {
        constructor () {
            return new Promise(resolve => setTimeout(resolve(this), 4));
        }
        // ooops, our constructor returns a Promise!
        // this will tell our App to wait for the Service initialization until
        // the Promise is resolved (with an instance of Bar as value)

        afterInitialized () {               // [optional] will be called after object construction
            console.log('4ms later..');

            return new Promise(resolve => setTimeout(resolve(this), 4));
                                            // returning a Promise is optional but very helpful
                                            // when you want to do some async stuff here
        }
    }

    App.Component(Bar);


    class FooBar {
        constructor (parent) {
            this.foo = parent;
        }
    }

    App.Component(FooBar, { construct: ['parent'] })
                                // Foo asks for 'fooBar' after object creation,
                                // so *parent* will be an instance of Foo in this case,
                                // otherwise *parent* will be null


    const app = new App({
        provider: {
            foo    : Foo,
            plah   : Plah,
            bar    : Bar,
            fooBar : FooBar
        }
    });

    app.createEntity('foo').then(foo => {

        foo.plah().then(plah => {
            // do something with service plah
        });

        console.log(foo.data);  // log json data

        foo.bar().then(bar => {  // create a new Bar entity
            // [8ms later] do something fantastic with bar
        });

        console.log(foo.fooBar);  // fooBar is an initialized FooBar entity

    });

jab's People

Contributors

spearwolf avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.