SlideShare a Scribd company logo
Redux Action Creators
01.12.2017
Common approach
• export const GetOrderAction = (orderId: string) => ({

type: OrdersActions.GET_ORDER_ACTION,

id: orderId,

});
• export const DoneGetOrderAction = (response: any) => ({

type: OrdersActions.DONE_GET_ORDER_ACTION,

response: response,

});
• export const FailedGetOrderAction = (error: Error) => ({

type: OrdersActions.FAILED_GET_ORDER_ACTION,

success: false,

error: error,

});
Problems
• A lot of boilerplate for async actions:

started, done, failed
• Parameters for the actions are not standartized:

export const GetOrderAction = (orderId: string) => ({

type: OrdersActions.GET_ORDER_ACTION,

id: orderId,

// has nothing to do with order

overlay: true,

});
• Not type-safe

this.actions$.ofType(OrdersActions.DONE_GET_ORDER)

// we want this code to fail

.map(response => response.response)
@ngrx/store way
• @ngrx/store is the library that provides Redux
bindings for Angular
• Their approach tries to address these problems,
but in a clumsy way.
@ngrx/store actions
Solution: TypeScript FSA
• Small TypeScript library that solves the above
problems:

https://github.com/aikoven/typescript-fsa
• Complies with the (de-facto standard) Flux
Standard Actions (AirBnB, Slack, BitBucket)

https://github.com/acdlite/flux-standard-action
• There is also a companion library that integrates
actions with type-safe reducers:

https://github.com/dphilipson/typescript-fsa-reducers
Async actions
• Instead of creating three action creators, we only
create one:

export const GetOrderAction =

actionCreator.async<string, IOrder, Error>

(OrdersActions.GET_ORDER, {overlay: true});
• Now the actions can be created like this:

GetOrderAction.started(1)

GetOrderAction.done({params: null, result})

GetOrderAction.failed({params: null, error})
Actions parameters
• Flux standard actions:

interface Action<P> {

type: string;

payload: P;

error?: boolean;

meta?: Object;

}
• TypeScript FSA:

GetOrderAction.started(1, {overlay: true})
• Async failure action has error: true:

this.actions$.filter(GetOrderAction.failed.match)

// maps to true

.map(action => action.error)
Type-safe actions
• export const GetOrderAction =

actionCreator.async<string, IOrder, Error>

(OrdersActions.GET_ORDER, {overlay: true});
• Type signature: <started, done, failed>
• This way our code is safer. For example, compiler will hint at the type errors in
following cases:



// expects a string

GetOrderAction.started({id: 1})



this.actions$.ofType(OrdersActions.DONE_GET_ORDER)

// expects IOrder which doesn't have response property

.map(response => response.response)
• These errors become apparent during compilation thanks to type inference
(ability of the compiler to deduce the type of the argument)
Effects example
• export const GetOrderAction =

actionCreator.async<string, IOrder, Error>(OrdersActions.GET_ORDER, {overlay: true});
• @Effect() getOrder$ =

this.actions$.filter(GetOrderAction.started.match)

.switchMap(({payload: orderId}) =>

Observable

.fromPromise(this.apiService.orderApi.getOrder(orderId))

.map((result: any) => GetOrderAction.done({params: null, result}))

.catch(error => Observable.of(GetOrderAction.failed({params: null, error}))),

);



@Effect() doneGetOrder$ =

this.actions$.filter(GetOrderAction.done.match)

.map(({payload: {result}}) => SetOrderAction(result));

Best practices
• Actions should end with "Action", as in
GetOrderAction
• Async actions (that involve an API request) should
be created using AsyncActionCreator.
Pro-tip: don't turn off Redux
DevTools off

More Related Content

What's hot

symfony & jQuery (phpDay)
symfony & jQuery (phpDay)symfony & jQuery (phpDay)
symfony & jQuery (phpDay)
Massimiliano Arione
 
Confident Refactoring - Ember SF Meetup
Confident Refactoring - Ember SF MeetupConfident Refactoring - Ember SF Meetup
Confident Refactoring - Ember SF Meetup
Fastly
 
Ngrx meta reducers
Ngrx meta reducersNgrx meta reducers
Ngrx meta reducers
Eliran Eliassy
 
