diff --git a/README.md b/README.md index 0a43135..bc72ac3 100644 --- a/README.md +++ b/README.md @@ -8,17 +8,17 @@ ### How to install ```bash -npm install vuex-simple-cache +npm install @vencakrecl/vuex-simple-cache ``` ```bash -yarn add vuex-simple-cache +yarn add @vencakrecl/vuex-simple-cache ``` ### How to use ```js import Vue from 'vue'; import Vuex, { Store } from 'vuex'; -import VuexSimpleCache from 'vuex-simple-cache'; +import VuexSimpleCache from '@vencakrecl/vuex-simple-cache'; Vue.use(Vuex); @@ -53,6 +53,7 @@ store.registerModule('test', { * key - name of key from vuex state * action - standard vuex action * expiration - time in seconds +* onCache - callback for cached data (default return cached data) [npm]: https://img.shields.io/npm/v/@vencakrecl/vuex-simple-cache.svg?style=flat-square [license]: https://img.shields.io/npm/l/@vencakrecl/vuex-simple-cache.svg?style=flat-square diff --git a/package.json b/package.json index f0661ab..29ffb1e 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "@vencakrecl/vuex-simple-cache", "author": "Venca Krecl ", "description": "Simple cache for vuex action", - "version": "1.0.0-alpha.9", + "version": "1.0.0-beta.0", "license": "MIT", "repository": { "type": "git", @@ -27,6 +27,7 @@ "build": "tsdx build", "test": "tsdx test", "lint": "tsdx lint", + "lint-fix": "tsdx lint --fix", "prepare": "tsdx build", "size": "size-limit", "analyze": "size-limit --why", diff --git a/src/index.ts b/src/index.ts index 82d5f76..422d230 100644 --- a/src/index.ts +++ b/src/index.ts @@ -10,7 +10,10 @@ class VuexSimpleCache { public cacheAction = , R>( key: string, action: (context: ActionContext) => any, - expiration: number = 0 + expiration: number = 30, + onCache: (context: ActionContext, data: any) => any = (_, data) => { + return data; + } ): any => { return (context: ActionContext) => { if (!(key in context.state)) { @@ -31,7 +34,7 @@ class VuexSimpleCache { if (Array.isArray(context.state[key])) { if (context.state[key].length) { - return context.state[key]; + return onCache(context, context.state[key]); } return action(context); @@ -39,14 +42,14 @@ class VuexSimpleCache { if (context.state[key] instanceof Object) { if (Object.keys(context.state[key]).length) { - return context.state[key]; + return onCache(context, context.state[key]); } return action(context); } if (context.state[key]) { - return context.state[key]; + return onCache(context, context.state[key]); } return action(context);