SlideShare a Scribd company logo
Better react/redux apps
using redux-saga
By @younes Front end Developer @
what about you ?
React Or …
Redux Or …
Thunk Or …
Saga Or …
Opinionated !
Redux !
Better react/redux apps using redux-saga
Better react/redux apps using redux-saga
deterministic part
non-deterministic part
Better react/redux apps using redux-saga
Seriously !
function makeSandwichesForEverybody() {
return function (dispatch, getState) {
if (!getState().sandwiches.isShopOpen) {
// You don’t have to return Promises, but it’s a handy convention
// so the caller can always call .then() on async dispatch result.
return Promise.resolve();
}
// We can dispatch both plain object actions and other thunks,
// which lets us compose the asynchronous actions in a single flow.
return dispatch(
makeASandwichWithSecretSauce('My Grandma')
).then(() =>
Promise.all([
dispatch(makeASandwichWithSecretSauce('Me')),
dispatch(makeASandwichWithSecretSauce('My wife'))
])
).then(() =>
dispatch(makeASandwichWithSecretSauce('Our kids'))
).then(() =>
dispatch(getState().myMoney > 42 ?
withdrawMoney(42) :
apologize('Me', 'The Sandwich Shop')
)
);
};
}
export default function* onboarding() {
yield take(GO_TO_NEXT_STAGE);
// stage: initial-confirmed
yield delay(1000);
yield put(goToNextStage());
// stage: keys-introduced
yield handleKeyExperiments();
yield put(goToNextStage());
yield delay(2000);
yield put(goToNextStage());
// stage: pad-introduced
yield handlePadExperiments();
yield put(goToNextStage());
yield delay(2000);
yield put(goToNextStage());
// stage: control-panel-introduced
yield delay(5000);
yield put(goToNextStage());
yield delay(2000);
// Persist a flag in localStorage, so that this user does not
// have to go through the onboarding flow again.
localStorage.setItem(ONBOARDING_COMPLETED_FLAG, true);
yield put(completeOnboarding());
}
Generators !
function* generator() {
yield 1;
console.log("hello");
const val = test(yield { name: "younes" })
// setting variable value from outside
console.log(val); // 5
}
const iterator = generator();
// Iterator (object with next method)
iterator.next();
// { value: 1, done: false }
iterator.next();
// { value: { name: 'younes' }, done: false }
console.log("random thing");
function delay(time = 3000, returnValue) {
return new Promise(function(resolve) {
setTimeout(resolve.bind(null, returnValue), time);
});
}
function* generator() {
let result = yield delay(2000, "return value");
console.log(result);
result = yield delay(2000);
console.log(result);
}
function runtime(generator) {
function next(nextVal) {
const { value, done } = iterator.next(nextVal);
if (done) {
console.log("done");
return Promise.resolve();
}
if (typeof value.then === "function") {
return Promise.resolve(value.then(next));
}
}
const iterator = generator();
return next(iterator);
}
runtime(generator);
function delay(time = 3000, returnValue) {
return new Promise(function(resolve) {
setTimeout(resolve.bind(null, returnValue), time);
});
}
function* generator() {
let result = yield delay(2000, "return value");
console.log(result);
result = yield delay(2000);
console.log(result);
}
function runtime(generator) {
function next(nextVal) {
const { value, done } = iterator.next(nextVal);
if (done) {
console.log("done");
return Promise.resolve();
}
if (typeof value.then === "function") {
return Promise.resolve(value.then(next));
}
}
const iterator = generator();
return next(iterator);
}
runtime(generator);
function delay(time = 3000, returnValue) {
return new Promise(function(resolve) {
setTimeout(resolve.bind(null, returnValue), time);
});
}
function* generator() {
let result = yield delay(2000, "return value");
console.log(result);
result = yield delay(2000);
console.log(result);
}
function runtime(generator) {
function next(nextVal) {
const { value, done } = iterator.next(nextVal);
if (done) {
console.log("done");
return Promise.resolve();
}
if (typeof value.then === "function") {
return Promise.resolve(value.then(next));
}
}
const iterator = generator();
return next(iterator);
}
runtime(generator);
• Runtime for our generators
Redux-saga
• Runtime for our generators
• Helpers (aka commands) for managing various operations
Redux-saga
• Runtime for our generators
• Helpers (aka commands) for managing various operations
• Integrating redux external APIs (channels)
Redux-saga
Show me some code !
Execution tree
sagamiddleware.run
Worker API
Auth Saga
Messages Feed APIs
Fork
takeLatest
Wait for
ActionX
Race
Execution tree
sagamiddleware.run
Worker API
Auth Saga
Messages Feed APIs
sagamiddleware.run
Admin
Panel
Fork
takeLatest
Wait for
ActionX
Race
Lazy
loaded
Testing ?
Channels
Channels
• Buffering (waiting dropping upcoming actions)
Channels
• Buffering (waiting dropping upcoming actions)
• Communication between sagas
Channels
• Buffering (waiting dropping upcoming actions)
• Communication between sagas
• Communication with external APIs (firebase webosocket
browser APIs …)
Channels
• Buffering (waiting dropping upcoming actions)
• Communication between sagas
• Communication with external APIs (firebase webosocket
browser APIs …)
• Abstracting platform code for sharing code react/react-
native (geolocation ??)
• Keep react strictly a view layer (stateless as much as
possible)