AngularJS (1.x) as fast as a lightning
AngularJS (1.x)as fast as a lightningAngularJS (1.x)as fast as a lightning
AngularJS (1.x) as fast as a lightning
Bartłomiej Narożnik
 
AngularJS Unit Testing
AngularJS Unit TestingAngularJS Unit Testing
AngularJS Unit Testing
Prince Norin
 
Unit Testing JavaScript Applications
Unit Testing JavaScript ApplicationsUnit Testing JavaScript Applications
Unit Testing JavaScript Applications
Ynon Perek
 
Spring AOP @ DevClub.eu
Spring AOP @ DevClub.euSpring AOP @ DevClub.eu
Spring AOP @ DevClub.eu
arsenikum
 
Testing in JavaScript - August 2018 - WebElement Bardejov
Testing in JavaScript - August 2018 - WebElement BardejovTesting in JavaScript - August 2018 - WebElement Bardejov
Testing in JavaScript - August 2018 - WebElement Bardejov
Marian Rusnak
 
Introduction To Angular's reactive forms
Introduction To Angular's reactive formsIntroduction To Angular's reactive forms
Introduction To Angular's reactive forms
Nir Kaufman
 
Automated testing for client-side - Adam Klein, 500 Tech
Automated testing for client-side - Adam Klein, 500 TechAutomated testing for client-side - Adam Klein, 500 Tech
Automated testing for client-side - Adam Klein, 500 Tech
Codemotion Tel Aviv
 
Angular - injection tokens & Custom libraries
Angular - injection tokens & Custom librariesAngular - injection tokens & Custom libraries
Angular - injection tokens & Custom libraries
Eliran Eliassy
 
ruby on rails pitfalls
ruby on rails pitfallsruby on rails pitfalls
ruby on rails pitfalls
Robbin Fan
 
Angular 2.0: Brighter future?
Angular 2.0: Brighter future?Angular 2.0: Brighter future?
Angular 2.0: Brighter future?
Eugene Zharkov
 
Apex 5 plugins for everyone version 2018
Apex 5 plugins for everyone   version 2018Apex 5 plugins for everyone   version 2018
Apex 5 plugins for everyone version 2018
Alan Arentsen
 
Good karma: UX Patterns and Unit Testing in Angular with Karma
Good karma: UX Patterns and Unit Testing in Angular with KarmaGood karma: UX Patterns and Unit Testing in Angular with Karma
Good karma: UX Patterns and Unit Testing in Angular with Karma
ExoLeaders.com
 
Ultimate Introduction To AngularJS
Ultimate Introduction To AngularJSUltimate Introduction To AngularJS
Ultimate Introduction To AngularJS
Jacopo Nardiello
 
You promise?
You promise?You promise?
You promise?
IT Weekend
 
Stored procedures
Stored proceduresStored procedures
Stored procedures
Prof.Nilesh Magar
 
Testing Javascript with Jasmine
Testing Javascript with JasmineTesting Javascript with Jasmine
Testing Javascript with Jasmine
Tim Tyrrell
 
Polyglot automation - QA Fest - 2015
Polyglot automation - QA Fest - 2015Polyglot automation - QA Fest - 2015
Polyglot automation - QA Fest - 2015
Iakiv Kramarenko
 

What's hot (20)

symfony & jQuery (phpDay)
symfony & jQuery (phpDay)symfony & jQuery (phpDay)
symfony & jQuery (phpDay)
 
Confident Refactoring - Ember SF Meetup
Confident Refactoring - Ember SF MeetupConfident Refactoring - Ember SF Meetup
Confident Refactoring - Ember SF Meetup
 
Ngrx meta reducers
Ngrx meta reducersNgrx meta reducers
Ngrx meta reducers
 
AngularJS (1.x) as fast as a lightning
AngularJS (1.x)as fast as a lightningAngularJS (1.x)as fast as a lightning
AngularJS (1.x) as fast as a lightning
 
AngularJS Unit Testing
AngularJS Unit TestingAngularJS Unit Testing
AngularJS Unit Testing
 
Unit Testing JavaScript Applications
Unit Testing JavaScript ApplicationsUnit Testing JavaScript Applications
Unit Testing JavaScript Applications
 
