Skip to content

Commit

Permalink
feat(entity) New stategy to filter entities with a custom function. (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
pelord authored Jun 9, 2020
1 parent 2ac67f1 commit df28ce7
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/common/src/lib/entity/shared/entity.interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ export interface EntityStateManagerOptions {

export interface EntityStoreStrategyOptions {}

export interface EntityStoreStrategyFuncOptions extends EntityStoreStrategyOptions {
filterClauseFunc: EntityFilterClause;
}

export interface EntityTransactionOptions {
getKey?: (entity: object) => EntityKey;
}
Expand Down
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);
}
}
1 change: 1 addition & 0 deletions packages/common/src/lib/entity/shared/strategies/index.ts
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';

0 comments on commit df28ce7

Please sign in to comment.