Slide 1

Slide 1 text

@ManfredSteyer Lightweight Architectures With Angular's Signals and Standalone ManfredSteyer

Slide 2

Slide 2 text

@ManfredSteyer

Slide 3

Slide 3 text

@ManfredSteyer

Slide 4

Slide 4 text

@ManfredSteyer

Slide 5

Slide 5 text

@ManfredSteyer

Slide 6

Slide 6 text

@ManfredSteyer

Slide 7

Slide 7 text

@ManfredSteyer Manfred Steyer

Slide 8

Slide 8 text

@ManfredSteyer

Slide 9

Slide 9 text

@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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

@ManfredSteyer

Slide 12

Slide 12 text

@ManfredSteyer Small and Medium Apps: Folder per Feature

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

@ManfredSteyer Medium and Large Apps: Folder per Domain

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

@ManfredSteyer

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

@ManfredSteyer

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

@ManfredSteyer Zone.js: Magic Zone.js: ~100K Cannot patch async/await coarse grained CD e.g. Components are always checked as a whole, even if only a tiny fraction changed

Slide 22

Slide 22 text

@ManfredSteyer Signals!

Slide 23

Slide 23 text

@ManfredSteyer Signals!

Slide 24

Slide 24 text

@ManfredSteyer Signal as Producer 4711 Consumer read set notify 4712

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

@ManfredSteyer

Slide 28

Slide 28 text

@ManfredSteyer

Slide 29

Slide 29 text

@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); } }

Slide 30

Slide 30 text

@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); } }

Slide 31

Slide 31 text

@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(); […] }

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

@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 }); […] }

Slide 34

Slide 34 text

@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

Slide 35

Slide 35 text

@ManfredSteyer

Slide 36

Slide 36 text

@ManfredSteyer select(selector) selectSignal(selector)

Slide 37

Slide 37 text

@ManfredSteyer

Slide 38

Slide 38 text

@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; […] }

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

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

Slide 41

Slide 41 text

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

Slide 42

Slide 42 text

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

Slide 43

Slide 43 text

@ManfredSteyer

Slide 44

Slide 44 text

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

Slide 45

Slide 45 text

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

Slide 46

Slide 46 text

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

Slide 47

Slide 47 text

@ManfredSteyer

Slide 48

Slide 48 text

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

Slide 49

Slide 49 text

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

Slide 50

Slide 50 text

@ManfredSteyer

Slide 51

Slide 51 text

@ManfredSteyer

Slide 52

Slide 52 text

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