Skip to content

Commit

Permalink
Merge pull request #115 from danybeltran/feat-suspense
Browse files Browse the repository at this point in the history
feat(Suspense):
  • Loading branch information
danybeltran authored Jan 17, 2023
2 parents cc8494c + 640acc5 commit f7047f3
Show file tree
Hide file tree
Showing 11 changed files with 13 additions and 3,112 deletions.
2,111 changes: 0 additions & 2,111 deletions dist/http-react.js

This file was deleted.

8 changes: 0 additions & 8 deletions dist/http-react.min.js

This file was deleted.

206 changes: 0 additions & 206 deletions dist/vanilla.js

This file was deleted.

7 changes: 0 additions & 7 deletions dist/vanilla.min.js

This file was deleted.

41 changes: 10 additions & 31 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1033,7 +1033,6 @@ var useFetcher = function (init, options) {
})];
case 2:
json = _b.sent();
suspenseInitialized[resolvedKey] = true;
requestEmitter.emit(resolvedKey, {
requestCallId: requestCallId,
response: json
Expand Down Expand Up @@ -1185,6 +1184,7 @@ var useFetcher = function (init, options) {
requestCallId: requestCallId,
loading: false
});
suspenseInitialized[resolvedKey] = true;
return [7 /*endfinally*/];
case 6: return [2 /*return*/];
}
Expand Down Expand Up @@ -1358,29 +1358,8 @@ var useFetcher = function (init, options) {
]);
var reValidate = React.useCallback(function reValidate() {
return __awaiter(this, void 0, void 0, function () {
var reqQ_1, reqP;
return __generator(this, function (_a) {
// Only revalidate if request was already completed
if (!loading) {
if (!runningRequests[resolvedKey]) {
previousConfig[resolvedKey] = undefined;
setLoading(true);
reqQ_1 = __assign(__assign({}, ctx.query), config.query);
reqP = __assign(__assign({}, ctx.params), config.params);
if (url !== '') {
fetchData({
query: Object.keys(reqQ_1)
.map(function (q) { return [q, reqQ_1[q]].join('='); })
.join('&'),
params: reqP
});
}
requestEmitter.emit(resolvedKey, {
requestCallId: requestCallId,
loading: true
});
}
}
revalidate(id);
return [2 /*return*/];
});
});
Expand All @@ -1397,7 +1376,7 @@ var useFetcher = function (init, options) {
(0, react_1.useEffect)(function () {
function forceRefresh(v) {
return __awaiter(this, void 0, void 0, function () {
var d, reqQ_2, reqP;
var d, reqQ_1, reqP;
return __generator(this, function (_a) {
if (isDefined(v === null || v === void 0 ? void 0 : v.data)) {
try {
Expand All @@ -1424,11 +1403,11 @@ var useFetcher = function (init, options) {
loading: true,
error: null
});
reqQ_2 = __assign(__assign({}, ctx.query), config.query);
reqQ_1 = __assign(__assign({}, ctx.query), config.query);
reqP = __assign(__assign({}, ctx.params), config.params);
fetchData({
query: Object.keys(reqQ_2)
.map(function (q) { return [q, reqQ_2[q]].join('='); })
query: Object.keys(reqQ_1)
.map(function (q) { return [q, reqQ_1[q]].join('='); })
.join('&'),
params: reqP
});
Expand Down Expand Up @@ -1572,18 +1551,18 @@ var useFetcher = function (init, options) {
var initMemo = React.useMemo(function () { return JSON.stringify(optionsConfig); }, []);
var initializeRevalidation = React.useCallback(function initializeRevalidation() {
return __awaiter(this, void 0, void 0, function () {
var reqQ_3, reqP;
var reqQ_2, reqP;
return __generator(this, function (_a) {
if (auto) {
if (url !== '') {
if (runningRequests[resolvedKey]) {
setLoading(true);
}
reqQ_3 = __assign(__assign({}, ctx.query), config.query);
reqQ_2 = __assign(__assign({}, ctx.query), config.query);
reqP = __assign(__assign({}, ctx.params), config.params);
fetchData({
query: Object.keys(reqQ_3)
.map(function (q) { return [q, reqQ_3[q]].join('='); })
query: Object.keys(reqQ_2)
.map(function (q) { return [q, reqQ_2[q]].join('='); })
.join('&'),
params: reqP
});
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "http-react",
"version": "2.4.4",
"version": "2.4.5",
"description": "React hooks for data fetching",
"main": "index.js",
"scripts": {
Expand Down
30 changes: 2 additions & 28 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1792,7 +1792,6 @@ const useFetcher = <FetchDataType = any, BodyType = any>(
: JSON.stringify({ ...config.body, ...c.body })
: undefined
})
suspenseInitialized[resolvedKey] = true

requestEmitter.emit(resolvedKey, {
requestCallId,
Expand Down Expand Up @@ -1943,6 +1942,7 @@ const useFetcher = <FetchDataType = any, BodyType = any>(
requestCallId,
loading: false
})
suspenseInitialized[resolvedKey] = true
}
}
}
Expand Down Expand Up @@ -2146,33 +2146,7 @@ const useFetcher = <FetchDataType = any, BodyType = any>(

const reValidate = React.useCallback(
async function reValidate() {
// Only revalidate if request was already completed
if (!loading) {
if (!runningRequests[resolvedKey]) {
previousConfig[resolvedKey] = undefined
setLoading(true)
const reqQ = {
...ctx.query,
...config.query
}
const reqP = {
...ctx.params,
...config.params
}
if (url !== '') {
fetchData({
query: Object.keys(reqQ)
.map(q => [q, reqQ[q]].join('='))
.join('&'),
params: reqP
})
}
requestEmitter.emit(resolvedKey, {
requestCallId,
loading: true
})
}
}
revalidate(id)
},
[
requestCallId,
Expand Down
Loading

0 comments on commit f7047f3

Please sign in to comment.