Skip to content

Commit

Permalink
feat: support non-ref value as vscode context value
Browse files Browse the repository at this point in the history
  • Loading branch information
kermanx committed Jun 14, 2024
1 parent 6181685 commit d667692
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
8 changes: 4 additions & 4 deletions packages/core/src/composables/useVscodeContext.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ComputedRef, MaybeRefOrGetter, Ref, WritableComputedRef } from '@reactive-vscode/reactivity'
import { computed, toValue, watchEffect } from '@reactive-vscode/reactivity'
import type { ComputedRef, MaybeRef, MaybeRefOrGetter, Ref, WritableComputedRef } from '@reactive-vscode/reactivity'
import { computed, isRef, ref, toValue, watchEffect } from '@reactive-vscode/reactivity'
import { commands } from 'vscode'

export function useVscodeContext<T>(
Expand All @@ -14,7 +14,7 @@ export function useVscodeContext<T>(
): WritableComputedRef<T>
export function useVscodeContext<T>(
name: string,
value: Ref<T>,
value: MaybeRef<T>,
shouldUpdate?: MaybeRefOrGetter<boolean>,
): Ref<T>

Expand All @@ -28,7 +28,7 @@ export function useVscodeContext<T>(
value: Ref<T> | (() => T),
shouldUpdate: MaybeRefOrGetter<boolean> = true,
) {
const normalized = typeof value === 'function' ? computed(value) : value
const normalized = isRef(value) ? value : typeof value === 'function' ? computed(value) : ref(value)
watchEffect(() => {
if (toValue(shouldUpdate))
commands.executeCommand('setContext', name, normalized.value)
Expand Down
1 change: 1 addition & 0 deletions test/composables/useVscodeContext.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { describe, expectTypeOf, it } from 'vitest'

describe('useVscodeContext', () => {
it.skip('should have correct types', () => {
expectTypeOf(useVscodeContext('myContext', 'a')).toEqualTypeOf<Ref<string>>()
expectTypeOf(useVscodeContext('myContext', ref('a'))).toEqualTypeOf<Ref<string>>()
expectTypeOf(useVscodeContext('myContext', () => 'a')).toEqualTypeOf<ComputedRef<string>>()
expectTypeOf(useVscodeContext('myContext', computed(() => 'a'))).toEqualTypeOf<ComputedRef<string>>()
Expand Down

0 comments on commit d667692

Please sign in to comment.