際際滷

際際滷Share a Scribd company logo
Angular Frankfurt | #angularffm | @angularffm | fb.com/angularfrankfurt
Florian Reifschneider
Founder at Rocketloop
florian@rocketloop.de | @flo_re2003
Managing Application State
in Reactive Angular Applications
Angular Frankfurt | #angularffm | @angularffm | fb.com/angularfrankfurt
What this talk is about
2
Managing Application State
in Reactive Angular Applications
Angular Frankfurt | #angularffm | @angularffm | fb.com/angularfrankfurt
What this talk is about
3
Managing Application State
in Reactive Angular Applications
Angular Frankfurt | #angularffm | @angularffm | fb.com/angularfrankfurt
What this talk is about
4
Managing Application State
in Reactive Angular Applications
Angular Frankfurt | #angularffm | @angularffm | fb.com/angularfrankfurt
What this talk is about
RxJS
5
Managing Application State
in Reactive Angular Applications
Angular ngrx
Angular Frankfurt | #angularffm | @angularffm | fb.com/angularfrankfurt
Why reactive apps are cool
? Apps should be consistent
? Data is not static, but rather a stream
? Views should automatically react to changes in data
? Observables are first-level citizen in Angular
6
Angular Frankfurt | #angularffm | @angularffm | fb.com/angularfrankfurt
Imperative and reactive patterns
? An imperative expression is calculated once, the result
never changes, even when the inputs change
? A reactive expression is calculated every time the
inputs change, emitting a new result to all subscribers
7
A B C+ =
Imperative
A1
B1
C1
+
=
B2
C2
A2
C3
t
Reactive
Angular Frankfurt | #angularffm | @angularffm | fb.com/angularfrankfurt
Application state
? Data fetched from server/API/back-end
? Locally created data
? Transient state / cache
? UI state
8
Angular Frankfurt | #angularffm | @angularffm | fb.com/angularfrankfurt
Imperative state management in Angular
9
Service Service Service
Component Component
Internal
State
Internal
State
Internal
State
Angular Frankfurt | #angularffm | @angularffm | fb.com/angularfrankfurt
Unidirectional data flow (Flux)
10
Action Dispatcher Store View
Action
This is where the app state lives
Angular Frankfurt | #angularffm | @angularffm | fb.com/angularfrankfurt
Redux-like architecture using ngrx/store
11
Component
Action
State
Reducer
ngrx/store
Store
subscribe
dispatch
Component
ActionState
Reducer
Service
ngrx/store +
Services
method callsubscribe
Store
dispatch
Angular Frankfurt | #angularffm | @angularffm | fb.com/angularfrankfurt 12
Let¨s see some code!
Angular Frankfurt | #angularffm | @angularffm | fb.com/angularfrankfurt
State
13
interface Todo {
id: string;
title: string;
done: boolean;
}
interface TodoState {
todos: {
[id: string]: Todo;
};
todoList: string[];
}
{
'todoList': [
'93f37bdf-6a45-4243-913c-cd057c91c2bc',
'5d744a71-39d3-4afe-8a98-66fa3464f2aa'
],
'todos': {
'93f37bdf-6a45-4243-913c-cd057c91c2bc': {
'id': '93f37bdf-6a45-4243-913c-cd057c91c2bc',
'title': 'Buy some milk',
'done': false
},
'5d744a71-39d3-4afe-8a98-66fa3464f2aa': {
'id': '5d744a71-39d3-4afe-8a98-66fa3464f2aa',
'title': 'Take out the trash',
'done': false
},
}
}
The state is a plain object (with a fixed schema) that represents the
entire application state
Angular Frankfurt | #angularffm | @angularffm | fb.com/angularfrankfurt
Action
14
An action is a plain object that represents the intended change to the
state
interface Action {
type: string;
payload?: any;
}
this.store.dispatch({
'type': 'TODOLIST:CREATE_TODO',
'payload': {
'title': 'Work on Todo app'
}
}
this.store.dispatch({
'type': 'TODOLIST:MARK_TODO_DONE',
'payload': {
'id': '93f37bdf-6a45-4243-913c-cd057c91c2bc'
}
}
Actions are the only way to
mutate (modify) the state
Actions can be understood
as state transitions of our
state machine
Angular Frankfurt | #angularffm | @angularffm | fb.com/angularfrankfurt
Reducer
15
A reducer is a plain function that accepts a state and an action and
returns a new version of the state
function todoReducer(state: TodoState, action: Action) {
switch (action.type) {
case "TODOLIST:CREATE_TODO":
const id = guid();
return {
...state,
'todoList': [ ...state.todoList, id ],
'todos': {
...state.todos,
[id]: {
'id': id,
'title': action.payload.title,
'done': false
}
}
}
default:
return state;
}
}
Angular Frankfurt | #angularffm | @angularffm | fb.com/angularfrankfurt
Component
16
<ul *ngFor="let todo of todos$ | async">
<li>{{todo.title}}</li>
</ul>
? The component can select a slice/subtree of the state from the store
? Using the resulting Observable, the component can reactively listen for
changes in the selected subtree
todos$: Observable<Todo[]>;
constructor(private store: Store<TodoState>) {
this.todos$ = Observable.combineLatest(
this.store.select('todoList'),
this.store.select('todos')
).map([todoList, todos] => {
return todoList.map(id => todos[id]);
});
}
Angular Frankfurt | #angularffm | @angularffm | fb.com/angularfrankfurt
Further thoughts
¢ How do you handle asynchronous side effects?
$ ngrx/effects!
¢ Does it work with lazy loading?
$ Since ngrx 4.0, yes!
¢ How can you debug reducers/actions/states?
$ ngrx/devtools to the rescue!
17
Angular Frankfurt | #angularffm | @angularffm | fb.com/angularfrankfurt 18
Questions?

More Related Content

Similar to Angular Frankfurt #1 - Managing Application State in Reactive Angular Applications (20)

Tamir Dresher Clarizen adventures with the wild GC during the holiday season
Tamir Dresher   Clarizen adventures with the wild GC during the holiday seasonTamir Dresher   Clarizen adventures with the wild GC during the holiday season
Tamir Dresher Clarizen adventures with the wild GC during the holiday season
Tamir Dresher
?
From GameMaker to Game Baker - Porting Hotline Miami
From GameMaker to Game Baker - Porting Hotline MiamiFrom GameMaker to Game Baker - Porting Hotline Miami
From GameMaker to Game Baker - Porting Hotline Miami
Frans Kasper
?
Spark Summit EU talk by William Benton
Spark Summit EU talk by William BentonSpark Summit EU talk by William Benton
Spark Summit EU talk by William Benton
Spark Summit
?
Two Years with botnet Asprox - Michal Ambro?
Two Years with botnet Asprox - Michal Ambro?Two Years with botnet Asprox - Michal Ambro?
Two Years with botnet Asprox - Michal Ambro?
Security Session
?
Analyze Git Commit
Analyze Git CommitAnalyze Git Commit
Analyze Git Commit
guest387c12
?
Analyze Git Commit
Analyze Git CommitAnalyze Git Commit
Analyze Git Commit
guest387c12
?
What's Missing? Microservices Meetup at Cisco
What's Missing? Microservices Meetup at CiscoWhat's Missing? Microservices Meetup at Cisco
What's Missing? Microservices Meetup at Cisco
Adrian Cockcroft
?
[BreizhCamp, format 15min] Construire et automatiser l'ecosyste?me de son Saa...
[BreizhCamp, format 15min] Construire et automatiser l'ecosyste?me de son Saa...[BreizhCamp, format 15min] Construire et automatiser l'ecosyste?me de son Saa...
[BreizhCamp, format 15min] Construire et automatiser l'ecosyste?me de son Saa...
Fran?ois-Guillaume Ribreau
?
Make ARM Shellcode Great Again
Make ARM Shellcode Great AgainMake ARM Shellcode Great Again
Make ARM Shellcode Great Again
Saumil Shah
?
[Golang] 參 Mobile App 垢殻叔ァ低M秘 Golang 議弊順 (Introduction of GoLang)
[Golang] 參 Mobile App 垢殻叔ァ低M秘 Golang 議弊順 (Introduction of GoLang) [Golang] 參 Mobile App 垢殻叔ァ低M秘 Golang 議弊順 (Introduction of GoLang)
[Golang] 參 Mobile App 垢殻叔ァ低M秘 Golang 議弊順 (Introduction of GoLang)
Johnny Sung
?
Server Side Swift
Server Side SwiftServer Side Swift
Server Side Swift
Jens Ravens
?
An intro to the JAMStack and Eleventy
An intro to the JAMStack and EleventyAn intro to the JAMStack and Eleventy
An intro to the JAMStack and Eleventy
Luciano Mammino
?
Greyhound - Powerful Pure Functional Kafka Library
Greyhound - Powerful Pure Functional Kafka LibraryGreyhound - Powerful Pure Functional Kafka Library
Greyhound - Powerful Pure Functional Kafka Library
Natan Silnitsky
?
REST API vs gRPC, which one should you use in breaking a monolith [Dev conf 2...
REST API vs gRPC, which one should you use in breaking a monolith [Dev conf 2...REST API vs gRPC, which one should you use in breaking a monolith [Dev conf 2...
REST API vs gRPC, which one should you use in breaking a monolith [Dev conf 2...
Vladimir Dejanovic
?
JCConf 2018, SpringCloud to Mircoservice
JCConf 2018, SpringCloud to MircoserviceJCConf 2018, SpringCloud to Mircoservice
JCConf 2018, SpringCloud to Mircoservice
Rhys Chang
?
Writing Testable Code (for Magento 1 and 2)
Writing Testable Code (for Magento 1 and 2)Writing Testable Code (for Magento 1 and 2)
Writing Testable Code (for Magento 1 and 2)
vinaikopp
?
mri ruby gil
mri ruby gilmri ruby gil
mri ruby gil
achempion
?
Serverless orchestration and automation with Cloud Workflows
Serverless orchestration and automation with Cloud WorkflowsServerless orchestration and automation with Cloud Workflows
Serverless orchestration and automation with Cloud Workflows
M│rton Kodok
?
Traffic Signal System.pptx
Traffic Signal System.pptxTraffic Signal System.pptx
Traffic Signal System.pptx
moviestadaka
?
Asynchronous programming 20180607
Asynchronous programming 20180607Asynchronous programming 20180607
Asynchronous programming 20180607
Yifan Xing
?
Tamir Dresher Clarizen adventures with the wild GC during the holiday season
Tamir Dresher   Clarizen adventures with the wild GC during the holiday seasonTamir Dresher   Clarizen adventures with the wild GC during the holiday season
Tamir Dresher Clarizen adventures with the wild GC during the holiday season
Tamir Dresher
?
From GameMaker to Game Baker - Porting Hotline Miami
From GameMaker to Game Baker - Porting Hotline MiamiFrom GameMaker to Game Baker - Porting Hotline Miami
From GameMaker to Game Baker - Porting Hotline Miami
Frans Kasper
?
Spark Summit EU talk by William Benton
Spark Summit EU talk by William BentonSpark Summit EU talk by William Benton
Spark Summit EU talk by William Benton
Spark Summit
?
Two Years with botnet Asprox - Michal Ambro?
Two Years with botnet Asprox - Michal Ambro?Two Years with botnet Asprox - Michal Ambro?
Two Years with botnet Asprox - Michal Ambro?
Security Session
?
What's Missing? Microservices Meetup at Cisco
What's Missing? Microservices Meetup at CiscoWhat's Missing? Microservices Meetup at Cisco
What's Missing? Microservices Meetup at Cisco
Adrian Cockcroft
?
[BreizhCamp, format 15min] Construire et automatiser l'ecosyste?me de son Saa...
[BreizhCamp, format 15min] Construire et automatiser l'ecosyste?me de son Saa...[BreizhCamp, format 15min] Construire et automatiser l'ecosyste?me de son Saa...
[BreizhCamp, format 15min] Construire et automatiser l'ecosyste?me de son Saa...
Fran?ois-Guillaume Ribreau
?
Make ARM Shellcode Great Again
Make ARM Shellcode Great AgainMake ARM Shellcode Great Again
Make ARM Shellcode Great Again
Saumil Shah
?
[Golang] 參 Mobile App 垢殻叔ァ低M秘 Golang 議弊順 (Introduction of GoLang)
[Golang] 參 Mobile App 垢殻叔ァ低M秘 Golang 議弊順 (Introduction of GoLang) [Golang] 參 Mobile App 垢殻叔ァ低M秘 Golang 議弊順 (Introduction of GoLang)
[Golang] 參 Mobile App 垢殻叔ァ低M秘 Golang 議弊順 (Introduction of GoLang)
Johnny Sung
?
An intro to the JAMStack and Eleventy
An intro to the JAMStack and EleventyAn intro to the JAMStack and Eleventy
An intro to the JAMStack and Eleventy
Luciano Mammino
?
Greyhound - Powerful Pure Functional Kafka Library
Greyhound - Powerful Pure Functional Kafka LibraryGreyhound - Powerful Pure Functional Kafka Library
Greyhound - Powerful Pure Functional Kafka Library
Natan Silnitsky
?
REST API vs gRPC, which one should you use in breaking a monolith [Dev conf 2...
REST API vs gRPC, which one should you use in breaking a monolith [Dev conf 2...REST API vs gRPC, which one should you use in breaking a monolith [Dev conf 2...
REST API vs gRPC, which one should you use in breaking a monolith [Dev conf 2...
Vladimir Dejanovic
?
JCConf 2018, SpringCloud to Mircoservice
JCConf 2018, SpringCloud to MircoserviceJCConf 2018, SpringCloud to Mircoservice
JCConf 2018, SpringCloud to Mircoservice
Rhys Chang
?
Writing Testable Code (for Magento 1 and 2)
Writing Testable Code (for Magento 1 and 2)Writing Testable Code (for Magento 1 and 2)
Writing Testable Code (for Magento 1 and 2)
vinaikopp
?
Serverless orchestration and automation with Cloud Workflows
Serverless orchestration and automation with Cloud WorkflowsServerless orchestration and automation with Cloud Workflows
Serverless orchestration and automation with Cloud Workflows
M│rton Kodok
?
Traffic Signal System.pptx
Traffic Signal System.pptxTraffic Signal System.pptx
Traffic Signal System.pptx
moviestadaka
?
Asynchronous programming 20180607
Asynchronous programming 20180607Asynchronous programming 20180607
Asynchronous programming 20180607
Yifan Xing
?

Recently uploaded (20)

Codequiry: A Code Similarity Checker Every Developer Should Know
Codequiry: A Code Similarity Checker Every Developer Should KnowCodequiry: A Code Similarity Checker Every Developer Should Know
Codequiry: A Code Similarity Checker Every Developer Should Know
Code Quiry
?
CitrineOS: Bridging the Past and Future of EV Charging with OCPP 1.6 & 2.x Su...
CitrineOS: Bridging the Past and Future of EV Charging with OCPP 1.6 & 2.x Su...CitrineOS: Bridging the Past and Future of EV Charging with OCPP 1.6 & 2.x Su...
CitrineOS: Bridging the Past and Future of EV Charging with OCPP 1.6 & 2.x Su...
DanBrown980551
?
Meet, Greet, and Explore Agentic AI with UiPath Scotland
Meet, Greet, and Explore Agentic AI with UiPath ScotlandMeet, Greet, and Explore Agentic AI with UiPath Scotland
Meet, Greet, and Explore Agentic AI with UiPath Scotland
UiPathCommunity
?
Handout - Demonolithing Think Again (Devoxx GR 2025).pdf
Handout - Demonolithing Think Again (Devoxx GR 2025).pdfHandout - Demonolithing Think Again (Devoxx GR 2025).pdf
Handout - Demonolithing Think Again (Devoxx GR 2025).pdf
Scott Sosna
?
Presentation Session 4 -Agent Builder.pdf
Presentation Session 4 -Agent Builder.pdfPresentation Session 4 -Agent Builder.pdf
Presentation Session 4 -Agent Builder.pdf
Mukesh Kala
?
SAP Automation with UiPath: Leveraging AI for SAP Automation - Part 8 of 8
SAP Automation with UiPath: Leveraging AI for SAP Automation - Part 8 of 8SAP Automation with UiPath: Leveraging AI for SAP Automation - Part 8 of 8
SAP Automation with UiPath: Leveraging AI for SAP Automation - Part 8 of 8
DianaGray10
?
BrightonSEO April 2025 - hreflang XML E-Commerce - Nick Samuel.pdf
BrightonSEO April 2025 - hreflang XML E-Commerce - Nick Samuel.pdfBrightonSEO April 2025 - hreflang XML E-Commerce - Nick Samuel.pdf
BrightonSEO April 2025 - hreflang XML E-Commerce - Nick Samuel.pdf
Nick Samuel
?
CEE Software Development M&A Report 2025
CEE Software Development M&A Report 2025CEE Software Development M&A Report 2025
CEE Software Development M&A Report 2025
Yevgen Sysoyev
?
Ricardo Jebb Bruno - A Structural CAD Technician
Ricardo Jebb Bruno - A Structural CAD TechnicianRicardo Jebb Bruno - A Structural CAD Technician
Ricardo Jebb Bruno - A Structural CAD Technician
Ricardo Jebb Bruno
?
Artificial Neural Networks, basics, its variations and examples
Artificial Neural Networks, basics, its variations and examplesArtificial Neural Networks, basics, its variations and examples
Artificial Neural Networks, basics, its variations and examples
anandsimple
?
Health Promotion explained ppt.pptx
Health Promotion  explained ppt.pptxHealth Promotion  explained ppt.pptx
Health Promotion explained ppt.pptx
MohamedIbrahim354734
?
April Patch Tuesday
April Patch TuesdayApril Patch Tuesday
April Patch Tuesday
Ivanti
?
Introduction to Generative AI refers to a subset of artificial intelligence
Introduction to Generative AI refers to a subset of artificial intelligenceIntroduction to Generative AI refers to a subset of artificial intelligence
Introduction to Generative AI refers to a subset of artificial intelligence
Kongu Engineering College, Perundurai, Erode
?
Automating Behavior-Driven Development: Boosting Productivity with Template-D...
Automating Behavior-Driven Development: Boosting Productivity with Template-D...Automating Behavior-Driven Development: Boosting Productivity with Template-D...
Automating Behavior-Driven Development: Boosting Productivity with Template-D...
DOCOMO Innovations, Inc.
?
MariaDB Berlin Roadshow 際際滷s - 8 April 2025
MariaDB Berlin Roadshow 際際滷s - 8 April 2025MariaDB Berlin Roadshow 際際滷s - 8 April 2025
MariaDB Berlin Roadshow 際際滷s - 8 April 2025
MariaDB plc
?
Misc: The Internet Story - How Data Travels, Transit Works, and the Role of C...
Misc: The Internet Story - How Data Travels, Transit Works, and the Role of C...Misc: The Internet Story - How Data Travels, Transit Works, and the Role of C...
Misc: The Internet Story - How Data Travels, Transit Works, and the Role of C...
3G4G
?
Cybersecurity-Threat-Landscape-March-31-April-7-2025.pdf
Cybersecurity-Threat-Landscape-March-31-April-7-2025.pdfCybersecurity-Threat-Landscape-March-31-April-7-2025.pdf
Cybersecurity-Threat-Landscape-March-31-April-7-2025.pdf
Joe Shenouda
?
Cloudflare¨s Game-Changing Move The First Remote MCP Server for AI Agent Deve...
Cloudflare¨s Game-Changing Move The First Remote MCP Server for AI Agent Deve...Cloudflare¨s Game-Changing Move The First Remote MCP Server for AI Agent Deve...
Cloudflare¨s Game-Changing Move The First Remote MCP Server for AI Agent Deve...
davidandersonofficia
?
ScotSecure Cyber Security Summit 2025 Edinburgh
ScotSecure Cyber Security Summit 2025 EdinburghScotSecure Cyber Security Summit 2025 Edinburgh
ScotSecure Cyber Security Summit 2025 Edinburgh
Ray Bugg
?
AC2-Agile-Agile concepts in an enterprise environment
AC2-Agile-Agile concepts in an enterprise environmentAC2-Agile-Agile concepts in an enterprise environment
AC2-Agile-Agile concepts in an enterprise environment
Dennis Van Aelst
?
Codequiry: A Code Similarity Checker Every Developer Should Know
Codequiry: A Code Similarity Checker Every Developer Should KnowCodequiry: A Code Similarity Checker Every Developer Should Know
Codequiry: A Code Similarity Checker Every Developer Should Know
Code Quiry
?
CitrineOS: Bridging the Past and Future of EV Charging with OCPP 1.6 & 2.x Su...
CitrineOS: Bridging the Past and Future of EV Charging with OCPP 1.6 & 2.x Su...CitrineOS: Bridging the Past and Future of EV Charging with OCPP 1.6 & 2.x Su...
CitrineOS: Bridging the Past and Future of EV Charging with OCPP 1.6 & 2.x Su...
DanBrown980551
?
Meet, Greet, and Explore Agentic AI with UiPath Scotland
Meet, Greet, and Explore Agentic AI with UiPath ScotlandMeet, Greet, and Explore Agentic AI with UiPath Scotland
Meet, Greet, and Explore Agentic AI with UiPath Scotland
UiPathCommunity
?
Handout - Demonolithing Think Again (Devoxx GR 2025).pdf
Handout - Demonolithing Think Again (Devoxx GR 2025).pdfHandout - Demonolithing Think Again (Devoxx GR 2025).pdf
Handout - Demonolithing Think Again (Devoxx GR 2025).pdf
Scott Sosna
?
Presentation Session 4 -Agent Builder.pdf
Presentation Session 4 -Agent Builder.pdfPresentation Session 4 -Agent Builder.pdf
Presentation Session 4 -Agent Builder.pdf
Mukesh Kala
?
SAP Automation with UiPath: Leveraging AI for SAP Automation - Part 8 of 8
SAP Automation with UiPath: Leveraging AI for SAP Automation - Part 8 of 8SAP Automation with UiPath: Leveraging AI for SAP Automation - Part 8 of 8
SAP Automation with UiPath: Leveraging AI for SAP Automation - Part 8 of 8
DianaGray10
?
BrightonSEO April 2025 - hreflang XML E-Commerce - Nick Samuel.pdf
BrightonSEO April 2025 - hreflang XML E-Commerce - Nick Samuel.pdfBrightonSEO April 2025 - hreflang XML E-Commerce - Nick Samuel.pdf
BrightonSEO April 2025 - hreflang XML E-Commerce - Nick Samuel.pdf
Nick Samuel
?
CEE Software Development M&A Report 2025
CEE Software Development M&A Report 2025CEE Software Development M&A Report 2025
CEE Software Development M&A Report 2025
Yevgen Sysoyev
?
Ricardo Jebb Bruno - A Structural CAD Technician
Ricardo Jebb Bruno - A Structural CAD TechnicianRicardo Jebb Bruno - A Structural CAD Technician
Ricardo Jebb Bruno - A Structural CAD Technician
Ricardo Jebb Bruno
?
Artificial Neural Networks, basics, its variations and examples
Artificial Neural Networks, basics, its variations and examplesArtificial Neural Networks, basics, its variations and examples
Artificial Neural Networks, basics, its variations and examples
anandsimple
?
April Patch Tuesday
April Patch TuesdayApril Patch Tuesday
April Patch Tuesday
Ivanti
?
Automating Behavior-Driven Development: Boosting Productivity with Template-D...
Automating Behavior-Driven Development: Boosting Productivity with Template-D...Automating Behavior-Driven Development: Boosting Productivity with Template-D...
Automating Behavior-Driven Development: Boosting Productivity with Template-D...
DOCOMO Innovations, Inc.
?
MariaDB Berlin Roadshow 際際滷s - 8 April 2025
MariaDB Berlin Roadshow 際際滷s - 8 April 2025MariaDB Berlin Roadshow 際際滷s - 8 April 2025
MariaDB Berlin Roadshow 際際滷s - 8 April 2025
MariaDB plc
?
Misc: The Internet Story - How Data Travels, Transit Works, and the Role of C...
Misc: The Internet Story - How Data Travels, Transit Works, and the Role of C...Misc: The Internet Story - How Data Travels, Transit Works, and the Role of C...
Misc: The Internet Story - How Data Travels, Transit Works, and the Role of C...
3G4G
?
Cybersecurity-Threat-Landscape-March-31-April-7-2025.pdf
Cybersecurity-Threat-Landscape-March-31-April-7-2025.pdfCybersecurity-Threat-Landscape-March-31-April-7-2025.pdf
Cybersecurity-Threat-Landscape-March-31-April-7-2025.pdf
Joe Shenouda
?
Cloudflare¨s Game-Changing Move The First Remote MCP Server for AI Agent Deve...
Cloudflare¨s Game-Changing Move The First Remote MCP Server for AI Agent Deve...Cloudflare¨s Game-Changing Move The First Remote MCP Server for AI Agent Deve...
Cloudflare¨s Game-Changing Move The First Remote MCP Server for AI Agent Deve...
davidandersonofficia
?
ScotSecure Cyber Security Summit 2025 Edinburgh
ScotSecure Cyber Security Summit 2025 EdinburghScotSecure Cyber Security Summit 2025 Edinburgh
ScotSecure Cyber Security Summit 2025 Edinburgh
Ray Bugg
?
AC2-Agile-Agile concepts in an enterprise environment
AC2-Agile-Agile concepts in an enterprise environmentAC2-Agile-Agile concepts in an enterprise environment
AC2-Agile-Agile concepts in an enterprise environment
Dennis Van Aelst
?

Angular Frankfurt #1 - Managing Application State in Reactive Angular Applications

  • 1. Angular Frankfurt | #angularffm | @angularffm | fb.com/angularfrankfurt Florian Reifschneider Founder at Rocketloop florian@rocketloop.de | @flo_re2003 Managing Application State in Reactive Angular Applications
  • 2. Angular Frankfurt | #angularffm | @angularffm | fb.com/angularfrankfurt What this talk is about 2 Managing Application State in Reactive Angular Applications
  • 3. Angular Frankfurt | #angularffm | @angularffm | fb.com/angularfrankfurt What this talk is about 3 Managing Application State in Reactive Angular Applications
  • 4. Angular Frankfurt | #angularffm | @angularffm | fb.com/angularfrankfurt What this talk is about 4 Managing Application State in Reactive Angular Applications
  • 5. Angular Frankfurt | #angularffm | @angularffm | fb.com/angularfrankfurt What this talk is about RxJS 5 Managing Application State in Reactive Angular Applications Angular ngrx
  • 6. Angular Frankfurt | #angularffm | @angularffm | fb.com/angularfrankfurt Why reactive apps are cool ? Apps should be consistent ? Data is not static, but rather a stream ? Views should automatically react to changes in data ? Observables are first-level citizen in Angular 6
  • 7. Angular Frankfurt | #angularffm | @angularffm | fb.com/angularfrankfurt Imperative and reactive patterns ? An imperative expression is calculated once, the result never changes, even when the inputs change ? A reactive expression is calculated every time the inputs change, emitting a new result to all subscribers 7 A B C+ = Imperative A1 B1 C1 + = B2 C2 A2 C3 t Reactive
  • 8. Angular Frankfurt | #angularffm | @angularffm | fb.com/angularfrankfurt Application state ? Data fetched from server/API/back-end ? Locally created data ? Transient state / cache ? UI state 8
  • 9. Angular Frankfurt | #angularffm | @angularffm | fb.com/angularfrankfurt Imperative state management in Angular 9 Service Service Service Component Component Internal State Internal State Internal State
  • 10. Angular Frankfurt | #angularffm | @angularffm | fb.com/angularfrankfurt Unidirectional data flow (Flux) 10 Action Dispatcher Store View Action This is where the app state lives
  • 11. Angular Frankfurt | #angularffm | @angularffm | fb.com/angularfrankfurt Redux-like architecture using ngrx/store 11 Component Action State Reducer ngrx/store Store subscribe dispatch Component ActionState Reducer Service ngrx/store + Services method callsubscribe Store dispatch
  • 12. Angular Frankfurt | #angularffm | @angularffm | fb.com/angularfrankfurt 12 Let¨s see some code!
  • 13. Angular Frankfurt | #angularffm | @angularffm | fb.com/angularfrankfurt State 13 interface Todo { id: string; title: string; done: boolean; } interface TodoState { todos: { [id: string]: Todo; }; todoList: string[]; } { 'todoList': [ '93f37bdf-6a45-4243-913c-cd057c91c2bc', '5d744a71-39d3-4afe-8a98-66fa3464f2aa' ], 'todos': { '93f37bdf-6a45-4243-913c-cd057c91c2bc': { 'id': '93f37bdf-6a45-4243-913c-cd057c91c2bc', 'title': 'Buy some milk', 'done': false }, '5d744a71-39d3-4afe-8a98-66fa3464f2aa': { 'id': '5d744a71-39d3-4afe-8a98-66fa3464f2aa', 'title': 'Take out the trash', 'done': false }, } } The state is a plain object (with a fixed schema) that represents the entire application state
  • 14. Angular Frankfurt | #angularffm | @angularffm | fb.com/angularfrankfurt Action 14 An action is a plain object that represents the intended change to the state interface Action { type: string; payload?: any; } this.store.dispatch({ 'type': 'TODOLIST:CREATE_TODO', 'payload': { 'title': 'Work on Todo app' } } this.store.dispatch({ 'type': 'TODOLIST:MARK_TODO_DONE', 'payload': { 'id': '93f37bdf-6a45-4243-913c-cd057c91c2bc' } } Actions are the only way to mutate (modify) the state Actions can be understood as state transitions of our state machine
  • 15. Angular Frankfurt | #angularffm | @angularffm | fb.com/angularfrankfurt Reducer 15 A reducer is a plain function that accepts a state and an action and returns a new version of the state function todoReducer(state: TodoState, action: Action) { switch (action.type) { case "TODOLIST:CREATE_TODO": const id = guid(); return { ...state, 'todoList': [ ...state.todoList, id ], 'todos': { ...state.todos, [id]: { 'id': id, 'title': action.payload.title, 'done': false } } } default: return state; } }
  • 16. Angular Frankfurt | #angularffm | @angularffm | fb.com/angularfrankfurt Component 16 <ul *ngFor="let todo of todos$ | async"> <li>{{todo.title}}</li> </ul> ? The component can select a slice/subtree of the state from the store ? Using the resulting Observable, the component can reactively listen for changes in the selected subtree todos$: Observable<Todo[]>; constructor(private store: Store<TodoState>) { this.todos$ = Observable.combineLatest( this.store.select('todoList'), this.store.select('todos') ).map([todoList, todos] => { return todoList.map(id => todos[id]); }); }
  • 17. Angular Frankfurt | #angularffm | @angularffm | fb.com/angularfrankfurt Further thoughts ¢ How do you handle asynchronous side effects? $ ngrx/effects! ¢ Does it work with lazy loading? $ Since ngrx 4.0, yes! ¢ How can you debug reducers/actions/states? $ ngrx/devtools to the rescue! 17
  • 18. Angular Frankfurt | #angularffm | @angularffm | fb.com/angularfrankfurt 18 Questions?