@@ -29,6 +29,15 @@ type PreactLikeClassComponent = new (
2929 render ( ...args : unknown [ ] ) : unknown ;
3030} ;
3131
32+ const PREACT_FUNCTION_COMPONENT_WRAPPERS = new WeakMap <
33+ ( props : Record < string , JsonValue > ) => unknown ,
34+ ( props : Record < string , JsonValue > ) => unknown
35+ > ( ) ;
36+ const PREACT_CLASS_COMPONENT_WRAPPERS = new WeakMap <
37+ PreactLikeClassComponent ,
38+ PreactLikeClassComponent
39+ > ( ) ;
40+
3241export type RuntimeComponentFactory = (
3342 props : Record < string , JsonValue > ,
3443 context : RuntimeExecutionContext ,
@@ -244,7 +253,12 @@ function isPreactClassComponent(value: unknown): boolean {
244253function wrapPreactClassComponent (
245254 sourceComponent : PreactLikeClassComponent ,
246255) : PreactLikeClassComponent {
247- return class RenderifyPreactSourceClassWrapper extends sourceComponent {
256+ const cached = PREACT_CLASS_COMPONENT_WRAPPERS . get ( sourceComponent ) ;
257+ if ( cached ) {
258+ return cached ;
259+ }
260+
261+ const wrapped = class RenderifyPreactSourceClassWrapper extends sourceComponent {
248262 render ( ...args : unknown [ ] ) : unknown {
249263 const output = super . render ( ...args ) ;
250264 if ( isPlainObjectPreactOutput ( output ) ) {
@@ -255,12 +269,20 @@ function wrapPreactClassComponent(
255269 return output ;
256270 }
257271 } ;
272+
273+ PREACT_CLASS_COMPONENT_WRAPPERS . set ( sourceComponent , wrapped ) ;
274+ return wrapped ;
258275}
259276
260277function wrapPreactFunctionComponent (
261278 sourceComponent : ( props : Record < string , JsonValue > ) => unknown ,
262279) : ( props : Record < string , JsonValue > ) => unknown {
263- return function RenderifyPreactSourceWrapper (
280+ const cached = PREACT_FUNCTION_COMPONENT_WRAPPERS . get ( sourceComponent ) ;
281+ if ( cached ) {
282+ return cached ;
283+ }
284+
285+ const wrapped = function RenderifyPreactSourceWrapper (
264286 props : Record < string , JsonValue > ,
265287 ) : unknown {
266288 const output = sourceComponent ( props ) ;
@@ -271,6 +293,9 @@ function wrapPreactFunctionComponent(
271293 }
272294 return output ;
273295 } ;
296+
297+ PREACT_FUNCTION_COMPONENT_WRAPPERS . set ( sourceComponent , wrapped ) ;
298+ return wrapped ;
274299}
275300
276301async function loadPreactModule ( ) : Promise < PreactLikeModule | undefined > {
0 commit comments