Spring AOP @ DevClub.eu
Spring AOP @ DevClub.euSpring AOP @ DevClub.eu
Spring AOP @ DevClub.eu
 
Testing in JavaScript - August 2018 - WebElement Bardejov
Testing in JavaScript - August 2018 - WebElement BardejovTesting in JavaScript - August 2018 - WebElement Bardejov
Testing in JavaScript - August 2018 - WebElement Bardejov
 
Introduction To Angular's reactive forms
Introduction To Angular's reactive formsIntroduction To Angular's reactive forms
Introduction To Angular's reactive forms
 
Automated testing for client-side - Adam Klein, 500 Tech
Automated testing for client-side - Adam Klein, 500 TechAutomated testing for client-side - Adam Klein, 500 Tech
Automated testing for client-side - Adam Klein, 500 Tech
 
Angular - injection tokens & Custom libraries
Angular - injection tokens & Custom librariesAngular - injection tokens & Custom libraries
Angular - injection tokens & Custom libraries
 
ruby on rails pitfalls
ruby on rails pitfallsruby on rails pitfalls
ruby on rails pitfalls
 
Angular 2.0: Brighter future?
Angular 2.0: Brighter future?Angular 2.0: Brighter future?
Angular 2.0: Brighter future?
 
Apex 5 plugins for everyone version 2018
Apex 5 plugins for everyone   version 2018Apex 5 plugins for everyone   version 2018
Apex 5 plugins for everyone version 2018
 
Good karma: UX Patterns and Unit Testing in Angular with Karma
Good karma: UX Patterns and Unit Testing in Angular with KarmaGood karma: UX Patterns and Unit Testing in Angular with Karma
Good karma: UX Patterns and Unit Testing in Angular with Karma
 
Ultimate Introduction To AngularJS
Ultimate Introduction To AngularJSUltimate Introduction To AngularJS
Ultimate Introduction To AngularJS
 
You promise?
You promise?You promise?
You promise?
 
Stored procedures
Stored proceduresStored procedures
Stored procedures
 
Testing Javascript with Jasmine
Testing Javascript with JasmineTesting Javascript with Jasmine
Testing Javascript with Jasmine
 
Polyglot automation - QA Fest - 2015
Polyglot automation - QA Fest - 2015Polyglot automation - QA Fest - 2015
Polyglot automation - QA Fest - 2015
 

Similar to Redux Action Creators

A Journey with React
A Journey with ReactA Journey with React
A Journey with React
FITC
 
Intro to Asynchronous Javascript
Intro to Asynchronous JavascriptIntro to Asynchronous Javascript
Intro to Asynchronous Javascript
Garrett Welson
 
Working effectively with legacy code
Working effectively with legacy codeWorking effectively with legacy code
Working effectively with legacy code
ShriKant Vashishtha
 
Side effects-con-redux
Side effects-con-reduxSide effects-con-redux
Side effects-con-redux
Nicolas Quiceno Benavides
 
Building an api using golang and postgre sql v1.0
Building an api using golang and postgre sql v1.0Building an api using golang and postgre sql v1.0
Building an api using golang and postgre sql v1.0
Frost
 
Scti 2011 minicurso jquery
Scti 2011 minicurso jqueryScti 2011 minicurso jquery
Scti 2011 minicurso jquery
ciberglo
 
Understanding Asynchronous JavaScript
Understanding Asynchronous JavaScriptUnderstanding Asynchronous JavaScript
Understanding Asynchronous JavaScript
jnewmanux
 
Native Payment - Part 3.pdf
Native Payment - Part 3.pdfNative Payment - Part 3.pdf
Native Payment - Part 3.pdf
ShaiAlmog1
 
Compatibility Detector Tool of Chrome extensions
Compatibility Detector Tool of Chrome extensionsCompatibility Detector Tool of Chrome extensions
Compatibility Detector Tool of Chrome extensions
Kai Cui
 
A la découverte de TypeScript
A la découverte de TypeScriptA la découverte de TypeScript
A la découverte de TypeScript
Denis Voituron
 
Top 5 magento secure coding best practices Alex Zarichnyi
Top 5 magento secure coding best practices   Alex ZarichnyiTop 5 magento secure coding best practices   Alex Zarichnyi
Top 5 magento secure coding best practices Alex Zarichnyi
Magento Dev
 
