vuex-cache
Advanced tools
Comparing version 0.3.1 to 1.0.0
31
index.js
'use strict'; | ||
var index = (function (store) { | ||
var cache = Object.create(null); | ||
store.cacheDispatch = function cacheDispatch() { | ||
store.cache = new Map(); | ||
store.cache.dispatch = function () { | ||
var type = arguments[0]; | ||
if (type in cache) { | ||
return cache[type]; | ||
if (!store.cache.has(type)) { | ||
store.cache.set(type, store.dispatch.apply(store, arguments)); | ||
} | ||
cache[type] = store.dispatch.apply(store, arguments); | ||
return cache[type]; | ||
return store.cache.get(type); | ||
}; | ||
store.removeCache = function (actionName) { | ||
if (actionName in cache) { | ||
delete cache[actionName]; | ||
return true; | ||
} | ||
return false; | ||
}; | ||
store.hasCache = function (key) { | ||
return key in cache; | ||
}; | ||
store.clearCache = function () { | ||
for (var key in cache) { | ||
delete cache[key]; | ||
} | ||
return true; | ||
}; | ||
}); | ||
module.exports = index; |
{ | ||
"name": "vuex-cache", | ||
"version": "0.3.1", | ||
"version": "1.0.0", | ||
"description": "cache vuex action", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
# vuex cache action | ||
* * * | ||
When vuex action fetch some data by request remote api, vuex-cache can store the action result, when next time the same action runs, it will not make a new request and just return the cached result. | ||
## install | ||
### Compatibility | ||
Compatible with Vue2.x and Vuex2.x | ||
### install | ||
```bash | ||
@@ -12,3 +13,3 @@ npm install vuex-cache | ||
## usage | ||
### usage | ||
@@ -34,9 +35,15 @@ ```javascript | ||
store.cacheDispatch('LIST') | ||
store.cache.dispatch('LIST') | ||
``` | ||
## api | ||
### api | ||
#### NOTICE: after update to 1.0.0, main api is different from the previous version 0.3.1 | ||
1.0.0 need your env has **Map** object, or import **Map** from babel-polyfill | ||
thanks [VitorLuizC](https://github.com/VitorLuizC) | ||
```javascript | ||
store.cacheDispatch(ACTION_NAME) | ||
store.cache.dispatch(ACTION_NAME) | ||
``` | ||
@@ -48,3 +55,3 @@ params is same with vuex store.dispatch | ||
```javascript | ||
store.removeCache(ACTION_NAME) | ||
store.cache.delete(ACTION_NAME) | ||
``` | ||
@@ -54,3 +61,3 @@ remove cached action, will **not** remove the data in store. when call cacheDispatch with same type, the request in that action will run again. | ||
```javascript | ||
store.hasCache(ACTION_NAME) | ||
store.cache.has(ACTION_NAME) | ||
``` | ||
@@ -60,4 +67,4 @@ return bool if ACTION\_NAME has been cached | ||
```javascript | ||
store.clearCache() | ||
store.cache.clear() | ||
``` | ||
clear all cached keys |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
0
66
3191
12