11const HDSModelItemsDefs = require ( './HDSModel-ItemsDefs' ) ;
2+ const HDSModelStreams = require ( './HDSModel-Streams' ) ;
23class HDSModel {
34 /**
45 * JSON definition file
@@ -19,10 +20,9 @@ class HDSModel {
1920 #modelItemsDefs;
2021
2122 /**
22- * streamsById
23- * Map to find streams by Id
23+ * @type {HDSModelStreams }
2424 */
25- #modelStreamsById ;
25+ #modelStreams ;
2626
2727 /**
2828 * JSON definition file
@@ -31,7 +31,6 @@ class HDSModel {
3131 */
3232 constructor ( modelUrl ) {
3333 this . #modelUrl = modelUrl ;
34- this . #modelStreamsById = { } ;
3534 }
3635
3736 /**
@@ -47,7 +46,6 @@ class HDSModel {
4746 item . key = key ;
4847 }
4948
50- loadModelStreamsById ( this . #modelData. streams , this . #modelStreamsById) ;
5149 deepFreeze ( this . #modelData) ; // make sure it cannot be modified
5250 }
5351
@@ -68,53 +66,11 @@ class HDSModel {
6866 }
6967
7068 /**
71- * Get a list of streams to be created for usage of these keys (whithout children)
72- * @param {Array<string> } itemKeys
69+ * @type HDSModelStreams
7370 */
74- streamsGetNecessaryListForItemKeys ( itemKeys ) {
75- const result = [ ] ;
76- const streams = new Map ( ) ; // tempMap to keep streams already in
77- for ( const itemKey of itemKeys ) {
78- const itemDef = this . itemsDefs . forKey ( itemKey ) ;
79- const streamParentIds = this . streamGetParentsIds ( itemDef . data . streamId , true , [ itemDef . data . streamId ] ) ;
80- for ( const streamId of streamParentIds ) {
81- if ( streams . has ( streamId ) ) continue ;
82- const stream = this . streamDataGetById ( streamId ) ;
83- streams . set ( streamId , true ) ; // just to flag
84- result . push ( {
85- id : streamId ,
86- name : stream . name , // to be translated
87- parentId : stream . parentId
88- } ) ;
89- }
90- }
91- return result ;
92- }
93-
94- /**
95- * Get stream Data by Id;
96- * @param {string } streamId
97- */
98- streamDataGetById ( streamId , throwErrorIfNotFound = true ) {
99- const streamData = this . #modelStreamsById[ streamId ] ;
100- if ( throwErrorIfNotFound && ! streamData ) throw new Error ( `Stream with id: "${ streamId } " not found` ) ;
101- return streamData ;
102- }
103-
104- /**
105- * Get all parents id;
106- * @param {string } streamId
107- * @param {boolean } [throwErrorIfNotFound] default `true`
108- * @param {Array } [initialArray] - a pre-filled array
109- */
110- streamGetParentsIds ( streamId , throwErrorIfNotFound = true , initialArray = [ ] ) {
111- const streamData = this . streamDataGetById ( streamId , throwErrorIfNotFound ) ;
112- if ( ! streamData ) return initialArray ;
113- if ( streamData . parentId !== null ) {
114- initialArray . unshift ( streamData . parentId ) ;
115- this . streamGetParentsIds ( streamData . parentId , true , initialArray ) ;
116- }
117- return initialArray ;
71+ get streams ( ) {
72+ if ( ! this . #modelStreams) this . #modelStreams = new HDSModelStreams ( this ) ;
73+ return this . #modelStreams;
11874 }
11975
12076 // --------- authorizations builder ------ //
@@ -149,7 +105,7 @@ class HDSModel {
149105 // complete pre with defaultName if missing
150106 if ( opts . includeDefaultName && ! pre . defaultName ) {
151107 // try to get it from streams Data
152- const stream = this . streamDataGetById ( pre . streamId , false ) ;
108+ const stream = this . streams . getDataById ( pre . streamId , false ) ;
153109 if ( stream ) {
154110 pre . defaultName = stream . name ;
155111 } else {
@@ -173,7 +129,7 @@ class HDSModel {
173129 if ( ! streamsRequested [ streamId ] ) { // new streamId
174130 const auth = { streamId, level : opts . defaultLevel } ;
175131 if ( opts . includeDefaultName ) {
176- const stream = this . streamDataGetById ( streamId ) ;
132+ const stream = this . streams . getDataById ( streamId ) ;
177133 auth . defaultName = stream . name ;
178134 }
179135 streamsRequested [ streamId ] = auth ;
@@ -183,7 +139,7 @@ class HDSModel {
183139 }
184140 // remove all permissions with a parent having identical or higher level
185141 for ( const auth of Object . values ( streamsRequested ) ) {
186- const parents = this . streamGetParentsIds ( auth . streamId , false ) ;
142+ const parents = this . streams . getParentsIds ( auth . streamId , false ) ;
187143 for ( const parent of parents ) {
188144 const found = streamsRequested [ parent ] ;
189145 if ( found && authorizationOverride ( found . level , auth . level ) ) {
@@ -248,19 +204,3 @@ function deepFreeze (object) {
248204
249205 return Object . freeze ( object ) ;
250206}
251-
252- /**
253- * @param {Array<stream> } streams
254- * @param {Object<string, stream> } map - key value map
255- */
256- function loadModelStreamsById ( streams , map ) {
257- if ( ! streams ) return ;
258- for ( const stream of streams ) {
259- if ( map [ stream . id ] ) {
260- // should be tested with a faulty model
261- throw new Error ( `Duplicate streamId "${ stream . id } " for strean ${ JSON . stringify ( stream ) } ` ) ;
262- }
263- map [ stream . id ] = stream ;
264- loadModelStreamsById ( stream . children , map ) ;
265- }
266- }
0 commit comments