From d1330b98b4634511ee737af7a4dd719c03b0da62 Mon Sep 17 00:00:00 2001 From: Tim Roes Date: Fri, 6 Jul 2018 18:17:55 +0200 Subject: [PATCH] Make aggTypeFilter registry static --- .../agg_types/filter/agg_type_filters.test.ts | 26 +++------------ .../agg_types/filter/agg_type_filters.ts | 32 ++++++------------- .../public/vis/editors/default/agg_params.js | 9 ++---- 3 files changed, 17 insertions(+), 50 deletions(-) diff --git a/src/ui/public/agg_types/filter/agg_type_filters.test.ts b/src/ui/public/agg_types/filter/agg_type_filters.test.ts index 915f688e7acaf9..47e7bebf9f3652 100644 --- a/src/ui/public/agg_types/filter/agg_type_filters.test.ts +++ b/src/ui/public/agg_types/filter/agg_type_filters.test.ts @@ -17,7 +17,6 @@ * under the License. */ -import { first } from 'rxjs/operators'; import { AggTypeFilters } from './agg_type_filters'; describe('AggTypeFilters', () => { @@ -31,46 +30,31 @@ describe('AggTypeFilters', () => { it('should filter nothing without registered filters', async () => { const aggTypes = [{ name: 'count' }, { name: 'sum' }]; - const observable = registry.filter$(aggTypes, indexPattern, aggConfig); - const filtered = await observable.pipe(first()).toPromise(); + const filtered = registry.filter(aggTypes, indexPattern, aggConfig); expect(filtered).toEqual(aggTypes); }); - it('should emit a new filtered list when registering a new filter', async () => { - const aggTypes = [{ name: 'count' }, { name: 'sum' }]; - const observable = registry.filter$(aggTypes, indexPattern, aggConfig); - const spy = jest.fn(); - observable.subscribe(spy); - expect(spy).toHaveBeenCalledTimes(1); - registry.addFilter(() => true); - expect(spy).toHaveBeenCalledTimes(2); - }); - it('should pass all aggTypes to the registered filter', async () => { const aggTypes = [{ name: 'count' }, { name: 'sum' }]; const filter = jest.fn(); registry.addFilter(filter); - await registry - .filter$(aggTypes, indexPattern, aggConfig) - .pipe(first()) - .toPromise(); + registry.filter(aggTypes, indexPattern, aggConfig); expect(filter).toHaveBeenCalledWith(aggTypes[0], indexPattern, aggConfig); expect(filter).toHaveBeenCalledWith(aggTypes[1], indexPattern, aggConfig); }); it('should allow registered filters to filter out aggTypes', async () => { const aggTypes = [{ name: 'count' }, { name: 'sum' }, { name: 'avg' }]; - const observable = registry.filter$(aggTypes, indexPattern, aggConfig); - let filtered = await observable.pipe(first()).toPromise(); + let filtered = registry.filter(aggTypes, indexPattern, aggConfig); expect(filtered).toEqual(aggTypes); registry.addFilter(() => true); registry.addFilter(aggType => aggType.name !== 'count'); - filtered = await observable.pipe(first()).toPromise(); + filtered = registry.filter(aggTypes, indexPattern, aggConfig); expect(filtered).toEqual([aggTypes[1], aggTypes[2]]); registry.addFilter(aggType => aggType.name !== 'avg'); - filtered = await observable.pipe(first()).toPromise(); + filtered = registry.filter(aggTypes, indexPattern, aggConfig); expect(filtered).toEqual([aggTypes[1]]); }); }); diff --git a/src/ui/public/agg_types/filter/agg_type_filters.ts b/src/ui/public/agg_types/filter/agg_type_filters.ts index 8840af54e8cd39..959387c55a0920 100644 --- a/src/ui/public/agg_types/filter/agg_type_filters.ts +++ b/src/ui/public/agg_types/filter/agg_type_filters.ts @@ -16,8 +16,6 @@ * specific language governing permissions and limitations * under the License. */ -import { BehaviorSubject } from 'rxjs'; -import { map } from 'rxjs/operators'; import { AggType } from '..'; import { IndexPattern } from '../../index_patterns'; import { AggConfig } from '../../vis'; @@ -34,47 +32,37 @@ type AggTypeFilter = ( */ class AggTypeFilters { private filters = new Set(); - private subject = new BehaviorSubject>(this.filters); /** * Register a new {@link AggTypeFilter} with this registry. - * This will emit a new set of filtered aggTypes on every Observer returned - * by the {@link #filter$|filter method}. * * @param filter The filter to register. */ public addFilter(filter: AggTypeFilter): void { this.filters.add(filter); - this.subject.next(this.filters); } /** - * Returns an Observable that will emit a filtered list of the passed {@link AggType|aggTypes}. - * A new filtered list will always be emitted when the {@link AggTypeFilter} - * registered with this registry will change. + * Returns the {@link AggType|aggTypes} filtered by all registered filters. * * @param aggTypes A list of aggTypes that will be filtered down by this registry. * @param indexPattern The indexPattern for which this list should be filtered down. * @param aggConfig The aggConfig for which the returning list will be used. * @return A filtered list of the passed aggTypes. */ - public filter$( + public filter( aggTypes: AggType[], indexPattern: IndexPattern, aggConfig: AggConfig ) { - return this.subject.pipe( - map(filters => { - const allFilters = Array.from(filters); - const allowedAggTypes = aggTypes.filter(aggType => { - const isAggTypeAllowed = allFilters.every(filter => - filter(aggType, indexPattern, aggConfig) - ); - return isAggTypeAllowed; - }); - return allowedAggTypes; - }) - ); + const allFilters = Array.from(this.filters); + const allowedAggTypes = aggTypes.filter(aggType => { + const isAggTypeAllowed = allFilters.every(filter => + filter(aggType, indexPattern, aggConfig) + ); + return isAggTypeAllowed; + }); + return allowedAggTypes; } } diff --git a/src/ui/public/vis/editors/default/agg_params.js b/src/ui/public/vis/editors/default/agg_params.js index cf4d7b1f7d4991..7314a37ced0dd5 100644 --- a/src/ui/public/vis/editors/default/agg_params.js +++ b/src/ui/public/vis/editors/default/agg_params.js @@ -42,13 +42,8 @@ uiModules $scope.$bind('groupName', attr.groupName); $scope.$bind('indexPattern', attr.indexPattern); - const aggTypeSubscription = aggTypeFilters - .filter$(aggTypes.byType[$scope.groupName], $scope.indexPattern, $scope.agg) - .subscribe(aggTypes => $scope.aggTypeOptions = aggTypes); - - $scope.$on('$destroy', () => { - aggTypeSubscription.unsubscribe(); - }); + $scope.aggTypeOptions = aggTypeFilters + .filter(aggTypes.byType[$scope.groupName], $scope.indexPattern, $scope.agg); $scope.advancedToggled = false;