@@ -20779,10 +20779,17 @@ class HDSModel {
2077920779 this.laziliyLoadedMap = { };
2078020780 }
2078120781
20782+ get isLoaded () {
20783+ return !!this.#modelData;
20784+ }
20785+
2078220786 /**
2078320787 * Load model definitions
2078420788 */
20785- async load () {
20789+ async load (modelUrl = null) {
20790+ if (modelUrl) {
20791+ this.#modelUrl = modelUrl;
20792+ }
2078620793 const response = await fetch(this.#modelUrl);
2078720794 const resultText = await response.text();
2078820795 const result = JSON.parse(resultText);
@@ -20797,7 +20804,7 @@ class HDSModel {
2079720804
2079820805 /** RAW model data */
2079920806 get modelData () {
20800- if (!this.#modelData ) throw new Error('Model not loaded call `await model.load()` first.');
20807+ if (!this.isLoaded ) throw new Error('Model not loaded call `HDSLib.initHDSModel()` or `await model.load()` first.');
2080120808 return this.#modelData;
2080220809 }
2080320810}
@@ -20806,6 +20813,7 @@ class HDSModel {
2080620813for (const [prop, Obj] of Object.entries(LAZILY_LOADED)) {
2080720814 Object.defineProperty(HDSModel.prototype, prop, {
2080820815 get: function () {
20816+ if (!this.isLoaded) throw new Error('Model not loaded call `HDSLib.initHDSModel()` or `await model.load()` first.');
2080920817 if (!this.laziliyLoadedMap[prop]) this.laziliyLoadedMap[prop] = new Obj(this);
2081020818 return this.laziliyLoadedMap[prop];
2081120819 }
@@ -20826,28 +20834,31 @@ module.exports = HDSModel;
2082620834let model = null;
2082720835const HDSModel = __webpack_require__(/*! ./HDSModel */ "./src/HDSModel/HDSModel.js");
2082820836const HDService = __webpack_require__(/*! ../HDSService */ "./src/HDSService.js");
20829- const { HDSLibError } = __webpack_require__(/*! ../errors */ "./src/errors.js");
2083020837
2083120838module.exports = {
2083220839 getModel,
2083320840 initHDSModel
2083420841};
2083520842
2083620843function getModel () {
20837- if (model == null) throw new HDSLibError('Call await HDSLib.initHDSModel() once');
20844+ if (model == null) {
20845+ model = new HDSModel();
20846+ }
2083820847 return model;
2083920848}
2084020849
2084120850/**
2084220851 * Initialized model singleton
2084320852 * @returns {HDSModel}
2084420853 */
20845- async function initHDSModel (forceNew = false) {
20846- if (!model || forceNew) {
20854+ async function initHDSModel () {
20855+ if (!model) {
20856+ getModel();
20857+ }
20858+ if (!model.isLoaded) {
2084720859 const service = new HDService();
2084820860 const serviceInfo = await service.info();
20849- model = new HDSModel(serviceInfo.assets['hds-model']);
20850- await model.load();
20861+ await model.load(serviceInfo.assets['hds-model']);
2085120862 }
2085220863 return model;
2085320864}
@@ -21392,7 +21403,7 @@ class Collector {
2139221403 }
2139321404
2139421405 /**
21395- * @type {StatusData}
21406+ * @type {StatusData}
2139621407 * Payload that can be modified
2139721408 * */
2139821409 get statusData () {
@@ -22167,7 +22178,7 @@ class CollectorInvite {
2216722178
2216822179 /**
2216922180 * private
22170- * @param {*} eventData
22181+ * @param {*} eventData
2217122182 */
2217222183 setEventData (eventData) {
2217322184 if (eventData.id !== this.eventData.id) throw new HDSLibError('CollectInvite event id does not match new Event');
@@ -23314,14 +23325,22 @@ describe('[HDLX] HDSLib.index.js', () => {
2331423325 it('[HDME] HDSLib.model throws error if not initialized', () => {
2331523326 try {
2331623327 // eslint-disable-next-line no-unused-expressions
23317- HDSLib.model;
23328+ HDSLib.model.modelData ;
2331823329 throw new Error('Should throw an error');
2331923330 } catch (e) {
23320- assert.equal(e.message, 'Call await HDSLib.initHDSModel() once');
23331+ assert.equal(e.message, 'Model not loaded call `HDSLib.initHDSModel()` or `await model.load()` first.');
23332+ }
23333+
23334+ try {
23335+ // eslint-disable-next-line no-unused-expressions
23336+ HDSLib.model.streams;
23337+ throw new Error('Should throw an error');
23338+ } catch (e) {
23339+ assert.equal(e.message, 'Model not loaded call `HDSLib.initHDSModel()` or `await model.load()` first.');
2332123340 }
2332223341 });
2332323342
23324- it('[HDME ] HDSLib.initHDSModel()', async () => {
23343+ it('[HDMF ] HDSLib.initHDSModel()', async () => {
2332523344 const model0 = await HDSLib.initHDSModel();
2332623345 const model1 = await HDSLib.initHDSModel();
2332723346 assert.equal(model0, model1, 'HDSLib.initHDSModel() should used cached model');
@@ -23330,14 +23349,6 @@ describe('[HDLX] HDSLib.index.js', () => {
2333023349 // -- refresh model
2333123350 });
2333223351
23333- it('[HDME] HDSLib.initHDSModel(forceRefresh = true) should refresh model', async () => {
23334- const model0 = await HDSLib.initHDSModel();
23335- const model1 = await HDSLib.initHDSModel(true);
23336- assert.ok(model0 !== model1, 'HDSLib.initHDSModel(true) should refresh cached model');
23337- const model2 = HDSLib.model;
23338- assert.equal(model1, model2, 'HDSLib.model should used cached model');
23339- });
23340-
2334123352 describe('[HDUX] Utils', () => {
2334223353 it('[HDUW] utils.waitUntilFalse', async function () {
2334323354 this.timeout('1000');
0 commit comments