-
-
Notifications
You must be signed in to change notification settings - Fork 174
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor filter (search query) event handling
Refactor filter event handling to a unified event with visitor pattern to simplify the code, avoid future bugs and provide better test coverage. This commit shifts from using separate `filtered` and `filterRemoved` events to a singular, more expressive `filterChanged` event. The new approach emits a detailed payload that explicitly indicates the filter action and the associated filter data. The event object unifies the way the presentation layer reacts to the events. Benefits with this approach include: - Simplifying event listeners by reducing the number of events to handle. - Increasing code clarity and reduces potential for oversight by providing explicit action details in the event payload. - Offering extensibility for future actions without introducing new events. - Providing visitor pattern to handle different kind of events in easy and robust manner without code repetition. Other changes: - Refactor components handling of events to follow DRY and KISS principles better. - Refactor `UserFilter.spec.ts` to: - Make it easier to add new tests. - Increase code coverage by running all event-based tests on the current property.
- Loading branch information
1 parent
ae75059
commit 6a20d80
Showing
17 changed files
with
488 additions
and
229 deletions.
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
4
src/application/Context/State/Filter/Event/FilterActionType.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export enum FilterActionType { | ||
Apply, | ||
Clear, | ||
} |
37 changes: 37 additions & 0 deletions
37
src/application/Context/State/Filter/Event/FilterChange.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { IFilterResult } from '@/application/Context/State/Filter/IFilterResult'; | ||
import { FilterActionType } from './FilterActionType'; | ||
import { IFilterChangeDetails, IFilterChangeDetailsVisitor } from './IFilterChangeDetails'; | ||
|
||
export class FilterChange implements IFilterChangeDetails { | ||
public static forApply(filter: IFilterResult) { | ||
if (!filter) { | ||
throw new Error('missing filter'); | ||
} | ||
return new FilterChange(FilterActionType.Apply, filter); | ||
} | ||
|
||
public static forClear() { | ||
return new FilterChange(FilterActionType.Clear); | ||
} | ||
|
||
private constructor( | ||
public readonly actionType: FilterActionType, | ||
public readonly filter?: IFilterResult, | ||
) { } | ||
|
||
public visit(visitor: IFilterChangeDetailsVisitor): void { | ||
if (!visitor) { | ||
throw new Error('missing visitor'); | ||
} | ||
switch (this.actionType) { | ||
case FilterActionType.Apply: | ||
visitor.onApply(this.filter); | ||
break; | ||
case FilterActionType.Clear: | ||
visitor.onClear(); | ||
break; | ||
default: | ||
throw new Error(`Unknown action type: ${this.actionType}`); | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/application/Context/State/Filter/Event/IFilterChangeDetails.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { IFilterResult } from '@/application/Context/State/Filter/IFilterResult'; | ||
import { FilterActionType } from './FilterActionType'; | ||
|
||
export interface IFilterChangeDetails { | ||
readonly actionType: FilterActionType; | ||
readonly filter?: IFilterResult; | ||
|
||
visit(visitor: IFilterChangeDetailsVisitor): void; | ||
} | ||
|
||
export interface IFilterChangeDetailsVisitor { | ||
onClear(): void; | ||
onApply(filter: IFilterResult): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
import { IEventSource } from '@/infrastructure/Events/IEventSource'; | ||
import { IFilterResult } from './IFilterResult'; | ||
import { IFilterChangeDetails } from './Event/IFilterChangeDetails'; | ||
|
||
export interface IReadOnlyUserFilter { | ||
readonly currentFilter: IFilterResult | undefined; | ||
readonly filtered: IEventSource<IFilterResult>; | ||
readonly filterRemoved: IEventSource<void>; | ||
readonly filterChanged: IEventSource<IFilterChangeDetails>; | ||
} | ||
|
||
export interface IUserFilter extends IReadOnlyUserFilter { | ||
setFilter(filter: string): void; | ||
removeFilter(): void; | ||
applyFilter(filter: string): void; | ||
clearFilter(): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.