Просмотр исходного кода

modify how to get count of pages

yohei0125 4 лет назад
Родитель
Сommit
79fa60f776
1 измененных файлов с 12 добавлено и 12 удалено
  1. 12 12
      packages/app/src/server/service/page.ts

+ 12 - 12
packages/app/src/server/service/page.ts

@@ -687,12 +687,12 @@ class PageService {
       );
       );
     }
     }
 
 
-    (async() => {
-      if (isRecursively) {
+    if (isRecursively) {
+      (async() => {
         const descendantCountAppliedToAncestors = await this.duplicateDescendantsWithStream(page, newPagePath, user, shouldUseV4Process);
         const descendantCountAppliedToAncestors = await this.duplicateDescendantsWithStream(page, newPagePath, user, shouldUseV4Process);
         await this.updateDescendantCountOfAncestors(createdPage._id, descendantCountAppliedToAncestors, false);
         await this.updateDescendantCountOfAncestors(createdPage._id, descendantCountAppliedToAncestors, false);
-      }
-    })();
+      })();
+    }
 
 
     // take over tags
     // take over tags
     const originTags = await page.findRelatedTagsById();
     const originTags = await page.findRelatedTagsById();
@@ -904,11 +904,13 @@ class PageService {
     const normalizeParentAndDescendantCountOfDescendants = this.normalizeParentAndDescendantCountOfDescendants.bind(this);
     const normalizeParentAndDescendantCountOfDescendants = this.normalizeParentAndDescendantCountOfDescendants.bind(this);
     const pageEvent = this.pageEvent;
     const pageEvent = this.pageEvent;
     let count = 0;
     let count = 0;
+    let nNonEmptyDuplicatedPages = 0;
     const writeStream = new Writable({
     const writeStream = new Writable({
       objectMode: true,
       objectMode: true,
       async write(batch, encoding, callback) {
       async write(batch, encoding, callback) {
         try {
         try {
           count += batch.length;
           count += batch.length;
+          nNonEmptyDuplicatedPages += (batch.filter(page => !page.isEmpty)).length;
           await duplicateDescendants(batch, user, pathRegExp, newPagePathPrefix, shouldUseV4Process);
           await duplicateDescendants(batch, user, pathRegExp, newPagePathPrefix, shouldUseV4Process);
           logger.debug(`Adding pages progressing: (count=${count})`);
           logger.debug(`Adding pages progressing: (count=${count})`);
         }
         }
@@ -944,15 +946,9 @@ class PageService {
       .pipe(createBatchStream(BULK_REINDEX_SIZE))
       .pipe(createBatchStream(BULK_REINDEX_SIZE))
       .pipe(writeStream);
       .pipe(writeStream);
 
 
-    // ******************************************************************************
-    // * Returns all the data objects in an array
-    // * https://github.com/bendrucker/stream-to-promise/blob/master/index.js#L15
-    // * https://github.com/stream-utils/stream-to-array#toarraystream-callbackerr-arr
-    // ******************************************************************************
-    const data = await streamToPromise(readStream);
-    const nonEmptyPagesCount = (data.filter(page => !page.isEmpty)).length; // count of pages with 'isEmpty: true'
+    await streamToPromise(writeStream);
 
 
-    return nonEmptyPagesCount;
+    return nNonEmptyDuplicatedPages;
   }
   }
 
 
   private async duplicateDescendantsWithStreamV4(page, newPagePath, user) {
   private async duplicateDescendantsWithStreamV4(page, newPagePath, user) {
@@ -991,6 +987,10 @@ class PageService {
       .pipe(createBatchStream(BULK_REINDEX_SIZE))
       .pipe(createBatchStream(BULK_REINDEX_SIZE))
       .pipe(writeStream);
       .pipe(writeStream);
 
 
+    await streamToPromise(writeStream);
+
+    return count;
+
   }
   }
 
 
   /*
   /*