question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Hello everyone,

While on a project, i had to dynamically call functions but the arguments I called them with were sometimes Refs. So my first thought was wrapping the function using the reactify utility; But then I remembered the wrapped function returns ComputedRef<T> and is evaluated on every changes of the arguments. And I want the function to only return T and only when I call it. I started looking for other utilities that might help and either did not found suitable one or did not search thoroughly enough.

This is where unrefy comes from. Below is it’s documentation entry.

Pull Request : #789

Documentation

Convert a plain function into a function that unref it’s aguments before every call. The converted function accepts refs and raw values as its arguments and returns the same value the unconverted function returns, with proper typing.

::: tip Make sure you’re using the right tool for the job. Using reactify might be more pertinent in some cases where you want to evaluate the function on each changes of it’s arguments. :::

Usage

import axios from 'axios'
import { ref } from 'vue-demi'
import { unrefy } from '.'

const url = ref('https://httpbin.org/post')
const data = ref({foo: 'bar'})
const post = (url, data) => axios.post(url, data)
const postUnrefied = unrefy(post)

post(url, data)           /* ❌ Will throw an error because the arguments are refs */
postUnrefied(url, data)   /* ✔️ Will Work because the arguments will be "unrefied" */

Related Functions

  • reactify

Source

import { unref } from 'vue-demi'
import { MaybeRef } from '../../shared/utils'

/** Unrefied method. */
type Unrefy<T> = T extends (...args: infer A) => infer R ? (...args: {
  [K in keyof A]: MaybeRef<A[K]>
}) => R : never

/**
 * Convert a plain function into a function that unref it's aguments before every call.
 * The converted function accepts refs as its arguments and returns the same value
 * the unconverted function returns, with proper typing.
 * @param fn - Source function
 */
export const unrefy = <T extends Function>(fn: T): Unrefy<T> => {
  return function(this: any, ...args: any[]) {
    return fn.apply(this, args.map(i => unref(i)))
  } as Unrefy<T>
}

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
antfucommented, Sep 25, 2021

I see. But maybe the ref sugar could be a better solution for this? https://github.com/vuejs/rfcs/discussions/369

Read more comments on GitHub >

github_iconTop Results From Across the Web

Belli Online Only Elasticity Belly Oil - Pinterest
Featuring Vitamin E, Gotu Kola, and Lavender oil, it nourishes and moisturizes to help skin ... Golden Jojoba Oil Pure Natural - 4...
Read more >
Jules Kennedvitch - Facebook
This was a quick project, a little bit unrefi... Keryliz Perez and 2 others ... Thanks to simply_kenna for featuring one of my...
Read more >
Est locus uni cuique suus: City and Status in Horace's Satires ...
Lucilius' style as muddy (lutulentus)—i.e., unrefi ned in the ... from the Augustan age featuring the god Priapus and his phallus.72 This linkage....
Read more >
Julian Perera (@norabeZ_DJ) / Twitter
Episode 5 of The Amazing Electronic Music is here, featuring 5 tracks selected by DJ Scotty Ryan. If you are a fan of...
Read more >
The Bluff Magazine Fall/Winter 2021 by Palmetto Bluff - Issuu
... featuring carefully curated photographs and stories about the people, pl. ... While it may not have the unrefi ned rawness of folk...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found