@gudhub/core 1.2.4-beta.0 → 1.2.4-beta.10

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.
@@ -6,7 +6,7 @@ export class AppProcessor {
6
6
  pipeService,
7
7
  req,
8
8
  websocket,
9
- chunksManager,
9
+ // chunksManager,
10
10
  util,
11
11
  activateSW,
12
12
  dataService,
@@ -17,10 +17,15 @@ export class AppProcessor {
17
17
  this.ws = websocket;
18
18
  this.applistReceived = false;
19
19
  this.activateSW = activateSW; // we use this flag to check if applist was received. The problem is that if you receive an app it also goes to app_list as a result you have the app_list with one app.
20
- this.chunksManager = chunksManager;
20
+ // this.chunksManager = chunksManager;
21
21
  this.util = util;
22
22
  this.appListeners();
23
23
  this.dataService = dataService;
24
+
25
+ this.appRequestCache = new Map;
26
+
27
+
28
+ // this.dataServiceRequestedIdSet = new Set;
24
29
  }
25
30
 
26
31
  async createNewAppApi(app) {
@@ -255,27 +260,46 @@ export class AppProcessor {
255
260
  let app = this.getAppFromStorage(app_id);
256
261
  if (app) return app;
257
262
 
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
- }
263
+ if (this.appRequestCache.has(app_id)) return this.appRequestCache.get(app_id);
264
+
265
+ let self = this;
266
+
267
+ let pr = new Promise(async (resolve, reject) => {
268
+ try {
269
+ let app = await self.dataService.getApp(app_id);
270
+ if (!app) reject();
271
+
272
+ //!!! temporary check !!!! if app has chanks property then we start getting it
273
+ // if (
274
+ // receivedApp.chunks &&
275
+ // receivedApp.chunks.length
276
+ // ) {
277
+ // // here should be checked is merge required
278
+
279
+
280
+ // let chunks = await self.chunksManager.getChunks(app_id, receivedApp.chunks);
281
+ // receivedApp.items_list = self.util.mergeChunks([
282
+ // ...chunks,
283
+ // receivedApp,
284
+ // ]);
285
+ // }
272
286
 
273
- this.saveAppInStorage(receivedApp);
274
- this.ws.addSubscription(app_id);
275
287
 
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;
288
+ // This code for trash must be changed
289
+ // return trash ? app : {...app, items_list: app.items_list.filter(item => !item.trash)};
290
+ let filtered = trash ? app : {...app, items_list: app.items_list.filter(item => !item.trash)};
291
+ resolve(filtered);
292
+
293
+ self.saveAppInStorage(filtered);
294
+ self.ws.addSubscription(app_id);
295
+ } catch (error) {
296
+ reject();
297
+ }
298
+ });
299
+
300
+ self.appRequestCache.set(app_id, pr);
301
+
302
+ return pr;
279
303
  }
280
304
 
281
305
  async updateApp(app) {
@@ -319,6 +343,10 @@ export class AppProcessor {
319
343
  }
320
344
  }
321
345
 
346
+ async handleAppChange(id, prevVersion, nextVersion) {
347
+ // generate required events etc, do job
348
+ }
349
+
322
350
  clearAppProcessor() {
323
351
  this.getAppListPromises = null;
324
352
  this.getAppPromises = {};
@@ -457,7 +485,7 @@ export class AppProcessor {
457
485
  }
458
486
  );
459
487
  }
460
- event.data.payload.items_list = this.util.mergeChunks([app, event.data.payload])
488
+ event.data.payload.items_list = await this.util.mergeChunks([app, event.data.payload]) //here check Ask Andrew
461
489
  this.saveAppInStorage(event.data.payload);
462
490
  }
463
491
  // if (event.data.type === "refresh appList") {
@@ -1,68 +1,68 @@
1
- import { ChunkAPI } from "../api/ChunkApi.js";
1
+ // import { ChunkAPI } from "../api/ChunkApi.js";
2
2
 
3
3
 
