Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add toValue to Vue 2.7 #272

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
os: [ubuntu-latest]
vue: [2.6, 2.7, 3]
fail-fast: false

runs-on: ${{ matrix.os }}

steps:
Expand All @@ -26,7 +26,7 @@ jobs:
node-version: ${{ matrix.node }}
- run: npm -v
- run: node .github/test.js "npm" ${{ matrix.vue }}

yarn:
strategy:
matrix:
Expand All @@ -35,7 +35,7 @@ jobs:
vue: [2.6, 2.7, 3]
yarn: [latest]
type: [commonjs, module]

runs-on: ${{ matrix.os }}

steps:
Expand All @@ -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:
Expand All @@ -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 }}
7 changes: 6 additions & 1 deletion lib/v2.7/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}

// not implemented in Vue 2, only in Vue 3.3+
exports.toValue = function(source) {
return typeof source === 'function' ? source() : VueModule.unref(source)
}
39 changes: 38 additions & 1 deletion lib/v2.7/index.d.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -36,3 +47,29 @@ export declare function createApp(rootComponent: any, rootProps?: any): App
// #endregion

export declare function hasInjectionContext(): boolean

export type MaybeRef<T = any> =
| T
| Ref<T>
| ShallowRef<T>
| WritableComputedRef<T>

export type MaybeRefOrGetter<T = any> = MaybeRef<T> | ComputedRef<T> | (() => 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<T>(source: MaybeRefOrGetter<T>): T
7 changes: 6 additions & 1 deletion lib/v2.7/index.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Vue from 'vue'
import { getCurrentInstance } from 'vue'
import { getCurrentInstance, unref } from 'vue'

var isVue2 = true
var isVue3 = false
Expand Down Expand Up @@ -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)
}
Loading