Skip to content

Commit

Permalink
viewstream emitters
Browse files Browse the repository at this point in the history
  • Loading branch information
unconed committed Apr 11, 2011
1 parent b8e2230 commit af77159
Show file tree
Hide file tree
Showing 15 changed files with 315 additions and 234 deletions.
12 changes: 6 additions & 6 deletions HTML/client/client.js
Expand Up @@ -32,12 +32,12 @@ var tc = termkit.client = function () {

tc.prototype = {

register: function (session, handler) {
this.sessions[session.session] = handler;
add: function (session) {
this.sessions[session.id] = session;
},

deregister: function (session) {
delete this.sessions[session.session];
delete this.sessions[session.id];
},

dispatch: function (message) {
Expand All @@ -49,9 +49,9 @@ tc.prototype = {

// must be regular viewstream message.
if (message.session) {
var handler = this.sessions[message.session];
if (handler) {
handler(message);
var session = this.sessions[message.session];
if (session) {
session.dispatch(message.method, message.args);
}
}
},
Expand Down
71 changes: 57 additions & 14 deletions HTML/client/shell.js
Expand Up @@ -10,57 +10,100 @@ tc.shell = function (client, environment, success) {

this.client = client;
this.environment = environment;
this.session = null;
this.id = null;

this.counter = 1;

this.frames = {};
this.views = {};

this.query('session.open.shell', { }, function (message) {
that.session = message.args.session;
that.id = message.args.session;

that.client.add(that);

console.log('session', that);
that.query('shell.environment', { }, function (message) {
that.environment = message.args;
success();
});

});

this.client.register(that, function (message) { that.callback(message); });

};

tc.shell.prototype = {

close: function () {
this.process.stdin.end();
},

query: function (method, args, callback) {
this.client.protocol.query(method, args, { session: this.session }, callback);
this.client.protocol.query(method, args, { session: this.id }, callback);
},

notify: function (method, args) {
this.client.protocol.notify(method, args, { session: this.session });
this.client.protocol.notify(method, args, { session: this.id });
},

// Handler for view.* invocations.
callback: function (method, args) {
dispatch: function (method, args) {
var that = this;

console.log('viewstream', method, args);
switch (method) {
case 'view.allocate':
case 'stream.open':
var frame = this.frames[args.rel];

// Allocate views.
if (frame) {
// Add views to viewstream list.
frame.allocate(args.streams.length);
for (i in args.streams) (function (id) {
view = frame.get(+i);
view.callback(function (method, args) {
// Lock callback to this view.
args.stream = id;

console.log('upstream', method, args);
that.notify(method, args);
});

that.views[id] = view;
})(args.streams[i]);
}
break;

case 'stream.close':
// Remove views from active viewstream list.
for (i in args.streams) {
delete this.views[args.streams[i]];
}
break;

default:
var view;
if (args.stream && (view = this.views[args.stream])) {
view.dispatch(method, args);
}
}
},

run: function (tokens, exit) {
run: function (tokens, frame, exit) {
var that = this,
ref = this.counter++,
rel = this.counter++,
callback = function (message) {
if (message.environment) {
that.environment = message.environment;
}

delete that.frames[rel];

exit(message.success, message.args, message);
};

this.frames[rel] = frame;

this.query('shell.run', {
tokens: tokens,
ref: ref,
rel: rel,
}, callback);
},
};
Expand Down
29 changes: 13 additions & 16 deletions HTML/commandview/command.js
Expand Up @@ -92,22 +92,19 @@ cv.command.prototype = {
var command = tokens.map(function (t) { return t.toCommand(); });

// Execute in current context.
this.context.shell.run(command, function (success, object, meta) {
// Set appropriate return state.
that.state = {
'1': 'ok',
'0': 'error',
'-1': 'warning',
}[+success] || 'ok';

// Open new command.
async(function () {
that.commandView.newCommand();
});
},
// Send all output to outputFrame.
this.outputFrame.hook()
);
this.context.shell.run(command, this.outputFrame, function (success, object, meta) {
// Set appropriate return state.
that.state = {
'1': 'ok',
'0': 'error',
'-1': 'warning',
}[+success] || 'ok';

// Open new command.
async(function () {
that.commandView.newCommand();
});
});
},

// Use triggers to respond to a creation or change event.
Expand Down
8 changes: 8 additions & 0 deletions HTML/outputview/outputfactory.js
Expand Up @@ -11,6 +11,12 @@ ov.outputFactory = function () {

ov.outputFactory.prototype = {

// Construct a tree of view objects.
tree: function (objects) {
var that = this;
return oneOrMany(objects).map(function (node) { return that.construct(node); });
},

construct: function (properties) {
var type = widgets[properties.type] || ov.outputNode,
node = new type(properties),
Expand Down Expand Up @@ -58,6 +64,8 @@ widgets.raw.prototype = $.extend(new ov.outputNode(), {
updateElement: function () {
this.$contents.text(this.properties.contents);
this.$element.data('controller', this);

this.notify('view.callback', { raw: 'foo' });
},

});
Expand Down
29 changes: 14 additions & 15 deletions HTML/outputview/outputframe.js
Expand Up @@ -20,27 +20,26 @@ of.prototype = {
return $outputFrame;
},

// Hook into the given set of handlers.
hook: function (handlers) {
var that = this;
handlers = handlers || {};
handlers['view'] = function (m,a) { that.viewHandler(m, a); };
return handlers;
// Get the n'th view in the frame.
get: function (i, callback) {
this.allocate(i + 1);
return this.views[i];
},

viewHandler: function (method, args) {
var subview = args.subview || 0;
this.allocate(subview + 1);
this.views[subview].viewHandler(method, args);

// Remove all views.
remove: function () {
this.$element.remove();
},

// Update the element's markup in response to internal changes.
allocate: function (subviews) {
if (this.views.length < subviews) {
subviews -= this.views.length;
while (subviews--) {
allocate: function (views) {
console.log('allocate from', this.views.length, ' to ', views);
if (this.views.length < views) {
views -= this.views.length;
while (views--) {
this.views.push(new termkit.outputView());
this.$element.append(this.views[this.views.length - 1].$element);
console.log('allocate -- ', this.views.length, views);
};
}
},
Expand Down

0 comments on commit af77159

Please sign in to comment.