@gudhub/core 1.2.4-beta.5 → 1.2.4-beta.7

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.
@@ -21,6 +21,8 @@ export class AppProcessor {
21
21
  this.util = util;
22
22
  this.appListeners();
23
23
  this.dataService = dataService;
24
+
25
+ this.appRequestCache = new Map;
24
26
  }
25
27
 
26
28
  async createNewAppApi(app) {
@@ -255,27 +257,42 @@ export class AppProcessor {
255
257
  let app = this.getAppFromStorage(app_id);
256
258
  if (app) return app;
257
259
 
258
- let receivedApp = await this.dataService.getApp(app_id);
259
- if (!receivedApp) return;
260
-
261
- //!!! temporary check !!!! if app has chanks property then we start getting it
262
- if (
263
- receivedApp.chunks &&
264
- receivedApp.chunks.length
265
- ) {
266
- let chunks = await this.chunksManager.getChunks(app_id, receivedApp.chunks);
267
- receivedApp.items_list = this.util.mergeChunks([
268
- ...chunks,
269
- receivedApp,
270
- ]);
271
- }
260
+ if (this.appRequestCache.has(app_id)) return this.appRequestCache.get(app_id);
261
+
262
+ let self = this;
263
+
264
+ let pr = new Promise(async (resolve, reject) => {
265
+ try {
266
+ let receivedApp = await self.dataService.getApp(app_id);
267
+ if (!receivedApp) reject();
268
+
269
+ //!!! temporary check !!!! if app has chanks property then we start getting it
270
+ if (
271
+ receivedApp.chunks &&
272
+ receivedApp.chunks.length
273
+ ) {
274
+ let chunks = await self.chunksManager.getChunks(app_id, receivedApp.chunks);
275
+ receivedApp.items_list = self.util.mergeChunks([
276
+ ...chunks,
277
+ receivedApp,
278
+ ]);
279
+ }
280
+
272
281
 
273
- this.saveAppInStorage(receivedApp);
274
- this.ws.addSubscription(app_id);
282
+ // This code for trash must be changed
283
+ // return trash ? app : {...app, items_list: app.items_list.filter(item => !item.trash)};
284
+ resolve(receivedApp);
285
+
286
+ self.saveAppInStorage(receivedApp);
287
+ self.ws.addSubscription(app_id);
288
+ } catch (error) {
289
+ reject();
290
+ }
291
+ });
275
292
 
276
- // This code for trash must be changed
277
- // return trash ? app : {...app, items_list: app.items_list.filter(item => !item.trash)};
278
- return receivedApp;
293
+ self.appRequestCache.set(app_id, pr);
294
+
295
+ return pr;
279
296
  }
280
297
 
281
298
  async updateApp(app) {
@@ -319,6 +336,10 @@ export class AppProcessor {
319
336
  }
320
337
  }
321
338
 
339
+ handleAppChange(v1, v2) {
340
+ // generate required events etc, do job
341
+ }
342
+
322
343
  clearAppProcessor() {
323
344
  this.getAppListPromises = null;
324
345
  this.getAppPromises = {};
@@ -11,13 +11,15 @@ import { IndexedDBService } from "./IndexedDBService.js";
11
11
  // }
12
12
 
13
13
  export class IndexedDBAppService extends AppDataService {
14
- constructor(req, conf) {
14
+ constructor(req, conf, gudhub) {
15
15
  super(req, conf);
16
16
 
17
17
  this.dataService = new AppHttpService(req);
18
18
 
19
19
  let indexDBService = new IndexedDBService(conf);
20
20
 
21
+ this.gudhub = gudhub;
22
+
21
23
  objectAssignWithOverride(this, indexDBService);
22
24
  }
23
25
 
@@ -28,7 +30,7 @@ export class IndexedDBAppService extends AppDataService {
28
30
  if (instance instanceof IndexedDBService) return true; //TODO requires multiple inheritance, thhink how to overcome it
29
31
  return false;
30
32
  } catch (error) {
31
- return false;
33
+ return false;
32
34
  }
33
35
  }
34
36
 
@@ -39,55 +41,54 @@ export class IndexedDBAppService extends AppDataService {
39
41
  async getApp(id) {
40
42
  if (this.requestCache.has(id)) return this.requestCache.get(id);
41
43
 
42
- let dataServiceRequest = this.dataService.getApp(id);
44
+ let self = this;
43
45
 
44
- dataServiceRequest.then(data => this.putApp(id, data));
45
-
46
- try {
47
- let self = this;
48
-
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);
46
+ let dataServiceRequest = this.dataService.getApp(id);
56
47
 
57
- storeRequest.onsuccess = (e) => {
58
-
59
- let cachedData = e.target.result;
60
-
61
- if (
62
- !cachedData
63
- ) {
64
- reject();
65
- }
48
+ let pr = new Promise(async (resolve, reject) => {
49
+ try {
50
+ const db = await self.openDatabase();
51
+ const transaction = db.transaction(self.store, "readonly");
52
+ const store = transaction.objectStore(self.store);
66
53
 
67
- if (
68
- cachedData
69
- ) {
70
- resolve(cachedData);
71
- }
72
- };
73
-
74
- storeRequest.onerror = reject
75
- } catch (error) {
76
- reject();
77
- }
78
- });
79
-
80
- self.requestCache.set(id, pr);
54
+ const storeRequest = store.get(id);
81
55
 
82
- await pr;
56
+ storeRequest.onsuccess = (e) => {
57
+
58
+ let cachedData = e.target.result;
83
59
 
84
- return pr;
85
-
86
- } catch(e) {
87
- this.requestCache.set(id, dataServiceRequest);
60
+ if (
61
+ !cachedData
62
+ ) {
63
+ dataServiceRequest.then(resolve, reject);
64
+ dataServiceRequest.then(data => self.putApp(id, data));
65
+ }
66
+
67
+ if (
68
+ cachedData
69
+ ) {
70
+ resolve(cachedData);
71
+
72
+ dataServiceRequest.then(data => {
73
+ self.gudhub.processAppUpd(data, cachedData);
74
+ self.putApp(id, data);
75
+ });
76
+ }
77
+ };
78
+
79
+ storeRequest.onerror = () => {
80
+ dataServiceRequest.then(resolve, reject);
81
+ dataServiceRequest.then(data => self.putApp(id, data));
82
+ }
83
+ } catch (error) {
84
+ dataServiceRequest.then(resolve, reject);
85
+ dataServiceRequest.then(data => self.putApp(id, data));
86
+ }
87
+ });
88
88
 
89
- return dataServiceRequest;
90
- }
89
+ self.requestCache.set(id, pr);
90
+
91
+ return pr;
91
92
  }
