You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
30 lines
884 B
30 lines
884 B
2 years ago
|
import { InjectionKey, provide, inject, reactive, readonly as defineReadonly, UnwrapRef } from 'vue'
|
||
|
|
||
|
export interface CreateContextOptions {
|
||
|
readonly?: boolean
|
||
|
createProvider?: boolean
|
||
|
native?: boolean
|
||
|
}
|
||
|
|
||
|
type ShallowUnwrap<T> = {
|
||
|
[P in keyof T]: UnwrapRef<T[P]>
|
||
|
}
|
||
|
|
||
|
export function createContext<T>(context: any, key: InjectionKey<T> = Symbol(), options: CreateContextOptions = {}) {
|
||
|
const { readonly = true, createProvider = false, native = false } = options
|
||
|
|
||
|
const state = reactive(context)
|
||
|
const provideData = readonly ? defineReadonly(state) : state
|
||
|
!createProvider && provide(key, native ? context : provideData)
|
||
|
|
||
|
return {
|
||
|
state
|
||
|
}
|
||
|
}
|
||
|
|
||
|
export function useContext<T>(key: InjectionKey<T>, native?: boolean): T
|
||
|
|
||
|
export function useContext<T>(key: InjectionKey<T> = Symbol(), defaultValue?: any): ShallowUnwrap<T> {
|
||
|
return inject(key, defaultValue || {})
|
||
|
}
|