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 * the default language to use
* *
@ -30,13 +16,6 @@ var _languageDefault = null;
var _languageFallback = 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 * 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) { if (regexp.exec(language) === null) {
return Promise.reject(new Error('invalid language code "' + language + '"')); return Promise.reject(new Error('invalid language code "' + language + '"'));
} else { } else {
const path = (_directory + '/' + language + '.json'); return (await import(`../loc/${language}.json`)).default
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);
} }
} }
@ -80,7 +46,6 @@ async function retrieveData (language) {
* @author svartoyg * @author svartoyg
*/ */
export async function initialize (languageDefault, languageFallback = 'en') { export async function initialize (languageDefault, languageFallback = 'en') {
_cache = new CacheLocalstorage('loc');
_languageFallback = languageFallback; _languageFallback = languageFallback;
_languageDefault = languageDefault; _languageDefault = languageDefault;
for (const language of [_languageFallback, _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 + '" ...'); console.log('--', 'loading localization data for language "' + language + '" ...');
let data; let data;
try { try {
data = await _cache.get(language, () => retrieveData(language)); data = await retrieveData(language);
} catch (exception) { } catch (exception) {
console.warn(exception.toString()); 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", devtool: "cheap-source-map",
output: { output: {
path: path.join(__dirname, 'dist'), path: path.join(__dirname, 'dist'),
chunkFilename: '[chunkhash].js',
filename: '[name].js' filename: '[name].js'
}, },
module: { module: {