Refactoring at Large
Refactoring at LargeRefactoring at Large
Refactoring at Large
Danilo Sato
 
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
Domenic Denicola
 
JavaScript Web Workers
JavaScript Web WorkersJavaScript Web Workers
JavaScript Web Workers
Tobias Pfeiffer
 
Learning Svelte
Learning SvelteLearning Svelte
Learning Svelte
Christoffer Noring
 
Take Data Validation Seriously - Paul Milham, WildWorks
Take Data Validation Seriously - Paul Milham, WildWorksTake Data Validation Seriously - Paul Milham, WildWorks
Take Data Validation Seriously - Paul Milham, WildWorks
NodejsFoundation
 
To Err Is Human
To Err Is HumanTo Err Is Human
To Err Is Human
Alex Liu
 
Introduction to Dart
Introduction to DartIntroduction to Dart
Introduction to Dart
Ramesh Nair
 
Dartprogramming
DartprogrammingDartprogramming
Dartprogramming
Ali Parmaksiz
 
Async Redux Actions With RxJS - React Rally 2016
Async Redux Actions With RxJS - React Rally 2016Async Redux Actions With RxJS - React Rally 2016
Async Redux Actions With RxJS - React Rally 2016
Ben Lesh
 

Similar to Redux Action Creators (20)

A Journey with React
A Journey with ReactA Journey with React
A Journey with React
 
Intro to Asynchronous Javascript
Intro to Asynchronous JavascriptIntro to Asynchronous Javascript
Intro to Asynchronous Javascript
 
Working effectively with legacy code
Working effectively with legacy codeWorking effectively with legacy code
Working effectively with legacy code
 
Side effects-con-redux
Side effects-con-reduxSide effects-con-redux
Side effects-con-redux
 
Building an api using golang and postgre sql v1.0
Building an api using golang and postgre sql v1.0Building an api using golang and postgre sql v1.0
Building an api using golang and postgre sql v1.0
 
Scti 2011 minicurso jquery
Scti 2011 minicurso jqueryScti 2011 minicurso jquery
Scti 2011 minicurso jquery
 
Understanding Asynchronous JavaScript
Understanding Asynchronous JavaScriptUnderstanding Asynchronous JavaScript
Understanding Asynchronous JavaScript
 
Native Payment - Part 3.pdf
Native Payment - Part 3.pdfNative Payment - Part 3.pdf
Native Payment - Part 3.pdf
 
Compatibility Detector Tool of Chrome extensions
Compatibility Detector Tool of Chrome extensionsCompatibility Detector Tool of Chrome extensions
Compatibility Detector Tool of Chrome extensions
 
A la découverte de TypeScript
A la découverte de TypeScriptA la découverte de TypeScript
A la découverte de TypeScript
 
Top 5 magento secure coding best practices Alex Zarichnyi
Top 5 magento secure coding best practices   Alex ZarichnyiTop 5 magento secure coding best practices   Alex Zarichnyi
Top 5 magento secure coding best practices Alex Zarichnyi
 
Refactoring at Large
Refactoring at LargeRefactoring at Large
Refactoring at Large
 
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
 
JavaScript Web Workers
JavaScript Web WorkersJavaScript Web Workers
JavaScript Web Workers
 
Learning Svelte
Learning SvelteLearning Svelte
Learning Svelte
 
Take Data Validation Seriously - Paul Milham, WildWorks
Take Data Validation Seriously - Paul Milham, WildWorksTake Data Validation Seriously - Paul Milham, WildWorks
Take Data Validation Seriously - Paul Milham, WildWorks
 
To Err Is Human
To Err Is HumanTo Err Is Human
To Err Is Human
 
Introduction to Dart
Introduction to DartIntroduction to Dart
Introduction to Dart
 
Dartprogramming
DartprogrammingDartprogramming
Dartprogramming
 
Async Redux Actions With RxJS - React Rally 2016
Async Redux Actions With RxJS - React Rally 2016Async Redux Actions With RxJS - React Rally 2016
Async Redux Actions With RxJS - React Rally 2016
 

Recently uploaded

Unveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdfUnveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdf
brainerhub1
 
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
XfilesPro
 
