| 1 | // @compilationMode:"infer" @enableResetCacheOnSourceFileChanges @validateExhaustiveMemoizationDependencies:false |
| 2 | import {useEffect, useMemo, useState} from 'react'; |
| 3 | import {ValidateMemoization} from 'shared-runtime'; |
| 4 | |
| 5 | let pretendConst = 0; |
| 6 | |
| 7 | function unsafeResetConst() { |
| 8 | pretendConst = 0; |
| 9 | } |
| 10 | |
| 11 | function unsafeUpdateConst() { |
| 12 | pretendConst += 1; |
| 13 | } |
| 14 | |
| 15 | function Component() { |
| 16 | useState(() => { |
| 17 | // unsafe: reset the constant when first rendering the instance |
| 18 | unsafeResetConst(); |
| 19 | }); |
| 20 | // UNSAFE! changing a module variable that is read by a component is normally |
| 21 | // unsafe, but in this case we're simulating a fast refresh between each render |
| 22 | unsafeUpdateConst(); |
| 23 | |
| 24 | // TODO: In fast refresh mode (@enableResetCacheOnSourceFileChanges) Forget should |
| 25 | // reset on changes to globals that impact the component/hook, effectively memoizing |
| 26 | // as if value was reactive. However, we don't want to actually treat globals as |
| 27 | // reactive (though that would be trivial) since it could change compilation too much |
| 28 | // btw dev and prod. Instead, we should reset the cache via a secondary mechanism. |
| 29 | const value = useMemo(() => [{pretendConst}], [pretendConst]); |
| 30 | |
| 31 | return <ValidateMemoization inputs={[pretendConst]} output={value} />; |
| 32 | } |
| 33 | |
| 34 | export const FIXTURE_ENTRYPOINT = { |
| 35 | fn: Component, |
| 36 | params: [{}], |
| 37 | sequentialRenders: [{}, {}], |
| 38 | }; |