ohzi-core 12.0.0 → 12.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/index.mjs +1 -1
- package/build/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/loaders/AsyncAbstractLoader.js +1 -1
- package/src/loaders/assets_loader/AssetLoader.js +2 -4
- package/src/resource_loader/AbstractLoader.js +28 -9
- package/src/loaders/assets_loader/AbstractLoader.js +0 -129
package/package.json
CHANGED
|
@@ -37,7 +37,7 @@ class AsyncAbstractLoader
|
|
|
37
37
|
if (asset_loader.has_error)
|
|
38
38
|
{
|
|
39
39
|
// Restore original url so the error message make more sense
|
|
40
|
-
|
|
40
|
+
asset_loader.url = asset_loader.original_url;
|
|
41
41
|
|
|
42
42
|
asset_loader.print_error();
|
|
43
43
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { AbstractLoader } from '
|
|
1
|
+
import { AbstractLoader } from '../../resource_loader/AbstractLoader';
|
|
2
2
|
|
|
3
|
-
class AssetLoader extends AbstractLoader
|
|
3
|
+
export class AssetLoader extends AbstractLoader
|
|
4
4
|
{
|
|
5
5
|
constructor(resource_id, url, size)
|
|
6
6
|
{
|
|
@@ -20,5 +20,3 @@ class AssetLoader extends AbstractLoader
|
|
|
20
20
|
});
|
|
21
21
|
}
|
|
22
22
|
}
|
|
23
|
-
|
|
24
|
-
export { AssetLoader };
|
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
import { Browser } from '../Browser';
|
|
2
|
-
import { Validation } from '../utilities/Validation';
|
|
3
1
|
|
|
4
|
-
class AbstractLoader
|
|
2
|
+
export class AbstractLoader
|
|
5
3
|
{
|
|
6
4
|
constructor(resource_id, url, size = 1)
|
|
7
5
|
{
|
|
@@ -17,8 +15,8 @@ class AbstractLoader
|
|
|
17
15
|
|
|
18
16
|
__update_downloaded_bytes(loaded, total)
|
|
19
17
|
{
|
|
20
|
-
loaded =
|
|
21
|
-
total =
|
|
18
|
+
loaded = this.is_number(loaded) ? loaded : 1;
|
|
19
|
+
total = this.is_number(total) ? total : 1;
|
|
22
20
|
|
|
23
21
|
this.loaded_bytes = loaded;
|
|
24
22
|
|
|
@@ -53,7 +51,7 @@ class AbstractLoader
|
|
|
53
51
|
{
|
|
54
52
|
if (resource_container.resources_by_url[this.url] === undefined)
|
|
55
53
|
{
|
|
56
|
-
const cache =
|
|
54
|
+
const cache = 'default';
|
|
57
55
|
|
|
58
56
|
fetch(this.url, {
|
|
59
57
|
method: 'GET', // *GET, POST, PUT, DELETE, etc.
|
|
@@ -100,7 +98,15 @@ class AbstractLoader
|
|
|
100
98
|
// console.log(`Received ${receivedLength} of ${contentLength}`);
|
|
101
99
|
}
|
|
102
100
|
|
|
103
|
-
|
|
101
|
+
if (response.ok)
|
|
102
|
+
{
|
|
103
|
+
this.on_preloaded_finished(resource_container, response_clone);
|
|
104
|
+
}
|
|
105
|
+
else
|
|
106
|
+
{
|
|
107
|
+
this.__set_error(`${response.status}: ${response.statusText}`);
|
|
108
|
+
this.__loading_ended();
|
|
109
|
+
}
|
|
104
110
|
}
|
|
105
111
|
|
|
106
112
|
on_preloaded_finished(resource_container, response)
|
|
@@ -111,6 +117,19 @@ class AbstractLoader
|
|
|
111
117
|
{
|
|
112
118
|
console.error(data);
|
|
113
119
|
}
|
|
114
|
-
}
|
|
115
120
|
|
|
116
|
-
|
|
121
|
+
is_int(n)
|
|
122
|
+
{
|
|
123
|
+
return Number(n) === n && n % 1 === 0;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
is_float(n)
|
|
127
|
+
{
|
|
128
|
+
return Number(n) === n && n % 1 !== 0;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
is_number(n)
|
|
132
|
+
{
|
|
133
|
+
return this.is_int(n) || this.is_float(n);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
@@ -1,129 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
class AbstractLoader
|
|
3
|
-
{
|
|
4
|
-
constructor(resource_id, url, size = 1)
|
|
5
|
-
{
|
|
6
|
-
this.resource_id = resource_id;
|
|
7
|
-
this.url = url;
|
|
8
|
-
this.loaded_bytes = 0;
|
|
9
|
-
this.total_bytes = size;
|
|
10
|
-
|
|
11
|
-
this.has_finished = false;
|
|
12
|
-
this.has_error = false;
|
|
13
|
-
this.error_message = 'none';
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
is_int(n)
|
|
17
|
-
{
|
|
18
|
-
return Number(n) === n && n % 1 === 0;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
is_float(n)
|
|
22
|
-
{
|
|
23
|
-
return Number(n) === n && n % 1 !== 0;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
is_number(n)
|
|
27
|
-
{
|
|
28
|
-
return this.is_int(n) || this.is_float(n);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
__update_downloaded_bytes(loaded, total)
|
|
32
|
-
{
|
|
33
|
-
loaded = this.is_number(loaded) ? loaded : 1;
|
|
34
|
-
total = this.is_number(total) ? total : 1;
|
|
35
|
-
|
|
36
|
-
this.loaded_bytes = loaded;
|
|
37
|
-
|
|
38
|
-
if (total > 0)
|
|
39
|
-
{
|
|
40
|
-
this.total_bytes = total;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
if (this.loaded_bytes > this.total_bytes)
|
|
44
|
-
{
|
|
45
|
-
console.warn('Total bytes is smaller than loaded ones. Please set total bytes by hand to:', this.url);
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
__loading_ended()
|
|
50
|
-
{
|
|
51
|
-
this.has_finished = true;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
__set_error(message)
|
|
55
|
-
{
|
|
56
|
-
this.has_error = true;
|
|
57
|
-
this.error_message = message;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
print_error()
|
|
61
|
-
{
|
|
62
|
-
console.error('Error while loading ' + this.resource_id + '\n\t path: ' + this.url + '\n\t\t' + this.error_message);
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
load(resource_container)
|
|
66
|
-
{
|
|
67
|
-
if (resource_container.resources_by_url[this.url] === undefined)
|
|
68
|
-
{
|
|
69
|
-
const cache = 'no-cache';
|
|
70
|
-
|
|
71
|
-
fetch(this.url, {
|
|
72
|
-
method: 'GET', // *GET, POST, PUT, DELETE, etc.
|
|
73
|
-
mode: 'cors', // no-cors, *cors, same-origin
|
|
74
|
-
cache: cache, // *default, no-cache, reload, force-cache, only-if-cached
|
|
75
|
-
credentials: 'same-origin', // include, *same-origin, omit
|
|
76
|
-
redirect: 'follow' // manual, *follow, error
|
|
77
|
-
})
|
|
78
|
-
.then(this.on_progress.bind(this, resource_container))
|
|
79
|
-
.catch(this.__on_error.bind(this));
|
|
80
|
-
}
|
|
81
|
-
else
|
|
82
|
-
{
|
|
83
|
-
resource_container.set_resource(this.resource_id, this.url, resource_container.resources_by_url[this.url]);
|
|
84
|
-
|
|
85
|
-
this.__update_downloaded_bytes(1, 1);
|
|
86
|
-
this.__loading_ended();
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
async on_progress(resource_container, response)
|
|
91
|
-
{
|
|
92
|
-
const response_clone = response.clone();
|
|
93
|
-
const reader = response.body.getReader();
|
|
94
|
-
|
|
95
|
-
// Step 2: get total length
|
|
96
|
-
const contentLength = +response.headers.get('Content-Length');
|
|
97
|
-
|
|
98
|
-
// Step 3: read the data
|
|
99
|
-
let receivedLength = 0; // received that many bytes at the moment
|
|
100
|
-
|
|
101
|
-
while (true)
|
|
102
|
-
{
|
|
103
|
-
const { done, value } = await reader.read();
|
|
104
|
-
|
|
105
|
-
if (done)
|
|
106
|
-
{
|
|
107
|
-
break;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
receivedLength += value.length;
|
|
111
|
-
|
|
112
|
-
this.__update_downloaded_bytes(receivedLength, contentLength);
|
|
113
|
-
// console.log(`Received ${receivedLength} of ${contentLength}`);
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
this.on_preloaded_finished(resource_container, response_clone);
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
on_preloaded_finished(resource_container, response)
|
|
120
|
-
{
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
__on_error(data)
|
|
124
|
-
{
|
|
125
|
-
console.error(data);
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
export { AbstractLoader };
|