react-rest-cache
Advanced tools
Comparing version
@@ -48,10 +48,10 @@ var $8zHUo$react = require("react"); | ||
const $8b428f8f07e0d73c$var$addResponseToCacheAndNotifyObservers = (cache, data, observer)=>{ | ||
const $8b428f8f07e0d73c$var$addResponseToCacheAndNotifyObservers = (cache, data, observer, observersToCall)=>{ | ||
if (data === undefined || data === null) return data; | ||
if (Array.isArray(data)) return data.map((item)=>$8b428f8f07e0d73c$var$addResponseToCacheAndNotifyObservers(cache, item, observer)); | ||
if (Array.isArray(data)) return data.map((item)=>$8b428f8f07e0d73c$var$addResponseToCacheAndNotifyObservers(cache, item, observer, observersToCall)); | ||
if (typeof data === "object") { | ||
if (!("id" in data) || !("__typename" in data)) { | ||
console.warn("No id or __typename in", data); | ||
// console.warn("No id or __typename in", data); | ||
Object.keys(data).forEach((key)=>{ | ||
data[key] = $8b428f8f07e0d73c$var$addResponseToCacheAndNotifyObservers(cache, data[key], observer); | ||
data[key] = $8b428f8f07e0d73c$var$addResponseToCacheAndNotifyObservers(cache, data[key], observer, observersToCall); | ||
}); | ||
@@ -69,5 +69,5 @@ return data; | ||
Object.keys(data).forEach((key)=>{ | ||
cache[typename][id].data[key] = $8b428f8f07e0d73c$var$addResponseToCacheAndNotifyObservers(cache, data[key], observer); | ||
cache[typename][id].data[key] = $8b428f8f07e0d73c$var$addResponseToCacheAndNotifyObservers(cache, data[key], observer, observersToCall); | ||
}); | ||
cache[typename][id].observers.forEach((obs)=>obs()); | ||
cache[typename][id].observers.forEach((obs)=>observersToCall.add(obs)); | ||
cache[typename][id].observers.add(observer); | ||
@@ -88,7 +88,8 @@ return cache[typename][id].data; | ||
signal: signal, | ||
...fetchOptions || {}, | ||
headers: { | ||
"Content-Type": "application/json", | ||
Accept: "application/json" | ||
}, | ||
...fetchOptions || {} | ||
Accept: "application/json", | ||
...fetchOptions?.headers || {} | ||
} | ||
}); | ||
@@ -101,3 +102,6 @@ if (!response.ok) { | ||
const data = await response.json(); | ||
return $8b428f8f07e0d73c$var$addResponseToCacheAndNotifyObservers(cache, data, observer); | ||
const observersToCall = new Set(); | ||
const result = $8b428f8f07e0d73c$var$addResponseToCacheAndNotifyObservers(cache, data, observer, observersToCall); | ||
observersToCall.forEach((obs)=>obs()); | ||
return result; | ||
}; | ||
@@ -104,0 +108,0 @@ const unsubscribe = (observer)=>{ |
@@ -27,10 +27,10 @@ import $hgUW1$react, {useState as $hgUW1$useState, useCallback as $hgUW1$useCallback, useEffect as $hgUW1$useEffect} from "react"; | ||
const $0f15643d38641aec$var$addResponseToCacheAndNotifyObservers = (cache, data, observer)=>{ | ||
const $0f15643d38641aec$var$addResponseToCacheAndNotifyObservers = (cache, data, observer, observersToCall)=>{ | ||
if (data === undefined || data === null) return data; | ||
if (Array.isArray(data)) return data.map((item)=>$0f15643d38641aec$var$addResponseToCacheAndNotifyObservers(cache, item, observer)); | ||
if (Array.isArray(data)) return data.map((item)=>$0f15643d38641aec$var$addResponseToCacheAndNotifyObservers(cache, item, observer, observersToCall)); | ||
if (typeof data === "object") { | ||
if (!("id" in data) || !("__typename" in data)) { | ||
console.warn("No id or __typename in", data); | ||
// console.warn("No id or __typename in", data); | ||
Object.keys(data).forEach((key)=>{ | ||
data[key] = $0f15643d38641aec$var$addResponseToCacheAndNotifyObservers(cache, data[key], observer); | ||
data[key] = $0f15643d38641aec$var$addResponseToCacheAndNotifyObservers(cache, data[key], observer, observersToCall); | ||
}); | ||
@@ -48,5 +48,5 @@ return data; | ||
Object.keys(data).forEach((key)=>{ | ||
cache[typename][id].data[key] = $0f15643d38641aec$var$addResponseToCacheAndNotifyObservers(cache, data[key], observer); | ||
cache[typename][id].data[key] = $0f15643d38641aec$var$addResponseToCacheAndNotifyObservers(cache, data[key], observer, observersToCall); | ||
}); | ||
cache[typename][id].observers.forEach((obs)=>obs()); | ||
cache[typename][id].observers.forEach((obs)=>observersToCall.add(obs)); | ||
cache[typename][id].observers.add(observer); | ||
@@ -67,7 +67,8 @@ return cache[typename][id].data; | ||
signal: signal, | ||
...fetchOptions || {}, | ||
headers: { | ||
"Content-Type": "application/json", | ||
Accept: "application/json" | ||
}, | ||
...fetchOptions || {} | ||
Accept: "application/json", | ||
...fetchOptions?.headers || {} | ||
} | ||
}); | ||
@@ -80,3 +81,6 @@ if (!response.ok) { | ||
const data = await response.json(); | ||
return $0f15643d38641aec$var$addResponseToCacheAndNotifyObservers(cache, data, observer); | ||
const observersToCall = new Set(); | ||
const result = $0f15643d38641aec$var$addResponseToCacheAndNotifyObservers(cache, data, observer, observersToCall); | ||
observersToCall.forEach((obs)=>obs()); | ||
return result; | ||
}; | ||
@@ -83,0 +87,0 @@ const unsubscribe = (observer)=>{ |
{ | ||
"name": "react-rest-cache", | ||
"version": "0.4.6", | ||
"version": "0.4.7", | ||
"description": "React library to fetch a REST API with cache and sync capabilities", | ||
@@ -5,0 +5,0 @@ "source": "src/index.ts", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
65253
2.68%558
1.45%