Skip to content

Commit db7d2b5

Browse files
committed
Add base loader class
1 parent 2d9ce48 commit db7d2b5

File tree

7 files changed

+146
-499
lines changed

7 files changed

+146
-499
lines changed

src/io/dicomDataLoader.js

Lines changed: 7 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,12 @@ import {getUrlFromUri} from '../utils/uri.js';
33
import {fileContentTypes} from './filesLoader.js';
44
import {urlContentTypes} from './urlsLoader.js';
55
import {DicomBufferToData} from '../image/dicomBufferToData.js';
6+
import {LoaderBase} from './loaderBase.js';
67

78
/**
89
* DICOM data loader.
910
*/
10-
export class DicomDataLoader {
11-
12-
/**
13-
* Loader options.
14-
*
15-
* @type {object}
16-
*/
17-
#options = {};
18-
19-
/**
20-
* Loading flag.
21-
*
22-
* @type {boolean}
23-
*/
24-
#isLoading = false;
25-
26-
/**
27-
* Set the loader options.
28-
*
29-
* @param {object} opt The input options.
30-
*/
31-
setOptions(opt) {
32-
this.#options = opt;
33-
}
34-
35-
/**
36-
* Is the load ongoing?
37-
*
38-
* @returns {boolean} True if loading.
39-
*/
40-
isLoading() {
41-
return this.#isLoading;
42-
}
11+
export class DicomDataLoader extends LoaderBase {
4312

4413
/**
4514
* DICOM buffer to Data (asynchronous).
@@ -56,17 +25,17 @@ export class DicomDataLoader {
5625
*/
5726
load(buffer, origin, index) {
5827
// setup db2d ony once
59-
if (!this.#isLoading) {
28+
if (!this.isLoading()) {
6029
// pass options
61-
this.#db2d.setOptions(this.#options);
30+
this.#db2d.setOptions(this.getOptions());
6231
// connect handlers
6332
this.#db2d.onloadstart = this.onloadstart;
6433
this.#db2d.onprogress = this.onprogress;
6534
this.#db2d.onloaditem = this.onloaditem;
6635
this.#db2d.onload = this.onload;
6736
this.#db2d.onloadend = (event) => {
6837
// reset loading flag
69-
this.#isLoading = false;
38+
this.setLoading(false);
7039
// call listeners
7140
this.onloadend(event);
7241
};
@@ -78,7 +47,7 @@ export class DicomDataLoader {
7847
}
7948

8049
// set loading flag
81-
this.#isLoading = true;
50+
this.setLoading(true);
8251
// convert
8352
this.#db2d.convert(buffer, origin, index);
8453
}
@@ -88,7 +57,7 @@ export class DicomDataLoader {
8857
*/
8958
abort() {
9059
// reset loading flag
91-
this.#isLoading = false;
60+
this.setLoading(false);
9261
// abort conversion, will trigger db2d.onabort
9362
this.#db2d.abort();
9463
}
@@ -196,62 +165,4 @@ export class DicomDataLoader {
196165
return urlContentTypes.ArrayBuffer;
197166
}
198167

199-
/**
200-
* Handle a load start event.
201-
* Default does nothing.
202-
*
203-
* @param {object} _event The load start event.
204-
*/
205-
onloadstart(_event) {}
206-
207-
/**
208-
* Handle a progress event.
209-
* Default does nothing.
210-
*
211-
* @param {object} _event The load progress event.
212-
*/
213-
onprogress(_event) {}
214-
215-
/**
216-
* Handle a load item event.
217-
* Default does nothing.
218-
*
219-
* @param {object} _event The load item event fired
220-
* when a file item has been loaded successfully.
221-
*/
222-
onloaditem(_event) {}
223-
224-
/**
225-
* Handle a load event.
226-
* Default does nothing.
227-
*
228-
* @param {object} _event The load event fired
229-
* when a file has been loaded successfully.
230-
*/
231-
onload(_event) {}
232-
233-
/**
234-
* Handle an load end event.
235-
* Default does nothing.
236-
*
237-
* @param {object} _event The load end event fired
238-
* when a file load has completed, successfully or not.
239-
*/
240-
onloadend(_event) {}
241-
242-
/**
243-
* Handle an error event.
244-
* Default does nothing.
245-
*
246-
* @param {object} _event The error event.
247-
*/
248-
onerror(_event) {}
249-
/**
250-
* Handle an abort event.
251-
* Default does nothing.
252-
*
253-
* @param {object} _event The abort event.
254-
*/
255-
onabort(_event) {}
256-
257168
} // class DicomDataLoader

src/io/jsonTextLoader.js

Lines changed: 5 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,12 @@ import {startsWith, getFileExtension} from '../utils/string.js';
22
import {getUrlFromUri} from '../utils/uri.js';
33
import {fileContentTypes} from './filesLoader.js';
44
import {urlContentTypes} from './urlsLoader.js';
5+
import {LoaderBase} from './loaderBase.js';
56

67
/**
78
* JSON text loader.
89
*/
9-
export class JSONTextLoader {
10-
11-
/**
12-
* Loading flag.
13-
*
14-
* @type {boolean}
15-
*/
16-
#isLoading = false;
17-
18-
/**
19-
* Set the loader options.
20-
*
21-
* @param {object} _opt The input options.
22-
*/
23-
setOptions(_opt) {
24-
// does nothing
25-
}
26-
27-
/**
28-
* Is the load ongoing?
29-
*
30-
* @returns {boolean} True if loading.
31-
*/
32-
isLoading() {
33-
return this.#isLoading;
34-
}
10+
export class JSONTextLoader extends LoaderBase {
3511

3612
/**
3713
* Load data.
@@ -42,7 +18,7 @@ export class JSONTextLoader {
4218
*/
4319
load(text, origin, index) {
4420
// set loading flag
45-
this.#isLoading = true;
21+
this.setLoading(true);
4622
this.onloadstart({
4723
source: origin
4824
});
@@ -69,7 +45,7 @@ export class JSONTextLoader {
6945
});
7046
} finally {
7147
// reset loading flag
72-
this.#isLoading = false;
48+
this.setLoading(false);
7349
this.onloadend({
7450
source: origin
7551
});
@@ -81,7 +57,7 @@ export class JSONTextLoader {
8157
*/
8258
abort() {
8359
// reset loading flag
84-
this.#isLoading = false;
60+
this.setLoading(false);
8561
// call listeners
8662
this.onabort({});
8763
this.onloadend({});
@@ -175,63 +151,4 @@ export class JSONTextLoader {
175151
return urlContentTypes.Text;
176152
}
177153

178-
/**
179-
* Handle a load start event.
180-
* Default does nothing.
181-
*
182-
* @param {object} _event The load start event.
183-
*/
184-
onloadstart(_event) {}
185-
186-
/**
187-
* Handle a progress event.
188-
* Default does nothing.
189-
*
190-
* @param {object} _event The load progress event.
191-
*/
192-
onprogress(_event) {}
193-
194-
/**
195-
* Handle a load item event.
196-
* Default does nothing.
197-
*
198-
* @param {object} _event The load item event fired
199-
* when a file item has been loaded successfully.
200-
*/
201-
onloaditem(_event) {}
202-
203-
/**
204-
* Handle a load event.
205-
* Default does nothing.
206-
*
207-
* @param {object} _event The load event fired
208-
* when a file has been loaded successfully.
209-
*/
210-
onload(_event) {}
211-
212-
/**
213-
* Handle an load end event.
214-
* Default does nothing.
215-
*
216-
* @param {object} _event The load end event fired
217-
* when a file load has completed, successfully or not.
218-
*/
219-
onloadend(_event) {}
220-
221-
/**
222-
* Handle an error event.
223-
* Default does nothing.
224-
*
225-
* @param {object} _event The error event.
226-
*/
227-
onerror(_event) {}
228-
229-
/**
230-
* Handle an abort event.
231-
* Default does nothing.
232-
*
233-
* @param {object} _event The abort event.
234-
*/
235-
onabort(_event) {}
236-
237154
} // class JSONTextLoader

src/io/loaderBase.js

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/**
2+
* Base class for single data loaders.
3+
*
4+
* Provides shared state (#options, #isLoading) and the seven
5+
* event-handler stubs that the orchestrators (FilesLoader, UrlsLoader)
6+
* assign per-instance before calling load().
7+
*/
8+
export class LoaderBase {
9+
10+
/**
11+
* Loader options.
12+
*
13+
* @type {object}
14+
*/
15+
#options = {};
16+
17+
/**
18+
* Loading flag.
19+
*
20+
* @type {boolean}
21+
*/
22+
#isLoading = false;
23+
24+
/**
25+
* Get the loader options.
26+
*
27+
* @returns {object} The loader options.
28+
*/
29+
getOptions() {
30+
return this.#options;
31+
}
32+
33+
/**
34+
* Set the loader options.
35+
*
36+
* @param {object} opt The input options.
37+
*/
38+
setOptions(opt) {
39+
this.#options = opt;
40+
}
41+
42+
/**
43+
* Is the load ongoing?
44+
*
45+
* @returns {boolean} True if loading.
46+
*/
47+
isLoading() {
48+
return this.#isLoading;
49+
}
50+
51+
/**
52+
* Set the loading flag.
53+
*
54+
* @param {boolean} flag The loading flag value.
55+
*/
56+
setLoading(flag) {
57+
this.#isLoading = flag;
58+
}
59+
60+
/**
61+
* Handle a load start event.
62+
* Default does nothing.
63+
*
64+
* @param {object} _event The load start event.
65+
*/
66+
onloadstart(_event) {}
67+
68+
/**
69+
* Handle a progress event.
70+
* Default does nothing.
71+
*
72+
* @param {object} _event The load progress event.
73+
*/
74+
onprogress(_event) {}
75+
76+
/**
77+
* Handle a load item event.
78+
* Default does nothing.
79+
*
80+
* @param {object} _event The load item event fired
81+
* when a file item has been loaded successfully.
82+
*/
83+
onloaditem(_event) {}
84+
85+
/**
86+
* Handle a load event.
87+
* Default does nothing.
88+
*
89+
* @param {object} _event The load event fired
90+
* when a file has been loaded successfully.
91+
*/
92+
onload(_event) {}
93+
94+
/**
95+
* Handle an load end event.
96+
* Default does nothing.
97+
*
98+
* @param {object} _event The load end event fired
99+
* when a file load has completed, successfully or not.
100+
*/
101+
onloadend(_event) {}
102+
103+
/**
104+
* Handle an error event.
105+
* Default does nothing.
106+
*
107+
* @param {object} _event The error event.
108+
*/
109+
onerror(_event) {}
110+
111+
/**
112+
* Handle an abort event.
113+
* Default does nothing.
114+
*
115+
* @param {object} _event The abort event.
116+
*/
117+
onabort(_event) {}
118+
119+
} // class LoaderBase

0 commit comments

Comments
 (0)