From 28afdccedd8697507663b7193f8064d02566ff17 Mon Sep 17 00:00:00 2001
From: cgeek <cem.moreau@gmail.com>
Date: Mon, 9 Jul 2018 15:49:08 +0200
Subject: [PATCH] [fix] sync: was long within duniter-ui

---
 app/lib/dal/indexDAL/loki/LokiCollection.ts        |  5 +++--
 app/lib/dal/indexDAL/loki/LokiCollectionManager.ts |  4 ++--
 app/lib/dal/indexDAL/loki/LokiTypes.ts             |  2 +-
 app/modules/crawler/index.ts                       | 14 +++++++-------
 4 files changed, 13 insertions(+), 12 deletions(-)

diff --git a/app/lib/dal/indexDAL/loki/LokiCollection.ts b/app/lib/dal/indexDAL/loki/LokiCollection.ts
index 515ff9c8c..c912a7547 100644
--- a/app/lib/dal/indexDAL/loki/LokiCollection.ts
+++ b/app/lib/dal/indexDAL/loki/LokiCollection.ts
@@ -36,7 +36,8 @@ export class LokiProxyCollection<T> implements LokiCollection<T> {
     return this.collection.chain()
   }
 
-  setDisableChangesAPI(disable: boolean) {
-    this.collection.setDisableChangesAPI(disable)
+  setChangesApi(enabled: boolean) {
+    this.collection.setChangesApi(enabled)
+    ;(this.collection as any).disableDeltaChangesApi = true
   }
 }
diff --git a/app/lib/dal/indexDAL/loki/LokiCollectionManager.ts b/app/lib/dal/indexDAL/loki/LokiCollectionManager.ts
index 38c696f68..fe0484af9 100755
--- a/app/lib/dal/indexDAL/loki/LokiCollectionManager.ts
+++ b/app/lib/dal/indexDAL/loki/LokiCollectionManager.ts
@@ -29,11 +29,11 @@ export abstract class LokiCollectionManager<T> implements LokiDAO {
   }
 
   public enableChangesAPI() {
-    this.collection.setDisableChangesAPI(false)
+    this.collection.setChangesApi(true)
   }
 
   public disableChangesAPI() {
-    this.collection.setDisableChangesAPI(true)
+    this.collection.setChangesApi(false)
   }
 
   async init(): Promise<void> {
diff --git a/app/lib/dal/indexDAL/loki/LokiTypes.ts b/app/lib/dal/indexDAL/loki/LokiTypes.ts
index 4e4f87d15..ff34a77f4 100644
--- a/app/lib/dal/indexDAL/loki/LokiTypes.ts
+++ b/app/lib/dal/indexDAL/loki/LokiTypes.ts
@@ -13,7 +13,7 @@ export interface LokiCollection<T> {
 
   chain(): LokiChainableFind<T>
 
-  setDisableChangesAPI(disable: boolean): void
+  setChangesApi(disable: boolean): void
 }
 
 export interface LokiChainableFind<T> {
diff --git a/app/modules/crawler/index.ts b/app/modules/crawler/index.ts
index a86cfeb99..2d72d7b41 100644
--- a/app/modules/crawler/index.ts
+++ b/app/modules/crawler/index.ts
@@ -51,14 +51,14 @@ export const CrawlerDependency = {
       synchronize: (server:Server, onHost:string, onPort:number, upTo:number, chunkLength:number) => {
         const strategy = new RemoteSynchronizer(onHost, onPort, server)
         const remote = new Synchroniser(server, strategy)
-        const syncPromise = remote.sync(upTo, chunkLength)
+        const syncPromise = (async () => {
+          await server.dal.disableChangesAPI()
+          await remote.sync(upTo, chunkLength)
+          await server.dal.enableChangesAPI()
+        })()
         return {
           flow: remote,
-          syncPromise: (async () => {
-            await server.dal.disableChangesAPI()
-            await syncPromise
-            await server.dal.enableChangesAPI()
-          })()
+          syncPromise
         };
       },
 
@@ -91,7 +91,7 @@ export const CrawlerDependency = {
       name: 'sync [source] [to]',
       desc: 'Synchronize blockchain from a remote Duniter node',
       preventIfRunning: true,
-      onDatabaseExecute: async (server:Server, conf:ConfDTO, program:any, params:any) => {
+      onDatabaseExecute: async (server:Server, conf:ConfDTO, program:any, params:any): Promise<any> => {
         const source = params[0]
         const to     = params[1]
         const HOST_PATTERN = /^[^:/]+(:[0-9]{1,5})?$/
-- 
GitLab