@@ -19,13 +19,35 @@ import { ITheme, Terminal } from "xterm"
19
19
import { FitAddon } from "xterm-addon-fit"
20
20
import { Events } from "@kui-shell/core"
21
21
22
+ type WatchInit = ( ) => {
23
+ /**
24
+ * Will be used to attach to an underlying streaming
25
+ * provider of additional terminal output.
26
+ */
27
+ on ( eventType : "data" , cb : ( data : any ) => void ) : void
28
+
29
+ /**
30
+ * Terminate any streaming. Will be invoked un unmount, whenever
31
+ * `this.props.streamer` is given.
32
+ */
33
+ unwatch ( ) : void
34
+ }
35
+
22
36
interface Props {
37
+ /** If given, the initial terminal output to render */
23
38
initialContent ?: string
24
- on ?( eventType : "data" , cb : ( data : any ) => void ) : void
25
- unwatch ?( ) : void
39
+
40
+ /**
41
+ * Commence/recommence streaming. Will be invoked on mount.
42
+ */
43
+ watch ?: WatchInit
44
+ }
45
+
46
+ interface State {
47
+ streamer ?: ReturnType < WatchInit >
26
48
}
27
49
28
- export default class XTerm extends React . PureComponent < Props > {
50
+ export default class XTerm extends React . PureComponent < Props , State > {
29
51
private terminal : Terminal = new Terminal ( {
30
52
convertEol : true ,
31
53
scrollback : 5000 ,
@@ -37,17 +59,19 @@ export default class XTerm extends React.PureComponent<Props> {
37
59
public componentDidMount ( ) {
38
60
this . mountTerminal ( )
39
61
40
- if ( this . props . on ) {
41
- this . props . on ( "data" , this . terminal . write . bind ( this . terminal ) )
62
+ if ( this . props . watch ) {
63
+ const streamer = this . props . watch ( )
64
+ streamer . on ( "data" , this . terminal . write . bind ( this . terminal ) )
65
+ this . setState ( { streamer } )
42
66
}
43
67
}
44
68
45
69
public componentWillUnmount ( ) {
46
70
this . unmountTerminal ( )
47
71
this . cleaners . forEach ( ( cleaner ) => cleaner ( ) )
48
72
49
- if ( this . props . unwatch ) {
50
- this . props . unwatch ( )
73
+ if ( this . state . streamer ) {
74
+ this . state . streamer . unwatch ( )
51
75
}
52
76
}
53
77
0 commit comments