Expose root as a public property#1023
Expose root as a public property#1023Rich-Harris merged 1 commit intosveltejs:masterfrom emilos:public-root
Conversation
Codecov Report
@@ Coverage Diff @@
## master #1023 +/- ##
======================================
Coverage 92.1% 92.1%
======================================
Files 115 115
Lines 4304 4304
Branches 1373 1373
======================================
Hits 3964 3964
Misses 147 147
Partials 193 193
Continue to review full report at Codecov.
|
|
Thanks! I think this is probably a good compromise — it basically enables all the things you'd want to use a more fully-fledged DI system for, but it doesn't 'encourage' it, and it doesn't cost anything (in fact we're just removing I'll leave this open for a short while to see if anyone else has thoughts and wants to weigh in. |
|
Might we also want Edit - Whoops nevermind, I'm dumb. It is. Carry on. |
|
This is released as 1.48.0 — thanks |
Hey,
I'm opening the PR to discuss the
rootproperty being public (it was briefly discussed in gitter and I'll try to describe the motivation here once again).I have an use case where I'd like to create a new instance of a component with an additional
serviceproperty. The main motivation is easier testing with a fake service, secondary motivation is to make the chat be less dependent on a particular vendor.the chat component itself looks like this:
{{#if closed}} <Bar on:click='toggle(OPEN)'>Leave a message</Bar> {{/if}} {{#if open}} <Widget on:click='toggle(CLOSE)'/> {{/if}}then inside of the Widget I use the service e.g. to send a message, receive messages etc.:
other approaches I though of or that were suggested to solve this:
a) propagate events from lowest components (e.g. form submits) and handle all of it outside of the chat component - I didn't like it because I'd need to propagate it via many layers, too much hassle, too close to redux kind of way which I find very verbose
b) check if
servicecan be passed like thestore, so that it can be automatically injected into components below - based on discussion in gitter I understood that it seems that dependency injection was discussed before and it's seen as a fragile pattern (I have the same feeling based on what I saw in other libraries, however I also think that it's not necessarily the pattern that is wrong but the excessive use, any pattern is wrong when overused)then the line:
is unnecessary.
c) pass it via
storeasstore.service. A little bit weird, unexpectedd) pass it via
data. Weird as well, unexpectedI do understand the desire to keep svelte as concise as possible so I'm open to all suggestions :) Thanks a lot.