-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(entity) New stategy to filter entities with a custom function. (#…
…663)
- Loading branch information
Showing
3 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
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
91 changes: 91 additions & 0 deletions
91
packages/common/src/lib/entity/shared/strategies/filter-custom-function.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,91 @@ | ||
import { EntityStoreStrategyFuncOptions } from '../entity.interfaces'; | ||
import { EntityStore } from '../store'; | ||
import { EntityStoreStrategy } from './strategy'; | ||
|
||
/** | ||
* When active, this strategy filters a store's stateView to return | ||
* selected entities only. | ||
*/ | ||
export class EntityStoreFilterCustomFuncStrategy extends EntityStoreStrategy { | ||
|
||
constructor(protected options: EntityStoreStrategyFuncOptions) { | ||
super(options); | ||
} | ||
|
||
/** | ||
* Store / filter ids map | ||
*/ | ||
private filters: Map<EntityStore, string> = new Map(); | ||
|
||
/** | ||
* Bind this strategy to a store and start filtering it | ||
* @param store Entity store | ||
*/ | ||
bindStore(store: EntityStore) { | ||
super.bindStore(store); | ||
if (this.active === true) { | ||
this.filterStore(store); | ||
} | ||
} | ||
|
||
/** | ||
* Unbind this strategy from a store and stop filtering it | ||
* @param store Entity store | ||
*/ | ||
unbindStore(store: EntityStore) { | ||
super.unbindStore(store); | ||
if (this.active === true) { | ||
this.unfilterStore(store); | ||
} | ||
} | ||
|
||
/** | ||
* Start filtering all stores | ||
* @internal | ||
*/ | ||
protected doActivate() { | ||
this.filterAll(); | ||
} | ||
|
||
/** | ||
* Stop filtering all stores | ||
* @internal | ||
*/ | ||
protected doDeactivate() { | ||
this.unfilterAll(); | ||
} | ||
|
||
/** | ||
* Filter all stores | ||
*/ | ||
private filterAll() { | ||
this.stores.forEach((store: EntityStore) => this.filterStore(store)); | ||
} | ||
|
||
/** | ||
* Unfilter all stores | ||
*/ | ||
private unfilterAll() { | ||
this.stores.forEach((store: EntityStore) => this.unfilterStore(store)); | ||
} | ||
|
||
/** | ||
* Filter a store and add it to the filters map | ||
*/ | ||
private filterStore(store: EntityStore) { | ||
this.filters.set(store, store.stateView.addFilter(this.options.filterClauseFunc)); | ||
} | ||
|
||
/** | ||
* Unfilter a store and delete it from the filters map | ||
*/ | ||
private unfilterStore(store: EntityStore) { | ||
const filterId = this.filters.get(store); | ||
if (filterId === undefined) { | ||
return; | ||
} | ||
|
||
store.stateView.removeFilter(filterId); | ||
this.filters.delete(store); | ||
} | ||
} |
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,2 +1,3 @@ | ||
export * from './strategy'; | ||
export * from './filter-custom-function'; | ||
export * from './filter-selection'; |