diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index a38a9f8..72abe87 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -14,7 +14,7 @@ jobs: os: [ubuntu-latest] vue: [2.6, 2.7, 3] fail-fast: false - + runs-on: ${{ matrix.os }} steps: @@ -26,7 +26,7 @@ jobs: node-version: ${{ matrix.node }} - run: npm -v - run: node .github/test.js "npm" ${{ matrix.vue }} - + yarn: strategy: matrix: @@ -35,7 +35,7 @@ jobs: vue: [2.6, 2.7, 3] yarn: [latest] type: [commonjs, module] - + runs-on: ${{ matrix.os }} steps: @@ -48,14 +48,14 @@ jobs: - run: yarn set version ${{ matrix.yarn }} - run: yarn --version - run: node .github/test.js "yarn" ${{ matrix.vue }} ${{matrix.type}} - + pnpm: strategy: matrix: node: [16.x] os: [ubuntu-latest] vue: [2.6, 2.7, 3] - + runs-on: ${{ matrix.os }} steps: @@ -65,6 +65,7 @@ jobs: - uses: actions/setup-node@v1 with: node-version: ${{ matrix.node }} - - run: npm i -g pnpm + + - run: npm i -g pnpm@8 - run: pnpm -v - run: node .github/test.js "pnpm" ${{ matrix.vue }} diff --git a/lib/v2.7/index.cjs b/lib/v2.7/index.cjs index 8fa3b68..b9844b4 100644 --- a/lib/v2.7/index.cjs +++ b/lib/v2.7/index.cjs @@ -57,4 +57,9 @@ Object.keys(VueModule).forEach(function (key) { // Not implemented https://github.com/vuejs/core/pull/8111, falls back to getCurrentInstance() exports.hasInjectionContext = function() { return !!VueModule.getCurrentInstance() -} \ No newline at end of file +} + +// not implemented in Vue 2, only in Vue 3.3+ +exports.toValue = function(source) { + return typeof source === 'function' ? source() : VueModule.unref(source) +} diff --git a/lib/v2.7/index.d.ts b/lib/v2.7/index.d.ts index 827c7b2..e21ae83 100644 --- a/lib/v2.7/index.d.ts +++ b/lib/v2.7/index.d.ts @@ -1,5 +1,16 @@ import Vue from 'vue' -import type { PluginFunction, PluginObject, VueConstructor, Directive, InjectionKey, Component } from 'vue' +import type { + PluginFunction, + PluginObject, + VueConstructor, + Directive, + InjectionKey, + Component, + Ref, + ShallowRef, + WritableComputedRef, + ComputedRef, +} from 'vue' declare const isVue2: boolean declare const isVue3: boolean @@ -36,3 +47,29 @@ export declare function createApp(rootComponent: any, rootProps?: any): App // #endregion export declare function hasInjectionContext(): boolean + +export type MaybeRef = + | T + | Ref + | ShallowRef + | WritableComputedRef + +export type MaybeRefOrGetter = MaybeRef | ComputedRef | (() => T) + +/** + * Normalizes values / refs / getters to values. + * This is similar to {@link unref()}, except that it also normalizes getters. + * If the argument is a getter, it will be invoked and its return value will + * be returned. + * + * @example + * ```js + * toValue(1) // 1 + * toValue(ref(1)) // 1 + * toValue(() => 1) // 1 + * ``` + * + * @param source - A getter, an existing ref, or a non-function value. + * @see {@link https://vuejs.org/api/reactivity-utilities.html#tovalue} + */ +export declare function toValue(source: MaybeRefOrGetter): T diff --git a/lib/v2.7/index.mjs b/lib/v2.7/index.mjs index e575059..72c493b 100644 --- a/lib/v2.7/index.mjs +++ b/lib/v2.7/index.mjs @@ -1,5 +1,5 @@ import Vue from 'vue' -import { getCurrentInstance } from 'vue' +import { getCurrentInstance, unref } from 'vue' var isVue2 = true var isVue3 = false @@ -78,3 +78,8 @@ export * from 'vue' export function hasInjectionContext() { return !!getCurrentInstance() } + +// not implemented in Vue 2, only in Vue 3.3+ +export function toValue(source) { + return typeof source === 'function' ? source() : unref(source) +}