Skip to content
This repository has been archived by the owner on Apr 13, 2024. It is now read-only.

Commit

Permalink
Migrate to AppConnection to communicate with the terminal on Puter
Browse files Browse the repository at this point in the history
  • Loading branch information
AtkinsSJ committed Apr 9, 2024
1 parent 2df8ab8 commit 7ee1318
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 180 deletions.
52 changes: 0 additions & 52 deletions src/XDocumentANSIShell.js

This file was deleted.

36 changes: 18 additions & 18 deletions src/main_puter.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,27 @@ window.main_shell = async () => {
const configured_ = new Promise(rslv => {
resolveConfigured = rslv;
});
window.addEventListener('message', evt => {
if ( evt.source !== window.parent ) return;
if ( evt.data instanceof Uint8Array ) {
return;
}
if ( ! evt.data.hasOwnProperty('$') ) {
console.error(`unrecognized window message`, evt);
return;
}
if ( evt.data.$ !== 'config' ) return;

console.log('received configuration at ANSI shell');
const configValues = { ...evt.data };
delete configValues.$;
for ( const k in configValues ) {
config[k] = configValues[k];
const terminal = puter.ui.parentApp();
if (!terminal) {
console.error('Phoenix cannot run without a parent Terminal. Exiting...');
puter.exit();
return;
}
terminal.on('message', message => {
if (message.$ === 'config') {
const configValues = { ...message };
delete configValues.$;
for ( const k in configValues ) {
config[k] = configValues[k];
}
resolveConfigured();
}
resolveConfigured();
});

window.parent.postMessage({ $: 'ready' }, '*');
// FIXME: on terminal close, close ourselves

terminal.postMessage({ $: 'ready' });

await configured_;

Expand All @@ -63,7 +63,7 @@ window.main_shell = async () => {
|| 'https://api.puter.com';
await puterSDK.setAPIOrigin(source_without_trailing_slash);

const ptt = new XDocumentPTT();
const ptt = new XDocumentPTT(terminal);
await launchPuterShell(new Context({
ptt,
config, puterSDK,
Expand Down
25 changes: 13 additions & 12 deletions src/pty/XDocumentPTT.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { BetterReader } from "dev-pty";
const encoder = new TextEncoder();

export class XDocumentPTT {
constructor() {
constructor(terminalConnection) {
this.ioctl_listeners = {};

this.readableStream = new ReadableStream({
Expand All @@ -37,24 +37,25 @@ export class XDocumentPTT {
if (typeof chunk === 'string') {
chunk = encoder.encode(chunk);
}
window.parent.postMessage(chunk, '*');
terminalConnection.postMessage({
$: 'output',
data: chunk,
});
}
});
this.out = this.writableStream.getWriter();
this.in = this.readableStream.getReader();
this.in = new BetterReader({ delegate: this.in });

window.addEventListener('message', evt => {
if ( ! evt.source === window.parent ) return;

if ( evt.data.hasOwnProperty('$') ) {
if ( evt.data.$ === 'ioctl.set' ) {
this.emit('ioctl.set', evt);
}
terminalConnection.on('message', message => {
if (message.$ === 'ioctl.set') {
this.emit('ioctl.set', message);
return;
}
if (message.$ === 'input') {
this.readController.enqueue(message.data);
return;
}

if ( ! (evt?.data instanceof Uint8Array) ) return;
this.readController.enqueue(evt.data);
});
}

Expand Down
96 changes: 0 additions & 96 deletions src/puter-shell/XDocumentPuterShell.js

This file was deleted.

3 changes: 1 addition & 2 deletions src/puter-shell/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import builtins from './coreutils/__exports__.js';
import { XDocumentPTT } from "../pty/XDocumentPTT.js";
import ReadlineLib from "../ansi-shell/readline/readline.js";

// TODO: auto-gen argument parser registry from files
Expand Down Expand Up @@ -125,7 +124,7 @@ export const launchPuterShell = async (ctx) => {
ptt.on('ioctl.set', evt => {
ansiShell.dispatchEvent(new CustomEvent('signal.window-resize', {
detail: {
...evt.data.windowSize
...evt.windowSize
}
}));
});
Expand Down

0 comments on commit 7ee1318

Please sign in to comment.