@gudhub/core 1.2.4-beta.22 → 1.2.4-beta.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -39,17 +39,30 @@ export class AppDataService extends DataService {
39
39
  // }
40
40
 
41
41
  // let res = await this.chunkDataService.getChunks(id, sentData.chunks);
42
- let res = await Promise.all(sentData.chunks.map((chunk_id) => this.chunkDataService.getChunk(id, chunk_id)));
43
- if (!res) res = [];
42
+ // let res = await Promise.all(sentData.chunks.map((chunk_id) => this.chunkDataService.getChunk(id, chunk_id)));
44
43
 
44
+ let res = await this.chunkDataService.getMergedChunkForIDList(id, sentData.chunks);
45
45
 
46
46
 
47
+ if (
48
+ res
49
+ ) {
50
+ sentData.items_list = await this.gudhub.util.mergeChunks([
51
+ res,
52
+ sentData,
53
+ ]);
54
+ }
47
55
 
56
+ // if (!res) res = [];
48
57
 
49
- sentData.items_list = await this.gudhub.util.mergeChunks([
50
- ...res,
51
- sentData,
52
- ]);
58
+
59
+
60
+ //TODO should be in new thread too even for Node.js becauses this will block loop
61
+
62
+ // sentData.items_list = await this.gudhub.util.mergeChunks([
63
+ // ...res,
64
+ // sentData,
65
+ // ]);
53
66
 
54
67
 
55
68
  return sentData;
@@ -69,25 +82,33 @@ export class AppDataService extends DataService {
69
82
 
70
83
 
71
84
  try {
72
- let cachedVersion = await this.getCached(id);
85
+
86
+ // я там собираюсь склеивать все данные в этом методе, надо посмотреть не будет ли проблем для ноды итп там тоже process вызывается
87
+ let cachedApp = await this.getCached(id); // this should be merged chunk + app from db
88
+
89
+
90
+ // let cachedMergedChunk = await this.chunkDataService.getCachedMergedChunk(id, cachedApp.chunks);
73
91
 
74
92
  if (
75
- !cachedVersion
93
+ !cachedApp
94
+ // cachedMergedChunk ?
76
95
  ) {
77
96
 
78
97
  //TODO maybe dont wait for chunks at first load
79
98
 
80
99
  // let res = await this.chunkDataService.getChunks(id, sentData.chunks);
81
- let res = await Promise.all(sentData.chunks.map((chunk_id) => this.chunkDataService.getChunk(id, chunk_id)));
82
- if (!res) res = [];
83
-
84
-
100
+ // let res = await Promise.all(sentData.chunks.map((chunk_id) => this.chunkDataService.getChunk(id, chunk_id)));
101
+ let res = await this.chunkDataService.getMergedChunkForIDList(id, sentData.chunks);
85
102
 
86
- sentData.items_list = await this.gudhub.util.mergeChunks([
87
- ...res,
88
- sentData,
89
- ]);
90
103
 
104
+ if (
105
+ res
106
+ ) {
107
+ sentData.items_list = await this.gudhub.util.mergeChunks([
108
+ res,
109
+ sentData,
110
+ ]);
111
+ }
91
112
 
92
113
  // this.putInCache(id, sentData);
93
114
 
@@ -96,27 +117,43 @@ export class AppDataService extends DataService {
96
117
  }
97
118
 
98
119
 
99
- let newHashesList = sentData.chunks.filter(c => !cachedVersion.chunks.includes(c));
120
+ // let newHashesList = sentData.chunks.filter(c => !cachedApp.chunks.includes(c));
100
121
 
101
122
 
102
123
  // let self = this;
103
124
 
104
125
 
105
126
  // let res = await this.chunkDataService.getChunks(id, newHashesList);
106
- let res = await Promise.all(newHashesList.map((chunk_id) => this.chunkDataService.getChunk(id, chunk_id)));
107
- if (!res) res = [];
108
-
127
+ // let res = await Promise.all(newHashesList.map((chunk_id) => this.chunkDataService.getChunk(id, chunk_id)));
109
128
 
129
+ let res = await this.chunkDataService.getCachedMergedChunk(id, cachedApp.chunks);
110
130
 
131
+ // if (!res) res = [];
111
132
 
112
- // this.chunkDataService.getChunks(id, newHashesList).then((res) => {
113
133
 
114
134
 
135
+ if (
136
+ res
137
+ ) {
115
138
  sentData.items_list = await this.gudhub.util.mergeChunks([
116
- cachedVersion,
117
- ...res,
139
+ cachedApp,
140
+ res,
118
141
  sentData,
119
142
  ]);
143
+ } else {
144
+ sentData.items_list = await this.gudhub.util.mergeChunks([
145
+ cachedApp,
146
+ sentData,
147
+ ]);
148
+ }
149
+
150
+
151
+
152
+
153
+ // this.chunkDataService.getChunks(id, newHashesList).then((res) => {
154
+
155
+
156
+
120
157
 
121
158
 
122
159
  //TODO check this for AppHttpService, seems isnt necessary
@@ -134,18 +171,24 @@ export class AppDataService extends DataService {
134
171
  // return cachedVersion;
135
172
 
136
173
  } catch (error) {
137
- // res = await this.chunkDataService.getChunks(id, sentData.chunks);
138
- let res = await Promise.all(sentData.chunks.map((chunk_id) => this.chunkDataService.getChunk(id, chunk_id)));
139
- if (!res) res = [];
140
174
 
175
+ // seems try catch here isnt handling almost anything, almost identical code here
141
176
 
177
+ // res = await this.chunkDataService.getChunks(id, sentData.chunks);
178
+ // let res = await Promise.all(sentData.chunks.map((chunk_id) => this.chunkDataService.getChunk(id, chunk_id)));
179
+ let res = await this.chunkDataService.getMergedChunkForIDList(sentData.chunks);
142
180
 
181
+ // if (!res) res = [];
143
182
 
144
- sentData.items_list = await this.gudhub.util.mergeChunks([
145
- ...res,
146
- sentData,
147
- ]);
148
183
 
184
+ if (
185
+ res
186
+ ) {
187
+ sentData.items_list = await this.gudhub.util.mergeChunks([
188
+ res,
189
+ sentData,
190
+ ]);
191
+ }
149
192
 
150
193
  // this.putInCache(id, sentData);
151
194
 
@@ -2,4 +2,9 @@ import { DataService } from "./DataService.js";
2
2
 
3
3
  export class ChunkDataService extends DataService {
4
4
  //interface for indexeddb and http services
5
+
6
+
7
+ getMergedChunkForIDList(idList) {
8
+
9
+ }
5
10
  }
@@ -9,6 +9,7 @@ import { chunksConf } from "./chunkDataConf.js";
9
9
  import { AppStoreManager } from "./StoreManager/managers.js";
10
10
  import { dbName, dbVersion } from "./consts.js";
11
11
  import { appsConf } from "./appDataConf.js";
12
+ import { chunksMergeConf } from "./storeManagerConf/chunkCacheStoreManagerConf.js";
12
13
 
13
14
 
14
15
  //this should be global in project
@@ -174,7 +175,7 @@ export class IndexedDBAppServiceForWorker extends AppDataService {
174
175
  this.gudhub = gudhub;
175
176
 
176
177
 
177
- this.chunkDataService = new IndexedDBChunkService(req, chunksConf, gudhub);
178
+ this.chunkDataService = new IndexedDBChunkService(req, chunksMergeConf, gudhub);
178
179
 
179
180
 
180
181
 
@@ -309,15 +310,18 @@ export class IndexedDBAppServiceForWorker extends AppDataService {
309
310
  async getApp(id) {
310
311
  // if (this.requestCache.has(id)) return this.requestCache.get(id);
311
312
 
313
+
314
+ // return at first time cached app request?????? no seems for worker its ok
315
+
312
316
  let self = this;
313
317
 
314
318
  // let dataServiceRequest = this.dataService.getAppWithoutProcessing(id);
315
319
  let data = await this.dataService.getAppWithoutProcessing(id);
316
320
 
317
- let processedData = await self.processRequestedApp(id, data);//here
321
+ let processedData = await self.processRequestedApp(id, data);//here нужно пересмотреть
318
322
 
319
323
 
320
- await self.putApp(id, processedData);
324
+ await self.putApp(id, data);
321
325
 
322
326
 
323
327
  return processedData;
@@ -407,7 +411,7 @@ export class IndexedDBAppServiceForWorker extends AppDataService {
407
411
 
408
412
  async getCached(id) {
409
413
 
410
- return this.storeManager.getAppStoreManagerVersion(id); //TODO investigate method name
414
+ return this.storeManager.getApp(id); //TODO investigate method name
411
415
 
412
416
 
413
417
  let self = this;
@@ -547,7 +551,7 @@ export class IndexedDBAppService extends AppDataService {
547
551
  }
548
552
 
549
553
 
550
-
554
+ // todo check this for new code
551
555
  if (
552
556
  e.data.type == 'processData'
553
557
  ) {
@@ -621,7 +625,7 @@ export class IndexedDBAppService extends AppDataService {
621
625
  }
622
626
 
623
627
  async getApp(id) {//TODO -> get?
624
- if (this.requestCache.has(id)) return this.requestCache.get(id);
628
+ if (this.requestCache.has(id)) return this.requestCache.get(id); // по этому коду есть вопрос - вызовы getApp кешируются выше и тогда выходит возвращается старая версия?
625
629
 
626
630
 
627
631
  let self = this;
@@ -633,8 +637,27 @@ export class IndexedDBAppService extends AppDataService {
633
637
  if (
634
638
  hasId
635
639
  ) {
636
- let getFromDBPrms = this.getCached(id);
637
640
 
641
+ // let promiseObject = Promise.
642
+
643
+ let cached = await this.getCached(id); // todo here should be merged with cached mergedChunk
644
+
645
+ // repetitive operation again, seems should be in getCached
646
+ let cachedMergedChunk = await this.chunkDataService.getCachedMergedChunk(id, cached.chunks);
647
+
648
+
649
+ // let mergedData = this.gudhub
650
+
651
+
652
+ cached.items_list = await this.gudhub.util.mergeChunks([
653
+ cachedMergedChunk,
654
+ cached,
655
+ ]);
656
+
657
+
658
+ let getFromDBPrms = Promise.resolve(cached);
659
+
660
+ // let getFromDBPrms = await this.getCached(id); // todo here should be merged with cached mergedChunk
638
661
  this.requestCache.set(id, getFromDBPrms);
639
662
 
640
663
  this.appRequestAndProcessWorker.postMessage({ type: 'processData', id: id });
@@ -713,7 +736,7 @@ export class IndexedDBAppService extends AppDataService {
713
736
  async getCached(id) {//TODO -> getFromStore?
714
737
 
715
738
 
716
- return this.storeManager.getAppStoreManagerVersion(id); //TODO investigate method name
739
+ return this.storeManager.getApp(id); //TODO investigate method name
717
740
 
718
741
  let self = this;
719
742
 
@@ -2,7 +2,7 @@ import { ChunkDataService } from "../ChunkDataService.js"; // removed "ChunkCach
2
2
  import { ChunkHttpService } from "../httpService/ChunkHttpService.js";
3
3
  import { objectAssignWithOverride } from "../utils.js";
4
4
  import { IndexedDBService } from "./IndexedDBService.js";
5
- import { ChunkCacheStoreManager, ChunksStoreManager } from "./StoreManager/managers.js";
5
+ import { ChunksStoreManager, MergedChunksStoreManager } from "./StoreManager/managers.js";
6
6
 
7
7
 
8
8
  //this should be global in project
@@ -31,7 +31,11 @@ export class IndexedDBChunkService extends ChunkDataService {
31
31
  this.gudhub = gudhub;
32
32
 
33
33
 
34
- this.storeManager = new ChunksStoreManager;
34
+ // this.storeManager = new ChunksStoreManager;
35
+ this.storeManager = new MergedChunksStoreManager;
36
+
37
+
38
+ // this.storeManager = new ChunksStoreManager;
35
39
 
36
40
 
37
41
  // objectAssignWithOverride(this, indexDBService);
@@ -53,9 +57,121 @@ export class IndexedDBChunkService extends ChunkDataService {
53
57
  }
54
58
  }
55
59
 
60
+ async getCachedMergedChunk(id, chunkHashesList) {
61
+ try {
62
+ let cachedData = await this.storeManager.get(id);
63
+
64
+ return cachedData.mergedData;
65
+ } catch (error) {
66
+ // let newHashes = chunkHashesList.filter(c => !cachedData.hashes.includes(c));
67
+
68
+
69
+ const requests = chunkHashesList.map(hash => this.dataService.getChunk(id, hash));
70
+
71
+ const newChunks = await Promise.all(requests);
72
+
73
+
74
+ let mergedData = await this.gudhub.util.mergeChunks(newChunks);
75
+
76
+ this.putMergedChunks(id, {hashes: chunkHashesList, mergedData: {items_list: mergedData}});
77
+
78
+ return {items_list: mergedData};
79
+ }
80
+ }
81
+
82
+ async getMergedChunkForIDList(id, chunkHashesList) {
83
+
84
+
85
+ //TODO promise cache
86
+
87
+ // TODO also try catch maybe
88
+
89
+
90
+ try {
91
+ let cachedData = await this.storeManager.get(id);
92
+
93
+
94
+ if (
95
+ chunkHashesList.every(h1 => cachedData.hashes.includes(h1))
96
+ ) {
97
+ return cachedData.mergedData;
98
+ }
99
+
100
+ let newHashes = chunkHashesList.filter(c => !cachedData.hashes.includes(c));
101
+
102
+
103
+ const requests = newHashes.map(hash => this.dataService.getChunk(id, hash));
104
+
105
+ const newChunks = await Promise.all(requests);
106
+
107
+
108
+ let mergedData = await this.gudhub.util.mergeChunks([
109
+ cachedData.mergedData,
110
+ ...newChunks,
111
+ ]);
112
+
113
+ this.putMergedChunks(id, {hashes: chunkHashesList, mergedData: {items_list: mergedData}});
114
+
115
+
116
+ return {items_list: mergedData};
117
+ } catch (error) {
118
+ // let newHashes = chunkHashesList.filter(c => !cachedData.hashes.includes(c));
119
+
120
+
121
+ const requests = chunkHashesList.map(hash => this.dataService.getChunk(id, hash));
122
+
123
+ const newChunks = await Promise.all(requests);
124
+
125
+
126
+ let mergedData = await this.gudhub.util.mergeChunks(newChunks);
127
+
128
+ this.putMergedChunks(id, {hashes: chunkHashesList, mergedData: {items_list: mergedData}});
129
+
130
+ return {items_list: mergedData};
131
+ }
132
+ }
133
+
134
+
135
+
136
+ // async function fetchAndMergeArrays(urls) {
137
+ // try {
138
+ // // Create an array of fetch promises
139
+ // const requests = urls.map(url => fetch(url).then(response => response.json()));
140
+
141
+ // // Wait for all requests to complete
142
+ // const results = await Promise.all(requests);
143
+
144
+ // // Merge all arrays
145
+ // const mergedArray = results.flat(); // or use reduce: results.reduce((acc, arr) => acc.concat(arr), []);
146
+
147
+ // return mergedArray;
148
+ // } catch (error) {
149
+ // console.error('Error fetching data:', error);
150
+ // throw error;
151
+ // }
152
+ // }
153
+
154
+ // // Example usage:
155
+ // const urls = ['https://api.example.com/data1', 'https://api.example.com/data2'];
156
+ // fetchAndMergeArrays(urls)
157
+ // .then(mergedArray => console.log(mergedArray))
158
+ // .catch(error => console.error('Error:', error));
159
+
160
+
56
161
  //todo also optimize chunks transactions etc
57
162
 
58
163
 
164
+
165
+ async putMergedChunks(id, data) {
166
+ return this.storeManager.put(id, data);
167
+ }
168
+
169
+
170
+ // async getMergedChunks(id, data) {
171
+ // return this.storeManager.get(id, data);
172
+ // }
173
+
174
+
59
175
  async putChunk(id, data) {
60
176
  return this.storeManager.putChunk(id, data);
61
177
 
@@ -132,7 +248,7 @@ export class IndexedDBChunkService extends ChunkDataService {
132
248
 
133
249
  try {
134
250
 
135
- let pr = this.storeManager.getChunkStoreManagerVersion(id);
251
+ let pr = this.storeManager.getChunk(id);
136
252
 
137
253
  // let self = this;
138
254
 
@@ -2,6 +2,7 @@ import { appsConf } from "../appDataConf.js";
2
2
  import { chunksConf } from "../chunkDataConf.js";
3
3
  // import { dbName } from "../consts";
4
4
  import { gudhubIndexedDBManager } from "../IndexedDBService.js";
5
+ import { chunksMergeConf } from "../storeManagerConf/chunkCacheStoreManagerConf.js";
5
6
  import { BaseStoreManager } from "./BaseStoreManager.js";
6
7
 
7
8
  export class AppStoreManager extends BaseStoreManager {
@@ -9,10 +10,7 @@ export class AppStoreManager extends BaseStoreManager {
9
10
  super(gudhubIndexedDBManager, appsConf.store);
10
11
  }
11
12
 
12
- //use item methods from BaseStoreManager !!!!!!!!!!
13
-
14
- //TODO getApp overrides getApp from IndexedDBAppService
15
- async getAppStoreManagerVersion(id) {
13
+ async getApp(id) {
16
14
 
17
15
  return this.getItem(id);
18
16
 
@@ -50,7 +48,7 @@ export class ChunksStoreManager extends BaseStoreManager {
50
48
  }
51
49
 
52
50
 
53
- async getChunkStoreManagerVersion(id) {
51
+ async getChunk(id) {
54
52
 
55
53
  return this.getItem(id);
56
54
 
@@ -82,10 +80,34 @@ export class ChunksStoreManager extends BaseStoreManager {
82
80
  // Additional methods specific to ChunksStoreManager if necessary
83
81
  }
84
82
 
85
- export class ChunkCacheStoreManager extends BaseStoreManager {
86
- constructor(dbManager) {
87
- super(dbManager, 'myDatabase', 'chunkCacheStore');
83
+ export class MergedChunksStoreManager extends BaseStoreManager {
84
+ constructor() {
85
+ super(gudhubIndexedDBManager, chunksMergeConf.store);
86
+ }
87
+
88
+ async get(id) {
89
+
90
+ return this.getItem(id);
91
+
92
+ // return dbManager.enqueueOperation('apps', 'readonly', (store) => store.get(id));
93
+ }
94
+
95
+ async add(id, chunk) {
96
+ return this.addItem(id, chunk);
97
+
98
+ // return dbManager.enqueueOperation('apps', 'readwrite', (store) => store.add(app));
88
99
  }
89
100
 
90
- // Additional methods specific to ChunkCacheStoreManager if necessary
101
+ //TODO update here ok?
102
+ async put(id, chunk) {
103
+ return this.putItem(id, chunk);
104
+
105
+ // return dbManager.enqueueOperation('apps', 'readwrite', (store) => store.add(app));
106
+ }
107
+
108
+ async delete(id) {
109
+ return this.deleteItem(id);
110
+
111
+ // return dbManager.enqueueOperation('apps', 'readwrite', (store) => store.delete(id));
112
+ }
91
113
  }
@@ -1,5 +1,8 @@
1
1
  // import { dbName } from "./consts.js";
2
2
 
3
+
4
+ // move to folder also remove chunksConf
5
+
3
6
  export const appsConf = {
4
7
  // dbName,
5
8
  // dbVersion,
@@ -3,11 +3,15 @@ import { appDataServiceConf } from "../export.js";
3
3
  import { GudHub } from "../../gudhub.js";
4
4
  import { IndexedDBAppServiceForWorker } from "../IndexedDB/IndexedDBAppService.js";
5
5
  import { appsConf } from "../IndexedDB/appDataConf.js";
6
+ import { IndexedDBChunkService } from "./IndexedDBChunkService.js";
7
+ import { chunksConf } from "./chunkDataConf.js";
8
+ import { chunksMergeConf } from "./storeManagerConf/chunkCacheStoreManagerConf.js";
6
9
 
7
10
 
8
11
 
9
12
  let gudhub = {};
10
13
  let appDataService = {};
14
+ let chunkDataService = {};
11
15
 
12
16
 
13
17
 
@@ -22,6 +26,8 @@ self.onmessage = async function(e) {
22
26
 
23
27
  gudhub = new GudHub(e.data.gudhubSerialized.authKey, e.data.gudhubSerialized.options);
24
28
  appDataService = new IndexedDBAppServiceForWorker(gudhub.req, appsConf, gudhub);
29
+ chunkDataService = new IndexedDBChunkService(gudhub.req, chunksMergeConf, gudhub);
30
+
25
31
 
26
32
  }
27
33
 
@@ -31,9 +37,36 @@ self.onmessage = async function(e) {
31
37
 
32
38
  //todo cached could not exist !!!!!!!!!!!!!!!!!!!
33
39
 
34
- let cached = await appDataService.getCached(e.data.id);
40
+ let cached = await appDataService.getCached(e.data.id); // merge ?
41
+
42
+
43
+
44
+ // TODO this is repetitive operation for getCached, maybe put all code in process or in getCached
45
+
46
+ let res = await chunkDataService.getCachedMergedChunk(e.data.id, cached.chunks);
47
+
48
+ // if (!res) res = [];
49
+
50
+
51
+ // TODO check that res always exists
52
+
53
+
54
+ if (
55
+ res
56
+ ) {
57
+ cached.items_list = await gudhub.util.mergeChunks([
58
+ // cachedApp,
59
+ res,
60
+ cached,
61
+ ]);
62
+ } else {
63
+ // sentData.items_list = await this.gudhub.util.mergeChunks([
64
+ // cachedApp,
65
+ // sentData,
66
+ // ]);
67
+ }
35
68
 
36
- let nextVersion = await appDataService.getApp(e.data.id);
69
+ let nextVersion = await appDataService.getApp(e.data.id); //todo check that here most recent always
37
70
 
38
71
 
39
72
 
@@ -82,7 +115,7 @@ self.onmessage = async function(e) {
82
115
  e.data.type === 'requestApp'
83
116
  ) {
84
117
 
85
- let data = await appDataService.getApp(e.data.id);
118
+ let data = await appDataService.getApp(e.data.id); //todo check process request method seems there i return cahced version not most recent data from server
86
119
 
87
120
 
88
121
  this.postMessage({id: e.data.id, type: e.data.type, data});
@@ -17,6 +17,22 @@ export class ChunkHttpService extends ChunkDataService {
17
17
  async getChunk(app_id, id) {
18
18
  return this.chunkApi.getChunk(app_id, id);
19
19
  }
20
+
21
+ async getChunksForId(app_id) {
22
+ // maybe use app get api for now and return app.chunks from there
23
+
24
+ return []; // list of chunks
25
+ }
26
+
27
+
28
+ getCachedMergedChunk(id, chunkHashesList) {
29
+
30
+ }
31
+
32
+
33
+ getMergedChunkForIDList(id, chunkHashesList) {
34
+
35
+ }
20
36
 
21
37
 
22
38
  putChunk() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gudhub/core",
3
- "version": "1.2.4-beta.22",
3
+ "version": "1.2.4-beta.23",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {