@gudhub/core 1.2.4-beta.16 → 1.2.4-beta.18

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.
@@ -1,6 +1,10 @@
1
1
  import { DataService } from "./DataService.js";
2
2
  import { ChunkDataService, chunkDataServiceConf } from "./export.js";
3
3
 
4
+
5
+
6
+ //TODO processRequestedApp should be checked that all code is ok !!!!!!!!!!!!!!!! Ive not ended checking
7
+
4
8
  export class AppDataService extends DataService {
5
9
  //interface for indexeddb and http services
6
10
 
@@ -2,10 +2,13 @@ import { AppDataService } from "../AppDataService.js";
2
2
  import { ChunkDataService } from "../ChunkDataService.js"; // removed "ChunkCacheDataService" from imported because it was causing an error "{ ChunkCacheDataService } not found in exported names" in "gudhub-node-server"
3
3
  import { AppHttpService } from "../httpService/AppHttpService.js";
4
4
  import { objectAssignWithOverride } from "../utils.js";
5
- import { IndexedDBService } from "./IndexedDBService.js";
5
+ import { IndexedDBManager, IndexedDBService } from "./IndexedDBService.js";
6
6
 
7
7
  import { IndexedDBChunkService } from "./IndexedDBChunkService.js";
8
8
  import { chunksConf } from "./chunkDataConf.js";
9
+ import { AppStoreManager } from "./StoreManager/managers.js";
10
+ import { dbName, dbVersion } from "./consts.js";
11
+ import { appsConf } from "./appDataConf.js";
9
12
 
10
13
 
11
14
  //this should be global in project
@@ -14,17 +17,159 @@ import { chunksConf } from "./chunkDataConf.js";
14
17
  // }
15
18
 
16
19
 
20
+
21
+ class IndexedDBManager1 {
22
+ constructor(dbName, storeName) {
23
+ this.dbName = dbName;
24
+ this.storeName = storeName;
25
+ this.db = null;
26
+ this.queue = [];
27
+ this.isProcessing = false;
28
+ }
29
+
30
+ async openDB() {
31
+ if (this.db) return this.db;
32
+
33
+ return new Promise((resolve, reject) => {
34
+ const request = indexedDB.open(this.dbName, 1);
35
+
36
+ request.onupgradeneeded = (event) => {
37
+ const db = event.target.result;
38
+ if (!db.objectStoreNames.contains(this.storeName)) {
39
+ db.createObjectStore(this.storeName, { keyPath: 'id', autoIncrement: true });
40
+ }
41
+ };
42
+
43
+ request.onsuccess = (event) => {
44
+ this.db = event.target.result;
45
+ resolve(this.db);
46
+ };
47
+
48
+ request.onerror = (event) => {
49
+ reject(event.target.error);
50
+ };
51
+ });
52
+ }
53
+
54
+ async executeOperation(operation) {
55
+ await this.openDB();
56
+ return new Promise((resolve, reject) => {
57
+ const transaction = this.db.transaction(this.storeName, 'readwrite');
58
+ const store = transaction.objectStore(this.storeName);
59
+
60
+ transaction.oncomplete = () => resolve();
61
+ transaction.onerror = (event) => reject(event.target.error);
62
+
63
+ operation(store);
64
+ });
65
+ }
66
+
67
+ async enqueueOperation(operation) {
68
+ this.queue.push(operation);
69
+
70
+ if (!this.isProcessing) {
71
+ this.isProcessing = true;
72
+ while (this.queue.length > 0) {
73
+ const currentOperation = this.queue.shift();
74
+ try {
75
+ await this.executeOperation(currentOperation);
76
+ } catch (error) {
77
+ console.error('Operation failed:', error);
78
+ }
79
+ }
80
+ this.isProcessing = false;
81
+ }
82
+ }
83
+ }
84
+
85
+
86
+
87
+
88
+
89
+ class IndexedDBManager2 {
90
+ constructor(dbName, storeName) {
91
+ this.dbName = dbName;
92
+ this.storeName = storeName;
93
+ this.db = null;
94
+ this.queue = [];
95
+ this.isProcessing = false;
96
+ }
97
+
98
+ async openDB() {
99
+ if (this.db) return this.db;
100
+
101
+ return new Promise((resolve, reject) => {
102
+ const request = indexedDB.open(this.dbName, 1);
103
+
104
+ request.onupgradeneeded = (event) => {
105
+ const db = event.target.result;
106
+ if (!db.objectStoreNames.contains(this.storeName)) {
107
+ db.createObjectStore(this.storeName, { keyPath: 'id', autoIncrement: true });
108
+ }
109
+ };
110
+
111
+ request.onsuccess = (event) => {
112
+ this.db = event.target.result;
113
+ resolve(this.db);
114
+ };
115
+
116
+ request.onerror = (event) => {
117
+ reject(event.target.error);
118
+ };
119
+ });
120
+ }
121
+
122
+ async executeOperation(operation) {
123
+ await this.openDB();
124
+ return new Promise((resolve, reject) => {
125
+ const transaction = this.db.transaction(this.storeName, 'readwrite');
126
+ const store = transaction.objectStore(this.storeName);
127
+
128
+ transaction.oncomplete = () => resolve();
129
+ transaction.onerror = (event) => reject(event.target.error);
130
+
131
+ operation(store);
132
+ });
133
+ }
134
+
135
+ async enqueueOperation(operation) {
136
+ this.queue.push(operation);
137
+
138
+ if (!this.isProcessing) {
139
+ this.isProcessing = true;
140
+ while (this.queue.length > 0) {
141
+ const currentOperation = this.queue.shift();
142
+ try {
143
+ await this.executeOperation(currentOperation);
144
+ } catch (error) {
145
+ console.error('Operation failed:', error);
146
+ }
147
+ }
148
+ this.isProcessing = false;
149
+ }
150
+ }
151
+ }
152
+
153
+
154
+
17
155
  //todo андрей сказал чанки два раза грузятся
18
156
  //при первой загрузке когда нет бд ничего не отображается
19
157
  //андрей предлагает грохать бд если проблемы со сторами
20
158
 
159
+
160
+ // может поменять имя на StoreAppService???? потому что я уже сделал абстракцию
161
+
21
162
  export class IndexedDBAppServiceForWorker extends AppDataService {
22
163
  constructor(req, conf, gudhub) {
23
164
  super(req, conf, gudhub);
24
165
 
25
166
  this.dataService = new AppHttpService(req, conf, gudhub);
26
167
 
27
- let indexDBService = new IndexedDBService(conf);
168
+ // let indexDBService = new IndexedDBService(conf);
169
+ // let indexDBAppStoreManager = new AppStoreManager;
170
+
171
+ this.requestCache = new Map; // id -> promise ///TODO seems irrelevant to class IndexedDBManager (could be renamed to Facade already)
172
+
28
173
 
29
174
  this.gudhub = gudhub;
30
175
 
@@ -33,17 +178,29 @@ export class IndexedDBAppServiceForWorker extends AppDataService {
33
178
 
34
179
 
35
180
 
181
+ //TODO objectAssignWithOverride here too?
182
+ // this.dbManager = new IndexedDBManager('myDatabase', 'appsStore');
183
+
184
+
185
+
36
186
  //TODO also think how to use AppDataService without overriding chunkDataService here. seems it gets http chunk service for worker somehow
37
187
  // maybe IS_WEB is wrong inside worker
38
188
 
39
189
 
40
190
 
191
+ // this.db = null;
192
+ // this.queue = [];
193
+ // this.isProcessing = false;
41
194
 
42
195
 
43
196
 
44
197
  //todo worker pool?
45
198
 
46
- objectAssignWithOverride(this, indexDBService);
199
+ this.storeManager = new AppStoreManager;
200
+
201
+
202
+ // objectAssignWithOverride(this, indexDBService);
203
+ // objectAssignWithOverride(this, new AppStoreManager);
47
204
  }
48
205
 
49
206
 
@@ -90,6 +247,63 @@ export class IndexedDBAppServiceForWorker extends AppDataService {
90
247
 
91
248
 
92
249
 
250
+ // }
251
+
252
+
253
+ //TODO для чанков с индексддб будет точно такой код поєтому может вынести его в какое-то место? например в отдельный класс
254
+
255
+
256
+ // async executeOperation(operation) {
257
+ // await this.openDB();
258
+ // return new Promise((resolve, reject) => {
259
+ // const transaction = this.db.transaction(this.storeName, 'readwrite');
260
+ // const store = transaction.objectStore(this.storeName);
261
+
262
+ // transaction.oncomplete = () => resolve();
263
+ // transaction.onerror = (event) => reject(event.target.error);
264
+
265
+ // operation(store);
266
+ // });
267
+ // }
268
+
269
+ // async enqueueOperation(operation) {
270
+ // this.queue.push(operation);
271
+
272
+ // if (!this.isProcessing) {
273
+ // this.isProcessing = true;
274
+ // while (this.queue.length > 0) {
275
+ // const currentOperation = this.queue.shift();
276
+ // try {
277
+ // await this.executeOperation(currentOperation);
278
+ // } catch (error) {
279
+ // console.error('Operation failed:', error);
280
+ // }
281
+ // }
282
+ // this.isProcessing = false;
283
+ // }
284
+ // }
285
+
286
+ // async addData(data) {
287
+ // await this.enqueueOperation((store) => {
288
+ // store.add(data);
289
+ // });
290
+ // }
291
+
292
+ // async updateData(id, updatedData) {
293
+ // await this.enqueueOperation((store) => {
294
+ // const request = store.get(id);
295
+ // request.onsuccess = () => {
296
+ // const data = request.result;
297
+ // Object.assign(data, updatedData);
298
+ // store.put(data);
299
+ // };
300
+ // });
301
+ // }
302
+
303
+ // async deleteData(id) {
304
+ // await this.enqueueOperation((store) => {
305
+ // store.delete(id);
306
+ // });
93
307
  // }
94
308
 
95
309
  async getApp(id) {
@@ -103,15 +317,17 @@ export class IndexedDBAppServiceForWorker extends AppDataService {
103
317
  let processedData = await self.processRequestedApp(id, data);//here
104
318
 
105
319
 
106
- self.putApp(id, processedData);
320
+ await self.putApp(id, processedData);
107
321
 
108
322
 
109
323
  return processedData;
110
324
 
111
325
  let pr = new Promise(async (resolve, reject) => {
112
326
  try {
113
- const db = await self.openDatabase();
114
- const transaction = db.transaction(self.store, "readonly");
327
+ await self.openDatabase();
328
+
329
+
330
+ const transaction = self.db.transaction(self.store, "readonly");
115
331
  const store = transaction.objectStore(self.store);
116
332
 
117
333
  const storeRequest = store.get(id);
@@ -191,12 +407,16 @@ export class IndexedDBAppServiceForWorker extends AppDataService {
191
407
 
192
408
  async getCached(id) {
193
409
 
410
+ return this.storeManager.getAppStoreManagerVersion(id); //TODO investigate method name
411
+
412
+
194
413
  let self = this;
195
414
 
196
415
  let pr = new Promise(async (resolve, reject) => {
197
416
  try {
198
- const db = await self.openDatabase();
199
- const transaction = db.transaction(self.store, "readonly");
417
+ await self.openDatabase();
418
+
419
+ const transaction = self.db.transaction(self.store, "readonly");
200
420
  const store = transaction.objectStore(self.store);
201
421
 
202
422
  const storeRequest = store.get(id);
@@ -228,30 +448,37 @@ export class IndexedDBAppServiceForWorker extends AppDataService {
228
448
  }
229
449
 
230
450
  async putApp(id, data) {
231
- try {
232
- const db = await this.openDatabase();
451
+ return this.storeManager.addApp(id, data);
233
452
 
234
- const transaction = db.transaction(this.store, "readwrite");
235
- const store = transaction.objectStore(this.store);
453
+ // try {
454
+ // await this.openDatabase();
236
455
 
237
- store.put(data, id);
238
- } catch (error) {
456
+ // const transaction = this.db.transaction(this.store, "readwrite");
457
+ // const store = transaction.objectStore(this.store);
239
458
 
240
- }
459
+ // store.put(data, id);
460
+ // } catch (error) {
461
+
462
+ // }
241
463
  }
242
464
 
243
- async openDatabase() {
244
- return new Promise((resolve, reject) => {
245
- const request = indexedDB.open(this.dbName, this.dbVersion);
465
+ // duplicated code 2 times in 2 classes openDatabase
246
466
 
247
- request.onsuccess = event => {
248
- resolve(event.target.result);
249
- };
250
- request.onerror = event => {
251
- reject(event.target.error);
252
- };
253
- });
254
- }
467
+ // async openDatabase() {
468
+ // if (this.db) return this.db;
469
+
470
+ // return new Promise((resolve, reject) => {
471
+ // const request = indexedDB.open(this.dbName, this.dbVersion);
472
+
473
+ // request.onsuccess = event => {
474
+ // this.db = event.target.result;
475
+ // resolve(this.db);
476
+ // };
477
+ // request.onerror = event => {
478
+ // reject(event.target.error);
479
+ // };
480
+ // });
481
+ // }
255
482
  }
256
483
 
257
484
 
@@ -261,7 +488,10 @@ export class IndexedDBAppService extends AppDataService {
261
488
 
262
489
  this.dataService = new AppHttpService(req, conf, gudhub);
263
490
 
264
- let indexDBService = new IndexedDBService(conf);
491
+ // let indexDBService = new IndexedDBService(conf);
492
+
493
+ this.requestCache = new Map; // id -> promise ///TODO seems irrelevant to class IndexedDBManager (could be renamed to Facade already)
494
+
265
495
 
266
496
  this.gudhub = gudhub;
267
497
 
@@ -272,7 +502,8 @@ export class IndexedDBAppService extends AppDataService {
272
502
 
273
503
  // this.appRequestAndProcessWorker = new Worker('./appRequestWorker.js');
274
504
  // this.appRequestAndProcessWorker = new Worker(new URL('./appRequestWorker.js', import.meta.url));
275
- this.appRequestAndProcessWorker = new Worker('./node_modules/@gudhub/core/umd/appRequestWorker.js');
505
+ // this.appRequestAndProcessWorker = new Worker('node_modules/@gudhub/core/umd/appRequestWorker.js');
506
+ this.appRequestAndProcessWorker = new Worker('override_npm/gudhub/umd/appRequestWorker.js');
276
507
  // this.appRequestAndProcessWorker = new AppRequestWorker();
277
508
 
278
509
  // new Worker(new URL('./worker.js', import.meta.url));
@@ -349,7 +580,11 @@ export class IndexedDBAppService extends AppDataService {
349
580
 
350
581
  //todo worker pool?
351
582
 
352
- objectAssignWithOverride(this, indexDBService);
583
+
584
+ this.storeManager = new AppStoreManager;
585
+
586
+ // objectAssignWithOverride(this, indexDBService);
587
+ // objectAssignWithOverride(this, new AppStoreManager());
353
588
  }
354
589
 
355
590
 
@@ -371,7 +606,7 @@ export class IndexedDBAppService extends AppDataService {
371
606
  return true;
372
607
  }
373
608
 
374
- async getApp(id) {
609
+ async getApp(id) {//TODO -> get?
375
610
  if (this.requestCache.has(id)) return this.requestCache.get(id);
376
611
 
377
612
 
@@ -461,14 +696,18 @@ export class IndexedDBAppService extends AppDataService {
461
696
 
462
697
  //TODO page isnt loaded when indexeddb empty. Investigate getApp in IndexedDBAppService here
463
698
 
464
- async getCached(id) {
699
+ async getCached(id) {//TODO -> getFromStore?
700
+
701
+
702
+ return this.storeManager.getAppStoreManagerVersion(id); //TODO investigate method name
465
703
 
466
704
  let self = this;
467
705
 
468
706
  let pr = new Promise(async (resolve, reject) => {
469
707
  try {
470
- const db = await self.openDatabase();
471
- const transaction = db.transaction(self.store, "readonly");
708
+ await self.openDatabase();
709
+
710
+ const transaction = self.db.transaction(self.store, "readonly");
472
711
  const store = transaction.objectStore(self.store);
473
712
 
474
713
  const storeRequest = store.get(id);
@@ -500,43 +739,56 @@ export class IndexedDBAppService extends AppDataService {
500
739
  }
501
740
 
502
741
  async has(id) {
742
+
503
743
  try {
504
- let cached = await this.getCached(id);
744
+
745
+ return this.storeManager.hasApp(id);
746
+ } catch (error) {
747
+ debugger
748
+ }
505
749
 
506
- if (!cached) return false;
750
+ // try {
751
+ // let cached = await this.getCached(id);
507
752
 
508
- return true;
509
- } catch(e) {
510
- //todo check error type and then rethrow maybe here
753
+ // if (!cached) return false;
511
754
 
512
- return false;
513
- }
755
+ // return true;
756
+ // } catch(e) {
757
+ // //todo check error type and then rethrow maybe here
758
+
759
+ // return false;
760
+ // }
514
761
  }
515
762
 
516
- async putApp(id, data) {
517
- try {
518
- const db = await this.openDatabase();
763
+ async putApp(id, data) { //TODO -> put?
764
+ return this.storeManager.addApp(id, data);
519
765
 
520
- const transaction = db.transaction(this.store, "readwrite");
521
- const store = transaction.objectStore(this.store);
766
+ // try {
767
+ // const db = await this.openDatabase();
522
768
 
523
- store.put(data, id);
524
- } catch (error) {
769
+ // const transaction = db.transaction(this.store, "readwrite");
770
+ // const store = transaction.objectStore(this.store);
525
771
 
526
- }
772
+ // store.put(data, id);
773
+ // } catch (error) {
774
+
775
+ // }
527
776
  }
528
777
 
529
778
  //todo openDatabase only once and then preserve it for further usage
530
- async openDatabase() {
531
- return new Promise((resolve, reject) => {
532
- const request = indexedDB.open(this.dbName, this.dbVersion);
533
-
534
- request.onsuccess = event => {
535
- resolve(event.target.result);
536
- };
537
- request.onerror = event => {
538
- reject(event.target.error);
539
- };
540
- });
541
- }
779
+ // async openDatabase() {
780
+ // if (this.db) return this.db;
781
+
782
+ // return new Promise((resolve, reject) => {
783
+ // const request = indexedDB.open(this.dbName, this.dbVersion);
784
+
785
+ // request.onsuccess = event => {
786
+ // this.db = event.target.result;
787
+ // resolve(this.db);
788
+ // };
789
+ // request.onerror = event => {
790
+ // reject(event.target.error);
791
+ // };
792
+ // });
793
+ // }
542
794
  }
@@ -2,6 +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
6
 
6
7
 
7
8
  //this should be global in project
@@ -22,11 +23,19 @@ export class IndexedDBChunkService extends ChunkDataService {
22
23
 
23
24
  this.dataService = new ChunkHttpService(req);
24
25
 
25
- let indexDBService = new IndexedDBService(conf);
26
+ // let indexDBService = new IndexedDBService(conf);
27
+
28
+ this.requestCache = new Map; // id -> promise ///TODO seems irrelevant to class IndexedDBManager (could be renamed to Facade already)
29
+
26
30
 
27
31
  this.gudhub = gudhub;
28
32
 
29
- objectAssignWithOverride(this, indexDBService);
33
+
34
+ this.storeManager = new ChunksStoreManager;
35
+
36
+
37
+ // objectAssignWithOverride(this, indexDBService);
38
+ // objectAssignWithOverride(this, new ChunksStoreManager);
30
39
  }
31
40
 
32
41
  //TODO use IndexedDBFacade here
@@ -44,20 +53,22 @@ export class IndexedDBChunkService extends ChunkDataService {
44
53
  }
45
54
  }
46
55
 
47
-
56
+ //todo also optimize chunks transactions etc
48
57
 
49
58
 
50
59
  async putChunk(id, data) {
51
- try {
52
- const db = await this.openDatabase();
60
+ return this.storeManager.addChunk(id, data);
53
61
 
54
- const transaction = db.transaction(this.store, "readwrite");
55
- const store = transaction.objectStore(this.store);
62
+ // try {
63
+ // const db = await this.openDatabase(); //here
56
64
 
57
- store.put(data, id);
58
- } catch (error) {
65
+ // const transaction = db.transaction(this.store, "readwrite");
66
+ // const store = transaction.objectStore(this.store);
59
67
 
60
- }
68
+ // store.put(data, id);
69
+ // } catch (error) {
70
+
71
+ // }
61
72
  }
62
73
 
63
74
 
@@ -120,39 +131,42 @@ export class IndexedDBChunkService extends ChunkDataService {
120
131
  if (this.requestCache.has(id)) return this.requestCache.get(id);
121
132
 
122
133
  try {
123
- let self = this;
124
134
 
125
- let pr = new Promise(async (resolve, reject) => {
126
- try {
127
- const db = await self.openDatabase();
128
- const transaction = db.transaction(self.store, "readonly");
129
- const store = transaction.objectStore(self.store);
135
+ let pr = this.storeManager.getChunkStoreManagerVersion(id);
136
+
137
+ // let self = this;
138
+
139
+ // let pr = new Promise(async (resolve, reject) => {
140
+ // try {
141
+ // const db = await self.openDatabase();
142
+ // const transaction = db.transaction(self.store, "readonly");
143
+ // const store = transaction.objectStore(self.store);
130
144
 
131
- const storeRequest = store.get(id);
145
+ // const storeRequest = store.get(id);
132
146
 
133
- storeRequest.onsuccess = (e) => {
147
+ // storeRequest.onsuccess = (e) => {
134
148
 
135
- let cachedData = e.target.result;
149
+ // let cachedData = e.target.result;
136
150
 
137
- if (
138
- !cachedData
139
- ) {
140
- reject();
141
- }
151
+ // if (
152
+ // !cachedData
153
+ // ) {
154
+ // reject();
155
+ // }
142
156
 
143
- if (
144
- cachedData
145
- ) {
157
+ // if (
158
+ // cachedData
159
+ // ) {
146
160
 
147
- resolve(cachedData);
148
- }
149
- };
161
+ // resolve(cachedData);
162
+ // }
163
+ // };
150
164
 
151
- storeRequest.onerror = reject
152
- } catch (error) {
153
- reject();
154
- }
155
- });
165
+ // storeRequest.onerror = reject
166
+ // } catch (error) {
167
+ // reject();
168
+ // }
169
+ // });
156
170
 
157
171
  self.requestCache.set(id, pr);
158
172
 
@@ -174,16 +188,16 @@ export class IndexedDBChunkService extends ChunkDataService {
174
188
  }
175
189
  }
176
190
 
177
- async openDatabase() {
178
- return new Promise((resolve, reject) => {
179
- const request = indexedDB.open(this.dbName, this.dbVersion);
180
-
181
- request.onsuccess = event => {
182
- resolve(event.target.result);
183
- };
184
- request.onerror = event => {
185
- reject(event.target.error);
186
- };
187
- });
188
- }
191
+ // async openDatabase() {
192
+ // return new Promise((resolve, reject) => {
193
+ // const request = indexedDB.open(this.dbName, this.dbVersion);
194
+
195
+ // request.onsuccess = event => {
196
+ // resolve(event.target.result);
197
+ // };
198
+ // request.onerror = event => {
199
+ // reject(event.target.error);
200
+ // };
201
+ // });
202
+ // }
189
203
  }