|
1 | | -/* eslint-disable no-console, @typescript-eslint/no-explicit-any, no-else-return */ |
| 1 | +/* eslint-disable no-console, @typescript-eslint/no-explicit-any */ |
2 | 2 | /** |
3 | 3 | * Simple test to demonstrate the sourceValue race condition. |
4 | 4 | * |
@@ -47,9 +47,9 @@ afterEach(async () => { |
47 | 47 | }); |
48 | 48 |
|
49 | 49 | 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 () => { |
51 | 51 | // Track all sourceValues we receive during the test |
52 | | - const receivedSourceValues: any[] = []; |
| 52 | + let receivedSourceValues: any[] = []; |
53 | 53 | let renderCount = 0; |
54 | 54 |
|
55 | 55 | const {result} = renderHook(() => { |
@@ -97,7 +97,7 @@ describe('Simple sourceValue Race Condition Demo', () => { |
97 | 97 |
|
98 | 98 | // Clear counters after initial setup and primer |
99 | 99 | const initialRenderCount = renderCount; |
100 | | - receivedSourceValues.length = 0; |
| 100 | + receivedSourceValues = []; |
101 | 101 |
|
102 | 102 | console.log('\n=== Starting the race condition test ==='); |
103 | 103 | console.log('About to perform 3 discrete updates that should be batched...\n'); |
@@ -171,33 +171,29 @@ describe('Simple sourceValue Race Condition Demo', () => { |
171 | 171 | }, |
172 | 172 | }); |
173 | 173 |
|
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 |
175 | 175 | // @ts-expect-error - sourceValue exists on the metadata object but TS doesn't know the type |
176 | 176 | 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. |
180 | 177 | // @ts-expect-error - sourceValue exists on the metadata object but TS doesn't know the type |
181 | 178 | 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', |
186 | 183 | }, |
187 | 184 | }); |
188 | 185 | } |
189 | 186 |
|
190 | 187 | // 🚨 THE PROBLEM: |
191 | | - // We lost information about the "processing" and "completed" states! |
192 | 188 | // A component using sourceValue to track state transitions would miss: |
| 189 | + // - step: 1, status: 'started' (never visible in sourceValue) |
193 | 190 | // - step: 2, status: 'processing' (never visible in sourceValue) |
194 | | - // - step: 3, status: 'completed' (never visible in sourceValue) |
195 | 191 |
|
196 | 192 | console.log('\n🚨 RACE CONDITION CONFIRMED:'); |
197 | 193 | console.log(`• Expected to see ${expectedUpdates} sourceValues`); |
198 | 194 | console.log(`• Actually received ${receivedSourceValues.length} sourceValue(s)`); |
199 | 195 | 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'); |
201 | 197 | console.log('\nThis means components cannot reliably track state transitions when updates are batched!'); |
202 | 198 | }); |
203 | 199 | }); |
0 commit comments