@gudhub/core 1.2.4-beta.1 → 1.2.4-beta.11

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.
@@ -4,31 +4,56 @@ import { AppHttpService } from "../httpService/AppHttpService.js";
4
4
  import { objectAssignWithOverride } from "../utils.js";
5
5
  import { IndexedDBService } from "./IndexedDBService.js";
6
6
 
7
+ import { IndexedDBChunkService } from "./IndexedDBChunkService.js";
8
+ import { chunksConf } from "./chunkDataConf.js";
9
+
7
10
 
8
11
  //this should be global in project
9
12
  // class IndexedDBFacade extends CacheService {
10
13
 
11
14
  // }
12
15
 
13
- export class IndexedDBAppService extends AppDataService {
14
- constructor(req, conf) {
15
- super(req, conf);
16
+
17
+ //todo андрей сказал чанки два раза грузятся
18
+ //при первой загрузке когда нет бд ничего не отображается
19
+ //андрей предлагает грохать бд если проблемы со сторами
20
+
21
+ export class IndexedDBAppServiceForWorker extends AppDataService {
22
+ constructor(req, conf, gudhub) {
23
+ super(req, conf, gudhub);
16
24
 
17
- this.dataService = new AppHttpService(req);
25
+ this.dataService = new AppHttpService(req, conf, gudhub);
18
26
 
19
27
  let indexDBService = new IndexedDBService(conf);
20
28
 
29
+ this.gudhub = gudhub;
30
+
31
+
32
+ this.chunkDataService = new IndexedDBChunkService(req, chunksConf, gudhub);
33
+
34
+
35
+
36
+ //TODO also think how to use AppDataService without overriding chunkDataService here. seems it gets http chunk service for worker somehow
37
+ // maybe IS_WEB is wrong inside worker
38
+
39
+
40
+
41
+
42
+
43
+
44
+ //todo worker pool?
45
+
21
46
  objectAssignWithOverride(this, indexDBService);
22
47
  }
23
48
 
24
49
 
25
50
  static [Symbol.hasInstance](instance) {
26
51
  try {
27
- if (instance instanceof IndexedDBAppService) return true;
52
+ if (instance instanceof IndexedDBAppServiceForWorker) return true;
28
53
  if (instance instanceof IndexedDBService) return true; //TODO requires multiple inheritance, thhink how to overcome it
29
54
  return false;
30
55
  } catch (error) {
31
- return false;
56
+ return false;
32
57
  }
33
58
  }
34
59
 
@@ -36,58 +61,170 @@ export class IndexedDBAppService extends AppDataService {
36
61
  // indexDBAccess = new IndexedDBFacade;
37
62
 
38
63
 
39
- async getApp(id) {
40
- if (this.requestCache.has(id)) return this.requestCache.get(id);
64
+ isWithCache() {
65
+ return true;
66
+ }
41
67
 
42
- let dataServiceRequest = this.dataService.getApp(id);
68
+ // getApp(id) {
69
+ // let worker = new Worker('../../appRequestWorker.js');
43
70
 
44
- dataServiceRequest.then(data => this.putApp(id, data));
45
71
 
46
- try {
47
- let self = this;
72
+ // worker.postMessage({ type: 'init', gudhubSerialized: this.gudhub.serialized });
48
73
 
49
- let pr = new Promise(async (resolve, reject) => {
50
- try {
51
- const db = await self.openDatabase();
52
- const transaction = db.transaction(self.store, "readonly");
53
- const store = transaction.objectStore(self.store);
54
-
55
- const storeRequest = store.get(id);
74
+ // // Send the main data to be processed
75
+ // const mainData = { /* your data here */ };
76
+ // worker.postMessage({ type: 'processData', data: mainData });
77
+
78
+ // worker.onmessage = function(e) {
79
+ // console.log('Data processed by worker:', e.data);
80
+ // };
81
+
82
+ // worker.onerror = function(e) {
83
+ // console.error('Error in worker:', e);
84
+ // };
56
85
 
57
- storeRequest.onsuccess = (e) => {
58
-
59
- let cachedData = e.target.result;
60
-
61
- if (
62
- !cachedData
63
- ) {
64
- reject();
65
- }
86
+
87
+ // worker.onmessage = (msgEv) => {
66
88
 
67
- if (
68
- cachedData
69
- ) {
70
- resolve(cachedData);
71
- }
72
- };
89
+ // }
90
+
91
+
92
+
93
+ // }
94
+
95
+ async getApp(id) {
96
+ // if (this.requestCache.has(id)) return this.requestCache.get(id);
97
+
98
+ let self = this;
99
+
100
+ // let dataServiceRequest = this.dataService.getAppWithoutProcessing(id);
101
+ let data = await this.dataService.getAppWithoutProcessing(id);
102
+
103
+ let processedData = await self.processRequestedApp(id, data);//here
104
+
105
+
106
+ self.putApp(id, processedData);
107
+
108
+
109
+ return processedData;
110
+
111
+ let pr = new Promise(async (resolve, reject) => {
112
+ try {
113
+ const db = await self.openDatabase();
114
+ const transaction = db.transaction(self.store, "readonly");
115
+ const store = transaction.objectStore(self.store);
73
116
 
74
- storeRequest.onerror = reject
75
- } catch (error) {
76
- reject();
77
- }
78
- });
117
+ const storeRequest = store.get(id);
79
118
 
80
- self.requestCache.set(id, pr);
119
+ storeRequest.onsuccess = (e) => {
120
+
121
+ let cachedData = e.target.result;
122
+
123
+ if (
124
+ !cachedData
125
+ ) {
126
+ dataServiceRequest.then(async data => {
127
+ let processedData = await self.processRequestedApp(id, data);//here
128
+
129
+
130
+ // let comparison = this.gudhub.util.compareItems([], processedData.items_list);
131
+
132
+ // let cached
81
133
 
82
- await pr;
134
+ resolve(processedData);
135
+
136
+ self.putApp(id, processedData);
137
+
138
+ }, reject);
139
+ }
83
140
 
84
- return pr;
141
+ if (
142
+ cachedData
143
+ ) {
144
+ // resolve(cachedData);
85
145
 
86
- } catch(e) {
87
- this.requestCache.set(id, dataServiceRequest);
146
+ dataServiceRequest.then(async data => {
147
+ let processedData = await self.processRequestedApp(id, data);//here
88
148
 
89
- return dataServiceRequest;
90
- }
149
+ // let comparison = this.gudhub.util.compareItems(cachedData.items_list, processedData.items_list);
150
+
151
+ resolve(processedData);
152
+ self.putApp(id, processedData);
153
+ });
154
+ }
155
+ };
156
+
157
+ storeRequest.onerror = () => {
158
+ dataServiceRequest.then(async data => {
159
+ let processedData = await self.processRequestedApp(id, data);//here
160
+
161
+ // let comparison = this.gudhub.util.compareItems([], processedData.items_list);
162
+
163
+ // let cached
164
+
165
+ resolve(processedData);
166
+
167
+ self.putApp(id, processedData);
168
+
169
+ }, reject);
170
+ }
171
+ } catch (error) {
172
+ dataServiceRequest.then(async data => {
173
+ let processedData = await self.processRequestedApp(id, data);//here
174
+
175
+ // let comparison = this.gudhub.util.compareItems([], processedData.items_list);
176
+
177
+ // let cached
178
+
179
+ resolve(processedData);
180
+
181
+ self.putApp(id, processedData);
182
+
183
+ }, reject);
184
+ }
185
+ });
186
+
187
+ // self.requestCache.set(id, pr);
188
+
189
+ return pr;
190
+ }
191
+
192
+ async getCached(id) {
193
+
194
+ let self = this;
195
+
196
+ let pr = new Promise(async (resolve, reject) => {
197
+ try {
198
+ const db = await self.openDatabase();
199
+ const transaction = db.transaction(self.store, "readonly");
200
+ const store = transaction.objectStore(self.store);
201
+
202
+ const storeRequest = store.get(id);
203
+
204
+ storeRequest.onsuccess = (e) => {
205
+
206
+ let cachedData = e.target.result;
207
+
208
+ if (
209
+ !cachedData
210
+ ) {
211
+ reject();
212
+ }
213
+
214
+ if (
215
+ cachedData
216
+ ) {
217
+ resolve(cachedData);
218
+ }
219
+ };
220
+
221
+ storeRequest.onerror = reject;
222
+ } catch (error) {
223
+ reject();
224
+ }
225
+ });
226
+
227
+ return pr;
91
228
  }
92
229
 
93
230
  async putApp(id, data) {
@@ -115,4 +252,291 @@ export class IndexedDBAppService extends AppDataService {
115
252
  };
116
253
  });
117
254
  }
255
+ }
256
+
257
+
258
+ export class IndexedDBAppService extends AppDataService {
259
+ constructor(req, conf, gudhub) {
260
+ super(req, conf, gudhub);
261
+
262
+ this.dataService = new AppHttpService(req, conf, gudhub);
263
+
264
+ let indexDBService = new IndexedDBService(conf);
265
+
266
+ this.gudhub = gudhub;
267
+
268
+
269
+ this.resolveFnMap = new Map; // id -> resolve
270
+
271
+
272
+
273
+ // this.appRequestAndProcessWorker = new Worker('./appRequestWorker.js');
274
+ // this.appRequestAndProcessWorker = new Worker(new URL('./appRequestWorker.js', import.meta.url));
275
+ this.appRequestAndProcessWorker = new Worker('node_modules/@gudhub/core/umd/appRequestWorker.js');
276
+ // this.appRequestAndProcessWorker = new AppRequestWorker();
277
+
278
+ // new Worker(new URL('./worker.js', import.meta.url));
279
+
280
+
281
+ this.appRequestAndProcessWorker.postMessage({ type: 'init', gudhubSerialized: this.gudhub.serialized });
282
+
283
+
284
+ let self = this;
285
+ this.appRequestAndProcessWorker.onerror = function(e) {
286
+ // self.appRequestAndProcessWorker.terminate();
287
+
288
+
289
+ // console.error('Error in worker:', e);
290
+ };
291
+
292
+
293
+ // let self = this;
294
+
295
+ this.appRequestAndProcessWorker.onmessage = function(e) {
296
+
297
+
298
+
299
+
300
+
301
+ if (
302
+ e.data.type == 'requestApp'
303
+ ) {
304
+
305
+ if (
306
+ self.resolveFnMap.has(e.data.id)
307
+ ) {
308
+ let resolver = self.resolveFnMap.get(e.data.id);
309
+
310
+ resolver(e.data.data);
311
+ }
312
+
313
+
314
+ // self.requestCache.set(id, new Promise);
315
+
316
+ }
317
+
318
+
319
+
320
+ if (
321
+ e.data.type == 'processData'
322
+ ) {
323
+ // if (id == e.data.id) {
324
+ self.gudhub.itemProcessor.handleDiff(e.data.id, e.data.compareRes);
325
+ // }
326
+ }
327
+
328
+
329
+
330
+
331
+
332
+
333
+ // worker.terminate();
334
+
335
+ // this.postMessage({id: e.data.id, compareRes: comparison});
336
+
337
+
338
+ // console.log('Data processed by worker:', e.data);
339
+ };
340
+
341
+ // worker.onerror = function(e) {
342
+ // // worker.terminate();
343
+
344
+
345
+ // // console.error('Error in worker:', e);
346
+ // };
347
+
348
+
349
+
350
+ //todo worker pool?
351
+
352
+ objectAssignWithOverride(this, indexDBService);
353
+ }
354
+
355
+
356
+ static [Symbol.hasInstance](instance) {
357
+ try {
358
+ if (instance instanceof IndexedDBAppService) return true;
359
+ if (instance instanceof IndexedDBService) return true; //TODO requires multiple inheritance, thhink how to overcome it
360
+ return false;
361
+ } catch (error) {
362
+ return false;
363
+ }
364
+ }
365
+
366
+ //TODO use IndexedDBFacade here
367
+ // indexDBAccess = new IndexedDBFacade;
368
+
369
+
370
+ isWithCache() {
371
+ return true;
372
+ }
373
+
374
+ async getApp(id) {
375
+ if (this.requestCache.has(id)) return this.requestCache.get(id);
376
+
377
+
378
+ let self = this;
379
+
380
+
381
+ let hasId = await this.has(id);
382
+
383
+
384
+ if (
385
+ hasId
386
+ ) {
387
+ let getFromDBPrms = this.getCached(id);
388
+
389
+ this.requestCache.set(id, getFromDBPrms);
390
+
391
+ this.appRequestAndProcessWorker.postMessage({ type: 'processData', id: id });
392
+
393
+ return getFromDBPrms;
394
+ } else {
395
+ // this.requestCache.set(id, new Promise);
396
+
397
+ let prms = new Promise((resolve, reject) => {
398
+ self.resolveFnMap.set(id, resolve);
399
+ });
400
+
401
+
402
+ this.requestCache.set(id, prms);
403
+
404
+ //todo define types as constants
405
+ this.appRequestAndProcessWorker.postMessage({ type: 'requestApp', id: id });
406
+
407
+
408
+
409
+ return prms;
410
+
411
+ //todo somehow handle answer from worker
412
+
413
+ //maybe use mesage library to communicate with worker
414
+ }
415
+
416
+
417
+
418
+
419
+ // let worker = new Worker('./appRequestWorker.js');
420
+
421
+
422
+ // worker.postMessage({ type: 'init', gudhubSerialized: this.gudhub.serialized });
423
+
424
+ // Send the main data to be processed
425
+ // const mainData = { /* your data here */ };
426
+ // this.appRequestAndProcessWorker.postMessage({ type: 'processData', id: id });
427
+
428
+ // let self = this;
429
+
430
+ // worker.onmessage = function(e) {
431
+
432
+
433
+ // if (id == e.data.id) {
434
+ // self.gudhub.itemProcessor.handleDiff(e.data.id, e.data.compareRes);
435
+ // }
436
+
437
+
438
+
439
+ // // worker.terminate();
440
+
441
+ // // this.postMessage({id: e.data.id, compareRes: comparison});
442
+
443
+
444
+ // // console.log('Data processed by worker:', e.data);
445
+ // };
446
+
447
+ // worker.onerror = function(e) {
448
+ // // worker.terminate();
449
+
450
+
451
+ // // console.error('Error in worker:', e);
452
+ // };
453
+
454
+
455
+ // return getFromDBPrms;
456
+
457
+
458
+
459
+ }
460
+
461
+
462
+ //TODO page isnt loaded when indexeddb empty. Investigate getApp in IndexedDBAppService here
463
+
464
+ async getCached(id) {
465
+
466
+ let self = this;
467
+
468
+ let pr = new Promise(async (resolve, reject) => {
469
+ try {
470
+ const db = await self.openDatabase();
471
+ const transaction = db.transaction(self.store, "readonly");
472
+ const store = transaction.objectStore(self.store);
473
+
474
+ const storeRequest = store.get(id);
475
+
476
+ storeRequest.onsuccess = (e) => {
477
+
478
+ let cachedData = e.target.result;
479
+
480
+ if (
481
+ !cachedData
482
+ ) {
483
+ reject();
484
+ }
485
+
486
+ if (
487
+ cachedData
488
+ ) {
489
+ resolve(cachedData);
490
+ }
491
+ };
492
+
493
+ storeRequest.onerror = reject;
494
+ } catch (error) {
495
+ reject();
496
+ }
497
+ });
498
+
499
+ return pr;
500
+ }
501
+
502
+ async has(id) {
503
+ try {
504
+ let cached = await this.getCached(id);
505
+
506
+ if (!cached) return false;
507
+
508
+ return true;
509
+ } catch(e) {
510
+ //todo check error type and then rethrow maybe here
511
+
512
+ return false;
513
+ }
514
+ }
515
+
516
+ async putApp(id, data) {
517
+ try {
518
+ const db = await this.openDatabase();
519
+
520
+ const transaction = db.transaction(this.store, "readwrite");
521
+ const store = transaction.objectStore(this.store);
522
+
523
+ store.put(data, id);
524
+ } catch (error) {
525
+
526
+ }
527
+ }
528
+
529
+ //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
+ }
118
542
  }
@@ -17,14 +17,15 @@ import { IndexedDBService } from "./IndexedDBService.js";
17
17
 
18
18
 
19
19
  export class IndexedDBChunkService extends ChunkDataService {
20
- constructor(req, conf) {
20
+ constructor(req, conf, gudhub) {
21
21
  super(req, conf);
22
22
 
23
23
  this.dataService = new ChunkHttpService(req);
24
24
 
25
-
26
25
  let indexDBService = new IndexedDBService(conf);
27
26
 
27
+ this.gudhub = gudhub;
28
+
28
29
  objectAssignWithOverride(this, indexDBService);
29
30
  }
30
31
 
@@ -60,6 +61,61 @@ export class IndexedDBChunkService extends ChunkDataService {
60
61
  }
61
62
 
62
63
 
64
+ // async getApp(id) {
65
+ // if (this.requestCache.has(id)) return this.requestCache.get(id);
66
+
67
+ // let self = this;
68
+
69
+ // let dataServiceRequest = this.dataService.getApp(id);
70
+
71
+ // let pr = new Promise(async (resolve, reject) => {
72
+ // try {
73
+ // const db = await self.openDatabase();
74
+ // const transaction = db.transaction(self.store, "readonly");
75
+ // const store = transaction.objectStore(self.store);
76
+
77
+ // const storeRequest = store.get(id);
78
+
79
+ // storeRequest.onsuccess = (e) => {
80
+
81
+ // let cachedData = e.target.result;
82
+
83
+ // if (
84
+ // !cachedData
85
+ // ) {
86
+ // dataServiceRequest.then(resolve, reject);
87
+ // dataServiceRequest.then(data => self.putApp(id, data));
88
+ // }
89
+
90
+ // if (
91
+ // cachedData
92
+ // ) {
93
+ // resolve(cachedData);
94
+
95
+ // dataServiceRequest.then(async (data) => {
96
+ // // self.gudhub.processAppUpd(data, cachedData);//
97
+ // await self.putApp(id, data);
98
+ // self.gudhub.triggerAppUpdate(id, prevVersion, newVersion);
99
+ // });
100
+ // }
101
+ // };
102
+
103
+ // storeRequest.onerror = () => {
104
+ // dataServiceRequest.then(resolve, reject);
105
+ // dataServiceRequest.then(data => self.putApp(id, data));
106
+ // }
107
+ // } catch (error) {
108
+ // dataServiceRequest.then(resolve, reject);
109
+ // dataServiceRequest.then(data => self.putApp(id, data));
110
+ // }
111
+ // });
112
+
113
+ // self.requestCache.set(id, pr);
114
+
115
+ // return pr;
116
+ // }
117
+
118
+
63
119
  async getChunk(app_id, id) {
64
120
  if (this.requestCache.has(id)) return this.requestCache.get(id);
65
121
 
@@ -112,6 +168,7 @@ export class IndexedDBChunkService extends ChunkDataService {
112
168
  let res = await reqPrms;
113
169
 
114
170
  this.putChunk(id, res);
171
+ // putPrms.then(() => self.gudhub.triggerAppUpdate(id));
115
172
 
116
173
  return reqPrms;
117
174
  }