@gudhub/core 1.2.4-beta.6 → 1.2.4-beta.9

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,45 @@ 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 receivedApp = await self.dataService.getApp(app_id);
270
+ if (!receivedApp) 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
272
278
 
273
- this.saveAppInStorage(receivedApp);
274
- this.ws.addSubscription(app_id);
275
279
 
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;
280
+ // let chunks = await self.chunksManager.getChunks(app_id, receivedApp.chunks);
281
+ // receivedApp.items_list = self.util.mergeChunks([
282
+ // ...chunks,
283
+ // receivedApp,
284
+ // ]);
285
+ // }
286
+
287
+
288
+ // This code for trash must be changed
289
+ // return trash ? app : {...app, items_list: app.items_list.filter(item => !item.trash)};
290
+ resolve(receivedApp);
291
+
292
+ self.saveAppInStorage(receivedApp);
293
+ self.ws.addSubscription(app_id);
294
+ } catch (error) {
295
+ reject();
296
+ }
297
+ });
298
+
299
+ self.appRequestCache.set(app_id, pr);
300
+
301
+ return pr;
279
302
  }
280
303
 
281
304
  async updateApp(app) {
@@ -319,7 +342,7 @@ export class AppProcessor {
319
342
  }
320
343
  }
321
344
 
322
- handleAppChange(v1, v2) {
345
+ async handleAppChange(id, prevVersion, nextVersion) {
323
346
  // generate required events etc, do job
324
347
  }
325
348
 
@@ -461,7 +484,7 @@ export class AppProcessor {
461
484
  }
462
485
  );
463
486
  }
464
- event.data.payload.items_list = this.util.mergeChunks([app, event.data.payload])
487
+ event.data.payload.items_list = await this.util.mergeChunks([app, event.data.payload])
465
488
  this.saveAppInStorage(event.data.payload);
466
489
  }
467
490
  // 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,150 @@
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
+ async processRequestedApp(id, sentData) {
20
+
21
+
22
+
23
+
24
+ if (
25
+ !this.isWithCache()
26
+ ) {
27
+
28
+ // async getChunks(app_id, chunk_ids) {
29
+ // let chunks = [];
30
+ // if(chunk_ids){
31
+ // chunks = await Promise.all(chunk_ids.map((chunk_id) => this.getChunk(app_id, chunk_id)));
32
+ // }
33
+ // return chunks;
34
+ // }
35
+
36
+ // let res = await this.chunkDataService.getChunks(id, sentData.chunks);
37
+ let res = await Promise.all(sentData.chunks.map((chunk_id) => this.chunkDataService.getChunk(id, chunk_id)));
38
+ if (!res) res = [];
39
+
40
+
41
+
42
+
43
+
44
+ sentData.items_list = await this.gudhub.util.mergeChunks([
45
+ ...res,
46
+ sentData,
47
+ ]);
48
+
49
+
50
+ return sentData;
51
+
52
+ // this.dataService.putApp(id, nextVersion);
53
+
54
+
55
+ // return this.mergeAndReturnStrategy.handle(sentData, chunks);
56
+
57
+
58
+
59
+ }
60
+
61
+
62
+
63
+
64
+
65
+
66
+ try {
67
+ let cachedVersion = await this.getCached(id);
68
+
69
+ if (
70
+ !cachedVersion
71
+ ) {
72
+
73
+ //TODO maybe dont wait for chunks at first load
74
+
75
+ // let res = await this.chunkDataService.getChunks(id, sentData.chunks);
76
+ let res = await Promise.all(sentData.chunks.map((chunk_id) => this.chunkDataService.getChunk(id, chunk_id)));
77
+ if (!res) res = [];
78
+
79
+
80
+
81
+ sentData.items_list = await this.gudhub.util.mergeChunks([
82
+ ...res,
83
+ sentData,
84
+ ]);
85
+
86
+
87
+ // this.putInCache(id, sentData);
88
+
89
+
90
+ return sentData;
91
+ }
92
+
93
+
94
+ let newHashesList = sentData.chunks.filter(c => !cachedVersion.chunks.includes(c));
95
+
96
+
97
+ // let self = this;
98
+
99
+
100
+ // let res = await this.chunkDataService.getChunks(id, newHashesList);
101
+ let res = await Promise.all(newHashesList.map((chunk_id) => this.chunkDataService.getChunk(id, chunk_id)));
102
+ if (!res) res = [];
103
+
104
+
105
+
106
+
107
+ // this.chunkDataService.getChunks(id, newHashesList).then((res) => {
108
+
109
+
110
+ sentData.items_list = await this.gudhub.util.mergeChunks([
111
+ cachedVersion,
112
+ ...res,
113
+ sentData,
114
+ ]);
115
+
116
+
117
+ this.gudhub.triggerAppChange(id, cachedVersion, sentData);
118
+
119
+
120
+ // self.putInCache(id, sentData);
121
+ // });
122
+
123
+
124
+ return sentData;
125
+
126
+
127
+
128
+ // return cachedVersion;
129
+
130
+ } catch (error) {
131
+ // res = await this.chunkDataService.getChunks(id, sentData.chunks);
132
+ let res = await Promise.all(sentData.chunks.map((chunk_id) => this.chunkDataService.getChunk(id, chunk_id)));
133
+ if (!res) res = [];
134
+
135
+
136
+
137
+
138
+ sentData.items_list = await this.gudhub.util.mergeChunks([
139
+ ...res,
140
+ sentData,
141
+ ]);
142
+
143
+
144
+ // this.putInCache(id, sentData);
145
+
146
+
147
+ return sentData;
148
+ }
149
+ }
5
150
  }
