@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.
@@ -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
- // Run the initial query
162
- const initialDocs = yield stackInstance.findDocuments(query.selector, query.fields);
163
- if (initialDocs.docs.length) {
164
- let docs = initialDocs.docs; // [TODO] Check types
165
- setDocs(docs);
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
- setError(err);
176
+ if (id === runId.current)
177
+ setError(err);
171
178
  }
172
179
  finally {
173
- setLoading(false);
180
+ if (id === runId.current)
181
+ setLoading(false);
174
182
  }
175
183
  });
176
184
  runQuery();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@docstack/react",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "One does not simply stack documents.",
5
5
  "main": "lib/index.js",
6
6
  "module": "lib/index.js",
@@ -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
- // Run the initial query
210
- const initialDocs = await stackInstance.findDocuments(query.selector, query.fields);
211
- if (initialDocs.docs.length) {
212
- let docs = initialDocs.docs as Document[]; // [TODO] Check types
213
- setDocs(docs);
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