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

Commit

Permalink
refs #7: Now use real Writable as base-class.
Browse files Browse the repository at this point in the history
 * It's actually browserified Writable provided by webpack.
  • Loading branch information
achimnol committed Feb 9, 2017
1 parent 1f5f901 commit 0d9bfbf
Showing 1 changed file with 21 additions and 29 deletions.
50 changes: 21 additions & 29 deletions assets/js/webterm.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,25 @@

const Terminal = require('terminal.js');
const SockJS = require('sockjs-client');
const Writable = require('stream').Writable;

class SocketWritable extends Writable {
constructor(sessId, sock) {
super();
this._sessId = sessId;
this._sock = sock;
}

write(chunk, encoding, cb) {
let msg = JSON.stringify({
"sid": this._sessId,
"type": "stdin",
"chars": btoa(chunk),
});
this._sock.send(msg);
if (cb) cb();
}
}

class Webterm {
constructor(container, sessionId) {
Expand Down Expand Up @@ -47,33 +66,8 @@ class Webterm {
+ (location.port ? (":" + location.port) : "") + "/ws?lang=git";
this._sock = new SockJS(url);

let writable = {};
writable.removeListener = () => { };
writable.on = (type, handler) => {
this.streamOnHandlers[type] = handler;
};
writable.once = (type, handler) => {
this.streamOnceHandlers[type] = handler;
};
writable.emit = (ev) => {
let args = [];
Array.prototype.push.apply(args, arguments);
args.shift();
if (this.streamOnHandlers[ev])
this.streamOnHandlers[ev].apply(writable, args);
};
writable.write = (data) => {
if (this._connected) {
let msg = JSON.stringify({
"sid": this.sessId,
"type": "stdin",
"chars": btoa(data),
});
this._sock.send(msg);
}
};
this.writable = writable;
this.term.dom(this.container).pipe(writable);
this.writable = new SocketWritable(this.sessId, this._sock);
this.term.dom(this.container).pipe(this.writable);

let is_settled = false;

Expand Down Expand Up @@ -196,8 +190,6 @@ class Webterm {
numRows = Math.min(numRows, opts.maxRows);
if (opts.maxCols > 0)
numCols = Math.min(numCols, opts.maxCols);

console.log('resize to ' + numRows + ' rows and ' + numCols + ' cols.');
this.term.state.resize({rows: numRows, columns: numCols});
if (this._connected) {
this._sock.send(JSON.stringify({
Expand Down

0 comments on commit 0d9bfbf

Please sign in to comment.