Skip to content

Commit

Permalink
update the client package logic
Browse files Browse the repository at this point in the history
Signed-off-by: popcorny <[email protected]>
  • Loading branch information
popcornylu committed Jan 31, 2022
1 parent 514aae8 commit 28e2f31
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 15 deletions.
2 changes: 1 addition & 1 deletion colabxterm/client/dist/main.js

Large diffs are not rendered by default.

44 changes: 30 additions & 14 deletions colabxterm/client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ function main() {
(window as any).fitAddon = fitAddon;
term.loadAddon(fitAddon);
term.open(document.getElementById('terminal'));

// handle resize
const handleResize = () => {
term.element.parentElement.style.height = (window.innerHeight - 16) + "px"
fitAddon.fit();
Expand All @@ -22,26 +24,40 @@ function main() {

// handle input
const queue: string[] = [];
const sendData = lodash.throttle(() => {
let data = queue.join('')
let base64str = window.btoa(data);
fetch("/in/" + base64str);
queue.length = 0;
}, 100);

term.onData((data) => {
queue.push(data);
sendData();
});
(async () => {
const sleep = (time: number) => new Promise((resolve) => setTimeout(resolve, time))

try {
while (true) {
await sleep(100);
if (!lodash.isEmpty(queue)) {
let data = queue.join('');
let base64str = window.btoa(data);
queue.length = 0;
await fetch("/in/" + base64str);
}
}
} finally {
console.log("input disconnect!");
}
})();


// handle output
async function pullOutput() {
while(true) {
const response = await fetch("/out");
const byteArray = new Uint8Array(await response.arrayBuffer());
if (response) {
term.write(byteArray);
}
try {
while (true) {
const response = await fetch("/out");
const byteArray = new Uint8Array(await response.arrayBuffer());
if (response) {
term.write(byteArray);
}
}
} finally {
console.log("input disconnect!");
}
}
pullOutput();
Expand Down

0 comments on commit 28e2f31

Please sign in to comment.