Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion document.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export default class Document {
constructor(element, createPage) {
constructor(element, createPage, meterFaultButton) {
const self = this;
this.document = element.ownerDocument;
this.parent = element;
Expand All @@ -20,6 +20,8 @@ export default class Document {
self.answer(event.target.number);
};
this.createPage = createPage || this.createPage;
this.meterFaultButton = meterFaultButton;

Object.seal(this);
}

Expand Down Expand Up @@ -144,6 +146,12 @@ export default class Document {
}
}

meterFault() {
if (this.meterFaultButton) {
this.body.appendChild(this.meterFaultButton);
}
}

ask(_cue) {}

answer(text) {
Expand Down
45 changes: 41 additions & 4 deletions engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ export default class Engine {
this.story = args.story;
this.labels = Object.keys(this.story);
this.handler = args.handler;
this.meter = 0;
this.limit = 10e3; // bottles.kni, for example, runs long
this.options = [];
this.keywords = {};
this.noOption = null;
Expand All @@ -47,6 +49,7 @@ export default class Engine {
this.dialog.engine = this;
this.randomer = args.randomer || Math;
this.waypoint = this.capture();
this.answerOnClearMeterFault = [];
Object.seal(this);
}

Expand All @@ -63,11 +66,15 @@ export default class Engine {
this.resume();
}

/**
* Runs the event loop until it yields.
*/
continue() {
let _continue;
do {
this.meter = 0;
for (;;) {
if (this.debug) {
console.log(`${this.label} ${this.instruction.type} ${describe(this.instruction)}`);
console.log(this.top);
}
if (this.instruction == null) {
// TODO user error for non-console interaction.
Expand All @@ -79,8 +86,31 @@ export default class Engine {
console.error(`Unexpected instruction type: ${this.instruction.type}`, this.instruction);
this.resume();
}
_continue = this[`$${this.instruction.type}`](this.instruction);
} while (_continue);
const proceed = this[`$${this.instruction.type}`](this.instruction);
if (!proceed) {
return;
}
this.meter += 1;
if (this.meter >= this.limit) {
this.display();
this.dialog.meterFault();
return;
}
}
}

clearMeterFault() {
if (this.meter >= this.limit) {
this.render.clear();
this.continue();

// flush answers posted while faulted
const count = this.answerOnClearMeterFault.length;
const answers = this.answerOnClearMeterFault.splice(0, count);
for (const answer of answers) {
this.answer(answer);
}
}
}

goto(label) {
Expand Down Expand Up @@ -170,6 +200,10 @@ export default class Engine {
}

answer(text) {
if (this.meter >= this.limit) {
this.answerOnClearMeterFault.push(text);
return;
}
if (this.handler && this.handler.answer) {
this.handler.answer(text, this);
}
Expand Down Expand Up @@ -256,6 +290,9 @@ export default class Engine {
];
}

/**
* Resumes from a snapshot.
*/
resume(snapshot) {
this.render.clear();
this.flush();
Expand Down
8 changes: 7 additions & 1 deletion entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,13 @@ const handler = {
},
};

const doc = new Document(document.body);
const meterFaultButton = document.createElement('a');
meterFaultButton.innerText = 'Continue…';
meterFaultButton.addEventListener('click', () => {
engine.clearMeterFault();
});

const doc = new Document(document.body, null, meterFaultButton);

const engine = new Engine({
story: story,
Expand Down
10 changes: 10 additions & 0 deletions readline.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ export default class Readline {
Object.seal(this);
}

meterFault() {
this.readline.question(`Enter any command to continue... `, (answer) => {
if (answer === 'quit') {
this.close();
} else {
this.engine.clearMeterFault();
}
});
}

ask(cue) {
this.readline.question(`${cue || ''}> `, this.boundAnswer);
}
Expand Down
9 changes: 7 additions & 2 deletions verify.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ const verify = (kni, trans, handler, kniscript) => {

const writer = new StringWriter();
const render = new Console(writer);
const readline = new FakeReadline(writer, answers);
const readline = new FakeReadline(writer, answers, kniscript);
const engine = new Engine({
story: states,
start: 'start',
Expand All @@ -98,9 +98,10 @@ const verify = (kni, trans, handler, kniscript) => {
export default verify;

class FakeReadline {
constructor(writer, answers) {
constructor(writer, answers, kniscript) {
this.writer = writer;
this.answers = answers;
this.kniscript = kniscript;
this.engine = null;
this.history = [];
Object.seal(this);
Expand Down Expand Up @@ -129,6 +130,10 @@ class FakeReadline {
}

close() {}

meterFault() {
throw new Error(`meter fault in ${this.kniscript}`);
}
}

class StringWriter {
Expand Down