-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(context): add binding.toAlias()
#2635
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice feature!
What happens when somebody attempts to change the value of an alias? Are going to rebind they key or updated the alias target instead? How about @inject.setter
, should it be allowed on aliases?
These questions apply to existing binding types too. IMO, It should rebind the same key to whatever we allows, such as: ctx.bind('existing-key').to<...>(); For return function setter(value: unknown) {
ctx.bind(bindingSelector).to(value);
}; The binding is rebound as a constant value. Maybe binding.to<...>(); This is even better aligned with the idea to have The other option is to allow the setter to accept a setter((binding) => {
binding.toClass(MyClass);
}); |
214bec4
to
ed71240
Compare
@bajtos Based on the discussion, I added two more commits:
|
b28a40d
to
1bfc5ef
Compare
docs/site/Decorators_inject.md
Outdated
or | ||
|
||
```ts | ||
const binding = this.greetingSetter(b => b.toDynamicValue(() => 'Greetings!')); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An alternative approach is:
this.greetingSetter().toDynamicValue(() => 'Greetings!');
Please note that the setter
function now returns Binding
.
1bfc5ef
to
c2f104e
Compare
@inject.setter
and add binding.toAlias()
431227a
to
700231a
Compare
packages/authentication/src/__tests__/unit/providers/authentication.provider.unit.ts
Outdated
Show resolved
Hide resolved
445d750
to
f36e233
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 👍 pls make sure appveyor passes.
f36e233
to
6520421
Compare
Uh oh, I am not a big fan of pull requests adding multiple features. I would have preferred to see |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Allow injected setter function from @inject.setter to take binding template functions
I find this problematic. At the moment, it's possible to bind values to functions too. For example, all sequence actions hold a function as the bound value.
Consider the following example that works well in the current implementation:
// A custom function/action accepting a single string argument
// and performing some work with side effects
export type MyAction = (arg: string) => Promise<void>;
class MyStrategy {
constructor(
@inject.setter('my.action')
setAction: Setter<MyAction>
){}
void run() {
setAction(arg => console.log('run my action with', arg));
}
}
How are you envisioning the detect whether the function argument passed to setAction
is a value (e.g. a function implementing an action) or a binding template?
In general, I agree we should improve Personally, I think I prefer |
6520421
to
b216ab9
Compare
@inject.setter
and add binding.toAlias()
binding.toAlias()
I have refactored other related commits to:
This PR will be only responsible for adding |
acf2868
to
6cb1fac
Compare
@raymondfeng I don't have bandwidth to review this patch today, but I do want to check your updates before this patch is merged. Please give me few more days. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like how is the pull request small & easy to understand now 👏
The changes look mostly good, PTAL at my two comments below. No further review is needed from my side.
6cb1fac
to
2863a56
Compare
A few improvements for
@loopback/context
related to #2259binding.toAlias()
Use case:
Related to #2657
Depends on #2656
Checklist
👉 Read and sign the CLA (Contributor License Agreement) 👈
npm test
passes on your machinepackages/cli
were updatedexamples/*
were updated