Git Product home page Git Product logo

Comments (4)

KnisterPeter avatar KnisterPeter commented on May 12, 2024

@Prior99 Thanks for reporting. I'll have to think about it.
Its a good use case which will require a bit more API I think.

Could be a problem in tsdi object graph creation.

from tsdi.

KnisterPeter avatar KnisterPeter commented on May 12, 2024

My idea to solve this is to wait for all injects to be created and have their initializer executed. Initializers which return a promise should be awaited in this case.

The biggest problem right now is the default lazy injection. The db above need to be eager injected instead of lazy, so the component is created before first access and could be awaited.

from tsdi.

KnisterPeter avatar KnisterPeter commented on May 12, 2024

@Prior99 As of #209 this issue is partially solved. The part missing are the factories. By the synchronous nature of the new keyword in javascript and therefore the synchronous nature of dependency injection it is not possible to implement asynchronous factories.

You may work around this with this pattern:

@component
class Database {
    private databaseConnection: DatabaseConnection;

    @initialize
    async connect() {
        this.databaseConnection = await connectToDatabase();
    }

    get connection(): DatabaseConnection {
        return this.databaseConnection;
    }
}

@component({ eager: true })
class Api {
    @inject private db: Database;

    @initialize
    public initialize() {
        this.db.connection.query(...); // `db` is now `undefined`.
    }
}

Since Api.initialize() is only called if all injected dependencies are itself done you can be sure that this.db.connection is ready in your example.
Hope that help a bit.

from tsdi.

KnisterPeter avatar KnisterPeter commented on May 12, 2024

Released in v1.0.0-alpha.0
This is alpha, because of the breaking change and I like to take the chance and rewrite bigger parts.

from tsdi.

Related Issues (20)

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.