How to write a program in any programming language
How to write a program in any programming languageHow to write a program in any programming language
How to write a program in any programming language
Rakesh Kumar R
 
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian CompaniesE-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
Quickdice ERP
 
E-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet DynamicsE-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet Dynamics
Hornet Dynamics
 
UI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
UI5con 2024 - Keynote: Latest News about UI5 and it’s EcosystemUI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
UI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
Peter Muessig
 
Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
safelyiotech
 
Malibou Pitch Deck For Its €3M Seed Round
Malibou Pitch Deck For Its €3M Seed RoundMalibou Pitch Deck For Its €3M Seed Round
Malibou Pitch Deck For Its €3M Seed Round
sjcobrien
 
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
Bert Jan Schrijver
 
Using Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query PerformanceUsing Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query Performance
Grant Fritchey
 
YAML crash COURSE how to write yaml file for adding configuring details
YAML crash COURSE how to write yaml file for adding configuring detailsYAML crash COURSE how to write yaml file for adding configuring details
YAML crash COURSE how to write yaml file for adding configuring details
NishanthaBulumulla1
 
How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?
ToXSL Technologies
 
Top Benefits of Using Salesforce Healthcare CRM for Patient Management.pdf
Top Benefits of Using Salesforce Healthcare CRM for Patient Management.pdfTop Benefits of Using Salesforce Healthcare CRM for Patient Management.pdf
Top Benefits of Using Salesforce Healthcare CRM for Patient Management.pdf
VALiNTRY360
 
fiscal year variant fiscal year variant.
fiscal year variant fiscal year variant.fiscal year variant fiscal year variant.
fiscal year variant fiscal year variant.
AnkitaPandya11
 
Liberarsi dai framework con i Web Component.pptx
Liberarsi dai framework con i Web Component.pptxLiberarsi dai framework con i Web Component.pptx
Liberarsi dai framework con i Web Component.pptx
Massimo Artizzu
 
Fundamentals of Programming and Language Processors
Fundamentals of Programming and Language ProcessorsFundamentals of Programming and Language Processors
Fundamentals of Programming and Language Processors
Rakesh Kumar R
 
Energy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina JonuziEnergy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina Jonuzi
Green Software Development
 
Modelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - AmsterdamModelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - Amsterdam
Alberto Brandolini
 
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling ExtensionsUI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
Peter Muessig
 
What next after learning python programming basics
What next after learning python programming basicsWhat next after learning python programming basics
What next after learning python programming basics
Rakesh Kumar R
 

Recently uploaded (20)

Unveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdfUnveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdf
 
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
 
How to write a program in any programming language
How to write a program in any programming languageHow to write a program in any programming language
How to write a program in any programming language
 
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian CompaniesE-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
 
E-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet DynamicsE-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet Dynamics
 
UI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
UI5con 2024 - Keynote: Latest News about UI5 and it’s EcosystemUI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
UI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
 
Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
 
Malibou Pitch Deck For Its €3M Seed Round
Malibou Pitch Deck For Its €3M Seed RoundMalibou Pitch Deck For Its €3M Seed Round
Malibou Pitch Deck For Its €3M Seed Round
 
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
 
Using Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query PerformanceUsing Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query Performance
 
YAML crash COURSE how to write yaml file for adding configuring details
YAML crash COURSE how to write yaml file for adding configuring detailsYAML crash COURSE how to write yaml file for adding configuring details
YAML crash COURSE how to write yaml file for adding configuring details
 
How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?
 
Top Benefits of Using Salesforce Healthcare CRM for Patient Management.pdf
Top Benefits of Using Salesforce Healthcare CRM for Patient Management.pdfTop Benefits of Using Salesforce Healthcare CRM for Patient Management.pdf
Top Benefits of Using Salesforce Healthcare CRM for Patient Management.pdf
 
fiscal year variant fiscal year variant.
fiscal year variant fiscal year variant.fiscal year variant fiscal year variant.
fiscal year variant fiscal year variant.
 
Liberarsi dai framework con i Web Component.pptx
Liberarsi dai framework con i Web Component.pptxLiberarsi dai framework con i Web Component.pptx
Liberarsi dai framework con i Web Component.pptx
 
