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

Angular Architectures with Signals

Angular Architectures with Signals

Manfred Steyer
PRO

November 08, 2023
Tweet

More Decks by Manfred Steyer

Other Decks in Programming

Transcript

  1. @ManfredSteyer
    ManfredSteyer
    Angular Architectures with Signals

    View Slide

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

    View Slide

  3. @ManfredSteyer
    flights = signal([]);
    const flights = await this.flightService.findAsPromise(from, to);
    this.flights.set(flights);



    View Slide

  4. @ManfredSteyer
    Simple
    Reactive
    Building Block
    Fine-grained
    CD
    Zone-less CD
    Interop with
    RxJS
    No need to
    unsubscribe

    View Slide

  5. @ManfredSteyer

    View Slide

  6. @ManfredSteyer

    View Slide

  7. @ManfredSteyer
    Manfred Steyer

    View Slide

  8. @ManfredSteyer

    View Slide

  9. @ManfredSteyer

    View Slide

  10. @ManfredSteyer

    View Slide

  11. @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 Slide

  12. @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 Slide

  13. @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 Slide

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

    View Slide

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

    View Slide

  16. @ManfredSteyer

    View Slide

  17. @ManfredSteyer

    View Slide

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

    View Slide

  19. @ManfredSteyer

    View Slide

  20. @ManfredSteyer

    View Slide

  21. @ManfredSteyer

    View Slide

  22. @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 Slide

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

    View Slide

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

    View Slide

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

    View Slide

  26. @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 Slide

  27. @ManfredSteyer
    private state = signalState({
    filter: {
    from: 'Paris',
    to: 'London',
    },
    flights: [] as Flight[],
    basket: {} as Record,
    });
    filter = this.state.filter();
    from = this.state.filter.from();
    to = this.state.filter.to();

    View Slide

  28. @ManfredSteyer

    View Slide

  29. @ManfredSteyer

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  33. @ManfredSteyer

    View Slide

  34. @ManfredSteyer

    View Slide

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

    View Slide

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

    View Slide

  37. @ManfredSteyer
    export const FlightBookingStore = signalStore(
    […]
    withMethods((state) => {
    […]
    return {
    connectCriteria: rxMethod((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 }))
    ))
    };
    }),
    […]
    );

    View Slide

  38. @ManfredSteyer
    export const FlightBookingStore = signalStore(
    […]
    withHooks({
    onInit({ connectCriteria, criteria }) {
    connectCriteria(criteria);
    },
    }),
    );
    takes: Signal, Observable, T

    View Slide

  39. @ManfredSteyer

    View Slide

  40. @ManfredSteyer

    View Slide

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

    View Slide

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

    View Slide

  43. @ManfredSteyer

    View Slide

  44. @ManfredSteyer

    View Slide

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

    View Slide

  46. @ManfredSteyer
    const BooksStore = signalStore(
    withLoadEntities(BookService),
    withLoadEntities(AuthorService),
    );

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide