Browse Source

OpenStreetBrowserLoader: load repository in one http request

master
parent
commit
1cec6a9240
  1. 55
      repo.php
  2. 50
      src/OpenStreetBrowserLoader.js

55
repo.php

@ -0,0 +1,55 @@
<?php include "conf.php"; /* load a local configuration */ ?>
<?php session_start(); ?>
<?php require 'vendor/autoload.php'; /* composer includes */ ?>
<?php include "modulekit/loader.php"; /* loads all php-includes */ ?>
<?php include "node_modules/json-multiline-strings/src/json-multiline-strings.php"; ?>
<?php call_hooks("init"); /* initialize submodules */ ?>
<?php
$path = $config['categoriesDir'];
$repo = 'default';
function newestTimestamp ($path) {
$ts = 0;
$d = opendir($path);
while ($f = readdir($d)) {
$t = filemtime("{$path}/{$f}");
if ($t > $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);

50
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()
}

Loading…
Cancel
Save