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

feat(core, rect): optional tokens #12

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/core/src/token.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,11 @@ describe('token', () => {
foo.key
expect(foo.key).toEqual('foo')
})
it('produces optional token for optional types', () => {
const foo = token('foo')<string | undefined>()
// $ExpectType string | undefined
type t1 = InjectableValue<typeof foo>
// check that token can be invoked without optional dependency
foo({})
})
})
6 changes: 4 additions & 2 deletions packages/core/src/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export function token<Name extends PropertyKey>(name: Name) {
{
readonly name: Name
readonly type: Type
readonly optional: false
readonly optional: undefined extends Type ? true : false
readonly children: readonly [
{
readonly name: typeof TOKEN_ACCESSOR_KEY
Expand All @@ -35,6 +35,8 @@ export function token<Name extends PropertyKey>(name: Name) {
return accessor ? accessor(dependencies, name) : dependencies[name]
}
f.key = name
return f

// eslint-disable-next-line no-restricted-syntax
return f as never
}
}
16 changes: 16 additions & 0 deletions packages/react/src/use-injectable.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,20 @@ describe('useInjectable', () => {
)
expect(cb).toHaveBeenLastCalledWith('bar')
})
it('supports optional tokens', () => {
const foo = token('foo')<string | undefined>()
const cb = jest.fn()
const Component = () => {
cb(useInjectable(foo))
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line throws due to missing dependency in DependenciesProvider

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that instead of:

const foo = token('foo')<string | undefined>()

we should call it like that:

const foo = token('foo')<string>('defaultValue')

And then we can get this default value when call useInjectable

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Conceptually, a token can't have a default value because it's a pure "input", a slot where you have to pass a value. On the other hand, if you need default values - you need a "computation", an injectable: const foo = injectable('foo', (): string => 'defaultValue')

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to support it we can do token like so:

export function token<Name extends PropertyKey>(name: Name) {
  return <Type = never>(defaultValue?: Type): InjectableWithName<
    {
      readonly name: Name
      readonly type: Type
      readonly optional: undefined extends Type ? true : false
      readonly children: readonly [
        {
          readonly name: typeof TOKEN_ACCESSOR_KEY
          readonly type: TokenAccessor
          readonly optional: true
          readonly children: readonly []
        }
      ]
    },
    Type
  > => {
    const f = (
      dependencies: {
        readonly [TOKEN_ACCESSOR_KEY]?: TokenAccessor
      } & Record<Name, Type>
    ): Type => {
      const accessor = dependencies[TOKEN_ACCESSOR_KEY]
      if (accessor === undefined) {
        return dependencies[name]
      }
      try {
          return accessor(dependencies, name)
      } catch (error) {
        if (defaultValue === undefined) {
          throw error
        }
        return defaultValue
      }
    }
    f.key = name

    // eslint-disable-next-line no-restricted-syntax
    return f as never
  }
}

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know we can, the issue is that it goes against the concept of a token.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can make it as special optionalToken.
I find it interesting only in case we have an ability to set default value as i have mentioned here:
#19

return null
}
render(
<StrictMode>
<DependenciesProvider value={{}}>
<Component />
</DependenciesProvider>
</StrictMode>
)
expect(cb).toHaveBeenLastCalledWith(undefined)
})
})