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 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
- if (currentRequestId !== requestTracker.current) {
89
- setLoading(false);
90
- return;
91
- }
92
- ;
93
- setLoading(false);
94
- return rawData;
84
+ return { data: rawData, error: null };
95
85
  } catch (err) {
96
- if (currentRequestId === requestTracker.current) {
97
- if (err instanceof Error) {
98
- setError(err);
99
- } else {
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
- if (cached) {
112
- setData(cached);
113
- return;
114
- }
96
+ if (isPageMode && cached) {
97
+ setData(cached);
98
+ return;
115
99
  }
116
- const rawData = await asyncNormalize();
117
- if (!rawData) return;
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
- return page === 1 ? rawData : [...prev, ...rawData];
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[page] = rawData;
129
+ cache.current[localPage] = rawData;
125
130
  Object.keys(cache.current).forEach((val) => {
126
- if (Math.abs(page - +val) > 1) {
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
- if (currentRequestId !== requestTracker.current) {
62
- setLoading(false);
63
- return;
64
- }
65
- ;
66
- setLoading(false);
67
- return rawData;
57
+ return { data: rawData, error: null };
68
58
  } catch (err) {
69
- if (currentRequestId === requestTracker.current) {
70
- if (err instanceof Error) {
71
- setError(err);
72
- } else {
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
- if (cached) {
85
- setData(cached);
86
- return;
87
- }
69
+ if (isPageMode && cached) {
70
+ setData(cached);
71
+ return;
88
72
  }
89
- const rawData = await asyncNormalize();
90
- if (!rawData) return;
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
- return page === 1 ? rawData : [...prev, ...rawData];
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[page] = rawData;
102
+ cache.current[localPage] = rawData;
98
103
  Object.keys(cache.current).forEach((val) => {
99
- if (Math.abs(page - +val) > 1) {
104
+ if (Math.abs(localPage - +val) > 1) {
100
105
  delete cache.current[+val];
101
106
  }
102
107
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-resource-ui",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "description": "A high-level data orchestration hook for React with pagination, caching, and virtualization",
5
5
  "type": "module",
6
6
  "main": "dist/index.cjs",