Filtering Firefly Semantics Slice Object Store Observables by Property Value | Task

Ole Ersoy
Oct - 01  -  2 min

Scenario

We want the ability to observe reactive Firefly Semantics Slice Object Store Events filtered by property name and value.

Approach

Use the onFilteredEvent<E> function provided as part of the Firefly Semantics Slice utility library. It is defined like this.

export function onFilteredEvent<E>(
    value: any,
    propertyName: string,
    obs: Observable<E>): Observable<E> {
    return obs.pipe(
           filter((e) => !!(e && e[propertyName] === value)));
    );
}

The RxJS filter expression checks that the event (e) is defined and that the value argument matches the value identified by the propertyName argument. The obs argument is the Observable argument provided by the Firefly Semantics Slice Object Store.

Demo

In this demo we created a NamedEvent type that has a name property that we will be targeting.

export type NamedEvent = { name: string }; const namedEvent: NamedEvent = { name: 'hilde' }; We also created the instance namedEvent setting name to hilde . We use it to initialize our object store.

const START: OStoreStart = {
    event: { value: namedEvent },
};
interface ISTART extends KeyObsValueReset {
    event: ObsValueReset;
}
let OS: OStore<ISTART> = new OStore(START);

We can now filter for events where the property name is set to hilde like this:

const hildeEvents$: Observable<NamedEvent> = onFilteredEvent<NamedEvent>(
    'hilde',
    'name',
    OS.S.event.obs
);