From cd4f16c096746d13502dc7b258dfe16c97344ba1 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Tue, 13 Sep 2016 13:38:37 -0700 Subject: Options cleanup --- ext/bg/js/options-form.js | 27 +++++++++++++++------------ ext/bg/js/options.js | 8 ++++---- ext/bg/js/yomichan.js | 2 +- 3 files changed, 20 insertions(+), 17 deletions(-) (limited to 'ext/bg/js') diff --git a/ext/bg/js/options-form.js b/ext/bg/js/options-form.js index 3dab0a87..a1ecccda 100644 --- a/ext/bg/js/options-form.js +++ b/ext/bg/js/options-form.js @@ -60,8 +60,8 @@ function modelIdToMarkers(id) { }[id]; } -function formToOptions(section, callback) { - loadOptions((optsOld) => { +function formToOptions(section) { + return loadOptions().then(optsOld => { const optsNew = $.extend({}, optsOld); switch (section) { @@ -86,7 +86,10 @@ function formToOptions(section, callback) { break; } - callback(sanitizeOptions(optsNew), sanitizeOptions(optsOld)); + return { + optsNew: sanitizeOptions(optsNew), + optsOld: sanitizeOptions(optsOld) + }; }); } @@ -185,8 +188,8 @@ function onOptionsGeneralChanged(e) { return; } - formToOptions('general', (optsNew, optsOld) => { - saveOptions(optsNew, () => { + formToOptions('general').then(({optsNew, optsOld}) => { + saveOptions(optsNew).then(() => { yomichan().setOptions(optsNew); if (!optsOld.enableAnkiConnect && optsNew.enableAnkiConnect) { updateAnkiStatus(); @@ -210,23 +213,23 @@ function onOptionsAnkiChanged(e) { return; } - formToOptions('anki', (opts) => { - saveOptions(opts, () => yomichan().setOptions(opts)); + formToOptions('anki').then(({optsNew, optsOld}) => { + saveOptions(optsNew).then(() => yomichan().setOptions(optsNew)); }); } function onAnkiModelChanged(e) { if (e.originalEvent) { - formToOptions('anki', (opts) => { - opts[modelIdToFieldOptKey($(this).id)] = {}; - populateAnkiFields($(this), opts); - saveOptions(opts, () => yomichan().setOptions(opts)); + formToOptions('anki').then(({optsNew, optsOld}) => { + optsNew[modelIdToFieldOptKey($(this).id)] = {}; + populateAnkiFields($(this), optsNew); + saveOptions(optsNew).then(() => yomichan().setOptions(optsNew)); }); } } $(document).ready(() => { - loadOptions((opts) => { + loadOptions().then(opts => { $('#scan-length').val(opts.scanLength); $('#activate-on-startup').prop('checked', opts.activateOnStartup); $('#load-enamdict').prop('checked', opts.loadEnamDict); diff --git a/ext/bg/js/options.js b/ext/bg/js/options.js index 831bb817..4c726730 100644 --- a/ext/bg/js/options.js +++ b/ext/bg/js/options.js @@ -45,10 +45,10 @@ function sanitizeOptions(options) { return options; } -function loadOptions(callback) { - chrome.storage.sync.get(null, (items) => callback(sanitizeOptions(items))); +function loadOptions() { + return new Promise((resolve, reject) => chrome.storage.sync.get(null, resolve)); } -function saveOptions(opts, callback) { - chrome.storage.sync.set(sanitizeOptions(opts), callback); +function saveOptions(opts) { + return new Promise((resolve, reject) => chrome.storage.sync.set(sanitizeOptions(opts), resolve)); } diff --git a/ext/bg/js/yomichan.js b/ext/bg/js/yomichan.js index 26b28138..10a42f47 100644 --- a/ext/bg/js/yomichan.js +++ b/ext/bg/js/yomichan.js @@ -33,7 +33,7 @@ class Yomichan { chrome.tabs.onCreated.addListener(tab => this.onTabReady(tab.id)); chrome.tabs.onUpdated.addListener(this.onTabReady.bind(this)); - loadOptions(opts => { + loadOptions().then(opts => { this.setOptions(opts); if (this.options.activateOnStartup) { this.setState('loading'); -- cgit v1.2.3