Fundamentals of Programming and Language Processors
Fundamentals of Programming and Language ProcessorsFundamentals of Programming and Language Processors
Fundamentals of Programming and Language Processors
 
Energy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina JonuziEnergy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina Jonuzi
 
Modelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - AmsterdamModelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - Amsterdam
 
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling ExtensionsUI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
 
What next after learning python programming basics
What next after learning python programming basicsWhat next after learning python programming basics
What next after learning python programming basics
 

Redux Action Creators

  • 2. Common approach • export const GetOrderAction = (orderId: string) => ({
 type: OrdersActions.GET_ORDER_ACTION,
 id: orderId,
 }); • export const DoneGetOrderAction = (response: any) => ({
 type: OrdersActions.DONE_GET_ORDER_ACTION,
 response: response,
 }); • export const FailedGetOrderAction = (error: Error) => ({
 type: OrdersActions.FAILED_GET_ORDER_ACTION,
 success: false,
 error: error,
 });
  • 3. Problems • A lot of boilerplate for async actions:
 started, done, failed • Parameters for the actions are not standartized:
 export const GetOrderAction = (orderId: string) => ({
 type: OrdersActions.GET_ORDER_ACTION,
 id: orderId,
 // has nothing to do with order
 overlay: true,
 }); • Not type-safe
 this.actions$.ofType(OrdersActions.DONE_GET_ORDER)
 // we want this code to fail
 .map(response => response.response)
  • 4. @ngrx/store way • @ngrx/store is the library that provides Redux bindings for Angular • Their approach tries to address these problems, but in a clumsy way.
  • 6. Solution: TypeScript FSA • Small TypeScript library that solves the above problems:
 https://github.com/aikoven/typescript-fsa • Complies with the (de-facto standard) Flux Standard Actions (AirBnB, Slack, BitBucket)
 https://github.com/acdlite/flux-standard-action • There is also a companion library that integrates actions with type-safe reducers:
 https://github.com/dphilipson/typescript-fsa-reducers
  • 7. Async actions • Instead of creating three action creators, we only create one:
 export const GetOrderAction =
 actionCreator.async<string, IOrder, Error>
 (OrdersActions.GET_ORDER, {overlay: true}); • Now the actions can be created like this:
 GetOrderAction.started(1)
 GetOrderAction.done({params: null, result})
 GetOrderAction.failed({params: null, error})
  • 8. Actions parameters • Flux standard actions:
 interface Action<P> {
 type: string;
 payload: P;
 error?: boolean;
 meta?: Object;
 } • TypeScript FSA:
 GetOrderAction.started(1, {overlay: true}) • Async failure action has error: true:
 this.actions$.filter(GetOrderAction.failed.match)
 // maps to true
 .map(action => action.error)
  • 9. Type-safe actions • export const GetOrderAction =
 actionCreator.async<string, IOrder, Error>
 (OrdersActions.GET_ORDER, {overlay: true}); • Type signature: <started, done, failed> • This way our code is safer. For example, compiler will hint at the type errors in following cases:
 
 // expects a string
 GetOrderAction.started({id: 1})
 
 this.actions$.ofType(OrdersActions.DONE_GET_ORDER)
 // expects IOrder which doesn't have response property
 .map(response => response.response) • These errors become apparent during compilation thanks to type inference (ability of the compiler to deduce the type of the argument)
  • 10. Effects example • export const GetOrderAction =
 actionCreator.async<string, IOrder, Error>(OrdersActions.GET_ORDER, {overlay: true}); • @Effect() getOrder$ =
 this.actions$.filter(GetOrderAction.started.match)
 .switchMap(({payload: orderId}) =>
 Observable
 .fromPromise(this.apiService.orderApi.getOrder(orderId))
 .map((result: any) => GetOrderAction.done({params: null, result}))
 .catch(error => Observable.of(GetOrderAction.failed({params: null, error}))),
 );
 
 @Effect() doneGetOrder$ =
 this.actions$.filter(GetOrderAction.done.match)
 .map(({payload: {result}}) => SetOrderAction(result));

  • 11. Best practices • Actions should end with "Action", as in GetOrderAction • Async actions (that involve an API request) should be created using AsyncActionCreator.
  • 12. Pro-tip: don't turn off Redux DevTools off