Put webpack in charge of language file copying and loading

And HTTP in charge of caching.
This commit is contained in:
Jonas Herzig 2020-05-03 17:25:02 +02:00
parent 812990c5bd
commit 0ca2a45358
5 changed files with 3 additions and 211 deletions

View File

@ -1,126 +0,0 @@
export /*abstract */class Cache {
/**
* @param {string} key
* @return {boolean}
* @author svartoyg
*/
/*protected */has(key) {
throw (new Error('not implemented'));
}
/**
* @param {string} key
* @return {any}
* @author svartoyg
*/
/*protected */fetch(key) {
throw (new Error('not implemented'));
}
/**
* @param {string} key
* @param {any} value
* @author svartoyg
*/
/*protected */store(key, value) {
throw (new Error('not implemented'));
}
/**
* @param {string} key
* @param {()=>Promise<any>} retrieve
* @return {Promise<any>}
* @author svartoyg
*/
/*public */async get(key, retrieve) {
if (this.has(key)) {
const value = this.fetch(key);
return Promise.resolve(value);
} else {
const value = await retrieve();
this.store(key, value);
return Promise.resolve(value);
}
}
}
/**
* @author svartoyg
*/
class CacheNone extends Cache {
/**
* @author svartoyg
*/
/*public */constructor() {
super();
}
/**
* @author svartoyg
*/
/*protected */has(key) {
return false;
}
/**
* @author svartoyg
*/
/*protected */fetch(key) {
throw (new Error('not possible'));
}
/**
* @author svartoyg
*/
/*protected */store(key, value) {
}
}
/**
* @author svartoyg
*/
export class CacheLocalstorage extends Cache {
/**
* @param {string} [corner] for separating the cache instance from others
* @author svartoyg
*/
/*public */constructor(corner = null) {
super();
this.corner = corner;
}
/**
* @author svartoyg
*/
/*private */augmentKey(key) {
return ((this.corner === null) ? key : (this.corner + '/' + key));
}
/**
* @author svartoyg
*/
/*protected */has(key) {
return (window.localStorage.getItem(this.augmentKey(key)) !== null);
}
/**
* @author svartoyg
*/
/*protected */fetch(key) {
const valueRaw = window.localStorage.getItem(this.augmentKey(key));
const value = JSON.parse(valueRaw);
return value;
}
/**
* @author svartoyg
*/
/*protected */store(key, value) {
const valueRaw = JSON.stringify(value);
window.localStorage.setItem(this.augmentKey(key), valueRaw);
}
}

View File

@ -1,40 +0,0 @@
/**
* @param {string} path
* @return Promise<string>
* @todo use Util.fetch instead?
* @author svartoyg
*/
export async function read (path) {
return (
new Promise(
(resolve, reject) => {
let request = new XMLHttpRequest();
request.open('GET', '/' + path, true);
request.onreadystatechange = () => {
switch (request.readyState) {
case XMLHttpRequest.DONE: {
switch (request.status) {
case 0: {
reject(new Error('XMLHttpRequest failed'));
break;
}
default: {
resolve(request.responseText);
break;
}
}
break;
}
default: {
console.warn('unhandled readyState "' + request.readyState + '"');
break;
}
}
};
request.send(null);
}
)
);
}

View File

@ -1,17 +1,3 @@
import {CacheLocalstorage} from './cache';
import {read as fileRead} from './file';
// import {Util} from 'util';
/**
* the relative path to the directory containing the JSON localization files
*
* @var {string}
* @author svartoyg
*/
var _directory = 'loc';
/**
* the default language to use
*
@ -30,13 +16,6 @@ var _languageDefault = null;
var _languageFallback = null;
/**
* @var {Cache}
* @author svartoyg
*/
var _cache = null;
/**
* two level map with ISO-639-1 code as first key and translation id as second key
*
@ -56,20 +35,7 @@ async function retrieveData (language) {
if (regexp.exec(language) === null) {
return Promise.reject(new Error('invalid language code "' + language + '"'));
} else {
const path = (_directory + '/' + language + '.json');
let content;
try {
content = await fileRead(path);
} catch (exception) {
return Promise.reject(new Error('could not load localization data for language "' + language + '": ' + error.toString()));
}
let data;
try {
data = JSON.parse(content);
} catch (exception) {
return Promise.reject(new Error('invalid JSON localization data for language "' + language + '": ' + exception.toString()));
}
return Promise.resolve(data);
return (await import(`../loc/${language}.json`)).default
}
}
@ -80,7 +46,6 @@ async function retrieveData (language) {
* @author svartoyg
*/
export async function initialize (languageDefault, languageFallback = 'en') {
_cache = new CacheLocalstorage('loc');
_languageFallback = languageFallback;
_languageDefault = languageDefault;
for (const language of [_languageFallback, _languageDefault]) {
@ -88,7 +53,7 @@ export async function initialize (languageDefault, languageFallback = 'en') {
console.log('--', 'loading localization data for language "' + language + '" ...');
let data;
try {
data = await _cache.get(language, () => retrieveData(language));
data = await retrieveData(language);
} catch (exception) {
console.warn(exception.toString());
}

View File

@ -1,8 +0,0 @@
#!/usr/bin/env sh
npm run build
## loc
mkdir -p dist/loc
cp -ruv loc/* dist/loc/

View File

@ -15,6 +15,7 @@ module.exports = {
devtool: "cheap-source-map",
output: {
path: path.join(__dirname, 'dist'),
chunkFilename: '[chunkhash].js',
filename: '[name].js'
},
module: {