4
- export class ChunksManager {
5
- constructor(
6
- storage,
7
- pipeService,
8
- req,
9
- util,
10
- dataService,
11
- ) {
12
- this.storage = storage;
13
- this.pipeService = pipeService;
14
- this.req = req;
15
- this.util = util;
16
- this.dataService = dataService;
17
- this.chunkApi = new ChunkAPI(req);
18
- this.itemListeners();
19
- }
4
+ // export class ChunksManager {
5
+ // constructor(
6
+ // storage,
7
+ // pipeService,
8
+ // req,
9
+ // util,
10
+ // dataService,
11
+ // ) {
12
+ // this.storage = storage;
13
+ // this.pipeService = pipeService;
14
+ // this.req = req;
15
+ // this.util = util;
16
+ // this.dataService = dataService;
17
+ // this.chunkApi = new ChunkAPI(req);
18
+ // this.itemListeners();
19
+ // }
20
20
 
21
- async getChunk(app_id, chunk_id) {
22
- try {
23
- return this.dataService.getChunk(app_id, chunk_id);
24
- } catch (err) {
25
- console.log(err);
26
- return null;
27
- }
28
- }
21
+ // async getChunk(app_id, chunk_id) {
22
+ // try {
23
+ // return this.dataService.getChunk(app_id, chunk_id);
24
+ // } catch (err) {
25
+ // console.log(err);
26
+ // return null;
27
+ // }
28
+ // }
29
29
 
30
- async getLastChunk(app_id) {
31
- try {
32
- return this.chunkApi.getLastChunk(app_id);
33
- } catch (err) {
34
- console.log(err);
35
- return null;
36
- }
37
- }
30
+ // async getLastChunk(app_id) {
31
+ // try {
32
+ // return this.chunkApi.getLastChunk(app_id);
33
+ // } catch (err) {
34
+ // console.log(err);
35
+ // return null;
36
+ // }
37
+ // }
38
38
 
39
- async getChunks(app_id, chunk_ids) {
40
- let chunks = [];
41
- if(chunk_ids){
42
- chunks = await Promise.all(chunk_ids.map((chunk_id) => this.getChunk(app_id, chunk_id)));
43
- }
44
- return chunks;
45
- }
39
+ // async getChunks(app_id, chunk_ids) {
40
+ // let chunks = [];
41
+ // if(chunk_ids){
42
+ // chunks = await Promise.all(chunk_ids.map((chunk_id) => this.getChunk(app_id, chunk_id)));
43
+ // }
44
+ // return chunks;
45
+ // }
46
46
 
47
- itemListeners() {
48
- }
47
+ // itemListeners() {
48
+ // }
49
49
 
50
- async getApp(app_id) {
51
- if (!app_id) return null;
52
- let app = this.getAppFromStorage(app_id);
53
- if (!app) {
54
- if (this.getAppPromises[app_id]) {
55
- return this.getAppPromises[app_id];
56
- }
57
- this.getAppPromises[app_id] = this.getAppApi(app_id);
58
- app = await this.getAppPromises[app_id];
59
- if (app) {
60
- this.saveAppInStorage(app);
61
- this.ws.addSubscription(app_id);
62
- } else {
63
- return null;
64
- }
65
- }
66
- return app;
67
- }
68
- }
50
+ // async getApp(app_id) {
51
+ // if (!app_id) return null;
52
+ // let app = this.getAppFromStorage(app_id);
53
+ // if (!app) {
54
+ // if (this.getAppPromises[app_id]) {
55
+ // return this.getAppPromises[app_id];
56
+ // }
57
+ // this.getAppPromises[app_id] = this.getAppApi(app_id);
58
+ // app = await this.getAppPromises[app_id];
59
+ // if (app) {
60
+ // this.saveAppInStorage(app);
61
+ // this.ws.addSubscription(app_id);
62
+ // } else {
63
+ // return null;
64
+ // }
65
+ // }
66
+ // return app;
67
+ // }
68
+ // }
@@ -1,5 +1,6 @@
1
1
  import should from "should";
2
2
  import {GudHub} from './../gudhub.js';
3
+ import { ChunkDataService } from "../DataService/export.js";
3
4
 
