@@ -10,6 +10,8 @@ import { get, getURLParams } from "./lib";
1010
1111let onAuthHandlers = [ ] ,
1212 userInputHandlers = [ ] ,
13+ outputHandlers = [ ] ,
14+ bufferedOutput = [ ] ,
1315 AUTHORIZED = false ,
1416 terminal = null ;
1517
@@ -33,9 +35,30 @@ export function authDone () {
3335 onAuthHandlers . forEach ( h => h ( terminal ) ) ;
3436}
3537
36- export function onUserInput ( text , mode ) {
38+ export const inputActivated = ( ) => {
39+ if ( bufferedOutput . length ) {
40+ for ( const handler of outputHandlers ) {
41+ if ( handler . stream )
42+ continue ;
43+ handler . callback ( bufferedOutput ) ;
44+ }
45+ bufferedOutput = [ ] ;
46+ }
47+ } ;
48+
49+ export const onUserInput = ( text , mode ) => {
3750 userInputHandlers . forEach ( ( h ) => h ( text , mode ) ) ;
38- }
51+ bufferedOutput = [ ] ;
52+ } ;
53+
54+ export const onOutput = ( string ) => {
55+ bufferedOutput . push ( string ) ;
56+ for ( const handler of outputHandlers ) {
57+ if ( ! handler . stream )
58+ continue ;
59+ handler . callback ( [ string ] ) ;
60+ }
61+ } ;
3962
4063/**
4164 * Register the callback which will be executed right after terminal is initialized. This callback
@@ -58,14 +81,43 @@ Terminal.prototype.MODE_PROMPT = 1;
5881Terminal . prototype . MODE_SQL = 2 ;
5982Terminal . prototype . MODE_READ = 3 ;
6083Terminal . prototype . MODE_READ_CHAR = 4 ;
61- Terminal . prototype . MODE_SPECIAL = 1 ;
84+ Terminal . prototype . MODE_SPECIAL = 5 ;
85+
86+ /**
87+ * Function accepts the callback, which is fired when user enter a command, character or a string.
88+ * @param {{ [stream]: boolean=false, [callback]: function } } [options]
89+ * @param {terminalOutputCallback } callback
90+ * @returns {function } - Your callback.
91+ */
92+ Terminal . prototype . onOutput = function ( options , callback ) {
93+ if ( ! options || typeof options === "function" ) {
94+ callback = options || ( ( ) => { throw new Error ( "onOutput: no callback provided!" ) ; } ) ;
95+ options = { } ;
96+ }
97+ if ( typeof options . stream === "undefined" )
98+ options . stream = false ;
99+ options . callback = callback ;
100+ outputHandlers . push ( options ) ;
101+ return callback ;
102+ } ;
103+
104+ /**
105+ * Handles output both in stream or prompt mode.
106+ * @callback terminalOutputCallback
107+ * @param {string[] } - Output data presented as an array of string chunks. You can get the full
108+ * output as a single string by doing chunks.join("").
109+ */
62110
63111/**
64112 * Function accepts the callback, which is fired when user enter a command, character or a string.
65113 * @param {terminalUserEntryCallback } callback
114+ * @returns {function } - Your callback.
66115 */
67116Terminal . prototype . onUserInput = function ( callback ) {
117+ if ( typeof callback !== "function" )
118+ throw new Error ( "onUserInput: no callback provided!" ) ;
68119 userInputHandlers . push ( callback ) ;
120+ return callback ;
69121} ;
70122
71123/**
0 commit comments