Skip to content
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

Optional token proposal #19

Open
coolassassin opened this issue Jan 15, 2024 · 1 comment
Open

Optional token proposal #19

coolassassin opened this issue Jan 15, 2024 · 1 comment
Labels
enhancement New feature or request question Further information is requested

Comments

@coolassassin
Copy link
Contributor

Hello!

If we imagine some kind of service which behaviour can be controlled outside (for example auth tokens), we would use tokens for that like so:

const myService = injectable('myService', token('authToken')<string>(), (token) => {
   ...
})

But what if we want to have some default behaviour or initial state for the service? We can't provide a default value for a token.
To get same result we can do it like so:

const myValue = injectable('authToken', () => 'defaultToken')
const myService = injectable('myService', myValue, (token) => {
   ...
})

Which is ok and work, but it seems inconsistent to use different ways for close purposes.
So, I would like to offer to make an optional token like so:

token('authToken')<string>('defaultToken')
/// or
optionalToken('authToken')<string>('defaultToken') // here we will force user to set default value
@raveclassic
Copy link
Owner

Hey @coolassassin, thanks for the proposal.
While I see some benefits here I also see that this would bloat the API: optionalToken('authToken')<string>('defaultToken') is essentially an alias to injectable('authToken', (): string => 'defaultToken').

Regarding inconsistency, I'm not sure I'm following since current implementation is consistent with the statement that a "token" is a dependency that the caller must provide when invoking the computation, and "injectable" is a computation itself with a default value which can be overridden.

The above means that if you want an optional dependency - you'd typically declare it as token('dependency')<string | undefined>(), but you'd have to explicitly provide the key - root({ dependency: undefined }).

I might be missing something here, but for now the current state of things seems consistent to me. Still, let's keep this open so that we could all think about this a bit more.

@raveclassic raveclassic added enhancement New feature or request question Further information is requested labels Jan 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants