@samitouri / QOS-React-2 / commits / cd98e63b47

[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

Dan Abramov committed Jun 17, 2019 at 16:00 UTC cd98e63b47198147cfd140cd4454a6db68e08a9f
1 file changed +11 -3
packages/react-refresh/src/ReactFreshRuntime.js
+11 -3
@@ -21,14 +21,22 @@ type Signature = {|
21 getCustomHooks: () => Array<Function>,
22 |};
23
24 +// In old environments, we'll leak previous types after every edit.
25 +const PossiblyWeakMap = typeof WeakMap === 'function' ? WeakMap : Map;
26 +const PossiblyWeakSet = typeof WeakSet === 'function' ? WeakSet : Set;
27 +
28 // We never remove these associations.
29 // It's OK to reference families, but use WeakMap/Set for types.
30 const allFamiliesByID: Map<string, Family> = new Map();
27 -const allTypes: WeakSet<any> = new WeakSet();
28 -const allSignaturesByType: WeakMap<any, Signature> = new WeakMap();
31 +// $FlowIssue
32 +const allTypes: WeakSet<any> | Set<any> = new PossiblyWeakSet();
33 +const allSignaturesByType: // $FlowIssue
34 +WeakMap<any, Signature> | Map<any, Signature> = new PossiblyWeakMap();
35 // This WeakMap is read by React, so we only put families
36 // that have actually been edited here. This keeps checks fast.
31 -const familiesByType: WeakMap<any, Family> = new WeakMap();
37 +// $FlowIssue
38 +const familiesByType: // $FlowIssue
39 +WeakMap<any, Family> | Map<any, Family> = new PossiblyWeakMap();
40
41 // This is cleared on every prepareUpdate() call.
42 // It is an array of [Family, NextType] tuples.