First commit
This commit is contained in:
75
framework/client/fileLoader.js
Normal file
75
framework/client/fileLoader.js
Normal file
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
|
||||
Copyright (c) 2020, 2023, The Unified Company.
|
||||
|
||||
This code is part of Unify.
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE,
|
||||
as published by the Free Software Foundation.
|
||||
See the GNU AFFERO GENERAL PUBLIC LICENSE, for more details.
|
||||
|
||||
https://unifyjs.org
|
||||
|
||||
*/
|
||||
|
||||
export default class fileLoader{
|
||||
|
||||
files = new Array();
|
||||
|
||||
filesExists = new Array();
|
||||
|
||||
async get( url ) {
|
||||
|
||||
return await this.getFile( url );
|
||||
|
||||
}
|
||||
|
||||
exists( url )
|
||||
{
|
||||
|
||||
if( this.filesExists[ url ] ) {
|
||||
|
||||
return this.filesExists[ url ];
|
||||
|
||||
}
|
||||
|
||||
var http = new XMLHttpRequest();
|
||||
|
||||
http.open('HEAD', url, false);
|
||||
|
||||
http.send();
|
||||
|
||||
this.filesExists[ url ] = http.status != 404;
|
||||
|
||||
return http.status != 404;
|
||||
|
||||
}
|
||||
|
||||
async getFile( url ) {
|
||||
|
||||
if( this.files[ url ] ) {
|
||||
|
||||
return this.files[ url ];
|
||||
|
||||
} else {
|
||||
|
||||
var response = await fetch( url );
|
||||
|
||||
var json = await response.json();
|
||||
|
||||
this.addFile( url, json );
|
||||
|
||||
return json;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
addFile( url, json ) {
|
||||
|
||||
this.files[ url ] = json;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user