Skip to content

Commit

Permalink
avoid calling listener when provider is not defined
Browse files Browse the repository at this point in the history
  • Loading branch information
pan-kot committed Sep 12, 2024
1 parent d3b0aba commit 14778f6
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
4 changes: 2 additions & 2 deletions pages/alert/runtime-content.page.tsx
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -72,7 +72,7 @@ export default function () {
const [unrelatedState, setUnrelatedState] = useState(false);
const [contentSwapped, setContentSwapped] = useState(false);

const content1 = loading ? <Box>Loading...</Box> : <Box>Content</Box>;
const content1 = useMemo(() => (loading ? <Box>Loading...</Box> : <Box>Content</Box>), [loading]);
const content2 = loading ? <Box>Loading...</Box> : <Box>There was an error: Access denied because of XYZ</Box>;

return (
Expand Down
19 changes: 11 additions & 8 deletions src/internal/plugins/controllers/alert-flash-content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -48,14 +48,16 @@ export interface AlertFlashContentApiInternal {

export class AlertFlashContentController {
#listeners: Array<AlertFlashContentRegistrationListener> = [];
#cleanups = new Map<AlertFlashContentRegistrationListener, null | (() => void)>();
#cleanups = new Map<AlertFlashContentRegistrationListener, () => 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
);
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/internal/plugins/helpers/use-discovered-content.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit 14778f6

Please sign in to comment.