|
|
@@ -4,6 +4,7 @@ import {
|
|
|
CollectionModificationEvent,
|
|
|
DeepRequired,
|
|
|
EventBus,
|
|
|
+ ID,
|
|
|
idsAreEqual,
|
|
|
Logger,
|
|
|
OnVendureBootstrap,
|
|
|
@@ -16,6 +17,7 @@ import {
|
|
|
VendurePlugin,
|
|
|
} from '@vendure/core';
|
|
|
import { gql } from 'apollo-server-core';
|
|
|
+import { buffer, debounceTime, filter, map } from 'rxjs/operators';
|
|
|
|
|
|
import { ELASTIC_SEARCH_CLIENT, ELASTIC_SEARCH_OPTIONS, loggerCtx } from './constants';
|
|
|
import { ElasticsearchIndexService } from './elasticsearch-index.service';
|
|
|
@@ -260,17 +262,29 @@ export class ElasticsearchPlugin implements OnVendureBootstrap, OnVendureClose {
|
|
|
|
|
|
await this.elasticsearchService.createIndicesIfNotExists();
|
|
|
|
|
|
- this.eventBus.subscribe(CatalogModificationEvent, event => {
|
|
|
+ this.eventBus.ofType(CatalogModificationEvent).subscribe(event => {
|
|
|
if (event.entity instanceof Product || event.entity instanceof ProductVariant) {
|
|
|
return this.elasticsearchIndexService.updateProductOrVariant(event.ctx, event.entity).start();
|
|
|
}
|
|
|
});
|
|
|
- this.eventBus.subscribe(CollectionModificationEvent, event => {
|
|
|
- return this.elasticsearchIndexService
|
|
|
- .updateVariantsById(event.ctx, event.productVariantIds)
|
|
|
- .start();
|
|
|
- });
|
|
|
- this.eventBus.subscribe(TaxRateModificationEvent, event => {
|
|
|
+
|
|
|
+ const collectionModification$ = this.eventBus.ofType(CollectionModificationEvent);
|
|
|
+ const closingNotifier$ = collectionModification$.pipe(debounceTime(50));
|
|
|
+ collectionModification$
|
|
|
+ .pipe(
|
|
|
+ buffer(closingNotifier$),
|
|
|
+ filter(events => 0 < events.length),
|
|
|
+ map(events => ({
|
|
|
+ ctx: events[0].ctx,
|
|
|
+ ids: events.reduce((ids, e) => [...ids, ...e.productVariantIds], [] as ID[]),
|
|
|
+ })),
|
|
|
+ filter(e => 0 < e.ids.length),
|
|
|
+ )
|
|
|
+ .subscribe(events => {
|
|
|
+ return this.elasticsearchIndexService.updateVariantsById(events.ctx, events.ids).start();
|
|
|
+ });
|
|
|
+
|
|
|
+ this.eventBus.ofType(TaxRateModificationEvent).subscribe(event => {
|
|
|
const defaultTaxZone = event.ctx.channel.defaultTaxZone;
|
|
|
if (defaultTaxZone && idsAreEqual(defaultTaxZone.id, event.taxRate.zone.id)) {
|
|
|
return this.elasticsearchService.reindex(event.ctx);
|