92
93
 
93
94
  async putApp(id, data) {
@@ -4,7 +4,7 @@ import { objectAssignWithOverride } from "../utils.js";
4
4
  import { HttpService } from "./HttpService.js";
5
5
 
6
6
  export class AppHttpService extends AppDataService {
7
- constructor(req, conf) {
7
+ constructor(req, conf, gudhub) {
8
8
  super();
9
9
 
10
10
  this.appApi = new AppAPI(req);
@@ -118,6 +118,13 @@ export class ItemProcessor {
118
118
  return items;
119
119
  }
120
120
 
121
+ handleItemsChange(v1, v2) {
122
+ let compareRes = this.util.compareItems(v1.items_list, v2.items_list);
123
+
124
+ this.updateItemsInStorage(v1.app_id, compareRes.diff_src_items);
125
+ this.addItemsToStorage(v1.app_id, compareRes.new_src_items);
126
+ }
127
+
121
128
  async deleteItemsFromStorage(app_id, itemsForDelete = []) {
122
129
  const app = await this.appProcessor.getApp(app_id);
123
130
  if (app) {
package/GUDHUB/gudhub.js CHANGED
@@ -74,7 +74,7 @@ export class GudHub {
74
74
  this.chunksManager,
75
75
  this.util,
76
76
  options.activateSW,
77
- new AppDataService(this.req, appDataServiceConf),
77
+ new AppDataService(this.req, appDataServiceConf, this),
78
78
  );
79
79
  this.itemProcessor = new ItemProcessor(
80
80
  this.storage,
@@ -234,6 +234,11 @@ export class GudHub {
234
234
  return this.util.sortItems(items, options);
235
235
  }
236
236
 
237
+ processAppUpd(v1, v2) {
238
+ this.itemProcessor.handleItemsChange(v1, v2);
239
+ this.appProcessor.handleAppChange(v1, v2);
240
+ }
241
+
237
242
  jsonToItems(json, map) {
238
243
  return this.util.jsonToItems(json, map);
239
244
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gudhub/core",
3
- "version": "1.2.4-beta.5",
3
+ "version": "1.2.4-beta.7",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -17,7 +17,7 @@
17
17
  "esmodules": true
18
18
  },
19
19
  "exclude": [
20
- "plugin-transform-classes"
20
+ "babel-plugin-transform-classes"
21
21
  ]
22
22
  }
23
23
  ]