@@ -19,13 +19,35 @@ import { ITheme, Terminal } from "xterm"
1919import { FitAddon } from "xterm-addon-fit"
2020import { Events } from "@kui-shell/core"
2121
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+
2236interface Props {
37+ /** If given, the initial terminal output to render */
2338 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 >
2648}
2749
28- export default class XTerm extends React . PureComponent < Props > {
50+ export default class XTerm extends React . PureComponent < Props , State > {
2951 private terminal : Terminal = new Terminal ( {
3052 convertEol : true ,
3153 scrollback : 5000 ,
@@ -37,17 +59,19 @@ export default class XTerm extends React.PureComponent<Props> {
3759 public componentDidMount ( ) {
3860 this . mountTerminal ( )
3961
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 } )
4266 }
4367 }
4468
4569 public componentWillUnmount ( ) {
4670 this . unmountTerminal ( )
4771 this . cleaners . forEach ( ( cleaner ) => cleaner ( ) )
4872
49- if ( this . props . unwatch ) {
50- this . props . unwatch ( )
73+ if ( this . state . streamer ) {
74+ this . state . streamer . unwatch ( )
5175 }
5276 }
5377
0 commit comments