4
5
  describe("CHUNKS MANAGER", async function() {
5
6
  const auth_key = 'Z/lxMHLenEaQTvPjW5U6c3jBDwWFYZrh2F9Kxa3fbt8drvabS2u2lXQ2zI+SRmic';
@@ -9,7 +10,10 @@ describe("CHUNKS MANAGER", async function() {
9
10
  it("GET CHUNK : here we get first chunk from App", async function () {
10
11
  this.timeout(10000);
11
12
  var app = await gudhub.getApp(8263);
12
- let firstChunk = await gudhub.chunksManager.getChunk(8263, app.chunks[0]);
13
+
14
+ let chunkDataService = new ChunkDataService(); //TODO args
15
+
16
+ let firstChunk = await gudhub.chunksManager.getChunk(8263, app.chunks[0]);// getapp - get firstChunk from it and then load via service or via api, maybe create here chunkDataService instance
13
17
  firstChunk.should.have.property("chunk_number");
14
18
  firstChunk.should.have.property("items_list");
15
19
  firstChunk.items_list[0].fields[0].should.have.property("history");
@@ -18,7 +22,7 @@ describe("CHUNKS MANAGER", async function() {
18
22
 
19
23
  it("GET LAST CHUNK : here we should get last chunk", async function () {
20
24
  this.timeout(5000);
21
- let firstChunk = await gudhub.chunksManager.getLastChunk(8263);
25
+ let firstChunk = await gudhub.chunksManager.getLastChunk(8263);//
22
26
 
23
27
  firstChunk.should.have.property("chunk_number");
24
28
  firstChunk.should.have.property("items_list");
@@ -27,7 +31,7 @@ describe("CHUNKS MANAGER", async function() {
27
31
 
28
32
 
29
33
  it("GET CHUNKS : here we getting chanks from empty array []", async function () {
30
- let chunkedItems = await gudhub.chunksManager.getChunks(8263, []);
34
+ let chunkedItems = await gudhub.chunksManager.getChunks(8263, []);//
31
35
  chunkedItems.length.should.equal(0);
32
36
  });
33
37
 
@@ -35,7 +39,7 @@ describe("CHUNKS MANAGER", async function() {
35
39
  it("GET CHUNKS : here we should receive all chunks of app", async function () {
36
40
  this.timeout(1000000);
37
41
  var app = await gudhub.getApp(8263);
38
- let chunkedItems = await gudhub.chunksManager.getChunks(8263, app.chunks);
42
+ let chunkedItems = await gudhub.chunksManager.getChunks(8263, app.chunks);//
39
43
 
40
44
  chunkedItems[0].items_list[0].should.have.property("fields");
41
45
  chunkedItems[0].items_list[0].should.have.property("item_id");
@@ -1,5 +1,151 @@
1
1
  import { DataService } from "./DataService.js";
2
+ import { ChunkDataService, chunkDataServiceConf } from "./export.js";
2
3
 
3
4
  export class AppDataService extends DataService {
4
5
  //interface for indexeddb and http services
6
+
7
+ constructor(req, conf, gudhub) {
8
+ super();
9
+ this.chunkDataService = new ChunkDataService(req, chunkDataServiceConf, gudhub);
10
+ this.gudhub = gudhub;
11
+ }
12
+
13
+
14
+ isWithCache() {
15
+ return false;
16
+ }
17
+
18
+
19
+ //here
20
+ async processRequestedApp(id, sentData) {
21
+
22
+
23
+
24
+
25
+ if (
26
+ !this.isWithCache()
27
+ ) {
28
+
29
+ // async getChunks(app_id, chunk_ids) {
30
+ // let chunks = [];
31
+ // if(chunk_ids){
32
+ // chunks = await Promise.all(chunk_ids.map((chunk_id) => this.getChunk(app_id, chunk_id)));
33
+ // }
34
+ // return chunks;
35
+ // }
36
+
37
+ // let res = await this.chunkDataService.getChunks(id, sentData.chunks);
38
+ let res = await Promise.all(sentData.chunks.map((chunk_id) => this.chunkDataService.getChunk(id, chunk_id)));
39
+ if (!res) res = [];
40
+
41
+
42
+
43
+
44
+
45
+ sentData.items_list = await this.gudhub.util.mergeChunks([
46
+ ...res,
47
+ sentData,
48
+ ]);
49
+
50
+
51
+ return sentData;
52
+
53
+ // this.dataService.putApp(id, nextVersion);
54
+
55
+
56
+ // return this.mergeAndReturnStrategy.handle(sentData, chunks);
57
+
58
+
59
+
60
+ }
61
+
62
+
63
+
64
+
65
+
66
+
67
+ try {
68
+ let cachedVersion = await this.getCached(id);
69
+
70
+ if (
71
+ !cachedVersion
72
+ ) {
73
+
74
+ //TODO maybe dont wait for chunks at first load
75
+
76
+ // let res = await this.chunkDataService.getChunks(id, sentData.chunks);
77
+ let res = await Promise.all(sentData.chunks.map((chunk_id) => this.chunkDataService.getChunk(id, chunk_id)));
78
+ if (!res) res = [];
79
+
80
+
81
+
82
+ sentData.items_list = await this.gudhub.util.mergeChunks([
83
+ ...res,
84
+ sentData,
85
+ ]);
86
+
87
+
88
+ // this.putInCache(id, sentData);
89
+
90
+
91
+ return sentData;
92
+ }
93
+
94
+
95
+ let newHashesList = sentData.chunks.filter(c => !cachedVersion.chunks.includes(c));
96
+
97
+
98
+ // let self = this;
99
+
100
+
101
+ // let res = await this.chunkDataService.getChunks(id, newHashesList);
102
+ let res = await Promise.all(newHashesList.map((chunk_id) => this.chunkDataService.getChunk(id, chunk_id)));
103
+ if (!res) res = [];
104
+
105
+
106
+
107
+
108
+ // this.chunkDataService.getChunks(id, newHashesList).then((res) => {
109
+
110
+
111
+ sentData.items_list = await this.gudhub.util.mergeChunks([
112
+ cachedVersion,
113
+ ...res,
114
+ sentData,
115
+ ]);
116
+
117
+
118
+ // this.gudhub.triggerAppChange(id, cachedVersion, sentData);
119
+
120
+
121
+ // self.putInCache(id, sentData);
122
+ // });
123
+
124
+
125
+ return sentData;
126
+
127
+
128
+
129
+ // return cachedVersion;
130
+
131
+ } catch (error) {
132
+ // res = await this.chunkDataService.getChunks(id, sentData.chunks);
133
+ let res = await Promise.all(sentData.chunks.map((chunk_id) => this.chunkDataService.getChunk(id, chunk_id)));
134
+ if (!res) res = [];
135
+
136
+
137
+
138
+
139
+ sentData.items_list = await this.gudhub.util.mergeChunks([
140
+ ...res,
141
+ sentData,
142
+ ]);
143
+
144
+
145
+ // this.putInCache(id, sentData);
146
+
147
+
148
+ return sentData;
149
+ }
150
+ }
5
151
  }