• Keep redux away for the how part

• separate what you need from how you get (yes redux
saga)
Thank you
_younesmln
hello@younesmln.me

More Related Content

PDF
Async History - javascript
PPTX
Process control nodejs
PDF
Async JavaScript in ES7
PDF
The Ring programming language version 1.9 book - Part 92 of 210
PDF
Redux Sagas - React Alicante
PDF
The Ring programming language version 1.9 book - Part 71 of 210
PDF
The Ring programming language version 1.6 book - Part 64 of 189
PDF
The Ring programming language version 1.7 book - Part 85 of 196
Async History - javascript
Process control nodejs
Async JavaScript in ES7
The Ring programming language version 1.9 book - Part 92 of 210
Redux Sagas - React Alicante
The Ring programming language version 1.9 book - Part 71 of 210
The Ring programming language version 1.6 book - Part 64 of 189
The Ring programming language version 1.7 book - Part 85 of 196

What's hot (20)

PDF
State managment in a world of hooks
PPTX
Asynchronous programming from Xamarin Hakcday in Melbourne
PDF
React Back to the Future
PPTX
JS Objects manipulation
PDF
Async Testing giving you a sinking feeling
KEY
Better Together: Building Scalable Real Time Collaborative Apps with Node.js
PDF
The Ring programming language version 1.10 book - Part 74 of 212
PDF
The Ring programming language version 1.7 book - Part 65 of 196
DOCX
7th lab
PDF
The Ring programming language version 1.8 book - Part 67 of 202
PDF
The evolution of java script asynchronous calls
PDF
The Ring programming language version 1.5.1 book - Part 58 of 180
PDF
Automation in angular js
PDF
The Ring programming language version 1.2 book - Part 43 of 84
PDF
Kivy Talk Python Meetup Innsbruck 2017.04.25
PDF
The Ring programming language version 1.10 book - Part 72 of 212
PDF
The Ring programming language version 1.5.3 book - Part 72 of 184
PDF
The Ring programming language version 1.7 book - Part 67 of 196
PPTX
[Android] PLAYING WITH FRAGMENT
PDF
Trisha gee concurrentprogrammingusingthedisruptor
State managment in a world of hooks
Asynchronous programming from Xamarin Hakcday in Melbourne
React Back to the Future
JS Objects manipulation
Async Testing giving you a sinking feeling
Better Together: Building Scalable Real Time Collaborative Apps with Node.js
The Ring programming language version 1.10 book - Part 74 of 212
The Ring programming language version 1.7 book - Part 65 of 196
7th lab
The Ring programming language version 1.8 book - Part 67 of 202
The evolution of java script asynchronous calls
The Ring programming language version 1.5.1 book - Part 58 of 180
Automation in angular js
The Ring programming language version 1.2 book - Part 43 of 84
Kivy Talk Python Meetup Innsbruck 2017.04.25
The Ring programming language version 1.10 book - Part 72 of 212
The Ring programming language version 1.5.3 book - Part 72 of 184
The Ring programming language version 1.7 book - Part 67 of 196
[Android] PLAYING WITH FRAGMENT
Trisha gee concurrentprogrammingusingthedisruptor
Ad

