Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

vuex-cache

Package Overview
Dependencies
Maintainers
2
Versions
19
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

vuex-cache - npm Package Compare versions

Comparing version 3.0.0 to 3.1.0

dist/vuex-cache.esm.js

39

CHANGELOG.md
# CHANGELOG
## 3.1.0
- Add `mapCacheAction` function helper to call actions with cache on components.
```js
import { mapCachedActions } from 'vuex-cache';
export default {
name: 'Users',
async mounted() {
this.GET_USER();
this.FETCH_REPOSITORY(219, {
timeout: 30000
});
}
methods: {
...mapCachedActions(['FETCH_REPOSITORY']),
...mapCachedActions('user', ['GET_USER'])
}
}
```
Thanks to [@cwilby](https://github.com/cwilby) for PR [#37](https://github.com/superwf/vuex-cache/pull/37).
## 3.0.0

@@ -26,4 +50,4 @@

actions: {
'ACTION': () => {}
}
ACTION: () => {},
},
})

@@ -35,4 +59,4 @@

store.cache.dispatch('ACTION', undefined, {
timeout: 100
});
timeout: 100,
})

@@ -113,2 +137,3 @@ store.cache.has('ACTION')

- Improve documentation.
- Fix title on `README.md`;

@@ -202,3 +227,3 @@ - Add `CHANGELOG` file.

param: payload,
timeout: 100
timeout: 100,
})

@@ -208,3 +233,3 @@

store.cache.dispatch('UPDATE_USER', payload, {
timeout: 100
timeout: 100,
})

