2018-06-03 01:02:20 +08:00
|
|
|
import server from "./server.js";
|
|
|
|
|
2019-05-13 03:45:30 +08:00
|
|
|
let optionsReady;
|
|
|
|
|
|
|
|
const loadListeners = [];
|
|
|
|
|
|
|
|
function loadOptions() {
|
|
|
|
optionsReady = new Promise((resolve, reject) => {
|
|
|
|
server.get('options').then(options => {
|
|
|
|
resolve(options);
|
|
|
|
|
|
|
|
for (const listener of loadListeners) {
|
|
|
|
listener(options);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
loadOptions(); // initial load
|
|
|
|
|
|
|
|
function addLoadListener(listener) {
|
|
|
|
loadListeners.push(listener);
|
|
|
|
|
|
|
|
// useful when listener has been added after the promise resolved, but can cause double emit if not yet
|
|
|
|
// that should not be an issue though
|
|
|
|
optionsReady.then(listener);
|
|
|
|
}
|
2018-06-03 01:02:20 +08:00
|
|
|
|
|
|
|
export default {
|
2019-05-13 03:45:30 +08:00
|
|
|
// use addLoadListener() which will be called also on refreshes
|
|
|
|
optionsReady,
|
|
|
|
addLoadListener,
|
|
|
|
loadOptions
|
2018-06-03 01:02:20 +08:00
|
|
|
}
|