summaryrefslogtreecommitdiff
path: root/ext/bg/js/ankiconnect.js
diff options
context:
space:
mode:
authorAlex Yatskov <alex@foosoft.net>2016-10-15 23:23:40 -0700
committerAlex Yatskov <alex@foosoft.net>2016-10-15 23:23:40 -0700
commit0b4bdec7f2f7add052009d4aa9c113229a7304cc (patch)
tree39655adb1e382e03350975ba23af48151696718d /ext/bg/js/ankiconnect.js
parent38c1a9a5936ff54f06175ef8d36f5422471c430a (diff)
WIP
Diffstat (limited to 'ext/bg/js/ankiconnect.js')
-rw-r--r--ext/bg/js/ankiconnect.js46
1 files changed, 14 insertions, 32 deletions
diff --git a/ext/bg/js/ankiconnect.js b/ext/bg/js/ankiconnect.js
index 66cea094..de199b2c 100644
--- a/ext/bg/js/ankiconnect.js
+++ b/ext/bg/js/ankiconnect.js
@@ -19,58 +19,40 @@
class AnkiConnect {
constructor() {
this.asyncPools = {};
- this.pluginVersion = null;
- this.apiVersion = 1;
+ this.localVersion = 1;
+ this.remoteVersion = null;
}
addNote(note) {
- return this.ankiInvokeSafe('addNote', {note}, null);
+ return this.checkVersion().then(() => this.ankiInvoke('addNote', {note}, null));
}
canAddNotes(notes) {
- return this.ankiInvokeSafe('canAddNotes', {notes}, 'notes');
+ return this.checkVersion().then(() => this.ankiInvoke('canAddNotes', {notes}, 'notes'));
}
getDeckNames() {
- return this.ankiInvokeSafe('deckNames', {}, null);
+ return this.checkVersion().then(() => this.ankiInvoke('deckNames', {}, null));
}
getModelNames() {
- return this.ankiInvokeSafe('modelNames', {}, null);
+ return this.checkVersion().then(() => this.ankiInvoke('modelNames', {}, null));
}
getModelFieldNames(modelName) {
- return this.ankiInvokeSafe('modelFieldNames', {modelName}, null);
+ return this.checkVersion().then(() => this.ankiInvoke('modelFieldNames', {modelName}, null));
}
- getStatus() {
- return this.getVersion().then(version => {
- if (version === null) {
- return 'disconnected';
- } else if (version === this.apiVersion) {
- return 'ready';
- } else {
- return 'mismatch';
- }
- });
- }
-
- getVersion() {
- return this.ankiInvoke('version', {}, null);
- }
-
- ankiInvokeSafe(action, params, pool) {
- if (this.pluginVersion === this.apiVersion) {
- return this.ankiInvoke(action, params, pool);
+ checkVersion() {
+ if (this.localVersion === this.remoteVersion) {
+ return Promise.resolve(true);
}
- return this.getVersion().then(version => {
- if (version === this.apiVersion) {
- this.pluginVersion = version;
- return this.ankiInvoke(action, params, pool);
+ return this.ankiInvoke('version', {}, null).then(version => {
+ this.remoteVersion = version;
+ if (this.remoteVersion !== this.localVersion) {
+ return Promise.reject('browser extension and anki plugin version mismatch');
}
-
- return null;
});
}