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

Lightweight Architectures With Angular's Lastest Features @basta 2023

Lightweight Architectures With Angular's Lastest Features @basta 2023

Manfred Steyer

September 27, 2023
Tweet

More Decks by Manfred Steyer

Other Decks in Programming

Transcript

  1. @ManfredSteyer
    Lightweight Architectures
    With Angular's Lastest Features
    ManfredSteyer

    View full-size slide

  2. @ManfredSteyer

    View full-size slide

  3. @ManfredSteyer

    View full-size slide

  4. @ManfredSteyer

    View full-size slide

  5. @ManfredSteyer

    View full-size slide

  6. @ManfredSteyer

    View full-size slide

  7. @ManfredSteyer
    Manfred Steyer

    View full-size slide

  8. @ManfredSteyer

    View full-size slide

  9. @ManfredSteyer
    NgModules + EcmaScript Modules
    import { NgModule } from '@angular/core';
    import { BrowserModule } from '@angular/platform-browser';
    import { AppComponent } from './app.component';
    […]
    @NgModule({
    imports: [BrowserModule, OtherModule],
    declarations: [AppComponent, OtherComponent, OtherDirective],
    providers: [],
    bootstrap: [AppComponent],
    })
    export class AppModule {}
    TypeScript Modules
    Angular Modules

    View full-size slide

  10. @ManfredSteyer
    @Component({
    standalone: true,
    imports: [
    […],
    FlightCardComponent,
    CityPipe,
    CityValidator,
    ],
    selector: 'flight-search',
    templateUrl: '…'
    })
    export class FlightSearchComponent {
    […]
    }

    View full-size slide

  11. @ManfredSteyer

    View full-size slide

  12. @ManfredSteyer
    Small and Medium Apps:
    Folder per Feature

    View full-size slide

  13. @ManfredSteyer
    Your Public APIs:
    Barrels
    // index.ts == Public API
    export *
    from './flight-booking.routes';

    View full-size slide

  14. @ManfredSteyer
    Medium and Large Apps:
    Folder per Domain

    View full-size slide

  15. @ManfredSteyer
    Restricting Access b/w Domains, etc.
    on a library basis

    View full-size slide

  16. @ManfredSteyer
    Restricting Access
    on a folder basis
    Tech Lead
    Rainer Hahnekamp,
    AngularArchitects
    @softarc/eslint-plugin-sheriff

    View full-size slide

  17. @ManfredSteyer

    View full-size slide

  18. @ManfredSteyer
    Free eBook
    ANGULARarchitects.io/standalone
    Standalone Components
    Download here:

    View full-size slide

  19. @ManfredSteyer

    View full-size slide

  20. @ManfredSteyer
    Zone.js:
    Monkey
    Patching
    After Event
    Handler:
    Inform Angular
    CD Checks
    Components
    all components (default)
    or selected ones (OnPush)

    View full-size slide

  21. @ManfredSteyer
    Zone.js:
    Magic
    Zone.js:
    ~100K
    Cannot patch
    async/await
    coarse
    grained CD

    View full-size slide

  22. @ManfredSteyer
    Signals!

    View full-size slide

  23. @ManfredSteyer
    Signals!

    View full-size slide

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

    View full-size slide

  25. @ManfredSteyer
    flights: Flight[] = [];
    const flights = await this.flightService.findAsPromise(from, to);
    this.flights = flights;



    View full-size slide

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



    View full-size slide

  27. @ManfredSteyer

    View full-size slide

  28. @ManfredSteyer

    View full-size slide

  29. @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

  30. @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

  31. @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

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  35. @ManfredSteyer

    View full-size slide

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

    View full-size slide

  37. @ManfredSteyer

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  43. @ManfredSteyer

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  47. @ManfredSteyer

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  50. @ManfredSteyer

    View full-size slide

  51. @ManfredSteyer

    View full-size slide

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

    View full-size slide