Skip to content

Commit

Permalink
Use shared promises
Browse files Browse the repository at this point in the history
  • Loading branch information
toasted-nutbread committed Nov 11, 2020
1 parent 219dfb4 commit 816c5fe
Showing 1 changed file with 43 additions and 7 deletions.
50 changes: 43 additions & 7 deletions ext/bg/js/settings/settings-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class SettingsController extends EventDispatcher {
this._pageExitPreventions = new Set();
this._pageExitPreventionEventListeners = new EventListenerCollection();
this._templates = new HtmlTemplateCollection(document);
this._sharedPromises = new Map();
}

get source() {
Expand All @@ -51,13 +52,12 @@ class SettingsController extends EventDispatcher {
await this._onOptionsUpdatedInternal();
}

async getOptions() {
const optionsContext = this.getOptionsContext();
return await api.optionsGet(optionsContext);
getOptions() {
return this._getSharedPromise('getOptions', this._getOptions.bind(this));
}

async getOptionsFull() {
return await api.optionsGetFull();
getOptionsFull() {
return this._getSharedPromise('getOptionsFull', this._getOptionsFull.bind(this));
}

async setAllSettings(value) {
Expand Down Expand Up @@ -98,8 +98,8 @@ class SettingsController extends EventDispatcher {
return await this.modifyProfileSettings([{action: 'set', path, value}]);
}

async getDictionaryInfo() {
return await api.getDictionaryInfo();
getDictionaryInfo() {
return this._getSharedPromise('getDictionaryInfo', this._getDictionaryInfo.bind(this));
}

getOptionsContext() {
Expand Down Expand Up @@ -128,6 +128,7 @@ class SettingsController extends EventDispatcher {

_setProfileIndex(value) {
this._profileIndex = value;
this._clearSharedPromise('getOptions');
this.trigger('optionsContextChanged');
this._onOptionsUpdatedInternal();
}
Expand Down Expand Up @@ -179,4 +180,39 @@ class SettingsController extends EventDispatcher {
this._pageExitPreventionEventListeners.removeAllEventListeners();
}
}

async _getOptions() {
const optionsContext = this.getOptionsContext();
return await api.optionsGet(optionsContext);
}

async _getOptionsFull() {
return await api.optionsGetFull();
}

async _getDictionaryInfo() {
return await api.getDictionaryInfo();
}

async _getSharedPromise(name, createPromise) {
let promise;
const sharedPromises = this._sharedPromises;
let info = sharedPromises.get(name);
if (typeof info === 'undefined') {
promise = createPromise();
info = {promise, multiple: false};
sharedPromises.set(name, info);
Promise.resolve().then(() => { sharedPromises.delete(name); });
} else {
info.multiple = true;
promise = info.promise;
}

const result = await promise;
return info.multiple ? clone(result) : result;
}

_clearSharedPromise(name) {
this._sharedPromises.delete(name);
}
}

0 comments on commit 816c5fe

Please sign in to comment.