@@ -233,3 +258,3 @@ ```

type: 'UPDATE_USER',
param: payload
param: payload,
})

@@ -236,0 +261,0 @@

/*!
* vuex-cache v3.0.0
* vuex-cache v3.1.0
* (c) superwf@gmail.com

@@ -220,4 +220,69 @@ * Released under the MIT License.

});
for (var namespace in store._modulesNamespaceMap) {
var module = getModuleByNamespace(store, 'mapCacheActions', namespace);
Object.defineProperty(module.context, 'cache', {
value: cache,
writable: false,
enumerable: true,
configurable: false
});
}
};
/**
* Normalize the map
* normalizeMap([1, 2, 3]) => [ { key: 1, val: 1 }, { key: 2, val: 2 }, { key: 3, val: 3 } ]
* normalizeMap({a: 1, b: 2, c: 3}) => [ { key: 'a', val: 1 }, { key: 'b', val: 2 }, { key: 'c', val: 3 } ]
* @param {Array|Object} map
* @return {Object}
*/
var normalizeMap = function (map) {
return Array.isArray(map) ? map.map(function (key) { return ({
key: key,
val: key
}); }) : Object.keys(map).map(function (key) { return ({
key: key,
val: map[key]
}); });
};
/**
* Search a special module from store by namespace. if module not exist, print error message.
* @param {Object} store
* @param {String} helper
* @param {String} namespace
* @return {Object}
*/
var getModuleByNamespace = function (store, helper, namespace) {
var module = store._modulesNamespaceMap[namespace];
if (process.env.NODE_ENV !== 'production' && !module) {
console.error(("[vuex-cache] module namespace not found in " + helper + "(): " + namespace));
}
return module;
};
/**
* Return a function expect two param contains namespace and map. it will normalize the namespace and then the param's function will handle the new namespace and the map.
* @param {Function} fn
* @return {Function}
*/
var normalizeNamespace = function (fn) {
return function (namespace, map) {
if (typeof namespace !== 'string') {
map = namespace;
namespace = '';
} else if (namespace.charAt(namespace.length - 1) !== '/') {
namespace += '/';
}
return fn(namespace, map);
};
};
/**
* Type alias for Action.

@@ -240,2 +305,43 @@ * @typedef {import('vuex').Action<any, any>} Action

/**
* Create cache actions object to map to a component
* @param {String} namespace
* @param {Array} actions
* @returns {Object}
*/
var mapCacheActions = normalizeNamespace(function (namespace, actions) {
var res = {};
normalizeMap(actions).forEach(function (ref) {
var key = ref.key;
var val = ref.val;
res[key] = function mappedAction() {
var this$1 = this;
var args = [], len = arguments.length;
while ( len-- ) args[ len ] = arguments[ len ];
var dispatch = this.$store.cache.dispatch;
if (namespace) {
var module = getModuleByNamespace(this.$store, 'mapCacheActions', namespace);
if (!module) {
return;
} // dispatch = module.context.cache.dispatch;
dispatch = typeof val === 'function' ? function () {
var params = [], len = arguments.length;
while ( len-- ) params[ len ] = arguments[ len ];
module.context.cache.dispatch.apply(this$1.$store, [("" + namespace + (params[0]))].concat(params.slice(1)));
} : module.context.cache.dispatch;
}
return typeof val === 'function' ? val.apply(this, [dispatch].concat(args)) : dispatch.apply(this.$store, [("" + namespace + val)].concat(args));
};
});
return res;
});
/**
* Create cache with options and define it on store instance.

@@ -250,1 +356,2 @@ * @param {Options} options

exports.default = createCache;
exports.mapCacheActions = mapCacheActions;
/*!
* vuex-cache v3.0.0
* vuex-cache v3.1.0
* (c) superwf@gmail.com

@@ -222,4 +222,69 @@ * Released under the MIT License.

});
for (var namespace in store._modulesNamespaceMap) {
var module = getModuleByNamespace(store, 'mapCacheActions', namespace);
Object.defineProperty(module.context, 'cache', {
value: cache,
writable: false,
enumerable: true,
configurable: false
});
}
};
/**
* Normalize the map
* normalizeMap([1, 2, 3]) => [ { key: 1, val: 1 }, { key: 2, val: 2 }, { key: 3, val: 3 } ]
* normalizeMap({a: 1, b: 2, c: 3}) => [ { key: 'a', val: 1 }, { key: 'b', val: 2 }, { key: 'c', val: 3 } ]
* @param {Array|Object} map
* @return {Object}
*/
var normalizeMap = function (map) {
return Array.isArray(map) ? map.map(function (key) { return ({
key: key,
val: key
}); }) : Object.keys(map).map(function (key) { return ({
key: key,
val: map[key]
}); });
};
/**
* Search a special module from store by namespace. if module not exist, print error message.
* @param {Object} store
* @param {String} helper
* @param {String} namespace
* @return {Object}
*/
var getModuleByNamespace = function (store, helper, namespace) {
var module = store._modulesNamespaceMap[namespace];
if (process.env.NODE_ENV !== 'production' && !module) {
console.error(("[vuex-cache] module namespace not found in " + helper + "(): " + namespace));
}
return module;
};
/**
* Return a function expect two param contains namespace and map. it will normalize the namespace and then the param's function will handle the new namespace and the map.
* @param {Function} fn
* @return {Function}
*/
var normalizeNamespace = function (fn) {
return function (namespace, map) {
if (typeof namespace !== 'string') {
map = namespace;
namespace = '';
} else if (namespace.charAt(namespace.length - 1) !== '/') {
namespace += '/';
}
return fn(namespace, map);
};
};
/**
* Type alias for Action.

@@ -242,2 +307,43 @@ * @typedef {import('vuex').Action<any, any>} Action

/**
* Create cache actions object to map to a component
* @param {String} namespace
* @param {Array} actions
* @returns {Object}
*/
var mapCacheActions = normalizeNamespace(function (namespace, actions) {
var res = {};
normalizeMap(actions).forEach(function (ref) {
var key = ref.key;
var val = ref.val;
res[key] = function mappedAction() {
var this$1 = this;
var args = [], len = arguments.length;
while ( len-- ) args[ len ] = arguments[ len ];
var dispatch = this.$store.cache.dispatch;
if (namespace) {
var module = getModuleByNamespace(this.$store, 'mapCacheActions', namespace);
if (!module) {
return;
} // dispatch = module.context.cache.dispatch;
dispatch = typeof val === 'function' ? function () {
var params = [], len = arguments.length;
while ( len-- ) params[ len ] = arguments[ len ];
module.context.cache.dispatch.apply(this$1.$store, [("" + namespace + (params[0]))].concat(params.slice(1)));
} : module.context.cache.dispatch;
}
return typeof val === 'function' ? val.apply(this, [dispatch].concat(args)) : dispatch.apply(this.$store, [("" + namespace + val)].concat(args));
};
});
return res;
});
/**
* Create cache with options and define it on store instance.

@@ -252,2 +358,3 @@ * @param {Options} options

exports.default = createCache;
exports.mapCacheActions = mapCacheActions;

@@ -254,0 +361,0 @@ Object.defineProperty(exports, '__esModule', { value: true });

4

dist/vuex-cache.umd.min.js
/*!
* vuex-cache v3.0.0
* vuex-cache v3.1.0
* (c) superwf@gmail.com
* Released under the MIT License.
*/
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e=e||self).VuexCache={})}(this,function(e){"use strict";var t=function(e){return!!e&&"object"==typeof e},r=new Error("Can't generate key from parameters."),n=function(e){try{var n=function(e){return t(e[0])?[e[0].type,e[0],e[1]]:e}(e),u=n[0],i=n[1];return u+":"+(t(o=i)?JSON.stringify(o):String(o))}catch(e){return r}var o},u=function(e){return t(e)&&"number"==typeof e.timeout},i=function(e){return!!e&&Date.now()>e},o=new Map,a=function(e,a){var c={dispatch:function(){for(var t=[],c=arguments.length;c--;)t[c]=arguments[c];var f=n(t);if(f===r)return e.dispatch.apply(e,t);var l=o.get(f)||{},p=l.value,s=l.expiresIn;if(p&&!i(s))return p;var v=function(e,t){var r="string"==typeof e[0]?e[2]:e[0];return u(r)?r.timeout:u(t)?t.timeout:0}(t,a),d={expiresIn:v?Date.now()+v:void 0,value:e.dispatch.apply(e,t)};return o.set(f,d),d.value.catch(function(e){return o.delete(f),Promise.reject(e)})},has:function(){for(var e=[],u=arguments.length;u--;)e[u]=arguments[u];var a=n(e);if(a===r)return!1;var c=o.get(a);return t(c)&&!i(c.expiresIn)},clear:function(){return o.clear()},delete:function(){for(var e=[],t=arguments.length;t--;)e[t]=arguments[t];var u=n(e);return u!==r&&o.delete(u)}};Object.defineProperty(e,"cache",{value:c,writable:!1,enumerable:!0,configurable:!1})};e.cacheAction=function(e,t){return function(r,n){return a(r,t),e.call(this,r,n)}},e.default=function(e){return function(t){return a(t,e)}},Object.defineProperty(e,"__esModule",{value:!0})});
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e=e||self).VuexCache={})}(this,function(e){"use strict";var t,n=function(e){return!!e&&"object"==typeof e},r=new Error("Can't generate key from parameters."),a=function(e){try{var t=function(e){return n(e[0])?[e[0].type,e[0],e[1]]:e}(e),a=t[0],c=t[1];return a+":"+(n(o=c)?JSON.stringify(o):String(o))}catch(e){return r}var o},c=function(e){return n(e)&&"number"==typeof e.timeout},o=function(e){return!!e&&Date.now()>e},i=new Map,u=function(e,t){var u={dispatch:function(){for(var n=[],u=arguments.length;u--;)n[u]=arguments[u];var f=a(n);if(f===r)return e.dispatch.apply(e,n);var p=i.get(f)||{},s=p.value,l=p.expiresIn;if(s&&!o(l))return s;var h=function(e,t){var n="string"==typeof e[0]?e[2]:e[0];return c(n)?n.timeout:c(t)?t.timeout:0}(n,t),v={expiresIn:h?Date.now()+h:void 0,value:e.dispatch.apply(e,n)};return i.set(f,v),v.value.catch(function(e){return i.delete(f),Promise.reject(e)})},has:function(){for(var e=[],t=arguments.length;t--;)e[t]=arguments[t];var c=a(e);if(c===r)return!1;var u=i.get(c);return n(u)&&!o(u.expiresIn)},clear:function(){return i.clear()},delete:function(){for(var e=[],t=arguments.length;t--;)e[t]=arguments[t];var n=a(e);return n!==r&&i.delete(n)}};for(var p in Object.defineProperty(e,"cache",{value:u,writable:!1,enumerable:!0,configurable:!1}),e._modulesNamespaceMap){var s=f(e,"mapCacheActions",p);Object.defineProperty(s.context,"cache",{value:u,writable:!1,enumerable:!0,configurable:!1})}},f=function(e,t,n){var r=e._modulesNamespaceMap[n];return"production"===process.env.NODE_ENV||r||console.error("[vuex-cache] module namespace not found in "+t+"(): "+n),r},p=(t=function(e,t){var n,r={};return(n=t,Array.isArray(n)?n.map(function(e){return{key:e,val:e}}):Object.keys(n).map(function(e){return{key:e,val:n[e]}})).forEach(function(t){var n=t.key,a=t.val;r[n]=function(){for(var t=this,n=[],r=arguments.length;r--;)n[r]=arguments[r];var c=this.$store.cache.dispatch;if(e){var o=f(this.$store,"mapCacheActions",e);if(!o)return;c="function"==typeof a?function(){for(var n=[],r=arguments.length;r--;)n[r]=arguments[r];o.context.cache.dispatch.apply(t.$store,[""+e+n[0]].concat(n.slice(1)))}:o.context.cache.dispatch}return"function"==typeof a?a.apply(this,[c].concat(n)):c.apply(this.$store,[""+e+a].concat(n))}}),r},function(e,n){return"string"!=typeof e?(n=e,e=""):"/"!==e.charAt(e.length-1)&&(e+="/"),t(e,n)});e.cacheAction=function(e,t){return function(n,r){return u(n,t),e.call(this,n,r)}},e.default=function(e){return function(t){return u(t,e)}},e.mapCacheActions=p,Object.defineProperty(e,"__esModule",{value:!0})});
//# sourceMappingURL=vuex-cache.umd.min.js.map
{
"name": "vuex-cache",
"version": "3.0.0",
"version": "3.1.0",
"description": "Cache dispatched actions and prevent repeated requests and heavy actions.",

@@ -9,3 +9,3 @@ "cdn": "dist/vuex-cache.umd.min.js",

"unpkg": "dist/vuex-cache.umd.min.js",
"module": "dist/vuex-cache.mjs",
"module": "dist/vuex-cache.esm.js",
"jsdelivr": "dist/vuex-cache.umd.min.js",

@@ -42,22 +42,22 @@ "umd:main": "dist/vuex-cache.umd.js",

"devDependencies": {
"@babel/core": "^7.3.4",
"@babel/plugin-transform-runtime": "^7.3.4",
"@babel/preset-env": "^7.3.4",
"@babel/runtime": "^7.3.4",
"babel-core": "^7.0.0-bridge",
"@babel/core": "^7.4.3",
"@babel/plugin-transform-runtime": "^7.4.3",
"@babel/preset-env": "^7.4.3",
"@babel/runtime": "^7.4.3",
"babel-core": "^6.26.3",
"babel-eslint": "^10.0.1",
"babel-jest": "^24.3.1",
"bili": "^4.4.0",
"eslint": "^5.15.1",
"babel-jest": "^24.7.1",
"bili": "^4.7.3",
"eslint": "^5.16.0",
"eslint-config-prettier": "^4.1.0",
"eslint-config-standard": "^12.0.0",
"eslint-plugin-import": "^2.16.0",
"eslint-plugin-jest": "^22.3.0",
"eslint-plugin-jest": "^22.4.1",
"eslint-plugin-node": "^8.0.1",
"eslint-plugin-prettier": "^3.0.1",
"eslint-plugin-promise": "^4.0.1",
"eslint-plugin-promise": "^4.1.1",
"eslint-plugin-standard": "^4.0.0",
"jest": "^24.3.1",
"jest": "^24.7.1",
"prettier": "^1.16.4",
"vue": "^2.6.8",
"vue": "^2.6.10",
"vuex": "^3.1.0"

@@ -70,3 +70,3 @@ },

"testMatch": [
"<rootDir>/__test__/**/?(*.)(spec|test).js"
"<rootDir>/__test__/*.test.js"
],

@@ -73,0 +73,0 @@ "transform": {

@@ -180,2 +180,24 @@ # vuex-cache

### `mapCacheActions`
Create component methods that dispatch a cached action.
```js
import { mapCacheActions } from 'vuex-cache';
export default {
name: 'Users',
methods: {
...mapCacheActions(['FETCH_REPOSITORY']),
...mapCacheActions('user', ['GET_USER']),
},
async mounted() {
this.GET_USER();
this.FETCH_REPOSITORY(219, {
timeout: 30000
});
}
}
```
### Payload

@@ -182,0 +204,0 @@

@@ -1,2 +0,10 @@

import { Action, Store, ActionContext, Payload, DispatchOptions } from 'vuex';
import Vue from 'vue'
import {
Action,
ActionContext,
Dispatch,
DispatchOptions,
Payload,
Store,
} from 'vuex'

@@ -7,4 +15,7 @@ interface StoreCache {

*/
dispatch (type: string, payload?: any, options?: DispatchOptions): Promise<any>;
dispatch <P extends Payload> (payloadWithType: P, options?: DispatchOptions): Promise<any>;
dispatch(type: string, payload?: any, options?: DispatchOptions): Promise<any>
dispatch<P extends Payload>(
payloadWithType: P,
options?: DispatchOptions,
): Promise<any>

@@ -14,4 +25,4 @@ /**

*/
has (type: string, payload?: any): boolean;
has <P extends Payload> (payloadWithType: P): boolean;
has(type: string, payload?: any): boolean
has<P extends Payload>(payloadWithType: P): boolean

@@ -21,3 +32,3 @@ /**

*/
clear (): boolean;
clear(): boolean

@@ -28,4 +39,4 @@ /**

*/
delete (type: string, payload?: any): boolean;
delete <P extends Payload> (payloadWithType: P): boolean;
delete(type: string, payload?: any): boolean
delete<P extends Payload>(payloadWithType: P): boolean
}

@@ -38,3 +49,3 @@

*/
timeout?: number;
timeout?: number
}

@@ -46,7 +57,7 @@

*/
timeout?: number;
timeout?: number
}
interface Store<S> {
readonly cache: StoreCache;
readonly cache: StoreCache
}

@@ -59,3 +70,3 @@ }

*/
timeout?: number;
timeout?: number
}

@@ -69,6 +80,6 @@

injectee: ActionContext<S, R> & {
readonly cache: StoreCache;
readonly cache: StoreCache
},
payload: any,
) => any;
) => any

@@ -80,3 +91,6 @@ /**

*/
export const cacheAction: <S, R>(action: ActionHandlerWithCache<S, R>, options?: Options) => ActionHandlerWithCache<S, R>;
export const cacheAction: <S, R>(
action: ActionHandlerWithCache<S, R>,
options?: Options,
) => ActionHandlerWithCache<S, R>

@@ -87,4 +101,25 @@ /**

*/
declare const createCache: (options?: Options) => (store: Store<any>) => void;
declare const createCache: (options?: Options) => (store: Store<any>) => void
export default createCache;
export default createCache
type Actions = Record<string, (...args: any[]) => Promise<any>>
type ActionCaller = (
this: Vue & Record<string, any>,
dispatch: Dispatch,
...args: any[]
) => any
/**
* Create cache actions object to map to a component.
* @param namespace
* @param actions
*/
export declare const mapCacheActions: {
(actions: string[] | Record<string, string | ActionCaller>): Actions
(
namespace: string,
actions: string[] | Record<string, string | ActionCaller>,
): Actions
}

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc