@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.
@@ -0,0 +1,71 @@
1
+
2
+ import { appDataServiceConf } from "../export.js";
3
+ import { GudHub } from "../../gudhub.js";
4
+ import { IndexedDBAppServiceForWorker } from "../IndexedDB/IndexedDBAppService.js";
5
+ import { appsConf } from "../IndexedDB/appDataConf.js";
6
+
7
+
8
+
9
+ let gudhub = {};
10
+ let appDataService = {};
11
+
12
+
13
+
14
+ //TODO also ChunkDataService !!!!!!!!!!!!!!!!!!!!!!!
15
+
16
+
17
+ self.onmessage = async function(e) {
18
+ if (e.data.type === 'init') {
19
+
20
+ gudhub = new GudHub(e.data.gudhubSerialized.authKey, e.data.gudhubSerialized.options);
21
+ appDataService = new IndexedDBAppServiceForWorker(gudhub.req, appsConf, gudhub);
22
+
23
+ }
24
+
25
+ if (e.data.type === 'processData') {
26
+
27
+
28
+
29
+ //todo cached could not exist !!!!!!!!!!!!!!!!!!!
30
+
31
+ let cached = await appDataService.getCached(e.data.id);
32
+
33
+ let newVersion = await appDataService.getApp(e.data.id);
34
+
35
+
36
+
37
+
38
+
39
+ // let comparison = gudhub.compareItems(cached.items_list, newVersion.items_list);
40
+ let comparison = gudhub.compareItems(newVersion.items_list, cached.items_list);
41
+
42
+
43
+
44
+ this.postMessage({id: e.data.id, compareRes: comparison, newVersion, type: e.data.type});
45
+
46
+
47
+
48
+
49
+ /// todo process data merge and then call merge and compare and then return from here for example event to update
50
+
51
+
52
+
53
+ // also dont forget that main thread also uses AppDataService !!!!!!!!!!!!!!!!!! make temporarile a separate class for it maybe
54
+
55
+ }
56
+
57
+
58
+ if (
59
+ e.data.type === 'requestApp'
60
+ ) {
61
+
62
+ let data = await appDataService.getApp(e.data.id);
63
+
64
+
65
+ this.postMessage({id: e.data.id, type: e.data.type, data});
66
+
67
+ }
68
+
69
+
70
+ // this.appRequestAndProcessWorker.postMessage({ type: 'requestApp', id: id });
71
+ };
@@ -1,26 +1,44 @@
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
- constructor(req, conf) {
8
- super();
8
+ constructor(req, conf, gudhub) {
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) {
23
+ let sentData = await this.appApi.getApp(id);
24
+ return this.processRequestedApp(id, sentData);// here is a problem. IndexedDB App service has Data service AppHttpService
25
+ }
26
+
27
+ //TODO should be getApp and getAppAndProcessData - refactor whole code to use them
28
+
29
+
30
+
31
+
32
+ //TODO андрей говорил про то что там надо фильтровать айтемы удаленные, я так и не доделал
33
+
34
+
35
+
36
+ async getAppWithoutProcessing(id) {
19
37
  return this.appApi.getApp(id);
20
38
  }
21
39
 
22
40
  async putApp(id, app) {
23
-
41
+ // do nothing
24
42
  }
25
43
 
26
44
  static [Symbol.hasInstance](instance) {
@@ -32,4 +50,4 @@ export class AppHttpService extends AppDataService {
32
50
  return false;
33
51
  }
34
52
  }
35
- }
53
+ };
@@ -9,9 +9,9 @@ export class ChunkHttpService extends ChunkDataService {
9
9
 
10
10
  this.chunkApi = new ChunkAPI(req);
11
11
 
12
- let indexDBService = new HttpService(conf);
12
+ let httpService = new HttpService(conf);
13
13
 
14
- objectAssignWithOverride(this, indexDBService);
14
+ objectAssignWithOverride(this, httpService);
15
15
  }
16
16
 
17
17
  async getChunk(app_id, id) {
@@ -118,6 +118,25 @@ export class ItemProcessor {
118
118
  return items;
119
119
  }
120
120
 
121
+ //here
122
+ handleItemsChange(id, prevVersion, nextVersion) {
123
+ let compareRes = this.util.compareItems(prevVersion.items_list, nextVersion.items_list);
124
+
125
+ this.updateItemsInStorage(id, compareRes.diff_src_items);
126
+ this.addItemsToStorage(id, compareRes.new_src_items);
127
+ }
128
+
129
+ triggerItemsChange(id, compareRes) {
130
+ this.updateItemsInStorage(id, compareRes.diff_src_items);
131
+ this.addItemsToStorage(id, compareRes.new_src_items);
132
+ }
133
+
134
+ handleDiff(id, compare) {
135
+ this.updateItemsInStorage(id, compare.diff_src_items);
136
+ this.addItemsToStorage(id, compare.new_src_items);
137
+ this.deleteItemsFromStorage(id, compare.trash_src_items);
138
+ }
139
+
121
140
  async deleteItemsFromStorage(app_id, itemsForDelete = []) {
122
141
  const app = await this.appProcessor.getApp(app_id);
123
142
  if (app) {
@@ -24,6 +24,7 @@ import { FileHelper } from "./FIleHelper/FileHelper.js";
24
24
  import { compareObjects } from "./compareObjects/compareObjects.js";
25
25
  import { dynamicPromiseAll } from "./dynamicPromiseAll/dynamicPromiseAll.js";
26
26
  import { sortItems } from './filter/sortItems.js';
27
+ import { mergeChunksWorker } from "./merge_chunks/merge_chunks.worker.js";
27
28
 
28
29
  export class Utils {
29
30
  constructor(gudhub) {
@@ -126,6 +127,7 @@ export class Utils {
126
127
  );
127
128
  }
128
129
 
130
+ //here
129
131
  compareItems(sourceItems, destinationItems, fieldToCompare) {
130
132
  return compareItems(sourceItems, destinationItems, fieldToCompare);
131
133
  }
@@ -142,8 +144,19 @@ export class Utils {
142
144
  return makeNestedList(arr, id, parent_id, children_property, priority_property);
143
145
  }
144
146
 
145
- mergeChunks(chunks) {
147
+ async mergeChunks(chunks) {
146
148
  return mergeChunks(chunks);
149
+
150
+
151
+ // const chunkWorkerBlob = new Blob([mergeChunksWorker()], {
152
+ // type: "application/javascript",
153
+ // });
154
+ // this.worker = new Worker(URL.createObjectURL(chunkWorkerBlob));
155
+ // this.worker.postMessage(chunks);
156
+ // this.worker.addEventListener("message", (event) => {
157
+ // const { diff } = event.data;
158
+ // callback(event.data);
159
+ // });
147
160
  }
148
161
 
149
162
  mergeFieldLists(fieldsToView, fieldList) {
@@ -1,31 +1,35 @@
1
+ import { mergeChunks as mergeChunksByMainThread, mergeChunksWorker } from "./merge_chunks.worker.js";
2
+
3
+
4
+ // const mergeChunksWorkerSingletone = new Worker('mergeWorker.js'); //TODO
5
+
6
+
1
7
  export function mergeChunks(chunks) {
2
- const result = {};
8
+ if (
9
+ typeof Worker !== 'undefined'
10
+ ) {
11
+ return new Promise((resolve, reject) => {
12
+ const chunkWorkerBlob = new Blob([mergeChunksWorker()], {
13
+ type: "application/javascript",
14
+ });
15
+
16
+ let worker = new Worker(URL.createObjectURL(chunkWorkerBlob));
17
+
18
+ worker.postMessage(chunks);
3
19
 
4
- chunks.forEach((chunk) => {
5
- chunk.items_list.forEach((item) => {
6
- const dstItem = result[item.item_id];
7
- if (dstItem) {
8
- dstItem.trash = item.trash;
9
- return item.fields.forEach((srcField) => {
10
- let isFieldNonExist = true;
11
- dstItem.fields = dstItem.fields.map((dstField) => {
12
- if (Number(dstField.field_id) === Number(srcField.field_id)) {
13
- isFieldNonExist = false;
14
- if (dstField.field_value != srcField.field_value) {
15
- return {
16
- ...srcField,
17
- history: srcField.history.concat(dstField.history),
18
- };
19
- }
20
- return dstField;
21
- }
22
- return dstField;
23
- });
24
- if (isFieldNonExist) dstItem.fields.push(srcField);
25
- });
26
- }
27
- return (result[item.item_id] = item);
20
+ worker.onmessage = function(e) {
21
+ resolve(e.data);
22
+ worker.terminate();
23
+ };
24
+
25
+ worker.onerror = function(e) {
26
+ reject();
27
+ worker.terminate();
28
+ };
29
+
30
+ worker.postMessage(chunks);
28
31
  });
29
- });
30
- return Object.values(result);
31
- }
32
+ } else {
33
+ return mergeChunksByMainThread(chunks);
34
+ }
35
+ }
@@ -0,0 +1,78 @@
1
+ export function mergeChunks(chunks) {
2
+ const result = {};
3
+
4
+ chunks.forEach((chunk) => {
5
+ chunk.items_list.forEach((item) => {
6
+ const dstItem = result[item.item_id];
7
+ if (dstItem) {
8
+ dstItem.trash = item.trash;
9
+ return item.fields.forEach((srcField) => {
10
+ let isFieldNonExist = true;
11
+ dstItem.fields = dstItem.fields.map((dstField) => {
12
+ if (Number(dstField.field_id) === Number(srcField.field_id)) {
13
+ isFieldNonExist = false;
14
+ if (dstField.field_value != srcField.field_value) {
15
+ return {
16
+ ...srcField,
17
+ history: srcField.history.concat(dstField.history),
18
+ };
19
+ }
20
+ return dstField;
21
+ }
22
+ return dstField;
23
+ });
24
+ if (isFieldNonExist) dstItem.fields.push(srcField);
25
+ });
26
+ }
27
+ return (result[item.item_id] = item);
28
+ });
29
+ });
30
+ return Object.values(result);
31
+ }
32
+
33
+
34
+ self.onmessage = function(e) {
35
+ const mergedChunks = mergeChunks(e.data);
36
+ self.postMessage(mergedChunks);
37
+ }
38
+
39
+
40
+ export function mergeChunksWorker() {
41
+ return `function mergeChunks(chunks) {
42
+ const result = {};
43
+
44
+ chunks.forEach((chunk) => {
45
+ chunk.items_list.forEach((item) => {
46
+ const dstItem = result[item.item_id];
47
+ if (dstItem) {
48
+ dstItem.trash = item.trash;
49
+ return item.fields.forEach((srcField) => {
50
+ let isFieldNonExist = true;
51
+ dstItem.fields = dstItem.fields.map((dstField) => {
52
+ if (Number(dstField.field_id) === Number(srcField.field_id)) {
53
+ isFieldNonExist = false;
54
+ if (dstField.field_value != srcField.field_value) {
55
+ return {
56
+ ...srcField,
57
+ history: srcField.history.concat(dstField.history),
58
+ };
59
+ }
60
+ return dstField;
61
+ }
62
+ return dstField;
63
+ });
64
+ if (isFieldNonExist) dstItem.fields.push(srcField);
65
+ });
66
+ }
67
+ return (result[item.item_id] = item);
68
+ });
69
+ });
70
+ return Object.values(result);
71
+ }
72
+
73
+
74
+ self.onmessage = function(e) {
75
+ const mergedChunks = mergeChunks(e.data);
76
+ self.postMessage(mergedChunks);
77
+ }`;
78
+ }
@@ -140,7 +140,8 @@
140
140
  is_items_diff: false,
141
141
  new_src_items:[],
142
142
  diff_src_items:[],
143
- same_items:[]
143
+ same_items:[],
144
+ trash_src_items: [],
144
145
  };
145
146
 
146
147
  const differentSrcItemFn = function(item){
@@ -155,11 +156,32 @@
155
156
  result.new_src_items.push(item);
156
157
  }
157
158
 
159
+ const trashSrcItemFn = function(item){
160
+ result.trash_src_items.push(item);
161
+ }
162
+
158
163
 
159
164
  //--------- Compearing Src & Dst Items by item_id ---------//
160
- if(fieldToCompare){
165
+ if (
166
+ fieldToCompare
167
+ ) {
168
+ //TODO trashSrcItemFn
161
169
  compareItemsByFieldId(src, dst, fieldToCompare, differentSrcItemFn, theSameSrcItemFn, newSrcItemFn)
162
- }else compareItemsByItemId(src, dst, differentSrcItemFn, theSameSrcItemFn, newSrcItemFn)
170
+ }
171
+
172
+ if (
173
+ !fieldToCompare
174
+ ) {
175
+ compareItemsByItemId(
176
+ src,
177
+ dst,
178
+ differentSrcItemFn,
179
+ theSameSrcItemFn,
180
+ newSrcItemFn,
181
+ null,
182
+ trashSrcItemFn,
183
+ );
184
+ }
163
185
 
164
186
 
165
187
  //--------- Final Report ---------//
@@ -214,16 +236,17 @@
214
236
 
215
237
 
216
238
 
217
- function compareItemsByItemId(src, dst, differentSrcItemFn, theSameSrcItemFn, newSrcItemFn, uniqDestItemFn){
239
+ function compareItemsByItemId(src, dst, differentSrcItemFn, theSameSrcItemFn, newSrcItemFn, uniqDestItemFn, trashSrcItemFn){
218
240
  let dstItemsMap = new ItemsMapper(dst);
219
241
 
220
242
  src.forEach(srcItem => {
221
243
  let destIndex = dstItemsMap.pullItemIndex(srcItem.item_id);
222
244
 
223
245
  if(destIndex != undefined){
224
- compareTwoItems(srcItem, dst[destIndex], differentSrcItemFn, theSameSrcItemFn);
246
+ compareTwoItems(srcItem, dst[destIndex], differentSrcItemFn, theSameSrcItemFn, trashSrcItemFn);
225
247
  }else{
226
- newSrcItemFn(srcItem);
248
+ if (srcItem.trash) trashSrcItemFn(srcItem);
249
+ if (!srcItem.trash) newSrcItemFn(srcItem);
227
250
  };
228
251
  });
229
252
 
@@ -236,7 +259,7 @@
236
259
 
237
260
 
238
261
 
239
- function compareTwoItems (src, dst, differentSrcItemFn, theSameSrcItemFn ){
262
+ function compareTwoItems (src, dst, differentSrcItemFn, theSameSrcItemFn, trashSrcItemFn){
240
263
  let sameItem = true;
241
264
 
242
265
  for(let i=0; i<src.fields.length; i++){
@@ -254,8 +277,16 @@
254
277
 
255
278
  //-- Sending Compearing Result
256
279
  if (sameItem){
257
- theSameSrcItemFn(src, dst);
258
- }else differentSrcItemFn(src, dst);
280
+ if (src.trash) trashSrcItemFn(src, dst);
281
+ if (!src.trash) theSameSrcItemFn(src, dst);
282
+ }
283
+
284
+ if (
285
+ !sameItem
286
+ ) {
287
+ if (src.trash) trashSrcItemFn(src, dst);
288
+ if (!src.trash) differentSrcItemFn(src, dst);
289
+ }
259
290
  }
260
291
 
261
292
 
package/GUDHUB/gudhub.js CHANGED
@@ -10,7 +10,7 @@ import { AppProcessor } from "./AppProcessor/AppProcessor.js";
10
10
  import { ItemProcessor } from "./ItemProcessor/ItemProcessor.js";
11
11
  import { FieldProcessor } from "./FieldProcessor/FieldProcessor.js";
12
12
  import { FileManager } from "./FileManager/FileManager.js";
13
- import { ChunksManager } from "./ChunksManager/ChunksManager.js";
13
+ // import { ChunksManager } from "./ChunksManager/ChunksManager.js";
14
14
  import { DocumentManager } from "./DocumentManager/DocumentManager.js";
15
15
  import { Interpritate } from './GHConstructor/interpritate.js'
16
16
  import { IS_WEB } from "./consts.js";
@@ -37,6 +37,12 @@ export class GudHub {
37
37
  expirydate: this.expirydate
38
38
  }
39
39
  ) {
40
+
41
+ this.serialized = {
42
+ authKey,
43
+ options,
44
+ }
45
+
40
46
  this.config = options;
41
47
  this.ghconstructor = new GHConstructor(this);
42
48
  this.interpritate = new Interpritate(this);
@@ -59,22 +65,24 @@ export class GudHub {
59
65
  this.req.init(this.auth.getToken.bind(this.auth));
60
66
 
61
67
  this.ws = new WebSocketApi(options.wss_url, this.auth);
62
- this.chunksManager = new ChunksManager(
63
- this.storage,
64
- this.pipeService,
65
- this.req,
66
- this.util,
67
- new ChunkDataService(this.req, chunkDataServiceConf),
68
- );
68
+
69
+ //todo
70
+ // this.chunksManager = new ChunksManager(
71
+ // this.storage,
72
+ // this.pipeService,
73
+ // this.req,
74
+ // this.util,
75
+ // new ChunkDataService(this.req, chunkDataServiceConf, this),
76
+ // );
69
77
  this.appProcessor = new AppProcessor(
70
78
  this.storage,
71
79
  this.pipeService,
72
80
  this.req,
73
81
  this.ws,
74
- this.chunksManager,
82
+ // this.chunksManager,
75
83
  this.util,
76
84
  options.activateSW,
77
- new AppDataService(this.req, appDataServiceConf),
85
+ new AppDataService(this.req, appDataServiceConf, this),
78
86
  );
79
87
  this.itemProcessor = new ItemProcessor(
80
88
  this.storage,
@@ -234,6 +242,17 @@ export class GudHub {
234
242
  return this.util.sortItems(items, options);
235
243
  }
236
244
 
245
+ //here
246
+ triggerAppChange(id, prevVersion, newVersion) {
247
+ this.itemProcessor.handleItemsChange(id, prevVersion, newVersion);
248
+ this.appProcessor.handleAppChange(id, prevVersion, newVersion);
249
+ }
250
+
251
+ //TODO handleAppChange
252
+ triggerIemsChange(id, compareRes) {
253
+ this.itemProcessor.triggerItemsChange(id, compareRes);
254
+ }
255
+
237
256
  jsonToItems(json, map) {
238
257
  return this.util.jsonToItems(json, map);
239
258
  }