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

refactor(core): Move asyncObservable to utils

Michael Bromley 4 лет назад
Родитель
Сommit
df4ad47f3a

+ 0 - 40
packages/core/src/async/async-observable.ts

@@ -1,40 +0,0 @@
-import { Observable, Observer } from 'rxjs';
-
-/**
- * @description
- * Returns an observable which executes the given async work function and completes with
- * the returned value. This is useful in Worker Controller methods which need to return
- * an Observable but also want to work with async (Promise-returning) code.
- *
- * @example
- * ```TypeScript
- * \@Controller()
- * export class MyWorkerController {
- *
- *     \@MessagePattern('test')
- *     handleTest() {
- *         return asyncObservable(async observer => {
- *             const value = await this.connection.fetchSomething();
- *             return value;
- *         });
- *     }
- * }
- * ```
- *
- * @docsCategory worker
- */
-export function asyncObservable<T>(work: (observer: Observer<T>) => Promise<T | void>): Observable<T> {
-    return new Observable<T>(subscriber => {
-        (async () => {
-            try {
-                const result = await work(subscriber);
-                if (result) {
-                    subscriber.next(result);
-                }
-                subscriber.complete();
-            } catch (e) {
-                subscriber.error(e);
-            }
-        })();
-    });
-}

+ 0 - 1
packages/core/src/async/index.ts

@@ -1 +0,0 @@
-export * from './async-observable';

+ 38 - 1
packages/core/src/common/utils.ts

@@ -1,6 +1,6 @@
 import { AssetType } from '@vendure/common/lib/generated-types';
 import { ID } from '@vendure/common/lib/shared-types';
-import { Observable } from 'rxjs';
+import { Observable, Observer } from 'rxjs';
 
 /**
  * Takes a predicate function and returns a negated version.
@@ -75,3 +75,40 @@ export async function awaitPromiseOrObservable<T>(value: T | Promise<T> | Observ
     }
     return result;
 }
+
+/**
+ * @description
+ * Returns an observable which executes the given async work function and completes with
+ * the returned value. This is useful in methods which need to return
+ * an Observable but also want to work with async (Promise-returning) code.
+ *
+ * @example
+ * ```TypeScript
+ * \@Controller()
+ * export class MyWorkerController {
+ *
+ *     \@MessagePattern('test')
+ *     handleTest() {
+ *         return asyncObservable(async observer => {
+ *             const value = await this.connection.fetchSomething();
+ *             return value;
+ *         });
+ *     }
+ * }
+ * ```
+ */
+export function asyncObservable<T>(work: (observer: Observer<T>) => Promise<T | void>): Observable<T> {
+    return new Observable<T>(subscriber => {
+        (async () => {
+            try {
+                const result = await work(subscriber);
+                if (result) {
+                    subscriber.next(result);
+                }
+                subscriber.complete();
+            } catch (e) {
+                subscriber.error(e);
+            }
+        })();
+    });
+}

+ 0 - 1
packages/core/src/index.ts

@@ -10,7 +10,6 @@ export * from './plugin/index';
 export * from './entity/index';
 export * from './data-import/index';
 export * from './service/index';
-export * from './async/index';
 export * from '@vendure/common/lib/shared-types';
 export {
     Permission,

+ 1 - 2
packages/core/src/plugin/default-search-plugin/indexer/indexer.controller.ts

@@ -6,10 +6,9 @@ import { Observable } from 'rxjs';
 import { FindOptionsUtils } from 'typeorm/find-options/FindOptionsUtils';
 
 import { RequestContext } from '../../../api/common/request-context';
-import { asyncObservable } from '../../../async';
 import { AsyncQueue } from '../../../common/async-queue';
 import { Translatable, Translation } from '../../../common/types/locale-types';
-import { idsAreEqual } from '../../../common/utils';
+import { asyncObservable, idsAreEqual } from '../../../common/utils';
 import { ConfigService } from '../../../config/config.service';
 import { Logger } from '../../../config/logger/vendure-logger';
 import { FacetValue } from '../../../entity/facet-value/facet-value.entity';