Skip to content

Commit

Permalink
prevent double initialization of playground
Browse files Browse the repository at this point in the history
  • Loading branch information
ashfame committed Oct 1, 2024
1 parent de42800 commit 4d27df8
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/ui/preview/Playground.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect } from 'react';
import { useEffect, useRef } from 'react';
import {
PlaygroundClient,

Check warning on line 3 in src/ui/preview/Playground.tsx

View workflow job for this annotation

GitHub Actions / lint

PlaygroundClient not found in '@wp-playground/client'
StartPlaygroundOptions,

Check warning on line 4 in src/ui/preview/Playground.tsx

View workflow job for this annotation

GitHub Actions / lint

StartPlaygroundOptions not found in '@wp-playground/client'
Expand All @@ -20,16 +20,20 @@ export function Playground( props: {
} ) {
const { slug, className, blogName, onReady } = props;

const initializationRef = useRef( false );

useEffect( () => {
const iframe = document.getElementById( playgroundIframeId );
if ( ! ( iframe instanceof HTMLIFrameElement ) ) {
throw Error( 'Playground container element must be an iframe' );
}
if ( iframe.src !== '' ) {
// Playground is already started.
if ( iframe.src !== '' || initializationRef.current ) {
// Playground is already started or initialization has been attempted.
return;
}

initializationRef.current = true;

initPlayground( iframe, slug, blogName )
.then( async ( client: PlaygroundClient ) => {
const url = await client.absoluteUrl;
Expand All @@ -39,7 +43,7 @@ export function Playground( props: {
.catch( ( error ) => {
throw error;
} );
}, [ slug, onReady ] );
}, [ slug, onReady, blogName ] );

return (
<iframe
Expand Down

0 comments on commit 4d27df8

Please sign in to comment.