Creating a unique rxjs store using a custom selector function

Incorporating a function as my selector, it allows me to choose between selecting either a specific section of my personalized store or the entire entity.

  select<K>(mapFn: (state: T) => K): Observable<K> {
    return this.internalState$.asObservable().pipe(
      map((state: T) => mapFn(state)),
      distinctUntilChanged()
    );
  }

For implementation, I utilize it in the following manner:

this.title$ = this.store.select((s) => s.title)

While this method serves its purpose adequately, there have been instances where I require two segments from my state. In such cases, I tackle the issue by composing it like so:

this.store.select((s) => s).pipe(map((m) => ({ date: m.date, zone: m.zone })))
, choosing the whole store and then extracting the relevant properties in subsequent operations, or utilizing combineLatest with the chosen states.

Could there be a way to devise a selector that picks out the needed properties from the store? I've previously employed Pick<T>, although how it can be applied within this context remains uncertain.

Answer №1

.filter((f) => f) doesn't have a specific purpose by itself.

It is recommended to use the following syntax:

this.database.filter(f => ({ time: f.time, location: f.location })).pipe(

)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the process for retrieving isolated scope values from typescript directives within a typescript controller class?

As I work with a Typescript directive class and controller class, I am facing an issue. I need to set up a watch on an isolated scope variable within the Typescript controller class, but I seem to be unable to access these isolated scope variables. How can ...

The export for HTTP_PROVIDERS is missing in @angular/http/index

Requirements https://i.sstatic.net/abc123.png app.js file https://i.sstatic.net/def456.png Trying to bring in HTTP_PROVIDERS but facing an issue which states "@angular/http/index does not have a module named HTTP_PROVIDERS" Attached are screenshot ...

Tips for infuriating TSC with Lookup categories

I'm looking for the Typescript compiler (TSC) to throw errors when I make mistakes in signatures. export class EventEmitter<EventTypes extends { [key: string]: any }> { subscribe<Event extends keyof EventTypes>(type: keyof EventTypes, ...

The Angular Karma tests are failing because the custom tag is not recognized as a known element

I've recently started learning Angular and encountered an issue with my first Karma test. The error message I received is as follows: AppComponent should create component Error: Template parse errors: 'ereturn-form' is not a known element: ...

Guide on obtaining the total value from a JSON Array in Angular 2 using TypeScript

I received a JSON response which includes carts values with different amounts. "carts": { "value": [ { "Amt": 40 }, { "Amt": 20.25 }, ...

Angular 5 throws EmptyError when trying to create child routes with no elements in the sequence

I am facing a problem with navigating from the login page to the dashboard page when using children in routing. Here is my current routing configuration: const appRoutes: Routes = [ { path: '', redirectTo: 'login', pathMatch: 'ful ...

Troubleshooting auxiliary routes in Angular: why won't they work

I am facing an issue where I am trying to load components into a router outlet nested within another component. Within my ProgramComponent (with the selector app-program), I have defined a router outlet. <a routerLink="admin1">One</a> <a ro ...

Navigation problem in Angular: Page remains unchanged despite URL updating

I am experiencing an issue with two components, home and welcome. The welcome component contains a button that, when clicked, is supposed to take me to the 'home' page. However, instead of navigating to the home page and displaying its content, i ...

Performing bulk operations on all selected rows in a table using Angular 6

Within my Angular 6 web application, there is a table with checkboxes in each row. My goal is to be able to perform bulk actions on the selected rows, such as deleting them. One approach I considered was adding an isSelected boolean property to the data m ...

Tips on using the map and filter methods to narrow down a nested array based on specific object

I am struggling to filter JSON data based on a specific date using Array.Filter and Map. The code I have tried below is not yielding the desired results. Can someone please provide guidance on how to effectively filter JSON data based on a particular date ...

Implementing circular generic in Typescript tutorial

I have a question regarding the code snippet below: interface BaseProps<TControl> { onEvent: (control: TControl) => void; } class BaseControl<TValue, BaseProps<any>> { onBlur = () => { onEvent(this); //subscriber must see ...

Angular is throwing an error with code TS2322 stating that a number cannot be assigned to a string type

While trying to convert a PHP variable gender from string to number, I ran into the following error: Error TS2322: Type 'number' is not assignable to type 'string' in Angular Here's the code snippet: param = { id: '&ap ...

Is it feasible to stop closure from capturing external variables in TypeScript?

Imagine I have the following piece of Typescript code: const a = 456 const b = () => a Is there a way to make the Typescript compiler detect and flag an error or warning during compilation indicating that function b is accessing an external variable a ...

Discover the best method for sorting an array into a new array using Angular

const arr1 = [ { id: "c1", section: { name: "emerald", room: { id: "r1", name: "Room 1" } } }, { id: "c2", section: { name: "diamond", room: { id: ...

The CORS policy has blocked access to 'http://localhost:8080/BeginSignup' from 'http://localhost:4200'

I'm having trouble with a CORS policy error when sending a fetch POST request to my Golang AppEngine API. Although I don't understand why this error is occurring. Below is the code I'm using: Below is the Angular code calling the API: priva ...

Conceal the sidebar once user is logged in

I need a dynamic webpage; upon loading the login page, the sidebar should be hidden and the login page should occupy the full width of the page. Once the user successfully logs in, the sidebar along with all components should be displayed. I've attemp ...

Can you selectively load a specific CSS file in React based on certain conditions?

Currently, my front end app is running on React v17.0.2 and I have a requirement to conditionally load a CSS file based on a switch between dark mode and light mode. Below is the code snippet that achieves this functionality: function App() { const { C ...

Having trouble with deserialization in Asp.net Core Web API - need help!

Currently, I am working with ASP.net core web api alongside Angular 10. Upon sending a post request from the Angular project, the correct JSON object is being passed. The POST method on the API side is hit successfully. However, when attempting to deserial ...

Transform the list of answers into a diverse range of items

When making an API call, I receive a list of items structured as follows: interface Event { id: string type: EventType_Enum timestamp: Date payload: string } Each event has a differently structured JSON string in the payload variable based on its ...

What could be the reason behind the glob promise yielding an array with empty strings?

I am currently utilizing TypeScript, ts-node, npm, and path modules My objective is to generate an array of file names based on a specific pattern using glob-promise - an npm package I imported that offers promise-based functionality for the glob module. ...