From 47f90d0a1f30a3e973313bbf8c7544e11ed42993 Mon Sep 17 00:00:00 2001 From: Robin Munn Date: Sun, 5 Dec 2021 11:38:27 +0700 Subject: [PATCH] Don't throw and catch useless errors in flush() Inside flush(), we don't care if there is a current component or not, we just want to save and restore it if there is one. Rather than throwing and catching an error that would happen often, we'll just create another version of get_current_component() that doesn't throw. --- src/runtime/internal/lifecycle.ts | 4 ++++ src/runtime/internal/scheduler.ts | 9 ++------- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/runtime/internal/lifecycle.ts b/src/runtime/internal/lifecycle.ts index bb3df3d295..028e02d56d 100644 --- a/src/runtime/internal/lifecycle.ts +++ b/src/runtime/internal/lifecycle.ts @@ -11,6 +11,10 @@ export function get_current_component() { return current_component; } +export function maybe_get_current_component() { + return current_component; +} + export function beforeUpdate(fn: () => any) { get_current_component().$$.before_update.push(fn); } diff --git a/src/runtime/internal/scheduler.ts b/src/runtime/internal/scheduler.ts index 73bd5e8395..0e5038df80 100644 --- a/src/runtime/internal/scheduler.ts +++ b/src/runtime/internal/scheduler.ts @@ -1,5 +1,5 @@ import { run_all } from './utils'; -import { get_current_component, set_current_component } from './lifecycle'; +import { maybe_get_current_component, set_current_component } from './lifecycle'; export const dirty_components = []; export const intros = { enabled: false }; @@ -35,12 +35,7 @@ const seen_callbacks = new Set(); let flushidx = 0; // Do *not* move this inside the flush() function export function flush() { - let current_component = null; - try { - current_component = get_current_component(); - } catch { - // no current component, so leave it as null - } + let current_component = maybe_get_current_component(); do { // first, call beforeUpdate functions