Skip to content

Commit

Permalink
feat(createInjectionState): return state when providing injection sta…
Browse files Browse the repository at this point in the history
…te (#2309)

* feat: return state when providing injection state

* Update packages/shared/createInjectionState/index.ts

Co-authored-by: lsdsjy <lsdsjy@163.com>

Co-authored-by: lsdsjy <lsdsjy@163.com>
  • Loading branch information
Tanimodori and lsdsjy committed Jan 17, 2023
1 parent 0359eb2 commit f7ce6c9
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions packages/shared/createInjectionState/index.ts
Expand Up @@ -8,10 +8,12 @@ import { type InjectionKey, inject, provide } from 'vue-demi'
*/
export function createInjectionState<Arguments extends Array<any>, Return>(
composable: (...args: Arguments) => Return,
): readonly [useProvidingState: (...args: Arguments) => void, useInjectedState: () => Return | undefined] {
): readonly [useProvidingState: (...args: Arguments) => Return, useInjectedState: () => Return | undefined] {
const key: string | InjectionKey<Return> = Symbol('InjectionState')
const useProvidingState = (...args: Arguments) => {
provide(key, composable(...args))
const state = composable(...args)
provide(key, state)
return state
}
const useInjectedState = () => inject(key)
return [useProvidingState, useInjectedState]
Expand Down

0 comments on commit f7ce6c9

Please sign in to comment.