Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Architectures with Angular Signals @ngBucharest

Architectures with Angular Signals @ngBucharest

Manfred Steyer

October 12, 2023
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 _flights = signal<Flight[]>([]); readonly flights = this._flights.asReadonly(); async load(from: string, to: string) { const flights = await […]; this._flights.set(flights); } }
  3. @ManfredSteyer @Injectable({ providedIn: 'root' }) export class FlightBookingFacade { […]

    private _flights = signal<Flight[]>([]); readonly flights = this._flights.asReadonly(); async load(from: string, to: string) { const flights = await […]; this._flights.set(flights); } }
  4. @ManfredSteyer @Injectable({ providedIn: 'root' }) export class FlightBookingFacade { […]

    private _flights = signal<Flight[]>([]); readonly flights = this._flights.asReadonly(); private _from = signal('Hamburg'); readonly from = this._from.asReadonly(); private _to = signal('Graz'); readonly to = this._to.asReadonly(); […] }
  5. @ManfredSteyer @Injectable({ providedIn: 'root' }) export class FlightBookingFacade { […]

    private state = signal({ from: 'Hamburg', to: 'Graz', flights: [] as Flight[], […] }); […] }
  6. @ManfredSteyer @Injectable({ providedIn: 'root' }) export class FlightBookingFacade { […]

    private state = signal({ from: 'Hamburg', to: 'Graz', flights: [] as Flight[], […] }); flights = computed(() => this.state().flights); from = computed(() => this.state().from); […] }
  7. @ManfredSteyer @Injectable({ providedIn: 'root' }) export class FlightBookingFacade { private

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

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

    selected2 = selectSignal( this.flights, this.basket, (flights, basket) => flights.filter((f) => basket[f.id]) ); […] }
  10. @ManfredSteyer @Injectable({ providedIn: 'root' }) export class FlightBookingFacade { […]

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

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

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

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

    withState({ from: 'Paris', to: 'London', […] }), withSignals(([…]) => ({ […] })), withMethods(([…]) => ({ })), withHooks({ […] }), withCallState() );
  15. @ManfredSteyer • Maintainable Architectures • 🆕🔥 Standalone Components & APIs

    • Micro Frontends and Module Federation • 🆕🔥 Signals and Your Architecture: CD, NGRX, RxJS-Interop Public or Company Training, Remote or On-Site Next: Nov 27 to Nov 30, 2023 https://www.angulararchitects.io