@specter/redux-effects-specter-cache
Advanced tools
Comparing version 0.3.0 to 0.4.0
@@ -18,4 +18,4 @@ import { Middleware } from "redux"; | ||
}; | ||
export declare function resetCacheData(): void; | ||
export default function reduxEffectsSpecterCache<S = any>({ middlewareOption, cacheOption }: { | ||
export declare function resetCacheData(): Promise<void>; | ||
export default function reduxEffectsSpecterCache<S = any>({ middlewareOption, cacheOption, }: { | ||
middlewareOption?: MiddlewareOption<S>; | ||
@@ -22,0 +22,0 @@ cacheOption?: Record<string, any>; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.resetCacheData = void 0; | ||
var tslib_1 = require("tslib"); | ||
var storage_1 = require("@specter/storage"); | ||
@@ -15,7 +17,19 @@ var redux_effects_specter_1 = require("@specter/redux-effects-specter"); | ||
function resetCacheData() { | ||
var cache = createCache(); | ||
cache.clearAll(); | ||
return tslib_1.__awaiter(this, void 0, void 0, function () { | ||
var cache; | ||
return tslib_1.__generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
cache = createCache(); | ||
return [4 /*yield*/, cache.clearAll()]; | ||
case 1: | ||
_a.sent(); | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); | ||
} | ||
exports.resetCacheData = resetCacheData; | ||
function reduxEffectsSpecterCache(_a) { | ||
var _this = this; | ||
var _b = _a.middlewareOption, middlewareOption = _b === void 0 ? {} : _b, _c = _a.cacheOption, cacheOption = _c === void 0 ? {} : _c; | ||
@@ -29,31 +43,43 @@ var excludes = middlewareOption.excludes, fromCache = middlewareOption.fromCache, toCache = middlewareOption.toCache, resetCache = middlewareOption.resetCache; | ||
return next(action); | ||
var _a = action.payload, type = _a.type, service = _a.service, query = _a.query; | ||
if (resetCache && resetCache(action, getState())) { | ||
resetCacheData(); | ||
} | ||
if (type !== "read" || | ||
(type === "read" && excludes && excludes.includes(service))) { | ||
return next(action); | ||
} | ||
var cacheKey = "@@$" + redux_effects_specter_1.SPECTER + "/" + service + "@@" + JSON.stringify(query, | ||
// refs: https://github.com/recruit-tech/redux-effects-fetchr-cache/pull/3 | ||
Object.keys(query).sort()); | ||
// MEMO: you can resolve cache from action and state of store. | ||
// if you dont set the fromCache function, always called cache.get function. | ||
var manualCache = fromCache && fromCache(action, getState()); | ||
if (!fromCache || manualCache) { | ||
var cacheResult = cache.get(cacheKey); | ||
if (cacheResult) { | ||
return Promise.resolve(cacheResult); | ||
} | ||
} | ||
// CAUTION: this middleware depend on the "@specter/redux-effects-specter" | ||
// and "@specter/redux-effects-specter" is expected next applied self. | ||
return next(action).then(function (resp) { | ||
var manualCache = toCache && toCache(action, getState()); | ||
if (!toCache || manualCache) { | ||
cache.put(cacheKey, resp); | ||
} | ||
return resp; | ||
}); | ||
return (function () { return tslib_1.__awaiter(_this, void 0, void 0, function () { | ||
var _a, type, service, query, cacheKey, shouldFromCache, cacheResult, resp, shouldToCache; | ||
return tslib_1.__generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: | ||
_a = action.payload, type = _a.type, service = _a.service, query = _a.query; | ||
if (!(resetCache && resetCache(action, getState()))) return [3 /*break*/, 2]; | ||
return [4 /*yield*/, resetCacheData()]; | ||
case 1: | ||
_b.sent(); | ||
_b.label = 2; | ||
case 2: | ||
if (type !== "read" || | ||
(type === "read" && excludes && excludes.includes(service))) { | ||
return [2 /*return*/, next(action)]; | ||
} | ||
cacheKey = "@@$" + redux_effects_specter_1.SPECTER + "/" + service + "@@" + JSON.stringify(query, | ||
// refs: https://github.com/recruit-tech/redux-effects-fetchr-cache/pull/3 | ||
Object.keys(query).sort()); | ||
shouldFromCache = fromCache && fromCache(action, getState()); | ||
if (!(!fromCache || shouldFromCache)) return [3 /*break*/, 4]; | ||
return [4 /*yield*/, cache.get(cacheKey)]; | ||
case 3: | ||
cacheResult = _b.sent(); | ||
if (cacheResult) { | ||
return [2 /*return*/, cacheResult]; | ||
} | ||
_b.label = 4; | ||
case 4: return [4 /*yield*/, next(action)]; | ||
case 5: | ||
resp = (_b.sent()); | ||
shouldToCache = toCache && toCache(action, getState()); | ||
if (!(!toCache || shouldToCache)) return [3 /*break*/, 7]; | ||
return [4 /*yield*/, cache.put(cacheKey, resp)]; | ||
case 6: | ||
_b.sent(); | ||
_b.label = 7; | ||
case 7: return [2 /*return*/, resp]; | ||
} | ||
}); | ||
}); })(); | ||
}; }; | ||
@@ -60,0 +86,0 @@ }; |
{ | ||
"name": "@specter/redux-effects-specter-cache", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"description": "cache structure for redux middleware of specter", | ||
@@ -14,2 +14,4 @@ "keywords": [ | ||
"main": "lib/index.js", | ||
"modules": "esm/index.js", | ||
"types": "lib/index.d.ts", | ||
"browser": "lib/browser/index.js", | ||
@@ -29,4 +31,6 @@ "directories": { | ||
"test": "jest", | ||
"prepublish": "npm run build", | ||
"build": "tsc -d" | ||
"prepublishOnly": "npm run build", | ||
"build": "npm run build:esm && npm run build:cjs", | ||
"build:esm": "tsc -d -p ./tsconfig.json", | ||
"build:cjs": "tsc -d -p ./tsconfig.esm.json" | ||
}, | ||
@@ -38,12 +42,13 @@ "bugs": { | ||
"@types/lru-cache": "^5.1.0", | ||
"@types/redux": "^3.6.0" | ||
"@types/redux": "^3.6.0", | ||
"tslib": "^2.0.0" | ||
}, | ||
"devDependencies": { | ||
"@specter/redux-effects-specter": "^0.3.0", | ||
"@specter/specter": "^0.3.0", | ||
"@specter/storage": "^0.3.0", | ||
"jest": "25.5.4", | ||
"@specter/redux-effects-specter": "^0.4.0", | ||
"@specter/specter": "^0.4.0", | ||
"@specter/storage": "^0.4.0", | ||
"jest": "26.0.1", | ||
"redux": "4.0.5", | ||
"ts-jest": "25.4.0", | ||
"typescript": "3.8.3" | ||
"ts-jest": "25.5.1", | ||
"typescript": "3.9.2" | ||
}, | ||
@@ -53,3 +58,3 @@ "peerDependencies": { | ||
}, | ||
"gitHead": "f4ff1e14fb1a90a37e6d9299d67ab4607dc92d05", | ||
"gitHead": "ca8de075cb41331d38b1176777bba3536a02723c", | ||
"publishConfig": { | ||
@@ -56,0 +61,0 @@ "access": "public" |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9413
109
4
+ Addedtslib@^2.0.0
+ Addedtslib@2.8.1(transitive)