From 0d06bcbb56daf26d443a5c05faedc9ed04276f87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stephan=20B=C3=B6sch-Plepelits?= Date: Wed, 3 Aug 2022 20:09:32 +0200 Subject: [PATCH] OpenStreetBrowserLoader.getRepositoryList() --- src/OpenStreetBrowserLoader.js | 41 ++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/src/OpenStreetBrowserLoader.js b/src/OpenStreetBrowserLoader.js index bc02949b..7a39467e 100644 --- a/src/OpenStreetBrowserLoader.js +++ b/src/OpenStreetBrowserLoader.js @@ -104,6 +104,47 @@ class OpenStreetBrowserLoader { this.repositories[id].load((err) => callback(err, this.repositories[id])) } + /** + * @param [object] options Options. + * @param {boolean} [options.force=false] Whether repository should be reloaded or not. + * @param function callback Callback which will be called with (err, list) + */ + getRepositoryList (options, callback) { + if (this.list) { + return callback(null, this.list) + } + + if (this.repositoryListCallbacks) { + return this.repositoryListCallbacks.push(callback) + } + + this.repositoryListCallbacks = [callback] + + var param = [] + param.push('lang=' + encodeURIComponent(ui_lang)) + param.push(config.categoriesRev) + param = param.length ? '?' + param.join('&') : '' + + fetch('repo.php' + param) + .then(res => res.json()) + .then(data => { + this.list = data + + global.setTimeout(() => { + const cbs = this.repositoryListCallbacks + this.repositoryListCallbacks = null + cbs.forEach(cb => cb(null, this.list)) + }, 0) + }) + .catch(err => { + global.setTimeout(() => { + const cbs = this.repositoryListCallbacks + this.repositoryListCallbacks = null + cbs.forEach(cb => cb(err)) + }, 0) + }) + } + /** * @param string id ID of the template * @parapm [object] options Options.