Skip to content

Commit 4bf1f91

Browse files
committed
reduce args on if_block()
1 parent b13c419 commit 4bf1f91

File tree

2 files changed

+10
-14
lines changed
  • packages/svelte/src
    • compiler/phases/3-transform/client/visitors
    • internal/client/dom/blocks

2 files changed

+10
-14
lines changed

packages/svelte/src/compiler/phases/3-transform/client/visitors/IfBlock.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,14 @@ export function IfBlock(node, context) {
1818

1919
let alternate_id;
2020

21-
const r_index = b.id('$$r_index');
22-
const h_index = b.id('$$h_index');
23-
2421
if (node.alternate) {
2522
alternate_id = context.state.scope.generate('alternate');
2623
const alternate = /** @type {BlockStatement} */ (context.visit(node.alternate));
2724
const nodes = node.alternate.nodes;
2825

2926
let alternate_args = [b.id('$$anchor')];
3027
if (nodes.length === 1 && nodes[0].type === 'IfBlock' && nodes[0].elseif) {
31-
alternate_args.push(r_index, h_index);
28+
alternate_args.push(b.id('$$elseif'));
3229
}
3330

3431
statements.push(b.var(b.id(alternate_id), b.arrow(alternate_args, alternate)));
@@ -73,7 +70,7 @@ export function IfBlock(node, context) {
7370
// ...even though they're logically equivalent. In the first case, the
7471
// transition will only play when `y` changes, but in the second it
7572
// should play when `x` or `y` change — both are considered 'local'
76-
args.push(r_index, h_index);
73+
args.push(b.id('$$elseif'));
7774
}
7875

7976
statements.push(b.stmt(b.call('$.if', ...args)));

packages/svelte/src/internal/client/dom/blocks/if.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,11 @@ import { HYDRATION_START, HYDRATION_START_ELSE, UNINITIALIZED } from '../../../.
1313

1414
/**
1515
* @param {TemplateNode} node
16-
* @param {(branch: (fn: (anchor: Node, r_index?: number, h_index?: number) => void, flag?: boolean) => void) => void} fn
17-
* @param {number} [root_index]
18-
* @param {number} [hydrate_index]
16+
* @param {(branch: (fn: (anchor: Node, elseif?: [number,number]) => void, flag?: boolean) => void) => void} fn
17+
* @param {[number,number]} [elseif]
1918
* @returns {void}
2019
*/
21-
export function if_block(node, fn, root_index = 0, hydrate_index) {
20+
export function if_block(node, fn, [root_index, hydrate_index] = [0, 0]) {
2221
if (hydrating && root_index === 0) {
2322
hydrate_next();
2423
}
@@ -39,7 +38,7 @@ export function if_block(node, fn, root_index = 0, hydrate_index) {
3938
var has_branch = false;
4039

4140
const set_branch = (
42-
/** @type {(anchor: Node, r_index?: number, h_index?: number) => void} */ fn,
41+
/** @type {(anchor: Node, elseif?: [number,number]) => void} */ fn,
4342
flag = true
4443
) => {
4544
has_branch = true;
@@ -48,15 +47,15 @@ export function if_block(node, fn, root_index = 0, hydrate_index) {
4847

4948
const update_branch = (
5049
/** @type {boolean | null} */ new_condition,
51-
/** @type {null | ((anchor: Node, r_index?: number, h_index?: number) => void)} */ fn
50+
/** @type {null | ((anchor: Node, elseif?: [number,number]) => void)} */ fn
5251
) => {
5352
if (condition === (condition = new_condition)) return;
5453

5554
/** Whether or not there was a hydration mismatch. Needs to be a `let` or else it isn't treeshaken out */
5655
let mismatch = false;
5756

58-
if (hydrating && hydrate_index != -1) {
59-
if (hydrate_index === undefined) {
57+
if (hydrating && hydrate_index !== -1) {
58+
if (root_index === 0) {
6059
const data = /** @type {Comment} */ (anchor).data;
6160
if (data === HYDRATION_START) {
6261
hydrate_index = 0;
@@ -96,7 +95,7 @@ export function if_block(node, fn, root_index = 0, hydrate_index) {
9695
if (alternate_effect) {
9796
resume_effect(alternate_effect);
9897
} else if (fn) {
99-
alternate_effect = branch(() => fn(anchor, root_index + 1, hydrate_index));
98+
alternate_effect = branch(() => fn(anchor, [root_index + 1, hydrate_index]));
10099
}
101100

102101
if (consequent_effect) {

0 commit comments

Comments
 (0)