@react-navigation/core 7.0.0-rc.14 → 7.0.0-rc.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -37,10 +37,24 @@ const getActiveRoute = (state: State): { name: string; params?: object } => {
37
37
  return route;
38
38
  };
39
39
 
40
- let cachedNormalizedConfigs: [
41
- PathConfigMap<{}> | undefined,
42
- Record<string, ConfigItem>,
43
- ] = [undefined, {}];
40
+ const cachedNormalizedConfigs = new WeakMap<
41
+ PathConfigMap<{}>,
42
+ Record<string, ConfigItem>
43
+ >();
44
+
45
+ const getNormalizedConfigs = (options?: Options<{}>) => {
46
+ if (!options?.screens) return {};
47
+
48
+ const cached = cachedNormalizedConfigs.get(options?.screens);
49
+
50
+ if (cached) return cached;
51
+
52
+ const normalizedConfigs = createNormalizedConfigs(options.screens);
53
+
54
+ cachedNormalizedConfigs.set(options.screens, normalizedConfigs);
55
+
56
+ return normalizedConfigs;
57
+ };
44
58
 
45
59
  /**
46
60
  * Utility to serialize a navigation state object to a path string.
@@ -85,14 +99,7 @@ export function getPathFromState<ParamList extends {}>(
85
99
  validatePathConfig(options);
86
100
  }
87
101
 
88
- // Create a normalized configs object which will be easier to use
89
- if (cachedNormalizedConfigs[0] !== options?.screens) {
90
- cachedNormalizedConfigs = [
91
- options?.screens,
92
- options?.screens ? createNormalizedConfigs(options.screens) : {},
93
- ];
94
- }
95
- const configs: Record<string, ConfigItem> = cachedNormalizedConfigs[1];
102
+ const configs = getNormalizedConfigs(options);
96
103
 
97
104
  let path = '/';
98
105
  let current: State | undefined = state;
@@ -169,19 +169,22 @@ export function getStateFromPath<ParamList extends {}>(
169
169
  /**
170
170
  * Reference to the last used config resources. This is used to avoid recomputing the config resources when the options are the same.
171
171
  */
172
- let cachedConfigResources: [Options<{}> | undefined, ConfigResources] = [
173
- undefined,
174
- prepareConfigResources(),
175
- ];
172
+ const cachedConfigResources = new WeakMap<Options<{}>, ConfigResources>();
176
173
 
177
174
  function getConfigResources<ParamList extends {}>(
178
175
  options: Options<ParamList> | undefined
179
176
  ) {
180
- if (cachedConfigResources[0] !== options) {
181
- cachedConfigResources = [options, prepareConfigResources(options)];
182
- }
177
+ if (!options) return prepareConfigResources();
178
+
179
+ const cached = cachedConfigResources.get(options);
180
+
181
+ if (cached) return cached;
182
+
183
+ const resources = prepareConfigResources(options);
184
+
185
+ cachedConfigResources.set(options, resources);
183
186
 
184
- return cachedConfigResources[1];
187
+ return resources;
185
188
  }
186
189
 
187
190
  function prepareConfigResources(options?: Options<{}>) {