Skip to content

Commit c032cf9

Browse files
committed
LanguageModel: Pass value as the first argument to event handlers
Previously this sometimes only passed the LanguageModel instance. Instead, always pass the best possible "value" as the first, and the instance as an (optional) second.
1 parent a7ad135 commit c032cf9

File tree

3 files changed

+7
-7
lines changed

3 files changed

+7
-7
lines changed

examples/LanguageModelEvent/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<script src="../../dist/ml5.js"></script>
99
</head>
1010
<body>
11-
<input placeholder="Prompt" id="prompt"></input> <input type="button" value="Generate" id="generate"></input>
11+
<input placeholder="Prompt" id="prompt"></input> <input type="button" value="Generate" id="generate"></input><br><br>
1212
<script src="sketch.js"></script>
1313
</body>
1414
</html>

examples/LanguageModelEvent/sketch.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ function generateText() {
3535
}
3636

3737

38-
function onToken(lm) {
39-
console.log('token', lm.tokens[lm.tokens.length-1]);
38+
function onToken(token, lm) {
39+
console.log('token', token);
4040
}
4141

4242
function onWord(word, lm) {
@@ -57,6 +57,6 @@ function onWord(word, lm) {
5757
pop();
5858
}
5959

60-
function onFinsh(lm) {
61-
console.log('Generation finished', lm.out);
60+
function onFinsh(out, lm) {
61+
console.log('Generation finished', out);
6262
}

src/LanguageModel/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ class LanguageModel extends EventEmitter {
109109
}
110110

111111
// on-token event
112-
this.emit('token', this);
112+
this.emit('token', this.tokens[this.tokens.length-1], this);
113113

114114
// redo word tokenization
115115
const wordDelimiters = ' .,:;"“?!\n';
@@ -131,7 +131,7 @@ class LanguageModel extends EventEmitter {
131131
if (this.promiseResolve) {
132132
this.promiseResolve(this.out);
133133
}
134-
this.emit('finsh', this);
134+
this.emit('finsh', this.out, this);
135135
if (this.callback) {
136136
this.callback(this.out, this);
137137
}

0 commit comments

Comments
 (0)