lazy construction of xhr object.

This commit is contained in:
Danny Yoo 2013-03-28 17:29:09 -06:00
parent 99b2c59382
commit 0f35faa159

View File

@ -6,18 +6,25 @@
// language: string } // language: string }
var Repl = function(options, afterSetup) { var Repl = function(options, afterSetup) {
this.M = plt.runtime.currentMachine; this.M = plt.runtime.currentMachine;
this.compilerUrl = options.compilerUrl || 'rpc.html';
this.xhr = new easyXDM.Rpc( this._xhr = undefined;
{ remote: options.compilerUrl || 'rpc.html' },
{ remote: { replCompile: {} } });
if (options.write) { this.write = options.write; } if (options.write) { this.write = options.write; }
this.language = (options.language || this.language = (options.language ||
'whalesong/wescheme/lang/semantics.rkt'); 'whalesong/wescheme/lang/semantics.rkt');
setupMachine(this, afterSetup); setupMachine(this, afterSetup);
}; };
var getXhr = function(that) {
if (! that._xhr) {
that._xhr = new easyXDM.Rpc(
{ remote: that.compilerUrl || 'rpc.html' },
{ remote: { replCompile: {} } });
}
return that._xhr;
};
// write: dom-node -> void // write: dom-node -> void
// Expected to be overridden by others via options.write. // Expected to be overridden by others via options.write.
Repl.prototype.write = function(dom) { Repl.prototype.write = function(dom) {
@ -35,7 +42,7 @@
// once the break succeeds. If no evaluation is running, immediately // once the break succeeds. If no evaluation is running, immediately
// call afterBreak. // call afterBreak.
Repl.prototype.requestBreak = function(afterBreak) { Repl.prototype.requestBreak = function(afterBreak) {
if (this.M.running) { if (this.isRunning()) {
interruptEvaluation(this, afterBreak); interruptEvaluation(this, afterBreak);
} else { } else {
afterBreak(); afterBreak();
@ -44,7 +51,7 @@
var interruptEvaluation = function(that, afterBreak) { var interruptEvaluation = function(that, afterBreak) {
if (! that.M.running) { if (! that.isRunning()) {
throw new Error("internal error: trying to interrupt evaluation but nothing is running."); throw new Error("internal error: trying to interrupt evaluation but nothing is running.");
} }
that.M.scheduleBreak(afterBreak); that.M.scheduleBreak(afterBreak);
@ -55,7 +62,7 @@
// necessary. // necessary.
Repl.prototype.reset = function(afterReset) { Repl.prototype.reset = function(afterReset) {
var that = this; var that = this;
if (this.M.running) { if (this.isRunning()) {
this.M.params.currentDisplayer = this.M.params.currentDisplayer =
function(MACHINE, domNode) {}; function(MACHINE, domNode) {};
this.M.params.currentErrorDisplayer = this.M.params.currentErrorDisplayer =
@ -118,7 +125,7 @@
Repl.prototype.compileProgram = function(programName, code, Repl.prototype.compileProgram = function(programName, code,
onDone, onDoneError) { onDone, onDoneError) {
this.xhr.replCompile(programName, code, onDone, onDoneError); getXhr(this).replCompile(programName, code, onDone, onDoneError);
}; };