Skip to content

Commit 770d624

Browse files
committed
refactoring model singleton logic
1 parent e99860e commit 770d624

4 files changed

Lines changed: 31 additions & 20 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "hds-lib",
3-
"version": "0.1.0",
3+
"version": "0.1.1",
44
"description": "Health Data Safe - Library",
55
"main": "src/index.js",
66
"scripts": {

src/HDSModel/HDSModel.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,17 @@ class HDSModel {
3838
this.laziliyLoadedMap = { };
3939
}
4040

41+
get isLoaded () {
42+
return !!this.#modelData;
43+
}
44+
4145
/**
4246
* Load model definitions
4347
*/
44-
async load () {
48+
async load (modelUrl = null) {
49+
if (modelUrl) {
50+
this.#modelUrl = modelUrl;
51+
}
4552
const response = await fetch(this.#modelUrl);
4653
const resultText = await response.text();
4754
const result = JSON.parse(resultText);
@@ -56,7 +63,7 @@ class HDSModel {
5663

5764
/** RAW model data */
5865
get modelData () {
59-
if (!this.#modelData) throw new Error('Model not loaded call `await model.load()` first.');
66+
if (!this.isLoaded) throw new Error('Model not loaded call `HDSLib.initHDSModel()` or `await model.load()` first.');
6067
return this.#modelData;
6168
}
6269
}
@@ -65,6 +72,7 @@ class HDSModel {
6572
for (const [prop, Obj] of Object.entries(LAZILY_LOADED)) {
6673
Object.defineProperty(HDSModel.prototype, prop, {
6774
get: function () {
75+
if (!this.isLoaded) throw new Error('Model not loaded call `HDSLib.initHDSModel()` or `await model.load()` first.');
6876
if (!this.laziliyLoadedMap[prop]) this.laziliyLoadedMap[prop] = new Obj(this);
6977
return this.laziliyLoadedMap[prop];
7078
}
Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,31 @@
11
let model = null;
22
const HDSModel = require('./HDSModel');
33
const HDService = require('../HDSService');
4-
const { HDSLibError } = require('../errors');
54

65
module.exports = {
76
getModel,
87
initHDSModel
98
};
109

1110
function getModel () {
12-
if (model == null) throw new HDSLibError('Call await HDSLib.initHDSModel() once');
11+
if (model == null) {
12+
model = new HDSModel();
13+
}
1314
return model;
1415
}
1516

1617
/**
1718
* Initialized model singleton
1819
* @returns {HDSModel}
1920
*/
20-
async function initHDSModel (forceNew = false) {
21-
if (!model || forceNew) {
21+
async function initHDSModel () {
22+
if (!model) {
23+
getModel();
24+
}
25+
if (!model.isLoaded) {
2226
const service = new HDService();
2327
const serviceInfo = await service.info();
24-
model = new HDSModel(serviceInfo.assets['hds-model']);
25-
await model.load();
28+
await model.load(serviceInfo.assets['hds-model']);
2629
}
2730
return model;
2831
}

tests/hdsLib.test.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,22 @@ describe('[HDLX] HDSLib.index.js', () => {
1010
it('[HDME] HDSLib.model throws error if not initialized', () => {
1111
try {
1212
// eslint-disable-next-line no-unused-expressions
13-
HDSLib.model;
13+
HDSLib.model.modelData;
1414
throw new Error('Should throw an error');
1515
} catch (e) {
16-
assert.equal(e.message, 'Call await HDSLib.initHDSModel() once');
16+
assert.equal(e.message, 'Model not loaded call `HDSLib.initHDSModel()` or `await model.load()` first.');
17+
}
18+
19+
try {
20+
// eslint-disable-next-line no-unused-expressions
21+
HDSLib.model.streams;
22+
throw new Error('Should throw an error');
23+
} catch (e) {
24+
assert.equal(e.message, 'Model not loaded call `HDSLib.initHDSModel()` or `await model.load()` first.');
1725
}
1826
});
1927

20-
it('[HDME] HDSLib.initHDSModel()', async () => {
28+
it('[HDMF] HDSLib.initHDSModel()', async () => {
2129
const model0 = await HDSLib.initHDSModel();
2230
const model1 = await HDSLib.initHDSModel();
2331
assert.equal(model0, model1, 'HDSLib.initHDSModel() should used cached model');
@@ -26,14 +34,6 @@ describe('[HDLX] HDSLib.index.js', () => {
2634
// -- refresh model
2735
});
2836

29-
it('[HDME] HDSLib.initHDSModel(forceRefresh = true) should refresh model', async () => {
30-
const model0 = await HDSLib.initHDSModel();
31-
const model1 = await HDSLib.initHDSModel(true);
32-
assert.ok(model0 !== model1, 'HDSLib.initHDSModel(true) should refresh cached model');
33-
const model2 = HDSLib.model;
34-
assert.equal(model1, model2, 'HDSLib.model should used cached model');
35-
});
36-
3737
describe('[HDUX] Utils', () => {
3838
it('[HDUW] utils.waitUntilFalse', async function () {
3939
this.timeout('1000');

0 commit comments

Comments
 (0)