Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Async Container Modules #802

Open
remojansen opened this issue Feb 23, 2018 · 4 comments
Open

Async Container Modules #802

remojansen opened this issue Feb 23, 2018 · 4 comments

Comments

@remojansen
Copy link
Member

remojansen commented Feb 23, 2018

In node.js, sometimes we need to initialize some async components before the application starts serving HTTP requests.

Currenr API

(async () => {

    const db = await getDbConn();

    let module = new ContainerModule(
        (bind: interfaces.Bind, unbind: interfaces.Unbind) => {
           bind("db").toContainerValue(db);
        }
    );

    let container = new Container();
    container.load(module);

    let server = new InversifyExpressServer(container);

    server.setConfig((app) => {
      // add body parser
       app.use(bodyParser.urlencoded({
         extended: true
      }));
      app.use(bodyParser.json());
    });

    let app = server.build();
    app.listen(3000);

})();

New API

The new API introduces 2 breaking changes:

- Container modules use now an async function
- The container.load method is now async

The new API introduces a new kind of module known as AsyncContainerModule and a new container method named loadAsync:

let module = new AsyncContainerModule(
    async (bind: interfaces.Bind, unbind: interfaces.Unbind) => { // async!
       const db = await getDbConn();
       bind("db").toContainerValue(db);
    }
);

(async () => {
    
    let container = new Container();
    await container.loadAsync(module); // await!

    let server = new InversifyExpressServer(container);

    server.setConfig((app) => {
      // add body parser
       app.use(bodyParser.urlencoded({
         extended: true
      }));
      app.use(bodyParser.json());
    });

    let app = server.build();
    app.listen(3000);

})();
@sanex3339
Copy link
Contributor

sanex3339 commented Feb 25, 2018

If possible, keep old sync API please. My package is fully sync and i very don't want to make it async.

@remojansen
Copy link
Member Author

@sanex3339 see updated API. It will be blackguards compatible.

@sanex3339
Copy link
Contributor

Amazing

@leoperria
Copy link

Great !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants