Reduce access to API by saving translation history

This commit is contained in:
sienori 2018-11-15 21:28:22 +09:00
parent 3dcc2229a5
commit 4fc51994cd

View file

@ -4,17 +4,45 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
class Translate {
constructor() {}
constructor() {
this.history = [];
}
set sourceWord(word) {
this.sourceWord = word;
}
getHistory(sourceWord, sourceLang, targetLang) {
const history = this.history.find(
history =>
history.sourceWord == sourceWord &&
history.sourceLang == sourceLang &&
history.targetLang == targetLang &&
history.result.statusText == "OK"
);
return history;
}
setHistory(sourceWord, sourceLang, targetLang, formattedResult) {
this.history.push({
sourceWord: sourceWord,
sourceLang: sourceLang,
targetLang: targetLang,
result: formattedResult
});
}
async translate(sourceWord, sourceLang = "auto", targetLang) {
sourceWord = sourceWord.trim();
const history = this.getHistory(sourceWord, sourceLang, targetLang);
if (history) return history.result;
const result = await this.sendRequest(sourceWord, sourceLang, targetLang);
return this.formatResult(result);
const formattedResult = this.formatResult(result);
this.setHistory(sourceWord, sourceLang, targetLang, formattedResult);
return formattedResult;
}
sendRequest(word, sourceLang, targetLang) {