vue-ts-async
Library for extremely type-safe Typescript Vue asynchronous
data
andcomputed
properties.
This library has two functions/decorators that allow you create extremely type-safe data
and computed
properties on Vue components using asynchronous functions.
Has convenient features for:
- loading, pending, and error flags
- ability to refresh data
- debouncing, with
cancel
andnow
functions - defaults
- error handling
Install and Setup
# peer dependency npm install --save vue vue-property-decorator npm install --save vue-ts-async
Here's a simple example.
// src/vue-async.ts // this object holds your default settings,// and you'll use it in other components// to create `data` and `computed` propertiesasync
Then in the component itself:
// <script lang="ts">
Very type-safe
This library uses advanced conditional types and function overloading to change the types of async.data
and async.computed
properties. If something could be null, you'll be informed.
API
type ErrorHandler = (e: Error) => void
A type alias for error handler functions.
function VueAsync({ debounce?: number = 1000, error?: ErrorHandler }) => VueTsAsync
The top level function that returns an instance used to create all other data
and computed
properties.
class VueTsAsync
The class that is returned by the VueAsync
function. Has a data
function to create data properties, computed
function to create computed properties, and a Computed
decorator that must be used on computed properties to make them reactive.
function data(vm: V extends Vue, options: AsyncDataOptions<T>) => AsyncData<T>
The full options object has this shape:
function data(vm: V extends Vue, (this: V) => Promise<T>) => AsyncData<T>
The data
function has a simple overload that allows you to only pass a function. It is equivalent to passing an options object with only the get
value.
class AsyncData<T>
This is (basically) what is returned from the data
function, and is the object the rest of your component will interact with.
The types of the properties of AsyncData
depend on what values you give for default
and lazy
.
function computed(vm: V extends Vue, options: AsyncComputedOptions<T>) => AsyncComputed<T>
The computed
function reactively updates when its dependencies change, and it must be decorated with Computed
. It can be debounced so that many quick reactive triggers don't cause a flood of asynchronous function calls, which can be very expensive.
You have to provide a get
function that returns a promise, and either a watch
or watchClosely
parameter which are either arrays of strings referring to properties on the vue instance, or a function that returns an object you want tracked.
You might be asking "Why are watch
and watchClosely
necessary? Why not just pass a function that's reactively watched?" Well, in order for Vue to reactively track a function, it has to invoke that function when you create the watcher. Since we have a function that performs an expensive async operation, which we also want to debounce, we can't really do that.
class AsyncComputed<T>
This is (basically) what is returned from the computed
function, and is the object the rest of your component will interact with.
The types of the properties of AsyncComputed
depend on what values you give for default
, eager
, watch
, and watchClosely
.
Computed: VueDecorator
This decorator must be used on async.computed
properties to make them reactive.
Contributing
This package has testing set up with mocha and chai expect. Since many of the tests are on the functionality of Vue components, the vue testing docs are a good place to look for guidance.
If you'd like to contribute, perhaps because you uncovered a bug or would like to add features:
- fork the project
- clone it locally
- write tests to either to reveal the bug you've discovered or cover the features you're adding (write them in the
test
directory, and take a look at existing tests as well as the mocha, chai expect, and vue testing docs to understand how) - run those tests with
npm test
(usenpm test -- -g "text matching test description"
to only run particular tests) - once you're done with development and all tests are passing (including the old ones), submit a pull request!