@@ -12,9 +12,9 @@ import { IndexedDBService } from "./IndexedDBService.js";
12
12
 
13
13
  export class IndexedDBAppService extends AppDataService {
14
14
  constructor(req, conf, gudhub) {
15
- super(req, conf);
15
+ super(req, conf, gudhub);
16
16
 
17
- this.dataService = new AppHttpService(req);
17
+ this.dataService = new AppHttpService(req, conf, gudhub);
18
18
 
19
19
  let indexDBService = new IndexedDBService(conf);
20
20
 
@@ -38,6 +38,10 @@ export class IndexedDBAppService extends AppDataService {
38
38
  // indexDBAccess = new IndexedDBFacade;
39
39
 
40
40
 
41
+ isWithCache() {
42
+ return true;
43
+ }
44
+
41
45
  async getApp(id) {
42
46
  if (this.requestCache.has(id)) return this.requestCache.get(id);
43
47
 
@@ -60,8 +64,14 @@ export class IndexedDBAppService extends AppDataService {
60
64
  if (
61
65
  !cachedData
62
66
  ) {
63
- dataServiceRequest.then(resolve, reject);
64
- dataServiceRequest.then(data => self.putApp(id, data));
67
+ dataServiceRequest.then(async data => {
68
+ let processedData = await self.processRequestedApp(id, data);
69
+
70
+ resolve(processedData);
71
+
72
+ self.putApp(id, processedData);
73
+
74
+ }, reject);
65
75
  }
66
76
 
67
77
  if (
@@ -69,20 +79,32 @@ export class IndexedDBAppService extends AppDataService {
69
79
  ) {
70
80
  resolve(cachedData);
71
81
 
72
- dataServiceRequest.then(data => {
73
- self.gudhub.processAppUpd(data, cachedData);
74
- self.putApp(id, data);
82
+ dataServiceRequest.then(async data => {
83
+ let processedData = await self.processRequestedApp(id, data);
84
+ self.putApp(id, processedData);
75
85
  });
76
86
  }
77
87
  };
78
88
 
79
89
  storeRequest.onerror = () => {
80
- dataServiceRequest.then(resolve, reject);
81
- dataServiceRequest.then(data => self.putApp(id, data));
90
+ dataServiceRequest.then(async data => {
91
+ let processedData = await self.processRequestedApp(id, data);
92
+
93
+ resolve(processedData);
94
+
95
+ self.putApp(id, processedData);
96
+
97
+ }, reject);
82
98
  }
83
99
  } catch (error) {
84
- dataServiceRequest.then(resolve, reject);
85
- dataServiceRequest.then(data => self.putApp(id, data));
100
+ dataServiceRequest.then(async data => {
101
+ let processedData = await self.processRequestedApp(id, data);
102
+
103
+ resolve(processedData);
104
+
105
+ self.putApp(id, processedData);
106
+
107
+ }, reject);
86
108
  }
87
109
  });
88
110
 
@@ -91,6 +113,44 @@ export class IndexedDBAppService extends AppDataService {
91
113
  return pr;
92
114
  }
93
115
 
116
+ async getCached(id) {
117
+
118
+ let self = this;
119
+
120
+ let pr = new Promise(async (resolve, reject) => {
121
+ try {
122
+ const db = await self.openDatabase();
123
+ const transaction = db.transaction(self.store, "readonly");
124
+ const store = transaction.objectStore(self.store);
125
+
126
+ const storeRequest = store.get(id);
127
+
128
+ storeRequest.onsuccess = (e) => {
129
+
130
+ let cachedData = e.target.result;
131
+
132
+ if (
133
+ !cachedData
134
+ ) {
135
+ reject();
136
+ }
137
+
138
+ if (
139
+ cachedData
140
+ ) {
141
+ resolve(cachedData);
142
+ }
143
+ };
144
+
145
+ storeRequest.onerror = reject;
146
+ } catch (error) {
147
+ reject();
148
+ }
149
+ });
150
+
151
+ return pr;
152
+ }
153
+
94
154
  async putApp(id, data) {
95
155
  try {
96
156
  const db = await this.openDatabase();
@@ -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
  }
@@ -1,26 +1,31 @@
1
1
  import { AppAPI } from "../../api/AppApi.js";
2
2
  import { AppDataService } from "../AppDataService.js";
3
+ import { ChunkDataService } from "../export.js";
3
4
  import { objectAssignWithOverride } from "../utils.js";
4
5
  import { HttpService } from "./HttpService.js";
5
6
 
6
7
  export class AppHttpService extends AppDataService {
7
8
  constructor(req, conf, gudhub) {
8
- super();
9
+ super(req, conf, gudhub);
9
10
 
10
11
  this.appApi = new AppAPI(req);
11
12
 
12
13
  let indexDBService = new HttpService(conf);
14
+
15
+
16
+ // this.chunkDataService = new ChunkDataService; //TODO move to
13
17
 
14
18
  objectAssignWithOverride(this, indexDBService);
15
19
  }
16
20
 
17
21
 
18
22
  async getApp(id) {
19
- return this.appApi.getApp(id);
23
+ let sentData = await this.appApi.getApp(id);
24
+ return this.processRequestedApp(id, sentData);
20
25
  }
21
26
 
22
27
  async putApp(id, app) {
23
-
28
+ // do nothing
24
29
  }
25
30
 
26
31
  static [Symbol.hasInstance](instance) {
@@ -32,4 +37,4 @@ export class AppHttpService extends AppDataService {
32
37
  return false;
33
38
  }
34
39
  }
35
- }
40
+ };