react-resource-ui 0.1.1 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +36 -31
- package/dist/index.js +36 -31
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -74,56 +74,61 @@ function useResource(config) {
|
|
|
74
74
|
const prevScrollTop = (0, import_react.useRef)(0);
|
|
75
75
|
const scrollRef = (0, import_react.useRef)(null);
|
|
76
76
|
const hasMore = (0, import_react.useRef)(true);
|
|
77
|
-
async function asyncNormalize() {
|
|
77
|
+
async function asyncNormalize(localPage) {
|
|
78
78
|
const params = {
|
|
79
|
-
page,
|
|
79
|
+
page: localPage,
|
|
80
80
|
pageSize
|
|
81
81
|
};
|
|
82
|
-
setLoading(true);
|
|
83
|
-
requestTracker.current += 1;
|
|
84
|
-
const currentRequestId = requestTracker.current;
|
|
85
|
-
setError(null);
|
|
86
82
|
try {
|
|
87
83
|
const rawData = await getData(params);
|
|
88
|
-
|
|
89
|
-
setLoading(false);
|
|
90
|
-
return;
|
|
91
|
-
}
|
|
92
|
-
;
|
|
93
|
-
setLoading(false);
|
|
94
|
-
return rawData;
|
|
84
|
+
return { data: rawData, error: null };
|
|
95
85
|
} catch (err) {
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
setError(new Error("unknown error"));
|
|
101
|
-
}
|
|
102
|
-
setLoading(false);
|
|
103
|
-
}
|
|
86
|
+
return {
|
|
87
|
+
data: null,
|
|
88
|
+
error: err instanceof Error ? err : new Error("unknown error")
|
|
89
|
+
};
|
|
104
90
|
}
|
|
105
91
|
}
|
|
106
92
|
async function orchestrator() {
|
|
107
93
|
const isPageMode = pagination?.type === "page";
|
|
108
94
|
prevScrollTop.current = scrollTop;
|
|
109
95
|
const cached = cache.current[page];
|
|
110
|
-
if (isPageMode) {
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
return;
|
|
114
|
-
}
|
|
96
|
+
if (isPageMode && cached) {
|
|
97
|
+
setData(cached);
|
|
98
|
+
return;
|
|
115
99
|
}
|
|
116
|
-
const
|
|
117
|
-
|
|
100
|
+
const localPage = page;
|
|
101
|
+
requestTracker.current += 1;
|
|
102
|
+
const currentRequestId = requestTracker.current;
|
|
103
|
+
setLoading(true);
|
|
104
|
+
setError(null);
|
|
105
|
+
const result = await asyncNormalize(localPage);
|
|
106
|
+
if (currentRequestId !== requestTracker.current) {
|
|
107
|
+
setLoading(false);
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
;
|
|
111
|
+
setLoading(false);
|
|
112
|
+
if (result.error) {
|
|
113
|
+
setError(result.error);
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
const rawData = result.data;
|
|
117
|
+
if (!rawData || rawData.length < 1) return;
|
|
118
118
|
if (rawData.length < pageSize) hasMore.current = false;
|
|
119
119
|
setData((prev) => {
|
|
120
120
|
if (isPageMode) return rawData;
|
|
121
|
-
|
|
121
|
+
const indexStart = (localPage - 1) * pageSize;
|
|
122
|
+
const newData = [...prev];
|
|
123
|
+
for (let i = 0; i < rawData.length; i++) {
|
|
124
|
+
newData[indexStart + i] = rawData[i];
|
|
125
|
+
}
|
|
126
|
+
return newData;
|
|
122
127
|
});
|
|
123
128
|
if (isPageMode) {
|
|
124
|
-
cache.current[
|
|
129
|
+
cache.current[localPage] = rawData;
|
|
125
130
|
Object.keys(cache.current).forEach((val) => {
|
|
126
|
-
if (Math.abs(
|
|
131
|
+
if (Math.abs(localPage - +val) > 1) {
|
|
127
132
|
delete cache.current[+val];
|
|
128
133
|
}
|
|
129
134
|
});
|
package/dist/index.js
CHANGED
|
@@ -47,56 +47,61 @@ function useResource(config) {
|
|
|
47
47
|
const prevScrollTop = useRef(0);
|
|
48
48
|
const scrollRef = useRef(null);
|
|
49
49
|
const hasMore = useRef(true);
|
|
50
|
-
async function asyncNormalize() {
|
|
50
|
+
async function asyncNormalize(localPage) {
|
|
51
51
|
const params = {
|
|
52
|
-
page,
|
|
52
|
+
page: localPage,
|
|
53
53
|
pageSize
|
|
54
54
|
};
|
|
55
|
-
setLoading(true);
|
|
56
|
-
requestTracker.current += 1;
|
|
57
|
-
const currentRequestId = requestTracker.current;
|
|
58
|
-
setError(null);
|
|
59
55
|
try {
|
|
60
56
|
const rawData = await getData(params);
|
|
61
|
-
|
|
62
|
-
setLoading(false);
|
|
63
|
-
return;
|
|
64
|
-
}
|
|
65
|
-
;
|
|
66
|
-
setLoading(false);
|
|
67
|
-
return rawData;
|
|
57
|
+
return { data: rawData, error: null };
|
|
68
58
|
} catch (err) {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
setError(new Error("unknown error"));
|
|
74
|
-
}
|
|
75
|
-
setLoading(false);
|
|
76
|
-
}
|
|
59
|
+
return {
|
|
60
|
+
data: null,
|
|
61
|
+
error: err instanceof Error ? err : new Error("unknown error")
|
|
62
|
+
};
|
|
77
63
|
}
|
|
78
64
|
}
|
|
79
65
|
async function orchestrator() {
|
|
80
66
|
const isPageMode = pagination?.type === "page";
|
|
81
67
|
prevScrollTop.current = scrollTop;
|
|
82
68
|
const cached = cache.current[page];
|
|
83
|
-
if (isPageMode) {
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
return;
|
|
87
|
-
}
|
|
69
|
+
if (isPageMode && cached) {
|
|
70
|
+
setData(cached);
|
|
71
|
+
return;
|
|
88
72
|
}
|
|
89
|
-
const
|
|
90
|
-
|
|
73
|
+
const localPage = page;
|
|
74
|
+
requestTracker.current += 1;
|
|
75
|
+
const currentRequestId = requestTracker.current;
|
|
76
|
+
setLoading(true);
|
|
77
|
+
setError(null);
|
|
78
|
+
const result = await asyncNormalize(localPage);
|
|
79
|
+
if (currentRequestId !== requestTracker.current) {
|
|
80
|
+
setLoading(false);
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
;
|
|
84
|
+
setLoading(false);
|
|
85
|
+
if (result.error) {
|
|
86
|
+
setError(result.error);
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
const rawData = result.data;
|
|
90
|
+
if (!rawData || rawData.length < 1) return;
|
|
91
91
|
if (rawData.length < pageSize) hasMore.current = false;
|
|
92
92
|
setData((prev) => {
|
|
93
93
|
if (isPageMode) return rawData;
|
|
94
|
-
|
|
94
|
+
const indexStart = (localPage - 1) * pageSize;
|
|
95
|
+
const newData = [...prev];
|
|
96
|
+
for (let i = 0; i < rawData.length; i++) {
|
|
97
|
+
newData[indexStart + i] = rawData[i];
|
|
98
|
+
}
|
|
99
|
+
return newData;
|
|
95
100
|
});
|
|
96
101
|
if (isPageMode) {
|
|
97
|
-
cache.current[
|
|
102
|
+
cache.current[localPage] = rawData;
|
|
98
103
|
Object.keys(cache.current).forEach((val) => {
|
|
99
|
-
if (Math.abs(
|
|
104
|
+
if (Math.abs(localPage - +val) > 1) {
|
|
100
105
|
delete cache.current[+val];
|
|
101
106
|
}
|
|
102
107
|
});
|