ladda-cache
Advanced tools
Comparing version 0.0.8 to 0.0.9
@@ -243,3 +243,3 @@ module.exports = | ||
function decorateApiFn(abstractEntity, apiFn, datastore) { | ||
var type = abstractEntity.name; | ||
apiFn.invalidates = apiFn.invalidates || []; | ||
@@ -250,3 +250,3 @@ switch (apiFn.operation) { | ||
case 'READ': | ||
return (0, _read.decorateRead)(apiFn, datastore, type); | ||
return (0, _read.decorateRead)(apiFn, datastore, abstractEntity); | ||
case 'UPDATE': | ||
@@ -288,3 +288,4 @@ return (0, _update.decorateUpdate)(apiFn, datastore, abstractEntity); | ||
replaceTempIdInItemAndViews(datastore, abstractEntity, insertId, result); | ||
(0, _invalidator.invalidate)(datastore, abstractEntity, 'CREATE'); | ||
(0, _invalidator.invalidateEntity)(datastore, abstractEntity, 'CREATE'); | ||
(0, _invalidator.invalidateFunction)(datastore, abstractEntity, apiFn); | ||
}); | ||
@@ -319,7 +320,8 @@ | ||
}); | ||
exports.invalidate = invalidate; | ||
exports.invalidateEntity = invalidateEntity; | ||
exports.invalidateFunction = invalidateFunction; | ||
var _datastore = __webpack_require__(5); | ||
function invalidate(datastore, abstractEntity, operation) { | ||
function invalidateEntity(datastore, abstractEntity, operation) { | ||
var invalidateOn = abstractEntity.val.invalidatesOn || ['CREATE']; | ||
@@ -335,2 +337,9 @@ if (invalidateOn.indexOf(operation) === -1) { | ||
function invalidateFunction(datastore, abstractEntity, apiFn) { | ||
var type = abstractEntity.name; | ||
apiFn.invalidates.forEach(function (method) { | ||
(0, _datastore.invalidate)(datastore, type, method); | ||
}); | ||
} | ||
/***/ }, | ||
@@ -487,8 +496,32 @@ /* 5 */ | ||
function invalidate(datastore, type) { | ||
function invalidate(datastore, type, method) { | ||
if (datastore.queryCache) { | ||
datastore.queryCache[type] = {}; | ||
if (method) { | ||
invalidateQueryCacheByFunction(datastore, type, method); | ||
} else { | ||
datastore.queryCache[type] = {}; | ||
} | ||
} | ||
} | ||
function invalidateQueryCacheByFunction(datastore, type, method) { | ||
var matcher = getMatcher(type, method); | ||
var candidates = Object.keys(datastore.queryCache[type] || {}); | ||
var toInvalidate = candidates.filter(function (x) { | ||
return matcher.test(x); | ||
}); | ||
toInvalidate.forEach(function (key) { | ||
delete datastore.queryCache[type][key]; | ||
}); | ||
} | ||
function getMatcher(type, method) { | ||
if (method.slice(method.length - 3, method.length) === '(*)') { | ||
var cleanMethod = method.slice(0, method.length - 3); | ||
return new RegExp('^' + type + '-' + cleanMethod + '-.*$'); | ||
} else { | ||
return new RegExp('^' + type + '-' + method + '-$'); | ||
} | ||
} | ||
function patchItem(datastore, query, item) { | ||
@@ -620,11 +653,13 @@ getItem(datastore, query).then(function (superItem) { | ||
var _invalidator = __webpack_require__(4); | ||
var _datastore = __webpack_require__(5); | ||
function decorateRead(apiFn, datastore, type) { | ||
function decorateRead(apiFn, datastore, abstractEntity) { | ||
return function (query) { | ||
if (apiFn.alwaysGetFreshData === true) { | ||
return executeApiFnAndCache(apiFn, datastore, type, query); | ||
return executeApiFnAndCache(apiFn, datastore, abstractEntity, query); | ||
} | ||
var fromCache = getFromCache(apiFn, datastore, type, query); | ||
var fromCache = getFromCache(apiFn, datastore, abstractEntity.name, query); | ||
return fromCache.then(function (itemFromCache) { | ||
@@ -634,3 +669,3 @@ if (itemFromCache) { | ||
} else { | ||
return executeApiFnAndCache(apiFn, datastore, type, query); | ||
return executeApiFnAndCache(apiFn, datastore, abstractEntity, query); | ||
} | ||
@@ -641,5 +676,5 @@ }); | ||
function executeApiFnAndCache(apiFn, datastore, type, query) { | ||
function executeApiFnAndCache(apiFn, datastore, abstractEntity, query) { | ||
var result = apiFn(query); | ||
result.then(addToCache(apiFn, datastore, type, query)); | ||
result.then(addToCache(apiFn, datastore, abstractEntity, query)); | ||
return result; | ||
@@ -668,11 +703,14 @@ } | ||
function addToCache(apiFn, datastore, type, query) { | ||
function addToCache(apiFn, datastore, abstractEntity, query) { | ||
return function (data) { | ||
if (shouldUseQueryCache(apiFn.plural, apiFn.byId)) { | ||
[apiFn.name].concat(apiFn.cacheAliases || []).map(function (fnName) { | ||
(0, _datastore.addCollection)(datastore, createQueryForCollection(type, query, fnName), data); | ||
(0, _datastore.addCollection)(datastore, createQueryForCollection(abstractEntity.name, query, fnName), data); | ||
}); | ||
} else { | ||
(0, _datastore.addItem)(datastore, (0, _query.createQuery)(type, query), data); | ||
(0, _datastore.addItem)(datastore, (0, _query.createQuery)(abstractEntity.name, query), data); | ||
} | ||
(0, _invalidator.invalidateEntity)(datastore, abstractEntity, 'READ'); | ||
(0, _invalidator.invalidateFunction)(datastore, abstractEntity, apiFn); | ||
}; | ||
@@ -710,3 +748,4 @@ } | ||
result.then(function () { | ||
(0, _invalidator.invalidate)(datastore, abstractEntity, 'UPDATE'); | ||
(0, _invalidator.invalidateEntity)(datastore, abstractEntity, 'UPDATE'); | ||
(0, _invalidator.invalidateFunction)(datastore, abstractEntity, apiFn); | ||
}); | ||
@@ -757,3 +796,4 @@ return result; | ||
result.then(function () { | ||
(0, _invalidator.invalidate)(datastore, abstractEntity, 'DELETE'); | ||
(0, _invalidator.invalidateEntity)(datastore, abstractEntity, 'DELETE'); | ||
(0, _invalidator.invalidateFunction)(datastore, abstractEntity, apiFn); | ||
}); | ||
@@ -760,0 +800,0 @@ return result; |
{ | ||
"name": "ladda-cache", | ||
"version": "0.0.8", | ||
"version": "0.0.9", | ||
"description": "Data fetching layaer with support for caching", | ||
@@ -5,0 +5,0 @@ "main": "dist/bundle.js", |
@@ -119,8 +119,30 @@ // Concepts: | ||
export function invalidate(datastore, type) { | ||
export function invalidate(datastore, type, method) { | ||
if (datastore.queryCache) { | ||
datastore.queryCache[type] = {}; | ||
if (method) { | ||
invalidateQueryCacheByFunction(datastore, type, method); | ||
} else { | ||
datastore.queryCache[type] = {}; | ||
} | ||
} | ||
} | ||
function invalidateQueryCacheByFunction(datastore, type, method) { | ||
const matcher = getMatcher(type, method); | ||
const candidates = Object.keys(datastore.queryCache[type] || {}); | ||
const toInvalidate = candidates.filter((x) => matcher.test(x)); | ||
toInvalidate.forEach(key => { | ||
delete datastore.queryCache[type][key]; | ||
}); | ||
} | ||
function getMatcher(type, method) { | ||
if (method.slice(method.length - 3, method.length) === '(*)') { | ||
const cleanMethod = method.slice(0, method.length - 3); | ||
return new RegExp('^' + type + '-' + cleanMethod + '-.*$'); | ||
} else { | ||
return new RegExp('^' + type + '-' + method + '-$'); | ||
} | ||
} | ||
export function patchItem(datastore, query, item) { | ||
@@ -127,0 +149,0 @@ getItem(datastore, query).then(superItem => { |
@@ -1,2 +0,2 @@ | ||
import { invalidate } from 'invalidator'; | ||
import { invalidateEntity, invalidateFunction } from 'invalidator'; | ||
import { createQuery, createQueryFromItem } from 'query'; | ||
@@ -22,3 +22,4 @@ import { | ||
replaceTempIdInItemAndViews(datastore, abstractEntity, insertId, result); | ||
invalidate(datastore, abstractEntity, 'CREATE'); | ||
invalidateEntity(datastore, abstractEntity, 'CREATE'); | ||
invalidateFunction(datastore, abstractEntity, apiFn); | ||
}); | ||
@@ -25,0 +26,0 @@ |
@@ -1,2 +0,2 @@ | ||
import { invalidate } from 'invalidator'; | ||
import { invalidateEntity, invalidateFunction } from 'invalidator'; | ||
import { createQuery } from 'query'; | ||
@@ -13,3 +13,4 @@ import { | ||
result.then(() => { | ||
invalidate(datastore, abstractEntity, 'DELETE'); | ||
invalidateEntity(datastore, abstractEntity, 'DELETE'); | ||
invalidateFunction(datastore, abstractEntity, apiFn); | ||
}); | ||
@@ -16,0 +17,0 @@ return result; |
@@ -32,3 +32,3 @@ // Concepts: AbstractEntity | ||
function decorateApiFn(abstractEntity, apiFn, datastore) { | ||
const type = abstractEntity.name; | ||
apiFn.invalidates = apiFn.invalidates || []; | ||
@@ -39,3 +39,3 @@ switch (apiFn.operation) { | ||
case 'READ': | ||
return decorateRead(apiFn, datastore, type); | ||
return decorateRead(apiFn, datastore, abstractEntity); | ||
case 'UPDATE': | ||
@@ -42,0 +42,0 @@ return decorateUpdate(apiFn, datastore, abstractEntity); |
import { createQuery } from 'query'; | ||
import { invalidateEntity, invalidateFunction } from 'invalidator'; | ||
import { | ||
@@ -9,9 +10,9 @@ getItem, | ||
export function decorateRead(apiFn, datastore, type) { | ||
export function decorateRead(apiFn, datastore, abstractEntity) { | ||
return (query) => { | ||
if (apiFn.alwaysGetFreshData === true) { | ||
return executeApiFnAndCache(apiFn, datastore, type, query); | ||
return executeApiFnAndCache(apiFn, datastore, abstractEntity, query); | ||
} | ||
const fromCache = getFromCache(apiFn, datastore, type, query); | ||
const fromCache = getFromCache(apiFn, datastore, abstractEntity.name, query); | ||
return fromCache.then(itemFromCache => { | ||
@@ -21,3 +22,3 @@ if (itemFromCache) { | ||
} else { | ||
return executeApiFnAndCache(apiFn, datastore, type, query); | ||
return executeApiFnAndCache(apiFn, datastore, abstractEntity, query); | ||
} | ||
@@ -28,5 +29,5 @@ }); | ||
function executeApiFnAndCache(apiFn, datastore, type, query) { | ||
function executeApiFnAndCache(apiFn, datastore, abstractEntity, query) { | ||
const result = apiFn(query); | ||
result.then(addToCache(apiFn, datastore, type, query)); | ||
result.then(addToCache(apiFn, datastore, abstractEntity, query)); | ||
return result; | ||
@@ -55,11 +56,18 @@ } | ||
function addToCache(apiFn, datastore, type, query) { | ||
function addToCache(apiFn, datastore, abstractEntity, query) { | ||
return data => { | ||
if (shouldUseQueryCache(apiFn.plural, apiFn.byId)) { | ||
[apiFn.name].concat(apiFn.cacheAliases || []).map((fnName) => { | ||
addCollection(datastore, createQueryForCollection(type, query, fnName), data); | ||
addCollection(datastore, | ||
createQueryForCollection(abstractEntity.name, | ||
query, | ||
fnName), | ||
data); | ||
}); | ||
} else { | ||
addItem(datastore, createQuery(type, query), data); | ||
addItem(datastore, createQuery(abstractEntity.name, query), data); | ||
} | ||
invalidateEntity(datastore, abstractEntity, 'READ'); | ||
invalidateFunction(datastore, abstractEntity, apiFn); | ||
}; | ||
@@ -66,0 +74,0 @@ } |
@@ -1,2 +0,2 @@ | ||
import { invalidate } from 'invalidator'; | ||
import { invalidateEntity, invalidateFunction } from 'invalidator'; | ||
import { createQueryFromItem } from 'query'; | ||
@@ -14,3 +14,4 @@ import { | ||
result.then(() => { | ||
invalidate(datastore, abstractEntity, 'UPDATE'); | ||
invalidateEntity(datastore, abstractEntity, 'UPDATE'); | ||
invalidateFunction(datastore, abstractEntity, apiFn); | ||
}); | ||
@@ -17,0 +18,0 @@ return result; |
import { invalidate as invalidateDatastore } from 'datastore'; | ||
export function invalidate(datastore, abstractEntity, operation) { | ||
export function invalidateEntity(datastore, abstractEntity, operation) { | ||
const invalidateOn = abstractEntity.val.invalidatesOn || ['CREATE']; | ||
@@ -13,1 +13,8 @@ if (invalidateOn.indexOf(operation) === -1) { | ||
} | ||
export function invalidateFunction(datastore, abstractEntity, apiFn) { | ||
const type = abstractEntity.name; | ||
apiFn.invalidates.forEach(method => { | ||
invalidateDatastore(datastore, type, method); | ||
}); | ||
} |
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
52207
27
1306