refactor(web/engine): returns prediction Promise on RuleBehavior

This commit is contained in:
jahorton 2020-04-09 13:16:08 +07:00
parent f9a9810ee3
commit e363542c9e
6 changed files with 20 additions and 13 deletions

View file

@ -224,7 +224,7 @@ namespace com.keyman.dom {
return true;
}
var LeventMatched = !core.processKeyEvent(Levent);
var LeventMatched = (core.processKeyEvent(Levent) != null);
if(LeventMatched) {
if(e && e.preventDefault) {

View file

@ -499,7 +499,7 @@ namespace com.keyman.text {
try {
// Now that we've manually constructed a proper keystroke-sourced KeyEvent, pass control
// off to the processor for its actual execution.
return keyman.core.processKeyEvent(Lkc); // Lack of second argument -> `usingOSK == false`
return keyman.core.processKeyEvent(Lkc) != null;
} catch (err) {
console.error(err.message, err);
return false;

View file

@ -83,7 +83,7 @@ namespace com.keyman.osk {
return true;
}
let retVal = keyman.core.processKeyEvent(Lkc);
let retVal = (keyman.core.processKeyEvent(Lkc) != null);
return retVal;
}

View file

@ -54,7 +54,7 @@ namespace com.keyman.text {
*
* @param {Object} e The abstracted KeyEvent to use for keystroke processing
*/
processKeyEvent(keyEvent: KeyEvent): boolean {
processKeyEvent(keyEvent: KeyEvent) {
let keyman = com.keyman.singleton;
let formFactor = keyEvent.device.formFactor;
@ -163,7 +163,7 @@ namespace com.keyman.text {
// Notify the ModelManager of new input - it's predictive text time!
ruleBehavior.transcription.alternates = alternates;
// Yes, even for ruleBehavior.triggersDefaultCommand. Those tend to change the context.
this.languageProcessor.predict(ruleBehavior.transcription);
ruleBehavior.predictionPromise = this.languageProcessor.predict(ruleBehavior.transcription);
// KMEA and KMEI (embedded mode) use direct insertion of the character string
if(keyman.isEmbedded) {
@ -188,7 +188,7 @@ namespace com.keyman.text {
// once THOSE are properly relocated. (They're too DOM-heavy to remain in web-core.)
// Only return true (for the eventual event handler's return value) if we didn't match a rule.
return ruleBehavior == null;
return ruleBehavior;
}
public resetContext() {

View file

@ -111,9 +111,9 @@ namespace com.keyman.text.prediction {
return this.lmEngine.wordbreak(context);
}
public predict(transcription?: Transcription) {
public predict(transcription?: Transcription): Promise<Suggestion[]> {
if(!this.isActive) {
return;
return null;
}
let keyman = com.keyman.singleton;
@ -121,14 +121,14 @@ namespace com.keyman.text.prediction {
// If there's no active model, there can be no predictions.
// We'll also be missing important data needed to even properly REQUEST the predictions.
if(!this.currentModel || !this.configuration) {
return;
return null;
}
// We've already invalidated any suggestions resulting from any previously-existing Promise -
// may as well officially invalidate them via event.
keyman.util.callEvent(ModelManager.EVENT_PREFIX + "invalidatesuggestions", 'new');
this.predict_internal(transcription);
return this.predict_internal(transcription);
}
/**
@ -136,7 +136,7 @@ namespace com.keyman.text.prediction {
* have been raised.
* @param transcription The triggering transcription (if it exists)
*/
private predict_internal(transcription?: Transcription) {
private predict_internal(transcription?: Transcription): Promise<Suggestion[]> {
let keyman = com.keyman.singleton;
if(!transcription) {
@ -144,7 +144,7 @@ namespace com.keyman.text.prediction {
if(t) {
transcription = t.buildTranscriptionFrom(t, null);
} else {
return;
return null;
}
}
@ -155,12 +155,14 @@ namespace com.keyman.text.prediction {
var promise = this.currentPromise = this.lmEngine.predict(transcription.alternates || transcription.transform, context);
let mm = this;
promise.then(function(suggestions: Suggestion[]) {
return promise.then(function(suggestions: Suggestion[]) {
if(promise == mm.currentPromise) {
let result = new ReadySuggestions(suggestions, transform.id);
keyman.util.callEvent(ModelManager.EVENT_PREFIX + "suggestionsready", result);
mm.currentPromise = null;
}
return suggestions;
})
}

View file

@ -38,6 +38,11 @@ namespace com.keyman.text {
*/
warningLog?: string;
/**
* If predictive text is active, contains a Promise returning predictive Suggestions.
*/
predictionPromise?: Promise<Suggestion[]>;
finalize(processor: KeyboardProcessor) {
let outputTarget = this.transcription.keystroke.Ltarg;