npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vue-async-cache

v1.1.0

Published

`vue-async-cache` is a library to cache asynchrone function call between different component. It was initially build to improve cache of api call with VueJs. This library can be especially useful to cache some fetch query, for example using axios. The con

Downloads

5

Readme

vue-async-cache

vue-async-cache is a library to cache asynchrone function call between different component. It was initially build to improve cache of api call with VueJs. This library can be especially useful to cache some fetch query, for example using axios. The concept was inspired from Apollo cache, even if it is far from being comparable.

The library take care to save the response of the async function and share it between components using context api. It will also avoid unnecessary call made simultaneously to the same async function. It will identify the cache id base on the name of the function and the parameters passed. So if you call multiple times the same function with different parameters, it will not use the same cache. Example:

api is an async function

export api = async (param1, param2) => ...
  call(api, '/counter');
  call(api, '/timer');

This 2 call to api function will have different cache because they don't share the same parameters.

  call(api, '/counter');
  call(api, '/counter');

This 2 call to api function will have the same cache and api will be called only once.

Example

See full example at here.

counter-example

Counter.vue

<template>
  <div class="counter">
    <p>Counter: {{count}}</p>
  </div>
</template>

<script lang="ts">
import {
  Component,
  Vue,
} from "vue-property-decorator";
import { api } from "../mockapi";
import { useAsyncCacheWatch } from "vue-async-cache";

@Component
export default class Counter extends Vue {
  private cacheWatch = useAsyncCacheWatch(api, "/counter");

  get count() {
    return this.cacheWatch.getResponse();
  }

  async mounted() {
    this.cacheWatch.call();
  }
}
</script>

app.vue

<template>
  <div id="app">
    <img alt="Vue logo" src="./assets/logo.png">
    <Counter />
    <Counter />
  </div>
</template>

<script lang="ts">
import { Component, Vue, Provide } from "vue-property-decorator";
import Counter from "./components/Counter.vue";

@Component({
  components: {
    Counter,
  }
})
export default class App extends Vue {}
</script>

In this example, without cache there would have been 2 calls to the api, but using vue-async-cache there is only 1 call. The library will take care to populate the response to all the components.

update cache

vue-async-cache provide as well different way to interact with the cache:

<template>
  <div class="hello">
    <button @click="increment()">+</button>
    <button @click="reset()">reset</button>
  </div>
</template>

<script lang="ts">
import {
  Component,
  Vue,
} from "vue-property-decorator";
import { api } from "../mockapi";
import { asyncCache, useAsyncCacheWatch } from "vue-async-cache";

@Component
export default class SetCounter extends Vue {
  private cacheWatch = useAsyncCacheWatch(api, "/counter");
  private cacheState!: any;

  async increment() {
      const count = this.cacheWatch.cache();
      const response = await api('/counter', 'POST', { value: count + 1 });
      await this.cacheWatch.update(response);
  }

  async reset() {
      const response = await api('/counter', 'POST', { value: 1 });
      await this.cacheWatch.update(response);
  }

  get response() {
    return this.cacheWatch.getResponse();
  }
}
</script>

How to use it

vue-async-cache is using the store pattern to share the state between component. So the only thing you have to do is to import the store in the component:

import { asyncCache } from "vue-async-cache";

asyncCache

asyncCache store provide methods and properties to interact with the cache: call, update, cache and responses.

asyncCache.call is a method that allow to cache the original function call. The first given parameter to call is the function you want to cache. The next parameters are the parameters you would have providen to the function you want to cache. call will return the id of the corresponding response in the cache.

async asyncCache.call(fn: (...args: any) => Promise<any>, ...args: any) => Promise<string>

eg.:

await asyncCache.call(getItems);
const id = await asyncCache.call(getItem, 'id-20', { withComment: true });

Note: in most of the case it is not necessary to use call and instead prefer using load from useAsyncCacheWatch that will take care to observe changes on the response.

asyncCache.cache is a asyncCache to access the cache. It work the same way as the call method, but it will retrieve the response from the cache, instead to call the async function.

update is a asyncCache that allow to update the cache without to make a call to the server. The first parameter is the new response you want to set. The second parameter is the cached function. The next parameter are the parameters you would have providen to the cached function.

eg.:

await asyncCache.update([
    {id:'id-1', title: 'hello'},
    {id:'id-2', title: 'hello2'},
], getItems);
await asyncCache.update({
    id:'id-1',
    title: 'hello',
    content: 'Lorem ipsum dolor sit amet, ac augue malesuada, tellus amet',
    comments: [],
}, getItem, 'id-20', { withComment: true });

asyncCache.responses property is the actual cache representing all the asynchrone call, error and response. To access the cache prefer using the cache function instead.

useAsyncCacheWatch

useAsyncCacheWatch helper is used for watching a specific response, allowing to automatically update the state of a component. This helper return the same attributes as asyncCache plus 3 extra attributes, getResponse and getError.

To use useAsyncCacheWatch, you need to provide some parameters, the first given parameter is the function you want to cache. The next parameters are the parameters you would have providen to the function you want to cache. It is actually exactly the same parameters as for asyncCache.call method.

import { useAsyncCacheWatch } from "vue-async-cache";

@Component
export default class Counter extends Vue {
  private cacheWatch = useAsyncCacheWatch(api, "/counter");

  get count() {
    return this.cacheWatch.getResponse();
  }

  async mounted() {
    this.cacheWatch.call();
  }
}

call does the same as asyncCache.call but you don't need to provide anymore the parameters.

cache does the same as asyncCache.cache but you don't need to provide anymore the parameters.

update does the same as asyncCache.update but you need to provide only the first parameter.

getResponse retrieve the response received after the function has been called.

getError retrieve the error received if the function called failed.