@docstack/react 0.1.0 → 0.1.1
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/lib/hooks/index.js +16 -8
- package/package.json +1 -1
- package/src/hooks/index.ts +14 -9
package/lib/hooks/index.js
CHANGED
|
@@ -137,6 +137,9 @@ export const useFind = (stack, query, sort, limit = 50) => {
|
|
|
137
137
|
const [docs, setDocs] = useState([]);
|
|
138
138
|
const [loading, setLoading] = useState(true);
|
|
139
139
|
const [error, setError] = useState(null);
|
|
140
|
+
// Guards against a slow earlier run overwriting a fast later one - the same
|
|
141
|
+
// discipline as useQuerySQL's runId above.
|
|
142
|
+
const runId = useRef(0);
|
|
140
143
|
useEffect(() => {
|
|
141
144
|
var _a;
|
|
142
145
|
// Check if the docStack instance is available
|
|
@@ -155,22 +158,27 @@ export const useFind = (stack, query, sort, limit = 50) => {
|
|
|
155
158
|
}
|
|
156
159
|
setLoading(true);
|
|
157
160
|
const runQuery = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
161
|
+
const id = ++runId.current;
|
|
158
162
|
try {
|
|
159
163
|
const stackInstance = docStack.getStack(stack);
|
|
160
164
|
if (stackInstance) {
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
165
|
+
const found = yield stackInstance.findDocuments(query.selector, query.fields);
|
|
166
|
+
// An empty result is a result. This setter used to be guarded on
|
|
167
|
+
// `.docs.length`, so a live list could gain rows but never lose its
|
|
168
|
+
// last one - a deleted document stayed on screen until a remount.
|
|
169
|
+
// The hazard that guard was standing in for is *staleness*, and the
|
|
170
|
+
// counter above owns that. See ADR-0035.
|
|
171
|
+
if (id === runId.current)
|
|
172
|
+
setDocs(found.docs);
|
|
167
173
|
}
|
|
168
174
|
}
|
|
169
175
|
catch (err) {
|
|
170
|
-
|
|
176
|
+
if (id === runId.current)
|
|
177
|
+
setError(err);
|
|
171
178
|
}
|
|
172
179
|
finally {
|
|
173
|
-
|
|
180
|
+
if (id === runId.current)
|
|
181
|
+
setLoading(false);
|
|
174
182
|
}
|
|
175
183
|
});
|
|
176
184
|
runQuery();
|
package/package.json
CHANGED
package/src/hooks/index.ts
CHANGED
|
@@ -184,6 +184,10 @@ export const useFind = (stack: string, query: {
|
|
|
184
184
|
const [loading, setLoading] = useState(true);
|
|
185
185
|
const [error, setError] = useState(null);
|
|
186
186
|
|
|
187
|
+
// Guards against a slow earlier run overwriting a fast later one - the same
|
|
188
|
+
// discipline as useQuerySQL's runId above.
|
|
189
|
+
const runId = useRef(0);
|
|
190
|
+
|
|
187
191
|
useEffect(() => {
|
|
188
192
|
// Check if the docStack instance is available
|
|
189
193
|
if (!docStack) {
|
|
@@ -203,21 +207,22 @@ export const useFind = (stack: string, query: {
|
|
|
203
207
|
setLoading(true);
|
|
204
208
|
|
|
205
209
|
const runQuery = async () => {
|
|
210
|
+
const id = ++runId.current;
|
|
206
211
|
try {
|
|
207
212
|
const stackInstance = docStack.getStack(stack);
|
|
208
213
|
if (stackInstance) {
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
214
|
+
const found = await stackInstance.findDocuments(query.selector, query.fields);
|
|
215
|
+
// An empty result is a result. This setter used to be guarded on
|
|
216
|
+
// `.docs.length`, so a live list could gain rows but never lose its
|
|
217
|
+
// last one - a deleted document stayed on screen until a remount.
|
|
218
|
+
// The hazard that guard was standing in for is *staleness*, and the
|
|
219
|
+
// counter above owns that. See ADR-0035.
|
|
220
|
+
if (id === runId.current) setDocs(found.docs as Document[]);
|
|
215
221
|
}
|
|
216
|
-
|
|
217
222
|
} catch (err: any) {
|
|
218
|
-
setError(err);
|
|
223
|
+
if (id === runId.current) setError(err);
|
|
219
224
|
} finally {
|
|
220
|
-
setLoading(false);
|
|
225
|
+
if (id === runId.current) setLoading(false);
|
|
221
226
|
}
|
|
222
227
|
};
|
|
223
228
|
|