$30 off During Our Annual Pro Sale. View Details »

The New NGRX Signal Store for Angular 3+n Flavo...

The New NGRX Signal Store for Angular 3+n Flavors @enterJS 2014 in Darmstadt

Manfred Steyer

May 07, 2024
Tweet

More Decks by Manfred Steyer

Other Decks in Programming

Transcript

  1. @ManfredSteyer flights = signal<Flight[]>([]); const flights = await this.flightService.findAsPromise(from, to);

    this.flights.set(flights); <div *ngFor="let f of flights()"> <flight-card [item]="f" /> </div>
  2. @ManfredSteyer @Injectable({ providedIn: 'root' }) export class FlightBookingFacade { private

    state = signalState({ from: 'Paris', to: 'London', flights: [] as Flight[], basket: {} as Record<number, boolean>, }); readonly flights = this.state.flights; readonly from = this.state.from; […] }
  3. @ManfredSteyer @Injectable({ providedIn: 'root' }) export class FlightBookingFacade { […]

    readonly selected = computed( () => this.flights().filter((f) => this.basket()[f.id]) ); […] }
  4. @ManfredSteyer @Injectable({ providedIn: 'root' }) export class FlightBookingFacade { […]

    updateCriteria(from: string, to: string): void { patchState(this.state, { from, to }) } […] }
  5. @ManfredSteyer @Injectable({ providedIn: 'root' }) export class FlightBookingFacade { […]

    updateCriteria(from: string, to: string): void { patchState(this.state, (state) => ({ from: state.to, to: state.from })); } […] }
  6. @ManfredSteyer export const FlightBookingStore = signalStore( { providedIn: 'root' },

    withState({ from: 'Paris', to: 'London', […] }), […] );
  7. @ManfredSteyer export const FlightBookingStore = signalStore( { providedIn: 'root' },

    withState({ from: 'Paris', to: 'London', […] }), withComputed(([…]) => ({ […] })), withMethods(([…]) => ({ })), withHooks({ […] }) );
  8. @ManfredSteyer export const FlightBookingStore = signalStore( […] withMethods((state) => {

    […] return { loadByCriteria: rxMethod<Criteria>((c$) => c$.pipe([…])) }; }), […] );
  9. @ManfredSteyer export const FlightBookingStore = signalStore( […] withMethods((state) => {

    […] return { loadByCriteria: rxMethod<Criteria>((c$) => c$.pipe([…])) }; }), […] );
  10. @ManfredSteyer export const FlightBookingStore = signalStore( […] withMethods((state) => {

    […] return { loadByCriteria: rxMethod<Criteria>((c$) => c$.pipe( filter(c => c.from.length >= 3 && c.to.length >= 3), debounceTime(300), switchMap((c) => flightService.find(c.from, c.to)), tap(flights => patchState(state, { flights })) )) }; }), […] );
  11. @ManfredSteyer export const FlightBookingStore = signalStore( […] withMethods((state) => {

    […] return { loadByCriteria: rxMethod<Criteria>((c$) => c$.pipe( filter(c => c.from.length >= 3 && c.to.length >= 3), debounceTime(300), switchMap((c) => flightService.find(c.from, c.to)), tap(flights => patchState(state, { flights })) )) }; }), […] );
  12. @ManfredSteyer export const FlightBookingStore = signalStore( […] withHooks({ onInit({ loadByCriteria,

    criteria }) { loadByCriteria(criteria); }, }), ); takes: Signal<T>, Observable<T>, T
  13. @ManfredSteyer Signals: Future of CD Take Care of Data Flow

    Stores are the Missing Link Signal Store Lightweight Highly Extensible