Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bundle down ESM and CommonJS modules #13

Merged
merged 2 commits into from
Nov 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions .npmignore

This file was deleted.

115 changes: 115 additions & 0 deletions dist/vuex-cache.cjs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*!
* vuex-cache v1.4.0
* (c) 2017-present [email protected]
* Released under the MIT License.
*/
'use strict';

var isVuexStore = function isVuexStore(obj) {
return 'dispatch' in obj && typeof obj.dispatch === 'function';
}; // convert string or obj to string


var toString = function toString(arg) {
return typeof arg === 'string' ? arg : JSON.stringify(arg);
}; // convert arguments to string


var argsToString = function argsToString(args) {
var type = toString(args[0]);

if (args[1]) {
type = "".concat(type, ":").concat(toString(args[1]));
}

return type;
}; // parse timeout prop in option


var getTimeout = function getTimeout(args, option) {
if (args.length === 1 && args[0].timeout) {
return args[0].timeout;
}

if (args.length === 3 && args[2].timeout) {
return args[2].timeout;
}

if (option && option.timeout) {
return option.timeout;
}

return 0;
};

var cachePlugin = function cachePlugin(store, option) {
var cache = new Map(); // use another map to store timeout for each type

var timeoutCache = new Map();

cache.dispatch = function () {
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}

var type = argsToString(args);
var timeout = getTimeout(args, option);

if (timeout) {
var now = Date.now();

if (!timeoutCache.has(type)) {
timeoutCache.set(type, now);
} else {
var timeoutOfCurrentType = timeoutCache.get(type); // console.log(now - timeout, timeoutOfCurrentType)

if (now - timeout > timeoutOfCurrentType) {
cache.delete(type);
timeoutCache.delete(type);
}
}
}

if (!cache.has(type)) {
cache.set(type, store.dispatch.apply(store, args));
}

return cache.get(type);
};

var _has = cache.has.bind(cache);

cache.has = function () {
for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
args[_key2] = arguments[_key2];
}

var key = argsToString(args);
return _has(toString(key));
};

var _delete = cache.delete.bind(cache);

cache.delete = function () {
for (var _len3 = arguments.length, args = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
args[_key3] = arguments[_key3];
}

var key = argsToString(args);
return _delete(toString(key));
};

store.cache = cache;
};

var resolveParams = function resolveParams(args) {
if (!isVuexStore(args)) {
return function (store) {
return cachePlugin(store, args);
};
}

return cachePlugin(args);
};

module.exports = resolveParams;
113 changes: 113 additions & 0 deletions dist/vuex-cache.es.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*!
* vuex-cache v1.4.0
* (c) 2017-present [email protected]
* Released under the MIT License.
*/
var isVuexStore = function isVuexStore(obj) {
return 'dispatch' in obj && typeof obj.dispatch === 'function';
}; // convert string or obj to string


var toString = function toString(arg) {
return typeof arg === 'string' ? arg : JSON.stringify(arg);
}; // convert arguments to string


var argsToString = function argsToString(args) {
var type = toString(args[0]);

if (args[1]) {
type = "".concat(type, ":").concat(toString(args[1]));
}

return type;
}; // parse timeout prop in option


var getTimeout = function getTimeout(args, option) {
if (args.length === 1 && args[0].timeout) {
return args[0].timeout;
}

if (args.length === 3 && args[2].timeout) {
return args[2].timeout;
}

if (option && option.timeout) {
return option.timeout;
}

return 0;
};

var cachePlugin = function cachePlugin(store, option) {
var cache = new Map(); // use another map to store timeout for each type

var timeoutCache = new Map();

cache.dispatch = function () {
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}

var type = argsToString(args);
var timeout = getTimeout(args, option);

if (timeout) {
var now = Date.now();

if (!timeoutCache.has(type)) {
timeoutCache.set(type, now);
} else {
var timeoutOfCurrentType = timeoutCache.get(type); // console.log(now - timeout, timeoutOfCurrentType)

if (now - timeout > timeoutOfCurrentType) {
cache.delete(type);
timeoutCache.delete(type);
}
}
}

if (!cache.has(type)) {
cache.set(type, store.dispatch.apply(store, args));
}

return cache.get(type);
};

var _has = cache.has.bind(cache);

cache.has = function () {
for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
args[_key2] = arguments[_key2];
}

var key = argsToString(args);
return _has(toString(key));
};

var _delete = cache.delete.bind(cache);

cache.delete = function () {
for (var _len3 = arguments.length, args = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
args[_key3] = arguments[_key3];
}

var key = argsToString(args);
return _delete(toString(key));
};

store.cache = cache;
};

var resolveParams = function resolveParams(args) {
if (!isVuexStore(args)) {
return function (store) {
return cachePlugin(store, args);
};
}

return cachePlugin(args);
};

export default resolveParams;
7 changes: 6 additions & 1 deletion index.umd.js → dist/vuex-cache.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
/*!
* vuex-cache v1.4.0
* (c) 2017-present [email protected]
* Released under the MIT License.
*/
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global['vuex-cache'] = factory());
(global.vuexCache = factory());
}(this, (function () { 'use strict';

var isVuexStore = function isVuexStore(obj) {
Expand Down
7 changes: 7 additions & 0 deletions dist/vuex-cache.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions dist/vuex-cache.min.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

83 changes: 0 additions & 83 deletions index.js

This file was deleted.

Loading