Skip to content

Commit b2ace54

Browse files
Stream onOutput in API fix, readme updates
1 parent b50cc10 commit b2ace54

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"printableName": "Cache Web Terminal",
66
"description": "Web-based terminal emulator for Caché administering.",
77
"author": "ZitRo",
8-
"version": "4.4.0",
8+
"version": "4.4.1",
99
"gaID": "UA-83005064-2",
1010
"releaseNumber": 26,
1111
"scripts": {

readme.md

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,27 @@ The next table demonstrates available API. Left column are `terminal` object pro
111111
the user after execution (prints "NAMESPACE > " as well).
112112
</td>
113113
</tr>
114+
<tr>
115+
<td>onOutput([ <b>options</b> ], <b>callback</b>)</td>
116+
<td>
117+
By default, <code>callback</code>(<u>strings</u>) will be called before the user is
118+
prompted for input, and <code>strings</code> array will always contain an array of
119+
chunks of all the text printed between the prompts. For example, if user writes
120+
<code>write 123</code> and presses "Enter", the <code>strings</code> will contain
121+
this array: <code>["\r\n", "1", "\r\n"]</code>. However, when user enters
122+
<code>write 1, 2, 3</code>, <code>strings</code> will result with
123+
<code>["\r\n", "1", "2", "3", "\r\n"]</code>. You can join this array with
124+
<code>join</code> array method to get the full output.<br/>
125+
Optional <code>options</code> object may include <code>stream</code> property, which
126+
is <code>false</code> by default. When set to <code>true</code>, callback will be
127+
fired any time any chunk is print to the terminal simultaneously.
128+
</td>
129+
</tr>
114130
<tr>
115-
<td>onUserInput(<b>cb</b>)</td>
131+
<td>onUserInput(<b>callback</b>)</td>
116132
<td>
117-
<b>cb</b>(<u>text</u>, <u>mode</u>) is fired right after user presses enter. Argument
118-
<code>text</code> is a <code>String</code> of user input, and
133+
<b>callback</b>(<u>text</u>, <u>mode</u>) is fired right after user presses enter.
134+
Argument <code>text</code> is a <code>String</code> of user input, and
119135
<code>mode</code> is a <code>Number</code>, which can be compared
120136
with one of the terminal mode constants, such as <code>MODE_PROMPT</code>.
121137
</td>
@@ -153,11 +169,18 @@ function myInitHandler (terminal) {
153169
terminal.execute("set hiddenVariable = 7", {
154170
echo: false // the default is false, this is just a demo
155171
});
156-
terminal.onUserInput(function (text, mode) {
172+
terminal.onUserInput((text, mode) => {
157173
if (mode !== terminal.MODE_PROMPT)
158174
return;
159175
terminal.print("\r\nYou've just entered the next command: " + text);
160176
});
177+
terminal.onOutput((chunks) => {
178+
// If you "write 12", chunks are ["\r\n", "12", "\r\n"].
179+
// If you "write 1, 2", chunks are ["\r\n", "1", "2", "\r\n"].
180+
if (chunks[1] === "duck") {
181+
alert(`You've found a secret phrase!`);
182+
}
183+
});
161184
}
162185

163186
// At first, handle iFrame load event. Note that the load handler won't work

src/client/js/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ let onAuthHandlers = [],
1212
userInputHandlers = [],
1313
outputHandlers = [],
1414
bufferedOutput = [],
15+
inputIsActivated = false,
1516
AUTHORIZED = false,
1617
terminal = null;
1718

@@ -36,6 +37,7 @@ export function authDone () {
3637
}
3738

3839
export const inputActivated = () => {
40+
inputIsActivated = true;
3941
if (bufferedOutput.length) {
4042
for (const handler of outputHandlers) {
4143
if (handler.stream)
@@ -49,12 +51,13 @@ export const inputActivated = () => {
4951
export const onUserInput = (text, mode) => {
5052
userInputHandlers.forEach((h) => h(text, mode));
5153
bufferedOutput = [];
54+
inputIsActivated = false;
5255
};
5356

5457
export const onOutput = (string) => {
5558
bufferedOutput.push(string);
5659
for (const handler of outputHandlers) {
57-
if (!handler.stream)
60+
if (!handler.stream || inputIsActivated)
5861
continue;
5962
handler.callback([ string ]);
6063
}

0 commit comments

Comments
 (0)