Comments (9)
that is true! you could pass something like that to the root injector by calling. I concede lol they are in fact related. implementing it this way also means you don't have to worry about passing an option every time you call inject
import { defineEnvironment } from "@joist/di";
defineEnvironment([{ provide: MyService, factory: () => throw new Error('Override expected!' }])
from joist.
@Phoscur I went ahead and published a minor version. https://www.npmjs.com/package/@joist/di
from joist.
I hope this is related enough to not require its on issue (yet):
I've been testing around a bit today concerning reusing a ProviderToken and had not much success. So I'd like to suggest a custom factory
inject(MyPublicService, { factory: () => myGenericServiceInstance });
inject(MyOtherService, { factory: () => myGenericServiceInstance });
where myGenericServiceInstance
implements MyPublicService as well as MyOtherService, and it's constructed in a different (static) way.
Can I expose an instance under two different ProviderTokens?
E.g. something like the following will result in two different ConsoleDebug instances:
static providers = [{ provide: Debug, use: ConsoleDebug }];
static providers = [{ provide: ConsoleDebug, use: ConsoleDebug }];
And this does not work:
static providers = [{ provide: Debug, use: () => new ConsoleDebug() }];
Am I trying things I should not do anyways or am I just missing some more documentation?
Also, how do I access the fallback injector for my custom elements?
from joist.
Hmmm yeah currently there is no factory option because I couldn't find a reason to have one. So to restate your ask. You want two different tokens to return the same instance of a service.
I think a factory would be the best way to go here. Then you could return the same instance for both tokens. I think this would be a different issue.
I plan on spending some time on this soon (maybe this weekend?)
from joist.
I still feel factory
is very much related to fallback
, if not you can still split the topics after your first iteration ;)
...so restate your ask. You want two different tokens to return the same instance of a service.
I don't think that's a good or at least not the only use case / example. However manually constructing the instance also allows to use constructor arguments, which I've been missing so far.
(I'm still looking for a nice example for this to share, but I'm still experimenting atm! :))
// building the injector procedurally
const inj = new Injector();
inj.set(MyService, (inj) => myServiceInstance);
// or supplying a factory in inject definition
#service = inject(MyService, factory: (inj) => new Decorator(inj.get(MyService)), fallback: false)); // MyService has a parent factory, fallback injector disallowed
// fallback: true (default) -> constructs MyService via fallback injector
// also need access to the fallback injector
inj.fallback.set(MyService, () => replacementInstance); // walks up injector parent chain to find the global shared fallback injector
// maybe fallback should be type boolean|factory?
#service = inject(MyService, fallback: () => fallbackInstance)
I plan on spending some time on this soon (maybe this weekend?)
Looking forward to your approach to this, don't get carried away by my ideas ;)
from joist.
Ha not at all. At a minimum I think a factory option makes sense.
from joist.
@Phoscur I have a PR open that allows you to define a factory method instead of use
when defining a provider.
const debug = new ConsoleDebug();
@injectable
class MyElementCtx extends HTMLElement {
static providers = [
{ provide: Debug, factory: () => debug },
{ provide: ConsoleDebug, factory: () => debug };
]
}
from joist.
I still think this particular issue is separate from requiring a manually defined parent and I should probably create a new issue
from joist.
Allow the inject function to require a provider to be defined. This would mean no fall back to the global injector.
inject(MyService, { fallback: false })
In #966 you are hinting at an existing way of doing this:
inject(AbstractService)
should fail to construct, so it requires configuring an override down the injector chain
And with this new feature, we can also provide a throwing factory:
providers: [{ provide: MyService, factory: () => throw new Error('Override expected!' }]
from joist.
Related Issues (20)
- Styles
- runtime property validation
- Allow handlers to use pattern to match actions.
- Decorators should account for ts decorators and the standards proposal HOT 1
- Better test coverage of router
- Migrate from karma to web-test-runner
- Batch property changes
- Dev Tools HOT 1
- Dependency Dashboard
- @joist/di: lint rule for inject array not matching Injected types.
- @joist/di: implement lint fix to autofill injected services
- @joist/di: clear local cached instances on disconnect and connect
- SSR
- 3.0
- Example on how to use this toolkit with Lit HOT 10
- Cannot register element before dom renders with @attr
- Custom Element definition order matters HOT 4
- Allow for easier debug of dependency graph HOT 3
- Create a non constructable ProviderToken HOT 6
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 joist.