Skip to content

Commit 7d462d0

Browse files
committed
Update simpleSourceValueRaceConditionDemo
1 parent a2c1936 commit 7d462d0

File tree

1 file changed

+11
-15
lines changed

1 file changed

+11
-15
lines changed

tests/unit/simpleSourceValueRaceConditionDemo.ts

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* eslint-disable no-console, @typescript-eslint/no-explicit-any, no-else-return */
1+
/* eslint-disable no-console, @typescript-eslint/no-explicit-any */
22
/**
33
* Simple test to demonstrate the sourceValue race condition.
44
*
@@ -47,9 +47,9 @@ afterEach(async () => {
4747
});
4848

4949
describe('Simple sourceValue Race Condition Demo', () => {
50-
it('should demonstrate that only the first sourceValue is visible when updates are batched', async () => {
50+
it('should demonstrate that only one sourceValue is visible when updates are batched', async () => {
5151
// Track all sourceValues we receive during the test
52-
const receivedSourceValues: any[] = [];
52+
let receivedSourceValues: any[] = [];
5353
let renderCount = 0;
5454

5555
const {result} = renderHook(() => {
@@ -97,7 +97,7 @@ describe('Simple sourceValue Race Condition Demo', () => {
9797

9898
// Clear counters after initial setup and primer
9999
const initialRenderCount = renderCount;
100-
receivedSourceValues.length = 0;
100+
receivedSourceValues = [];
101101

102102
console.log('\n=== Starting the race condition test ===');
103103
console.log('About to perform 3 discrete updates that should be batched...\n');
@@ -171,33 +171,29 @@ describe('Simple sourceValue Race Condition Demo', () => {
171171
},
172172
});
173173

174-
// 4. But sourceValue only shows the FIRST update that triggered the batch!
174+
// 4. But sourceValue only shows the last update that triggered the batch
175175
// @ts-expect-error - sourceValue exists on the metadata object but TS doesn't know the type
176176
if (result.current[1]?.sourceValue) {
177-
// sourceValue contains data from the FIRST update, not the last!
178-
// This is because it gets set when the first callback fires, then gets
179-
// overwritten during batching but the component only renders once.
180177
// @ts-expect-error - sourceValue exists on the metadata object but TS doesn't know the type
181178
expect(result.current[1].sourceValue).toEqual({
182-
[`${ONYXKEYS.COLLECTION.TEST_ITEMS}item1`]: {
183-
step: 1,
184-
status: 'started',
185-
message: 'First update',
179+
[`${ONYXKEYS.COLLECTION.TEST_ITEMS}item3`]: {
180+
step: 3,
181+
status: 'completed',
182+
message: 'Third update',
186183
},
187184
});
188185
}
189186

190187
// 🚨 THE PROBLEM:
191-
// We lost information about the "processing" and "completed" states!
192188
// A component using sourceValue to track state transitions would miss:
189+
// - step: 1, status: 'started' (never visible in sourceValue)
193190
// - step: 2, status: 'processing' (never visible in sourceValue)
194-
// - step: 3, status: 'completed' (never visible in sourceValue)
195191

196192
console.log('\n🚨 RACE CONDITION CONFIRMED:');
197193
console.log(`• Expected to see ${expectedUpdates} sourceValues`);
198194
console.log(`• Actually received ${receivedSourceValues.length} sourceValue(s)`);
199195
console.log(`• Lost ${expectedUpdates - receivedSourceValues.length} intermediate updates`);
200-
console.log('• Only the FIRST update is visible in sourceValue due to batching!');
196+
console.log('• Only the last update is visible in sourceValue due to batching');
201197
console.log('\nThis means components cannot reliably track state transitions when updates are batched!');
202198
});
203199
});

0 commit comments

Comments
 (0)