From e363542c9e2aeb77a970afa0c7fb2e2eeb16fe36 Mon Sep 17 00:00:00 2001 From: jahorton Date: Thu, 9 Apr 2020 13:16:08 +0700 Subject: [PATCH] refactor(web/engine): returns prediction Promise on RuleBehavior --- web/source/dom/preProcessor.ts | 2 +- web/source/kmwembedded.ts | 2 +- web/source/osk/preProcessor.ts | 2 +- web/source/text/inputProcessor.ts | 6 +++--- web/source/text/prediction/languageProcessor.ts | 16 +++++++++------- web/source/text/ruleBehavior.ts | 5 +++++ 6 files changed, 20 insertions(+), 13 deletions(-) diff --git a/web/source/dom/preProcessor.ts b/web/source/dom/preProcessor.ts index 5bae80b47c..ea45a3b7b3 100644 --- a/web/source/dom/preProcessor.ts +++ b/web/source/dom/preProcessor.ts @@ -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) { diff --git a/web/source/kmwembedded.ts b/web/source/kmwembedded.ts index a52e521c1c..97b7822c90 100644 --- a/web/source/kmwembedded.ts +++ b/web/source/kmwembedded.ts @@ -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; diff --git a/web/source/osk/preProcessor.ts b/web/source/osk/preProcessor.ts index 4659e47329..925d16f1bb 100644 --- a/web/source/osk/preProcessor.ts +++ b/web/source/osk/preProcessor.ts @@ -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; } diff --git a/web/source/text/inputProcessor.ts b/web/source/text/inputProcessor.ts index 7c87a565b0..915939704f 100644 --- a/web/source/text/inputProcessor.ts +++ b/web/source/text/inputProcessor.ts @@ -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() { diff --git a/web/source/text/prediction/languageProcessor.ts b/web/source/text/prediction/languageProcessor.ts index e6f0ee34ac..a15aaddb57 100644 --- a/web/source/text/prediction/languageProcessor.ts +++ b/web/source/text/prediction/languageProcessor.ts @@ -111,9 +111,9 @@ namespace com.keyman.text.prediction { return this.lmEngine.wordbreak(context); } - public predict(transcription?: Transcription) { + public predict(transcription?: Transcription): Promise { 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 { 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; }) } diff --git a/web/source/text/ruleBehavior.ts b/web/source/text/ruleBehavior.ts index 61cbc21b41..012cb44c97 100644 --- a/web/source/text/ruleBehavior.ts +++ b/web/source/text/ruleBehavior.ts @@ -38,6 +38,11 @@ namespace com.keyman.text { */ warningLog?: string; + /** + * If predictive text is active, contains a Promise returning predictive Suggestions. + */ + predictionPromise?: Promise; + finalize(processor: KeyboardProcessor) { let outputTarget = this.transcription.keystroke.Ltarg;