11import { type ResultRenderer } from '../../engine/ResultRenderer' ;
22import { type JsFunc } from '../../engine/JsExecution' ;
3+ import { MessageType } from 'jsEngine/messages/MessageManager' ;
4+ import { type API } from 'jsEngine/api/API' ;
35
46/**
57 * A reactive component is a component that can be refreshed.
@@ -8,14 +10,16 @@ import { type JsFunc } from '../../engine/JsExecution';
810 * See {@link API.reactive}
911 */
1012export class ReactiveComponent {
13+ private readonly apiInstance : API ;
1114 private readonly _render : JsFunc ;
1215 private readonly initialArgs : unknown [ ] ;
1316 /**
1417 * @internal
1518 */
1619 renderer : ResultRenderer | undefined ;
1720
18- constructor ( _render : JsFunc , initialArgs : unknown [ ] ) {
21+ constructor ( api : API , _render : JsFunc , initialArgs : unknown [ ] ) {
22+ this . apiInstance = api ;
1923 this . _render = _render ;
2024 this . initialArgs = initialArgs ;
2125 }
@@ -26,7 +30,25 @@ export class ReactiveComponent {
2630 * @param args
2731 */
2832 public async refresh ( ...args : unknown [ ] ) : Promise < void > {
29- void this . renderer ?. render ( await this . _render ( ...args ) ) ;
33+ let result : unknown ;
34+
35+ try {
36+ // eslint-disable-next-line
37+ result = await this . _render ( ...args ) ;
38+ } catch ( e ) {
39+ console . warn ( 'failed to execute JS' , e ) ;
40+
41+ if ( e instanceof Error ) {
42+ result = this . apiInstance . message . createMessage (
43+ MessageType . ERROR ,
44+ 'Failed to execute JS' ,
45+ `Failed to execute JS during reactive execution in execution "${ this . apiInstance . instanceId . id } "` ,
46+ e . stack ,
47+ ) ;
48+ }
49+ }
50+
51+ void this . renderer ?. render ( result ) ;
3052 }
3153
3254 /**
0 commit comments