From 07a0ae449bfd0542caa8a6925cb952b5a1e58a40 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Thu, 25 Jan 2024 19:39:15 -0500 Subject: [PATCH] chore: simplify code (#10294) * chore: simplify code * a few more * constify --------- Co-authored-by: Rich Harris --- .../svelte/src/internal/client/runtime.js | 18 +++------ .../svelte/src/internal/client/transitions.js | 37 +++++++------------ packages/svelte/src/main/main-client.js | 10 +---- 3 files changed, 20 insertions(+), 45 deletions(-) diff --git a/packages/svelte/src/internal/client/runtime.js b/packages/svelte/src/internal/client/runtime.js index 6fdd30e229..80059dc929 100644 --- a/packages/svelte/src/internal/client/runtime.js +++ b/packages/svelte/src/internal/client/runtime.js @@ -1420,13 +1420,10 @@ export function user_effect(init) { !apply_component_effect_heuristics ); if (apply_component_effect_heuristics) { - let effects = /** @type {import('./types.js').ComponentContext} */ (current_component_context) - .e; - if (effects === null) { - effects = /** @type {import('./types.js').ComponentContext} */ (current_component_context).e = - []; - } - effects.push(effect); + const context = /** @type {import('./types.js').ComponentContext} */ ( + current_component_context + ); + (context.e ??= []).push(effect); } return effect; } @@ -1744,12 +1741,7 @@ export function get_or_init_context_map() { (DEV ? 'Context can only be used during component initialisation.' : '') ); } - let context_map = component_context.c; - if (context_map === null) { - const parent_context = get_parent_context(component_context); - context_map = component_context.c = new Map(parent_context || undefined); - } - return context_map; + return (component_context.c ??= new Map(get_parent_context(component_context) || undefined)); } /** diff --git a/packages/svelte/src/internal/client/transitions.js b/packages/svelte/src/internal/client/transitions.js index 254b06e0dd..f9d0133719 100644 --- a/packages/svelte/src/internal/client/transitions.js +++ b/packages/svelte/src/internal/client/transitions.js @@ -683,10 +683,7 @@ function if_block_transition(transition) { const block = this; // block.value === true if (block.v) { - let consequent_transitions = block.c; - if (consequent_transitions === null) { - consequent_transitions = block.c = new Set(); - } + const consequent_transitions = (block.c ??= new Set()); consequent_transitions.add(transition); transition.f(() => { const c = /** @type {Set} */ (consequent_transitions); @@ -697,10 +694,7 @@ function if_block_transition(transition) { } }); } else { - let alternate_transitions = block.a; - if (alternate_transitions === null) { - alternate_transitions = block.a = new Set(); - } + const alternate_transitions = (block.a ??= new Set()); alternate_transitions.add(transition); transition.f(() => { const a = /** @type {Set} */ (alternate_transitions); @@ -732,25 +726,20 @@ function each_item_transition(transition) { if (transition.r === 'key' && (each_block.f & EACH_IS_ANIMATED) === 0) { each_block.f |= EACH_IS_ANIMATED; } - let transitions = block.s; - if (transitions === null) { - block.s = transitions = new Set(); - } + const transitions = (block.s ??= new Set()); transition.f(() => { - if (transitions !== null) { - transitions.delete(transition); - if (transition.r !== 'key') { - for (let other of transitions) { - const type = other.r; - if (type === 'key' || type === 'in') { - transitions.delete(other); - } - } - if (transitions.size === 0) { - block.s = null; - destroy_each_item_block(block, null, true); + transitions.delete(transition); + if (transition.r !== 'key') { + for (let other of transitions) { + const type = other.r; + if (type === 'key' || type === 'in') { + transitions.delete(other); } } + if (transitions.size === 0) { + block.s = null; + destroy_each_item_block(block, null, true); + } } }); transitions.add(transition); diff --git a/packages/svelte/src/main/main-client.js b/packages/svelte/src/main/main-client.js index 5924eb6c01..c813008d2b 100644 --- a/packages/svelte/src/main/main-client.js +++ b/packages/svelte/src/main/main-client.js @@ -215,10 +215,7 @@ export function beforeUpdate(fn) { throw new Error('beforeUpdate can only be used during component initialisation.'); } - if (component_context.u === null) { - component_context.u = init_update_callbacks(); - } - component_context.u.b.push(fn); + (component_context.u ??= init_update_callbacks()).b.push(fn); } /** @@ -239,10 +236,7 @@ export function afterUpdate(fn) { throw new Error('afterUpdate can only be used during component initialisation.'); } - if (component_context.u === null) { - component_context.u = init_update_callbacks(); - } - component_context.u.a.push(fn); + (component_context.u ??= init_update_callbacks()).a.push(fn); } // TODO bring implementations in here