Skip to content

Commit 4173947

Browse files
committed
Refactoring model streams in a file
1 parent 19c2b09 commit 4173947

4 files changed

Lines changed: 104 additions & 75 deletions

File tree

src/HDSModel-ItemsDefs.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const HDSItemDef = require('./HDSItemDef');
22

33
/**
4-
* Items - Extension of HDSModel
4+
* ItemsDefs - Extension of HDSModel
55
*/
66
class HDSModelItemsDefs {
77
/**

src/HDSModel-Streams.js

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/**
2+
* Streams - Extension of HDSModel
3+
*/
4+
class HDSModelStreams {
5+
/**
6+
* @type {HDSModel}
7+
*/
8+
#model;
9+
10+
/**
11+
* streamsById
12+
* Map to find streams by Id
13+
*/
14+
#modelStreamsById;
15+
16+
constructor (model) {
17+
this.#model = model;
18+
this.#modelStreamsById = {};
19+
loadModelStreamsById(this.#model.modelData.streams, this.#modelStreamsById);
20+
}
21+
22+
/**
23+
* Get a list of streams to be created for usage of these keys (whithout children)
24+
* @param {Array<string>} itemKeys
25+
*/
26+
getNecessaryListForItemKeys (itemKeys) {
27+
const result = [];
28+
const streams = new Map(); // tempMap to keep streams already in
29+
for (const itemKey of itemKeys) {
30+
const itemDef = this.#model.itemsDefs.forKey(itemKey);
31+
const streamParentIds = this.getParentsIds(itemDef.data.streamId, true, [itemDef.data.streamId]);
32+
for (const streamId of streamParentIds) {
33+
if (streams.has(streamId)) continue;
34+
const stream = this.getDataById(streamId);
35+
streams.set(streamId, true); // just to flag
36+
result.push({
37+
id: streamId,
38+
name: stream.name, // to be translated
39+
parentId: stream.parentId
40+
});
41+
}
42+
}
43+
return result;
44+
}
45+
46+
/**
47+
* Get stream Data by Id;
48+
* @param {string} streamId
49+
*/
50+
getDataById (streamId, throwErrorIfNotFound = true) {
51+
const streamData = this.#modelStreamsById[streamId];
52+
if (throwErrorIfNotFound && !streamData) throw new Error(`Stream with id: "${streamId}" not found`);
53+
return streamData;
54+
}
55+
56+
/**
57+
* Get all parents id;
58+
* @param {string} streamId
59+
* @param {boolean} [throwErrorIfNotFound] default `true`
60+
* @param {Array} [initialArray] - a pre-filled array
61+
*/
62+
getParentsIds (streamId, throwErrorIfNotFound = true, initialArray = []) {
63+
const streamData = this.getDataById(streamId, throwErrorIfNotFound);
64+
if (!streamData) return initialArray;
65+
if (streamData.parentId !== null) {
66+
initialArray.unshift(streamData.parentId);
67+
this.getParentsIds(streamData.parentId, true, initialArray);
68+
}
69+
return initialArray;
70+
}
71+
}
72+
73+
module.exports = HDSModelStreams;
74+
75+
/**
76+
* @param {Array<stream>} streams
77+
* @param {Object<string, stream>} map - key value map
78+
*/
79+
function loadModelStreamsById (streams, map) {
80+
if (!streams) return;
81+
for (const stream of streams) {
82+
if (map[stream.id]) {
83+
// should be tested with a faulty model
84+
throw new Error(`Duplicate streamId "${stream.id}" for strean ${JSON.stringify(stream)}`);
85+
}
86+
map[stream.id] = stream;
87+
loadModelStreamsById(stream.children, map);
88+
}
89+
}

src/HDSModel.js

Lines changed: 10 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const HDSModelItemsDefs = require('./HDSModel-ItemsDefs');
2+
const HDSModelStreams = require('./HDSModel-Streams');
23
class 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-
}

tests/hdsModel.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,21 +98,21 @@ describe('[MODX] Model', () => {
9898

9999
describe('[MOSX] streams', function () {
100100
it('[MOSB] Streams Data by Id', async () => {
101-
const streamData = model.streamDataGetById('fertility-cycles-start');
101+
const streamData = model.streams.getDataById('fertility-cycles-start');
102102
assert.equal(streamData.parentId, 'fertility-cycles');
103103
});
104104

105105
it('[MOSE] Streams Data by Id, Throw Error if not found', async () => {
106106
try {
107-
model.streamDataGetById('dummy');
107+
model.streams.getDataById('dummy');
108108
throw new Error('Should throw Error');
109109
} catch (e) {
110110
assert.equal(e.message, 'Stream with id: "dummy" not found');
111111
}
112112
});
113113

114114
it('[MOSP] Streams Data parents', async () => {
115-
const streamParentIds = model.streamGetParentsIds('fertility-cycles-start');
115+
const streamParentIds = model.streams.getParentsIds('fertility-cycles-start');
116116
assert.deepEqual(streamParentIds, ['fertility', 'fertility-cycles']);
117117
});
118118

@@ -124,7 +124,7 @@ describe('[MODX] Model', () => {
124124
'body-vulva-mucus-stretch',
125125
'profile-surname'
126126
];
127-
const streamsToBeCreated = model.streamsGetNecessaryListForItemKeys(itemKeys);
127+
const streamsToBeCreated = model.streams.getNecessaryListForItemKeys(itemKeys);
128128
// keeè a list of streams check that necessary streams exists
129129
const streamIdsToCheck = {};
130130
for (const itemKey of itemKeys) {

0 commit comments

Comments
 (0)