From 1cec6a9240e51b4d12fa1b8b26def4e27c70c2ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stephan=20B=C3=B6sch-Plepelits?= Date: Mon, 4 Dec 2017 20:45:11 +0100 Subject: [PATCH] OpenStreetBrowserLoader: load repository in one http request --- repo.php | 55 ++++++++++++++++++++++++++++++++++ src/OpenStreetBrowserLoader.js | 50 +++++++++++++++++++++---------- 2 files changed, 90 insertions(+), 15 deletions(-) create mode 100644 repo.php diff --git a/repo.php b/repo.php new file mode 100644 index 00000000..570b8106 --- /dev/null +++ b/repo.php @@ -0,0 +1,55 @@ + + + + + + + $ts) { + $ts = $t; + } + } + closedir($d); + + return $ts; +} + +$cacheDir = null; +$ts = newestTimestamp($path); +if (isset($config['cache'])) { + $cacheDir = "{$config['cache']}/repo"; + @mkdir($cacheDir); + $cacheTs = filemtime("{$cacheDir}/{$repo}.json"); + if ($cacheTs === $ts) { + Header("Content-Type: application/json; charset=utf-8"); + readfile("{$cacheDir}/{$repo}.json"); + exit(0); + } +} + +$data = array(); + +$d = opendir($path); +while ($f = readdir($d)) { + if (preg_match("/^([0-9a-zA-Z_\-]+)\.json$/", $f, $m) && $f !== 'package.json') { + $d1 = json_decode(file_get_contents("{$path}/{$f}"), true); + $data[$m[1]] = jsonMultilineStringsJoin($d1, array('exclude' => array(array('const')))); + } +} +closedir($d); + +$ret = json_encode($data, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES); + +Header("Content-Type: application/json; charset=utf-8"); +print $ret; + +file_put_contents("{$cacheDir}/{$repo}.json", $ret); +touch("{$cacheDir}/{$repo}.json", $ts); diff --git a/src/OpenStreetBrowserLoader.js b/src/OpenStreetBrowserLoader.js index 0aff3dfa..230cd073 100644 --- a/src/OpenStreetBrowserLoader.js +++ b/src/OpenStreetBrowserLoader.js @@ -4,6 +4,7 @@ var jsonMultilineStrings = require('json-multiline-strings') function OpenStreetBrowserLoader () { this.types = {} this.categories = {} + this.repoCache = {} this.templates = {} this._loadClash = {} // if a category is being loaded multiple times, collect callbacks } @@ -18,12 +19,25 @@ OpenStreetBrowserLoader.prototype.getCategory = function (id, callback) { return } - if (id in this._loadClash) { - this._loadClash[id].push(callback) + var repo = 'default' + + if (repo in this.repoCache) { + this.getCategoryFromData(id, this.repoCache[repo][id], function (err, category) { + if (category) { + category.setMap(this.map) + } + + callback(err, category) + }) return } - this._loadClash[id] = [] + if (repo in this._loadClash) { + this._loadClash[repo].push([ id, callback ]) + return + } + + this._loadClash[repo] = [ [ id, callback ] ] function reqListener (req) { if (req.status !== 200) { @@ -31,26 +45,32 @@ OpenStreetBrowserLoader.prototype.getCategory = function (id, callback) { return callback(req.statusText, null) } - var data = JSON.parse(req.responseText) - data = jsonMultilineStrings.join(data, { exclude: [ [ 'const' ] ] }) + this.repoCache[repo] = JSON.parse(req.responseText) - this.getCategoryFromData(id, data, function (err, category) { - if (category) { - category.setMap(this.map) - } + var todo = this._loadClash[repo] + delete this._loadClash[repo] - callback(err, category) + todo.forEach(function (c) { + var id = c[0] + var callback = c[1] - this._loadClash[id].forEach(function (c) { - c(err, category) - }) - delete this._loadClash[id] + if (id in this.categories) { + callback(null, this.categories[id]) + } else { + this.getCategoryFromData(id, this.repoCache[repo][id], function (err, category) { + if (category) { + category.setMap(this.map) + } + + callback(err, category) + }) + } }.bind(this)) } var req = new XMLHttpRequest() req.addEventListener('load', reqListener.bind(this, req)) - req.open('GET', config.categoriesDir + '/' + id + '.json?' + config.categoriesRev) + req.open('GET', 'repo.php?repo=' + repo + '&' + config.categoriesRev) req.send() }