Comments (4)
@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.
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.
@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.
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)
- Asynchronous initializers not working with externals HOT 3
- Auto-update website
- Remove travis
- Add componenent onReady lifecycle
- Auto Website Release fails
- Switch from cypress to playwright
- @destroy lifecycle not called on every instance of non-singleton component HOT 4
- Action Required: Fix Renovate Configuration
- Add docs for `asyncGet` HOT 3
- Bug: Cannot read properties of undefined (reading 'configureExternal') HOT 2
- State of this project HOT 2
- Properties should only be configured once
- Move to reference based api instead of name based
- No usage of tsdi compiler decorators found. Better error message.
- Add documentation for compiler cli and migration from runtime version HOT 1
- Create compiler plugins HOT 2
- Add test for the UMD bundle
- Use correct function name instead of 'InjectedConstructor'
- Usage with react hooks (no more classes?) HOT 10
- release issue HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tsdi.