Skip to content

Commit 00ece7e

Browse files
author
Ari Bouius
committed
Fix cache invalidation logic
1 parent 584c064 commit 00ece7e

File tree

2 files changed

+16
-18
lines changed

2 files changed

+16
-18
lines changed

.size-snapshot.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
{
22
"dist/index.js": {
3-
"bundled": 31464,
4-
"minified": 16507,
5-
"gzipped": 5069
3+
"bundled": 31493,
4+
"minified": 16539,
5+
"gzipped": 5075
66
},
77
"dist/index.es.js": {
8-
"bundled": 31098,
9-
"minified": 16202,
10-
"gzipped": 4995,
8+
"bundled": 31127,
9+
"minified": 16234,
10+
"gzipped": 5001,
1111
"treeshaked": {
1212
"rollup": {
1313
"code": 75,

src/client.js

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ export class ApiClient {
147147
query.cache = await this.request(query.url, { headers })
148148
query.timestamp = new Date().getTime()
149149
query.promise = null
150-
150+
151151
const result = this.normalize(query.cache)
152152

153153
this.dispatch({
@@ -199,33 +199,31 @@ export class ApiClient {
199199
})
200200

201201
if (!schema.error && !schema.errors) {
202-
let related = invalidate
202+
let invalid
203203

204-
if (related === undefined) {
205-
related = relationships.length ? relationships : null
206-
} else if (related && !Array.isArray(related)) {
207-
related = [related]
204+
if (invalidate) {
205+
invalid = Array.isArray(invalidate) ? invalidate : [invalidate]
206+
} else if (invalidate !== false) {
207+
invalid = [type, ...relationships]
208208
}
209209

210210
this.cache.forEach(q => {
211211
if (q.id && q.url === query.url && schema.data) {
212212
q.cache = schema
213-
return query.dispatch({ result })
213+
return q.dispatch({ result })
214214
}
215215

216-
if (!related) {
216+
if (!invalid) {
217217
return
218218
}
219219

220220
if (!q.type) {
221221
Object.assign(q, getTypeMap(q, this.schema))
222222
}
223223

224-
const invalid =
225-
q.type === type ||
226-
related.find(r => query.relationships.indexOf(r) >= 0)
224+
const types = [q.type, ...q.relationships]
227225

228-
if (invalid) {
226+
if (types.find(t => invalid.indexOf(t) >= 0)) {
229227
q.cache = null
230228

231229
if (q.subscribers.length) {

0 commit comments

Comments
 (0)