Skip to content
This repository has been archived by the owner on Nov 27, 2023. It is now read-only.

feat: implement async component initializers #209

Merged
merged 15 commits into from May 1, 2018
Merged

Conversation

KnisterPeter
Copy link
Owner

@KnisterPeter KnisterPeter commented Apr 29, 2018

Since components may require async initializers
(e.g. to connect to some resource) the dependency
tree could only be created async in some cases.
To support this tsdi calls component initializers
only if all injected component initializers are done.

There are a few parts missing in this implementation.

  • Warn if usage is not correct
  • Fail to synchronously get asyncronous components
  • Check that components are still created lazily
  • Add test for async dependency tree with depth > 1
  • Add documentation for this feature (works only if function is declared as async)

Closes #208

Since components may require async initializers
(e.g. to connect to some resource) the dependency
tree could only be created async in some cases.
To support this tsdi calls component initializers
only if all injected component initializers are done.

Closes #208
@Prior99
Copy link
Collaborator

Prior99 commented Apr 29, 2018

I'd suggest to add a method to tsdi which can be awaited and which will resolve once all async initializers are done. This way it's for example possible to hold the initial rendering of a page until all api requests are performed.

Also, sometimes it might not be desirable to always await all initializers just because they return a promise. We could add a @initialize({ sync: true }) option.

I can try to tackle that and also take a look at Add test for async dependency tree with depth > 1.

What do you mean by Fail to synchronously get asyncronous components? Should tsdi.get() instead return a Promise? That would be a breaking change, instead we should return undefined when calling tsdi.get() and add a tsdi.getAsync().

@KnisterPeter
Copy link
Owner Author

I would like to add a method (e.g. start) which will create all eager components and call its initializer if the dependency tree stabilizes. That way there is a hook into the components to define the system is ready to go. The start method itself could return a promise, but I think that may not be required for now.

If an initializer returns a promise this will not be enough (right now it is, but I have to rework a smaller part). It is requried to declare the initializer as async which will implicitly let dependencies wait for it.
I think there should be no more api surface required, so I would vote against a property on the initialize decorator.

Would be awesome if you create a test with deeper async level. The main focus should be that there is a dependency which async initializer which is injected into a component without async initializer. That one in turn is also injected and must be awaited because of the deeper async initializer.
Should already be like this but a test is required to make sure of that.

tsdi.get should throw an error if an async component is requested. The get should stay sync. The described start method above should be good to go.

@Prior99
Copy link
Collaborator

Prior99 commented Apr 29, 2018

Okay then I'll take a look at the test.

@KnisterPeter KnisterPeter merged commit ef6a916 into master May 1, 2018
@KnisterPeter KnisterPeter deleted the async-inits branch May 1, 2018 09:46
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants