From cd98e63b47198147cfd140cd4454a6db68e08a9f Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Mon, 17 Jun 2019 16:00:29 +0100 Subject: [PATCH] [Fresh] Fall back to Map/Set if Weak equivalents are not available (#15907) * Fall back to Map/Set if Weak equivalents are not available * Fix Prettier vs Flow fighting --- packages/react-refresh/src/ReactFreshRuntime.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/packages/react-refresh/src/ReactFreshRuntime.js b/packages/react-refresh/src/ReactFreshRuntime.js index 24f434702e..2ccfe7eece 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.