diff --git a/packages/react-refresh/src/ReactFreshRuntime.js b/packages/react-refresh/src/ReactFreshRuntime.js index 24f434702eb4..2ccfe7eece80 100644 --- a/packages/react-refresh/src/ReactFreshRuntime.js +++ b/packages/react-refresh/src/ReactFreshRuntime.js @@ -21,14 +21,22 @@ type Signature = {| getCustomHooks: () => Array, |}; +// In old environments, we'll leak previous types after every edit. +const PossiblyWeakMap = typeof WeakMap === 'function' ? WeakMap : Map; +const PossiblyWeakSet = typeof WeakSet === 'function' ? WeakSet : Set; + // We never remove these associations. // It's OK to reference families, but use WeakMap/Set for types. const allFamiliesByID: Map = new Map(); -const allTypes: WeakSet = new WeakSet(); -const allSignaturesByType: WeakMap = new WeakMap(); +// $FlowIssue +const allTypes: WeakSet | Set = new PossiblyWeakSet(); +const allSignaturesByType: // $FlowIssue +WeakMap | Map = new PossiblyWeakMap(); // This WeakMap is read by React, so we only put families // that have actually been edited here. This keeps checks fast. -const familiesByType: WeakMap = new WeakMap(); +// $FlowIssue +const familiesByType: // $FlowIssue +WeakMap | Map = new PossiblyWeakMap(); // This is cleared on every prepareUpdate() call. // It is an array of [Family, NextType] tuples.