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

Angular Architectures with Signals @ngPoland

Angular Architectures with Signals @ngPoland

Manfred Steyer

November 06, 2023
Tweet

More Decks by Manfred Steyer

Other Decks in Programming

Transcript

  1. @ManfredSteyer
    ManfredSteyer
    Angular Architectures with Signals

    View full-size slide

  2. @ManfredSteyer
    Signal
    as Producer
    4711
    Consumer
    read
    set
    notify
    4712

    View full-size slide

  3. @ManfredSteyer

    View full-size slide

  4. @ManfredSteyer

    View full-size slide

  5. @ManfredSteyer
    Manfred Steyer

    View full-size slide

  6. @ManfredSteyer

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  10. @ManfredSteyer

    View full-size slide

  11. @ManfredSteyer
    select(selector)
    selectSignal(selector)

    View full-size slide

  12. @ManfredSteyer

    View full-size slide

  13. @ManfredSteyer

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  19. @ManfredSteyer

    View full-size slide

  20. @ManfredSteyer
    export const FlightBookingStore = signalStore(
    { providedIn: 'root' },
    […]
    );

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  23. @ManfredSteyer

    View full-size slide

  24. @ManfredSteyer
    export const FlightBookingStore = signalStore(
    { providedIn: 'root' },
    withState({
    from: 'Paris',
    to: 'London',
    […]
    }),
    withSignals(([…]) => ({ […] })),
    withMethods(([…]) => ({ })),
    withHooks({ […] }),
    withCallState()
    );

    View full-size slide

  25. @ManfredSteyer
    export const FlightBookingStore = signalStore(
    { providedIn: 'root' },
    withState({
    from: 'Paris',
    to: 'London',
    […]
    }),
    withSignals(([…]) => ({ […] })),
    withMethods(([…]) => ({ })),
    withHooks({ […] }),
    withCallState()
    );

    View full-size slide

  26. @ManfredSteyer

    View full-size slide

  27. @ManfredSteyer

    View full-size slide

  28. @ManfredSteyer
    const BooksStore = signalStore(
    withEntities({ collection: 'book' }),
    withEntities({ collection: 'author' })
    );

    View full-size slide

  29. @ManfredSteyer
    Free eBook (5th Edition)
    ANGULARarchitects.io/book
    Module Federation & Nx

    View full-size slide

  30. @ManfredSteyer
    Services +
    Signals
    NGRX
    NGRX
    Signal Store
    Different
    Flavors
    rxMethod
    Custom
    Features

    View full-size slide

  31. @ManfredSteyer
    d
    Slides & Examples Remote Company Workshops
    and Consulting
    http://angulararchitects.io

    View full-size slide