Similar to Better react/redux apps using redux-saga (20)

PDF
Promises are so passé - Tim Perry - Codemotion Milan 2016
PDF
Promise is a Promise
PDF
The evolution of redux action creators
PDF
Promises - Asynchronous Control Flow
PDF
Promise: async programming hero
PDF
Callbacks, promises, generators - asynchronous javascript
PDF
Angular promises and http
PPTX
AngularJS, More Than Directives !
PDF
Side effects-con-redux
PPTX
The Promised Land (in Angular)
PPTX
Async all around us (promises)
PDF
A promise is a Promise
PDF
Recompacting your react application
PDF
Why react matters
PDF
$q and Promises in AngularJS
PDF
React lecture
PDF
Think Async: Asynchronous Patterns in NodeJS
PPTX
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
PPTX
Angular2 rxjs
PPTX
Grails transactions
Promises are so passé - Tim Perry - Codemotion Milan 2016
Promise is a Promise
The evolution of redux action creators
Promises - Asynchronous Control Flow
Promise: async programming hero
Callbacks, promises, generators - asynchronous javascript
Angular promises and http
AngularJS, More Than Directives !
Side effects-con-redux
The Promised Land (in Angular)
Async all around us (promises)
A promise is a Promise
Recompacting your react application
Why react matters
$q and Promises in AngularJS
React lecture
Think Async: Asynchronous Patterns in NodeJS
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
Angular2 rxjs
Grails transactions
Ad

Recently uploaded (20)

