Skip to content

Commit c7938a4

Browse files
committed
Finalized refactor
1 parent eddfb06 commit c7938a4

4 files changed

Lines changed: 30 additions & 11 deletions

File tree

src/appTemplates/AppClientAccount.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@ class AppClientAccount extends Application {
1111
};
1212
}
1313

14-
get streamData () {
15-
return this.cache.streamData;
16-
}
17-
1814
/**
1915
* - Check connection validity
2016
* - Make sure stream structure exists

src/appTemplates/AppManagingAccount.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ class AppManagingAccount extends Application {
4141
async getCollectors (forceRefresh) {
4242
if (!forceRefresh && this.cache.collectorsMap) return Object.values(this.cache.collectorsMap);
4343
// Collectors are materialized by streams
44-
const streams = await this.connection.apiOne('streams.get', { parentId: this.baseStreamId }, 'streams');
44+
if (forceRefresh) await this.loadStreamData();
45+
const streams = this.streamData.children || [];
4546
const collectorsMap = {};
4647
for (const stream of streams) {
4748
const collector = new Collector(this, stream);
@@ -75,6 +76,8 @@ class AppManagingAccount extends Application {
7576
parentId: this.baseStreamId
7677
};
7778
const stream = await this.connection.apiOne('streams.create', params, 'stream');
79+
// add new stream to streamCache
80+
this.streamData.children.push(stream);
7881
const collector = new Collector(this, stream);
7982
this.cache.collectorsMap[collector.streamId] = collector;
8083
return collector;

src/appTemplates/Application.js

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,18 @@ class Application {
99

1010
cache;
1111

12+
/**
13+
* Get application stream structure
14+
* Initialized at init()
15+
* Can be refreshed with loadStreamData
16+
*/
17+
get streamData () {
18+
if (!this.cache.streamData) throw new Error('Call .init() first');
19+
return this.cache.streamData;
20+
}
21+
1222
get appSettings () {
13-
throw new Error('appSettings must be implemneted');
23+
throw new Error('appSettings must be implemented');
1424
// possible return values:
1525
/**
1626
* return {
@@ -69,6 +79,17 @@ class Application {
6979
async init () {
7080
await createAppStreams(this);
7181
}
82+
83+
/**
84+
* Force loading of streamData
85+
*/
86+
async loadStreamData () {
87+
const streamData = (await this.connection.apiOne('streams.get', { id: this.baseStreamId }, 'streams'))[0];
88+
if (streamData) {
89+
this.cache.streamData = streamData;
90+
}
91+
return streamData;
92+
}
7293
}
7394

7495
module.exports = Application;
@@ -101,11 +122,8 @@ async function createAppStreams (app) {
101122
// get streamStructure
102123
let found = false;
103124
try {
104-
const stream = (await app.connection.apiOne('streams.get', { id: app.baseStreamId }, 'streams'))[0];
105-
if (stream) {
106-
app.cache.streamData = stream;
107-
}
108-
found = true;
125+
const streamData = await app.loadStreamData();
126+
if (streamData) found = true;
109127
} catch (e) {
110128
if (e.innerObject?.id !== 'unknown-referenced-resource' || e.innerObject?.data?.id !== 'test-app-template-client') {
111129
throw e;

tests/apptemplates.test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ describe('[APTX] appTemplates', function () {
3636
const newCollector = await appManaging.createCollectorUnitialized(collectorName);
3737
assert.ok(newCollector.streamId.startsWith(baseStreamIdManager), 'Collectors id should start with baseStreamId');
3838
assert.ok(newCollector.name, collectorName);
39+
// check that streams has been addes to streamData
40+
assert.ok(appManaging.streamData.children[0].name, collectorName);
3941

4042
// Create a Collector with the same name should fail
4143
try {

0 commit comments

Comments
 (0)