1
1
import { type ResultRenderer } from '../../engine/ResultRenderer' ;
2
2
import { type JsFunc } from '../../engine/JsExecution' ;
3
+ import { MessageType } from 'jsEngine/messages/MessageManager' ;
4
+ import { type API } from 'jsEngine/api/API' ;
3
5
4
6
/**
5
7
* A reactive component is a component that can be refreshed.
@@ -8,14 +10,16 @@ import { type JsFunc } from '../../engine/JsExecution';
8
10
* See {@link API.reactive}
9
11
*/
10
12
export class ReactiveComponent {
13
+ private readonly apiInstance : API ;
11
14
private readonly _render : JsFunc ;
12
15
private readonly initialArgs : unknown [ ] ;
13
16
/**
14
17
* @internal
15
18
*/
16
19
renderer : ResultRenderer | undefined ;
17
20
18
- constructor ( _render : JsFunc , initialArgs : unknown [ ] ) {
21
+ constructor ( api : API , _render : JsFunc , initialArgs : unknown [ ] ) {
22
+ this . apiInstance = api ;
19
23
this . _render = _render ;
20
24
this . initialArgs = initialArgs ;
21
25
}
@@ -26,7 +30,25 @@ export class ReactiveComponent {
26
30
* @param args
27
31
*/
28
32
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 ) ;
30
52
}
31
53
32
54
/**
0 commit comments