PPTX
OOP with Java - Java Introduction (Basics)
PPTX
TE-AI-Unit VI notes using planning model
PPTX
Glazing at Facade, functions, types of glazing
PPTX
MET 305 MODULE 1 KTU 2019 SCHEME 25.pptx
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PPTX
Strings in CPP - Strings in C++ are sequences of characters used to store and...
PDF
International Journal of Information Technology Convergence and Services (IJI...
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PPT
Chapter 6 Design in software Engineeing.ppt
PPTX
Practice Questions on recent development part 1.pptx
PDF
B.Tech (Electrical Engineering ) 2024 syllabus.pdf
PPT
Drone Technology Electronics components_1
PPTX
436813905-LNG-Process-Overview-Short.pptx
PPTX
24AI201_AI_Unit_4 (1).pptx Artificial intelligence
PDF
flutter Launcher Icons, Splash Screens & Fonts
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPTX
CH1 Production IntroductoryConcepts.pptx
PDF
Introduction to Data Science: data science process
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
OOP with Java - Java Introduction (Basics)
TE-AI-Unit VI notes using planning model
Glazing at Facade, functions, types of glazing
MET 305 MODULE 1 KTU 2019 SCHEME 25.pptx
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
Strings in CPP - Strings in C++ are sequences of characters used to store and...
International Journal of Information Technology Convergence and Services (IJI...
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
Chapter 6 Design in software Engineeing.ppt
Practice Questions on recent development part 1.pptx
B.Tech (Electrical Engineering ) 2024 syllabus.pdf
Drone Technology Electronics components_1
436813905-LNG-Process-Overview-Short.pptx
24AI201_AI_Unit_4 (1).pptx Artificial intelligence
flutter Launcher Icons, Splash Screens & Fonts
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
CH1 Production IntroductoryConcepts.pptx
Introduction to Data Science: data science process
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT

Better react/redux apps using redux-saga

  • 1. Better react/redux apps using redux-saga By @younes Front end Developer @
  • 2. what about you ? React Or … Redux Or … Thunk Or … Saga Or …
  • 10. function makeSandwichesForEverybody() { return function (dispatch, getState) { if (!getState().sandwiches.isShopOpen) { // You don’t have to return Promises, but it’s a handy convention // so the caller can always call .then() on async dispatch result. return Promise.resolve(); } // We can dispatch both plain object actions and other thunks, // which lets us compose the asynchronous actions in a single flow. return dispatch( makeASandwichWithSecretSauce('My Grandma') ).then(() => Promise.all([ dispatch(makeASandwichWithSecretSauce('Me')), dispatch(makeASandwichWithSecretSauce('My wife')) ]) ).then(() => dispatch(makeASandwichWithSecretSauce('Our kids')) ).then(() => dispatch(getState().myMoney > 42 ? withdrawMoney(42) : apologize('Me', 'The Sandwich Shop') ) ); }; }
  • 11. export default function* onboarding() { yield take(GO_TO_NEXT_STAGE); // stage: initial-confirmed yield delay(1000); yield put(goToNextStage()); // stage: keys-introduced yield handleKeyExperiments(); yield put(goToNextStage()); yield delay(2000); yield put(goToNextStage()); // stage: pad-introduced yield handlePadExperiments(); yield put(goToNextStage()); yield delay(2000); yield put(goToNextStage()); // stage: control-panel-introduced yield delay(5000); yield put(goToNextStage()); yield delay(2000); // Persist a flag in localStorage, so that this user does not // have to go through the onboarding flow again. localStorage.setItem(ONBOARDING_COMPLETED_FLAG, true); yield put(completeOnboarding()); }
  • 13. function* generator() { yield 1; console.log("hello"); const val = test(yield { name: "younes" }) // setting variable value from outside console.log(val); // 5 } const iterator = generator(); // Iterator (object with next method) iterator.next(); // { value: 1, done: false } iterator.next(); // { value: { name: 'younes' }, done: false } console.log("random thing");
  • 14. function delay(time = 3000, returnValue) { return new Promise(function(resolve) { setTimeout(resolve.bind(null, returnValue), time); }); } function* generator() { let result = yield delay(2000, "return value"); console.log(result); result = yield delay(2000); console.log(result); } function runtime(generator) { function next(nextVal) { const { value, done } = iterator.next(nextVal); if (done) { console.log("done"); return Promise.resolve(); } if (typeof value.then === "function") { return Promise.resolve(value.then(next)); } } const iterator = generator(); return next(iterator); } runtime(generator);
  • 15. function delay(time = 3000, returnValue) { return new Promise(function(resolve) { setTimeout(resolve.bind(null, returnValue), time); }); } function* generator() { let result = yield delay(2000, "return value"); console.log(result); result = yield delay(2000); console.log(result); } function runtime(generator) { function next(nextVal) { const { value, done } = iterator.next(nextVal); if (done) { console.log("done"); return Promise.resolve(); } if (typeof value.then === "function") { return Promise.resolve(value.then(next)); } } const iterator = generator(); return next(iterator); } runtime(generator);
  • 16. function delay(time = 3000, returnValue) { return new Promise(function(resolve) { setTimeout(resolve.bind(null, returnValue), time); }); } function* generator() { let result = yield delay(2000, "return value"); console.log(result); result = yield delay(2000); console.log(result); } function runtime(generator) { function next(nextVal) { const { value, done } = iterator.next(nextVal); if (done) { console.log("done"); return Promise.resolve(); } if (typeof value.then === "function") { return Promise.resolve(value.then(next)); } } const iterator = generator(); return next(iterator); } runtime(generator);
  • 17. • Runtime for our generators Redux-saga
  • 18. • Runtime for our generators • Helpers (aka commands) for managing various operations Redux-saga
  • 19. • Runtime for our generators • Helpers (aka commands) for managing various operations • Integrating redux external APIs (channels) Redux-saga
  • 20. Show me some code !
  • 21. Execution tree sagamiddleware.run Worker API Auth Saga Messages Feed APIs Fork takeLatest Wait for ActionX Race
  • 22. Execution tree sagamiddleware.run Worker API Auth Saga Messages Feed APIs sagamiddleware.run Admin Panel Fork takeLatest Wait for ActionX Race Lazy loaded
  • 25. Channels • Buffering (waiting dropping upcoming actions)
  • 26. Channels • Buffering (waiting dropping upcoming actions) • Communication between sagas
  • 27. Channels • Buffering (waiting dropping upcoming actions) • Communication between sagas • Communication with external APIs (firebase webosocket browser APIs …)
  • 28. Channels • Buffering (waiting dropping upcoming actions) • Communication between sagas • Communication with external APIs (firebase webosocket browser APIs …) • Abstracting platform code for sharing code react/react- native (geolocation ??)
  • 29. • Keep react strictly a view layer (stateless as much as possible) • Keep redux away for the how part • separate what you need from how you get (yes redux saga)