From 14778f64017d46e6ee6d73218926d7f35281955e Mon Sep 17 00:00:00 2001 From: Andrei Zhaleznichenka Date: Thu, 12 Sep 2024 13:24:24 +0200 Subject: [PATCH] avoid calling listener when provider is not defined --- pages/alert/runtime-content.page.tsx | 4 ++-- .../controllers/alert-flash-content.ts | 19 +++++++++++-------- .../helpers/use-discovered-content.tsx | 2 +- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/pages/alert/runtime-content.page.tsx b/pages/alert/runtime-content.page.tsx index 735b2706b5d..589d2b552bd 100644 --- a/pages/alert/runtime-content.page.tsx +++ b/pages/alert/runtime-content.page.tsx @@ -1,7 +1,7 @@ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 -import React, { useContext, useState } from 'react'; +import React, { useContext, useMemo, useState } from 'react'; import { render, unmountComponentAtNode } from 'react-dom'; import { @@ -72,7 +72,7 @@ export default function () { const [unrelatedState, setUnrelatedState] = useState(false); const [contentSwapped, setContentSwapped] = useState(false); - const content1 = loading ? Loading... : Content; + const content1 = useMemo(() => (loading ? Loading... : Content), [loading]); const content2 = loading ? Loading... : There was an error: Access denied because of XYZ; return ( diff --git a/src/internal/plugins/controllers/alert-flash-content.ts b/src/internal/plugins/controllers/alert-flash-content.ts index 6d339d3b85d..ea9722ece16 100644 --- a/src/internal/plugins/controllers/alert-flash-content.ts +++ b/src/internal/plugins/controllers/alert-flash-content.ts @@ -35,7 +35,7 @@ export interface AlertFlashContentConfig { runReplacer: (context: AlertFlashContentContext, replacementApi: ReplacementApi) => AlertFlashContentResult; } -export type AlertFlashContentRegistrationListener = (provider?: AlertFlashContentConfig) => void | (() => void); +export type AlertFlashContentRegistrationListener = (provider: AlertFlashContentConfig) => () => void; export interface AlertFlashContentApiPublic { registerContentReplacer(config: AlertFlashContentConfig): void; @@ -48,14 +48,16 @@ export interface AlertFlashContentApiInternal { export class AlertFlashContentController { #listeners: Array = []; - #cleanups = new Map void)>(); + #cleanups = new Map void>(); #provider?: AlertFlashContentConfig; #scheduleUpdate = debounce( () => this.#listeners.forEach(listener => { - const cleanup = listener(this.#provider) ?? null; - this.#cleanups.set(listener, cleanup); + if (this.#provider) { + const cleanup = listener(this.#provider); + this.#cleanups.set(listener, cleanup); + } }), 0 ); @@ -78,12 +80,13 @@ export class AlertFlashContentController { }; onContentRegistered = (listener: AlertFlashContentRegistrationListener) => { - const cleanup = listener(this.#provider); - this.#listeners.push(listener); - if (cleanup) { + if (this.#provider) { + const cleanup = listener(this.#provider); + this.#listeners.push(listener); this.#cleanups.set(listener, cleanup); + } else { + this.#listeners.push(listener); } - return () => { this.#cleanups.get(listener)?.(); this.#listeners = this.#listeners.filter(item => item !== listener); diff --git a/src/internal/plugins/helpers/use-discovered-content.tsx b/src/internal/plugins/helpers/use-discovered-content.tsx index 2de50d8999b..3b46c4d7afb 100644 --- a/src/internal/plugins/helpers/use-discovered-content.tsx +++ b/src/internal/plugins/helpers/use-discovered-content.tsx @@ -45,7 +45,7 @@ export function createUseDiscoveredContent( return true; } - mountedProvider.current = provider?.runReplacer(context, { + mountedProvider.current = provider.runReplacer(context, { hideHeader() { if (checkMounted('hideHeader')) { setFoundHeaderReplacement('remove');