move anki to async

This commit is contained in:
Alex Yatskov 2017-07-10 16:24:31 -07:00
parent efb5ed2af8
commit b0cdf59bd8
4 changed files with 63 additions and 75 deletions

View File

@ -20,69 +20,50 @@
class AnkiConnect { class AnkiConnect {
constructor(server) { constructor(server) {
this.server = server; this.server = server;
this.asyncPools = {};
this.localVersion = 2; this.localVersion = 2;
this.remoteVersion = null; this.remoteVersion = 0;
} }
addNote(note) { async addNote(note) {
return this.checkVersion().then(() => this.ankiInvoke('addNote', {note})); await this.checkVersion();
return await this.ankiInvoke('addNote', {note});
} }
canAddNotes(notes) { async canAddNotes(notes) {
return this.checkVersion().then(() => this.ankiInvoke('canAddNotes', {notes}, 'notes')); await this.checkVersion();
return await this.ankiInvoke('canAddNotes', {notes});
} }
getDeckNames() { async getDeckNames() {
return this.checkVersion().then(() => this.ankiInvoke('deckNames', {})); await this.checkVersion();
return await this.ankiInvoke('deckNames');
} }
getModelNames() { async getModelNames() {
return this.checkVersion().then(() => this.ankiInvoke('modelNames', {})); await this.checkVersion();
return await this.ankiInvoke('modelNames');
} }
getModelFieldNames(modelName) { async getModelFieldNames(modelName) {
return this.checkVersion().then(() => this.ankiInvoke('modelFieldNames', {modelName})); await this.checkVersion();
return await this.ankiInvoke('modelFieldNames', {modelName});
} }
guiBrowse(query) { async guiBrowse(query) {
return this.checkVersion().then(() => this.ankiInvoke('guiBrowse', {query})); await this.checkVersion();
return await this.ankiInvoke('guiBrowse', {query});
} }
checkVersion() { async checkVersion() {
if (this.localVersion === this.remoteVersion) { if (this.remoteVersion < this.localVersion) {
return Promise.resolve(true); this.remoteVersion = await this.ankiInvoke('version');
}
return this.ankiInvoke('version', {}, null).then(version => {
this.remoteVersion = version;
if (this.remoteVersion < this.localVersion) { if (this.remoteVersion < this.localVersion) {
return Promise.reject('extension and plugin versions incompatible'); return Promise.reject('extension and plugin versions incompatible');
} }
}); }
} }
ankiInvoke(action, params, pool) { ankiInvoke(action, params) {
return new Promise((resolve, reject) => { return jsonRequest(this.server, 'POST', {action, params, version: this.localVersion});
if (pool && this.asyncPools.hasOwnProperty(pool)) {
this.asyncPools[pool].abort();
}
const xhr = new XMLHttpRequest();
xhr.addEventListener('loadend', () => {
if (pool) {
delete this.asyncPools[pool];
}
if (xhr.responseText) {
resolve(JSON.parse(xhr.responseText));
} else {
reject('unable to connect to plugin');
}
});
xhr.open('POST', this.server);
xhr.send(JSON.stringify({action, params}));
});
} }
} }

View File

@ -18,27 +18,27 @@
class AnkiNull { class AnkiNull {
addNote(note) { async addNote(note) {
return Promise.reject('unsupported action'); return null;
} }
canAddNotes(notes) { async canAddNotes(notes) {
return Promise.resolve([]); return [];
} }
getDeckNames() { async getDeckNames() {
return Promise.resolve([]); return [];
} }
getModelNames() { async getModelNames() {
return Promise.resolve([]); return [];
} }
getModelFieldNames(modelName) { async getModelFieldNames(modelName) {
return Promise.resolve([]); return [];
} }
guiBrowse(query) { async guiBrowse(query) {
return Promise.resolve([]); return [];
} }
} }

View File

@ -31,7 +31,7 @@ class Translator {
if (!this.deinflector) { if (!this.deinflector) {
const url = chrome.extension.getURL('/bg/lang/deinflect.json'); const url = chrome.extension.getURL('/bg/lang/deinflect.json');
const reasons = await Translator.loadRules(url); const reasons = await jsonRequest(url, 'GET');
this.deinflector = new Deinflector(reasons); this.deinflector = new Deinflector(reasons);
} }
} }
@ -124,22 +124,4 @@ class Translator {
return definitions; return definitions;
} }
static loadRules(url) {
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.overrideMimeType('application/json');
xhr.addEventListener('load', () => resolve(xhr.responseText));
xhr.addEventListener('error', () => reject('failed to execute network request'));
xhr.open('GET', url);
xhr.send();
}).then(responseText => {
try {
return JSON.parse(responseText);
}
catch (e) {
return Promise.reject('invalid JSON response');
}
});
}
} }

View File

@ -428,6 +428,31 @@ function dictFieldFormat(field, definition, mode, options) {
return field; return field;
} }
/*
* JSON
*/
function jsonRequest(url, action, params) {
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.overrideMimeType('application/json');
xhr.addEventListener('load', () => resolve(xhr.responseText));
xhr.addEventListener('error', () => reject('failed to execute network request'));
xhr.open(action, url);
if (params) {
xhr.send(JSON.stringify(params));
} else {
xhr.send();
}
}).then(responseText => {
try {
return JSON.parse(responseText);
}
catch (e) {
return Promise.reject('invalid JSON response');
}
});
}
/* /*
* Helpers * Helpers