1- const HDSItemDef = require ( './HDSItemDef' ) ;
2-
1+ const HDSModelItemsDefs = require ( './HDSModel-ItemsDefs' ) ;
32class HDSModel {
43 /**
54 * JSON definition file
@@ -15,15 +14,9 @@ class HDSModel {
1514 #modelData;
1615
1716 /**
18- * ItemDefs Cache
19- * KeyValue of itemsDefs
17+ * @type {HDSModelItemsDefs }
2018 */
21- #itemsDefs;
22-
23- /**
24- * get itemsDefs by streamId and eventType
25- */
26- #modelDataByStreamIdEventTypes;
19+ #modelItemsDefs;
2720
2821 /**
2922 * streamsById
@@ -38,8 +31,6 @@ class HDSModel {
3831 */
3932 constructor ( modelUrl ) {
4033 this . #modelUrl = modelUrl ;
41- this . #itemsDefs = { } ;
42- this . #modelDataByStreamIdEventTypes = { } ;
4334 this . #modelStreamsById = { } ;
4435 }
4536
@@ -51,47 +42,29 @@ class HDSModel {
5142 const resultText = await response . text ( ) ;
5243 const result = JSON . parse ( resultText ) ;
5344 this . #modelData = result ;
54- loadModelDataByStreamIdEventTypes ( this . #modelData. items , this . #modelDataByStreamIdEventTypes) ;
45+ // add key to items before freezing;
46+ for ( const [ key , item ] of Object . entries ( this . #modelData. items ) ) {
47+ item . key = key ;
48+ }
49+
5550 loadModelStreamsById ( this . #modelData. streams , this . #modelStreamsById) ;
5651 deepFreeze ( this . #modelData) ; // make sure it cannot be modified
5752 }
5853
5954 /**
60- * get item for a key
61- * @param {string } key
62- * @param {boolean } [throwErrorIfNotFound] default `true`
55+ * RAW model data
6356 */
64- itemDefForKey ( key , throwErrorIfNotFound = true ) {
65- if ( this . #itemsDefs[ key ] ) return this . #itemsDefs[ key ] ;
66- const defData = this . #modelData. items [ key ] ;
67- if ( ! defData ) {
68- if ( throwErrorIfNotFound ) throw new Error ( 'Cannot find item definition with key: ' + key ) ;
69- return null ;
70- }
71- this . #itemsDefs[ key ] = new HDSItemDef ( key , defData ) ;
72- return this . #itemsDefs[ key ] ;
57+ get modelData ( ) {
58+ if ( ! this . #modelData) throw new Error ( 'Model not loaded call `await model.load()` first.' ) ;
59+ return this . #modelData;
7360 }
7461
7562 /**
76- * get a definition for an event
77- * @param {Event } event
78- * @param {boolean } [throwErrorIfNotFound] default `true`
63+ * @type HDSModelItemsDefs
7964 */
80- itemDefForEvent ( event , throwErrorIfNotFound = true ) {
81- const candidates = [ ] ;
82- for ( const streamId of event . streamIds ) {
83- const keyStreamIdEventType = streamId + ':' + event . type ;
84- const candidate = this . #modelDataByStreamIdEventTypes[ keyStreamIdEventType ] ;
85- if ( candidate ) candidates . push ( candidate ) ;
86- }
87- if ( candidates . length === 0 ) {
88- if ( throwErrorIfNotFound ) throw new Error ( 'Cannot find definition for event: ' + JSON . stringify ( event ) ) ;
89- return null ;
90- }
91- if ( candidates . length > 1 ) {
92- throw new Error ( `Found multiple matching definitions "${ candidates . map ( c => ( c . key ) ) . join ( ', ' ) } " for event: ${ JSON . stringify ( event ) } ` ) ;
93- }
94- return this . itemDefForKey ( candidates [ 0 ] . key , throwErrorIfNotFound ) ;
65+ get itemsDefs ( ) {
66+ if ( ! this . #modelItemsDefs) this . #modelItemsDefs = new HDSModelItemsDefs ( this ) ;
67+ return this . #modelItemsDefs;
9568 }
9669
9770 /**
@@ -102,7 +75,7 @@ class HDSModel {
10275 const result = [ ] ;
10376 const streams = new Map ( ) ; // tempMap to keep streams already in
10477 for ( const itemKey of itemKeys ) {
105- const itemDef = this . itemDefForKey ( itemKey ) ;
78+ const itemDef = this . itemsDefs . forKey ( itemKey ) ;
10679 const streamParentIds = this . streamGetParentsIds ( itemDef . data . streamId , true , [ itemDef . data . streamId ] ) ;
10780 for ( const streamId of streamParentIds ) {
10881 if ( streams . has ( streamId ) ) continue ;
@@ -195,7 +168,7 @@ class HDSModel {
195168 }
196169 // add streamId not already in
197170 for ( const itemKey of itemKeys ) {
198- const itemDef = this . itemDefForKey ( itemKey ) ;
171+ const itemDef = this . itemsDefs . forKey ( itemKey ) ;
199172 const streamId = itemDef . data . streamId ;
200173 if ( ! streamsRequested [ streamId ] ) { // new streamId
201174 const auth = { streamId, level : opts . defaultLevel } ;
@@ -276,32 +249,6 @@ function deepFreeze (object) {
276249 return Object . freeze ( object ) ;
277250}
278251
279- /**
280- * @private
281- * Add key to model items and
282- * load modeldata item into modelDataByStreamIdEventTypes for fast search
283- */
284- function loadModelDataByStreamIdEventTypes ( model , map ) {
285- for ( const [ key , item ] of Object . entries ( model ) ) {
286- // add key to item
287- item . key = key ;
288- const eventTypes = [ ] ;
289- if ( item . eventType ) {
290- eventTypes . push ( item . eventType ) ;
291- } else {
292- eventTypes . push ( ...Object . keys ( item . variations . eventType ) ) ;
293- }
294- for ( const eventType of eventTypes ) {
295- const keyStreamIdEventType = item . streamId + ':' + eventType ;
296- if ( map [ keyStreamIdEventType ] ) {
297- // should be tested with a faulty model
298- throw new Error ( `Duplicate streamId + eventType "${ keyStreamIdEventType } " for item ${ JSON . stringify ( item ) } ` ) ;
299- }
300- map [ keyStreamIdEventType ] = item ;
301- }
302- }
303- }
304-
305252/**
306253 * @param {Array<stream> } streams
307254 * @param {Object<string, stream> } map - key value map
0 commit comments