Skip to content

Commit 03339d5

Browse files
arturovtkirjs
authored andcommitted
refactor(core): change node navigation step to plain const (#59469)
We change the `enum` to a plain `const` to eliminate extra bytes, as `enum` is not really required. We might not be able to switch to `const enum` due to single-file compilation restrictions. PR Close #59469
1 parent 6fc5f18 commit 03339d5

File tree

4 files changed

+19
-12
lines changed

4 files changed

+19
-12
lines changed

packages/core/src/hydration/interfaces.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,15 @@ export const REFERENCE_NODE_BODY = 'b';
1919
/**
2020
* Describes navigation steps that the runtime logic need to perform,
2121
* starting from a given (known) element.
22+
* We're not using enum `NodeNavigationStep` because it produces more code overhead;
23+
* thus, using plain `const` eliminates extra bytes. We can't use `const enum` due
24+
* to single-file compilation restrictions.
2225
*/
23-
export enum NodeNavigationStep {
24-
FirstChild = 'f',
25-
NextSibling = 'n',
26-
}
26+
27+
export type NodeNavigationStep = 'f' | 'n';
28+
29+
export const NODE_NAVIGATION_STEP_FIRST_CHILD = 'f';
30+
export const NODE_NAVIGATION_STEP_NEXT_SIBLING = 'n';
2731

2832
/**
2933
* Keys within serialized view data structure to represent various

packages/core/src/hydration/node_lookup_utils.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ import {
2929
} from './error_handling';
3030
import {
3131
DehydratedView,
32+
NODE_NAVIGATION_STEP_FIRST_CHILD,
33+
NODE_NAVIGATION_STEP_NEXT_SIBLING,
3234
NodeNavigationStep,
3335
NODES,
3436
REFERENCE_NODE_BODY,
@@ -198,7 +200,7 @@ function stringifyNavigationInstructions(instructions: (number | NodeNavigationS
198200
const step = instructions[i];
199201
const repeat = instructions[i + 1] as number;
200202
for (let r = 0; r < repeat; r++) {
201-
container.push(step === NodeNavigationStep.FirstChild ? 'firstChild' : 'nextSibling');
203+
container.push(step === NODE_NAVIGATION_STEP_FIRST_CHILD ? 'firstChild' : 'nextSibling');
202204
}
203205
}
204206
return container.join('.');
@@ -218,10 +220,10 @@ function navigateToNode(from: Node, instructions: (number | NodeNavigationStep)[
218220
throw nodeNotFoundAtPathError(from, stringifyNavigationInstructions(instructions));
219221
}
220222
switch (step) {
221-
case NodeNavigationStep.FirstChild:
223+
case NODE_NAVIGATION_STEP_FIRST_CHILD:
222224
node = node.firstChild!;
223225
break;
224-
case NodeNavigationStep.NextSibling:
226+
case NODE_NAVIGATION_STEP_NEXT_SIBLING:
225227
node = node.nextSibling!;
226228
break;
227229
}
@@ -279,7 +281,7 @@ export function navigateBetween(start: Node, finish: Node): NodeNavigationStep[]
279281
// First navigate to `finish`'s parent
280282
...parentPath,
281283
// Then to its first child.
282-
NodeNavigationStep.FirstChild,
284+
NODE_NAVIGATION_STEP_FIRST_CHILD,
283285
// And finally from that node to `finish` (maybe a no-op if we're already there).
284286
...childPath,
285287
];
@@ -294,7 +296,7 @@ function navigateBetweenSiblings(start: Node, finish: Node): NodeNavigationStep[
294296
const nav: NodeNavigationStep[] = [];
295297
let node: Node | null = null;
296298
for (node = start; node != null && node !== finish; node = node.nextSibling) {
297-
nav.push(NodeNavigationStep.NextSibling);
299+
nav.push(NODE_NAVIGATION_STEP_NEXT_SIBLING);
298300
}
299301
// If the `node` becomes `null` or `undefined` at the end, that means that we
300302
// didn't find the `end` node, thus return `null` (which would trigger serialization

packages/core/test/bundling/hydration/bundle.golden_symbols.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@
100100
"NodeInjector",
101101
"NodeInjectorDestroyRef",
102102
"NodeInjectorFactory",
103-
"NodeNavigationStep",
104103
"NoneEncapsulationDomRenderer",
105104
"NoopNgZone",
106105
"NullInjector",

packages/core/test/hydration/compression_spec.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,17 @@
88

99
import {compressNodeLocation, decompressNodeLocation} from '../../src/hydration/compression';
1010
import {
11+
NODE_NAVIGATION_STEP_FIRST_CHILD,
12+
NODE_NAVIGATION_STEP_NEXT_SIBLING,
1113
NodeNavigationStep,
1214
REFERENCE_NODE_BODY,
1315
REFERENCE_NODE_HOST,
1416
} from '../../src/hydration/interfaces';
1517

1618
describe('compression of node location', () => {
1719
it('should handle basic cases', () => {
18-
const fc = NodeNavigationStep.FirstChild;
19-
const ns = NodeNavigationStep.NextSibling;
20+
const fc = NODE_NAVIGATION_STEP_FIRST_CHILD;
21+
const ns = NODE_NAVIGATION_STEP_NEXT_SIBLING;
2022
const cases = [
2123
[[REFERENCE_NODE_HOST, fc, 1], 'hf'],
2224
[[REFERENCE_NODE_BODY, fc, 1], 'bf'],

0 commit comments

Comments
 (0)