Skip to content

Commit

Permalink
fix: vue 2 support for provideLocal and injectLocal
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoxiangmoe committed Oct 7, 2023
1 parent 87ea324 commit 9bfc23c
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 4 deletions.
48 changes: 48 additions & 0 deletions packages/shared/injectLocal/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { type InjectionKey, defineComponent, h } from 'vue-demi'
import { injectLocal, provideLocal } from '@vueuse/shared'
import { describe, expect, it } from 'vitest'
import { mount } from '../../.test'

{
const CountKey: InjectionKey<number> | string = Symbol('count')
const ChildComponent = defineComponent({
setup() {
const count = injectLocal(CountKey)
expect(count).toBe(2333)

return () => h('div')
},
})

const RootComponent = defineComponent({
setup() {
provideLocal(CountKey, 2333)

return () => h(ChildComponent)
},
})
describe('provideLocal injectLocal cross component example', () => {
it('should work', () => {
mount(RootComponent)
})
})
}

{
const CountKey: InjectionKey<number> | string = Symbol('count')
const TheComponent = defineComponent({
setup() {
provideLocal(CountKey, 2333)
const count = injectLocal(CountKey)
expect(count).toBe(2333)

return () => h('div')
},
})

describe('provideLocal injectLocal same component example', () => {
it('should work', () => {
mount(TheComponent)
})
})
}
4 changes: 2 additions & 2 deletions packages/shared/injectLocal/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import { localProvidedStateMap } from '../provideLocal/map'
// @ts-expect-error overloads are not compatible
export const injectLocal: typeof inject = (...args) => {
const key = args[0] as string | symbol
const instance = getCurrentInstance()
const instance = getCurrentInstance()?.proxy
if (instance == null)
throw new Error('injectEnhancedAllowanceOfCallsFromTheSameComponent must be called in setup')
throw new Error('injectLocal must be called in setup')

if (localProvidedStateMap.has(instance) && key in localProvidedStateMap.get(instance)!)
return localProvidedStateMap.get(instance)![key]
Expand Down
2 changes: 1 addition & 1 deletion packages/shared/provideLocal/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { localProvidedStateMap } from './map'
* ```
*/
export const provideLocal: typeof provide = (key, value) => {
const instance = getCurrentInstance()
const instance = getCurrentInstance()?.proxy
if (instance == null)
throw new Error('provideLocal must be called in setup')

Expand Down
2 changes: 1 addition & 1 deletion packages/shared/provideLocal/map.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import type { getCurrentInstance } from 'vue-demi'

export const localProvidedStateMap = new WeakMap<NonNullable<ReturnType<typeof getCurrentInstance>>, Record<string | symbol, any>>()
export const localProvidedStateMap = new WeakMap<NonNullable<NonNullable<ReturnType<typeof getCurrentInstance>>['proxy']>, Record<string | symbol, any>>()

0 comments on commit 9bfc23c

Please sign in to comment.