Skip to content

Commit fc280f2

Browse files
committed
fix(data): prevent publish from racing with loads, using wrong sequence, and wiping cache
- Assign the publish promise to entry.inflight so load() joins it instead of starting a competing network request during validation. - Capture startedSeq at publish start, not after parse, so writes that confirm during validation keep their overlay. - On a refused publish with no in-memory base, skip invalidateEntry (which would delete the persisted copy) and start a load if observed.
1 parent a8e63b3 commit fc280f2

1 file changed

Lines changed: 17 additions & 4 deletions

File tree

‎packages/octane/src/data/store.ts‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1364,24 +1364,37 @@ export function createDataStore(options: DataStoreOptions): DataStore {
13641364
}
13651365
const entry = entryFor(read, input);
13661366
const attempt = supersede(entry);
1367+
const startedSeq = ++sequence;
13671368
entry.checkedStorage = true;
13681369
const current = () => !retired && entry.attempt === attempt;
1369-
return Promise.resolve()
1370+
const done = Promise.resolve()
13701371
.then(() => parse(read.output, value))
13711372
.then(
13721373
(parsed) => {
13731374
if (!current()) return;
1374-
adopt(entry, parsed, ++sequence);
1375+
entry.inflight = null;
1376+
adopt(entry, parsed, startedSeq);
13751377
save(entry);
13761378
touchWithViews(entry);
13771379
idle(entry);
13781380
},
13791381
(error) => {
1380-
// What it holds is older than the value that failed, and the load it superseded is gone.
1381-
if (current()) invalidateEntry(entry);
1382+
if (!current()) throw error;
1383+
entry.inflight = null;
1384+
if (entry.hasBase) {
1385+
invalidateEntry(entry);
1386+
} else if (entry.subscribers.size > 0) {
1387+
void load(entry);
1388+
} else {
1389+
touch(entry);
1390+
idle(entry);
1391+
}
13821392
throw error;
13831393
},
13841394
);
1395+
entry.inflight = done;
1396+
touch(entry);
1397+
return done;
13851398
},
13861399
forget(read, input) {
13871400
assertLive();

0 commit comments

Comments
 (0)