rxjs#scan TypeScript Examples

The following examples show how to use rxjs#scan. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: create-required-search-filter-observable.ts    From js-client with MIT License 6 votes vote down vote up
createRequiredSearchFilterObservable = ({
	filter$,
	initialFilter,
	previewDateRange,
	defaultValues,
}: CreateRequiredSearchFilterObservable): Observable<RequiredSearchFilter> => {
	const expandDateRange = (dateRange: SearchFilter['dateRange']): Partial<DateRange> => {
		if (dateRange === 'preview') return previewDateRange;
		return dateRange ?? {};
	};

	const searchFilter$: Observable<RequiredSearchFilter> = filter$.pipe(
		scan(
			(acc, curr) => ({
				entriesOffset: {
					index: curr.entriesOffset?.index ?? acc.entriesOffset.index,
					count: curr.entriesOffset?.count ?? acc.entriesOffset.count,
				},
				dateRange: {
					start: defaultValues.dateStart,
					end: defaultValues.dateEnd,
					...expandDateRange(acc.dateRange),
					...expandDateRange(curr.dateRange),
				},
				desiredGranularity: curr.desiredGranularity ?? acc.desiredGranularity,
				overviewGranularity: curr.overviewGranularity ?? acc.overviewGranularity,
				zoomGranularity: curr.zoomGranularity ?? acc.zoomGranularity,
				elementFilters: initialFilter.elementFilters,
			}),
			initialFilter,
		),
	);

	return searchFilter$;
}