SlideShare une entreprise Scribd logo
1  sur  20
Flux architecture and design
and implementation examples
Iruca3
Aqutras Inc.
What’s Flux?
An architecture of single directional data flow
data-flow programming
or
flow-based programming
https://www.youtube.com/watch?list=PLb0IAmt7-GS188xDYE-u1ShQmFFGbrk0v&v=nYkdrAPrdcw1
Flux provides improvements
- Data consistency
- Easier to pinpoint root of a bug
- More meaningful unit tests
2
Data consistency
Store component manages all of data.
No other components deal with any data.
3
Customer List
Customer Map
Customer Summary
Bad case
Internal DB View Component External DB
Customer
Data
Cached
Customer
Data
Cached
Customer
Data
No consistency
among view components
No consistency
4
Internal DB View Component External DB
Like Flux (not flux but having data consistency)
Customer List
Customer Map
Customer Summary
Store Customer
Data
Web API
Action
ex. CLICK, EDIT, ADD, REMOVE
All changes always come
from some action
Data always come from store
External DB or WebAPI
divided from other components
5
Flux
Store Customer
Data
Web API
Dispatcher handle actions.
Action provides itself to dispatcher.
Dispatcher
Action
View
ex. Customer List, Customer Map, Customer Summary
Which action
does store want?
Which change events
does view want?
Which elements or data
should redraw?
Where do actions
come from?
6
Where do actions come from?
Actions come from View or API.
Web API
View
Action
ex. Update something
ex. Add something
Actions by user interaction
Actions about data changing
7
Implementation sample on Action
createMessage: function(text) {
ChatAppDispatcher.handleViewAction({
type: ActionTypes.CREATE_MESSAGE,
text: text
});
var message = MessageStore.getCreatedMessageData(text);
ChatWebAPIUtils.createMessage(message);
}
Calling WebAPI from Action
https://github.com/facebook/flux/tree/master/examples/flux-chat8
Implementation sample on WebAPI
getAllMessages: function() {
// simulate retrieving data from a database
var rawMessages = JSON.parse(localStorage.getItem('messages'));
// simulate success callback
ChatServerActionCreators.receiveAll(rawMessages);
}
Generating action from WebAPI
https://github.com/facebook/flux/tree/master/examples/flux-chat9
Implementation sample on View
_onKeyDown: function(event) {
if (event.keyCode === ENTER_KEY_CODE) {
var text = this.state.text.trim();
if (text) {
ChatMessageActionCreators.createMessage(text);
}
this.setState({text: ''});
}
}
Generating action from View
https://github.com/facebook/flux/tree/master/examples/flux-chat10
Which action does store wants?
Each store registers itself and provides a callback.
Callback is called with action.
Action
http://facebook.github.io/flux/docs/overview.html#content
Dispatcher Store
Store register itself to Dispatcher with callback below:
function (action) {
if ( action == ‘ADD_CUSTOMER’ ) { /* something */ }
}
Preparing
(Register)
DataFlow
ex.
- ADD_CUSTOMER
- RESIZE
- MOUSEOVER
Store can deal with something
when customer data is changed
11
More complicated case
Dispatcher
Preparing
(Register)
DataFlow
Dispatcher ProductStore
CustomerStore
SalesStore
CustomerStore register
ProductStore register
SalesStore register
ProductStore
CustomerStore
SalesStore
SOLD
Dispatcher should send SOLD
to all storesWeb API
View
Action
Something is brought and
SOLD action emitted
12
Do something
Do something
Do nothing
Implementation sample about
Dispatcher
13
Creating a Dispatcher
http://facebook.github.io/flux/docs/todo-list.html#creating-a-dispatcher
See the document below:
Implementation sample on Store
MessageStore.dispatchToken = ChatAppDispatcher.register(function(payload)
{
var action = payload.action;
switch(action.type) {
case ActionTypes.CLICK_THREAD: (...)
case ActionTypes.CREATE_MESSAGE: (...)
case ActionTypes.RECEIVE_RAW_MESSAGES:
_addMessages(action.rawMessages);
ChatAppDispatcher.waitFor([ThreadStore.dispatchToken]);
_markAllInThreadRead(ThreadStore.getCurrentID());
MessageStore.emitChange();
break;
...
Dealing with specified action
https://github.com/facebook/flux/tree/master/examples/flux-chat14
Registering to dispatcher
Which change events does view want?
View add event listener to Store.
Store has to implement event emitter.
15
Store View
1. addChangeListener(callback)
Store View
3. this.emit(ON_CHANGE)
2. this.on(ON_CHANGE, callback)
ON_CHANGE is a simple constant string
4. Callback is called
Implementation sample on View
componentDidMount: function() {
this._scrollToBottom();
MessageStore.addChangeListener(this._onChange);
ThreadStore.addChangeListener(this._onChange);
}
Registering store change listener
https://github.com/facebook/flux/tree/master/examples/flux-chat16
Implementation sample on Store
emitChange: function() {
this.emit(CHANGE_EVENT);
},
addChangeListener: function(callback) {
this.on(CHANGE_EVENT, callback);
}
Registering store change listener
https://github.com/facebook/flux/tree/master/examples/flux-chat17
Event should be fired
on ONLY dispatching
because data flow shows that
input is only Dispatcher.
Which elements or data should redraw?
React does very well
if you use props and state correctly.
See the document below:
15
Advanced Performance
http://facebook.github.io/react/docs/advanced-performance.html
Reconciliation
http://facebook.github.io/react/docs/reconciliation.html
Flux flow loop
15
Store
Server DBWeb API
Dispatcher
Action
View
Store registers itself
to dispatcher.
View adds event listener
to store.
View creates an action.
Action notify to dispatcher.
Dispatcher handle actions.
Action calls some web api.
Web api creates an action.

Contenu connexe

Similaire à Flux memo

SQL Server 2008 Portfolio for Saumya Bhatnagar
SQL Server 2008 Portfolio for Saumya BhatnagarSQL Server 2008 Portfolio for Saumya Bhatnagar
SQL Server 2008 Portfolio for Saumya Bhatnagar
sammykb
 

Similaire à Flux memo (20)

Academy PRO: React JS. Redux & Tooling
Academy PRO: React JS. Redux & ToolingAcademy PRO: React JS. Redux & Tooling
Academy PRO: React JS. Redux & Tooling
 
Hazelcast Striim Hot Cache Presentation
Hazelcast Striim Hot Cache PresentationHazelcast Striim Hot Cache Presentation
Hazelcast Striim Hot Cache Presentation
 
SQL Server 2008 Portfolio for Saumya Bhatnagar
SQL Server 2008 Portfolio for Saumya BhatnagarSQL Server 2008 Portfolio for Saumya Bhatnagar
SQL Server 2008 Portfolio for Saumya Bhatnagar
 
Getting started with Redux js
Getting started with Redux jsGetting started with Redux js
Getting started with Redux js
 
[@NaukriEngineering] Flux Architecture
[@NaukriEngineering] Flux Architecture[@NaukriEngineering] Flux Architecture
[@NaukriEngineering] Flux Architecture
 
Applications: A Series of States
Applications: A Series of StatesApplications: A Series of States
Applications: A Series of States
 
Sql Portfolio
Sql PortfolioSql Portfolio
Sql Portfolio
 
Building a Backend with Flask
Building a Backend with FlaskBuilding a Backend with Flask
Building a Backend with Flask
 
Using Embulk at Treasure Data
Using Embulk at Treasure DataUsing Embulk at Treasure Data
Using Embulk at Treasure Data
 
Asp.net tips
Asp.net tipsAsp.net tips
Asp.net tips
 
Introduction to BreezeJs
Introduction to BreezeJsIntroduction to BreezeJs
Introduction to BreezeJs
 
Azure Data Factory Introduction.pdf
Azure Data Factory Introduction.pdfAzure Data Factory Introduction.pdf
Azure Data Factory Introduction.pdf
 
Battle of React State Managers in frontend applications
Battle of React State Managers in frontend applicationsBattle of React State Managers in frontend applications
Battle of React State Managers in frontend applications
 
Database Change Management as a Service
Database Change Management as a ServiceDatabase Change Management as a Service
Database Change Management as a Service
 
Skills Portfolio
Skills PortfolioSkills Portfolio
Skills Portfolio
 
BP204 - Take a REST and put your data to work with APIs!
BP204 - Take a REST and put your data to work with APIs!BP204 - Take a REST and put your data to work with APIs!
BP204 - Take a REST and put your data to work with APIs!
 
Java Web Programming [8/9] : JSF and AJAX
Java Web Programming [8/9] : JSF and AJAXJava Web Programming [8/9] : JSF and AJAX
Java Web Programming [8/9] : JSF and AJAX
 
Server side rendering with React and Symfony
Server side rendering with React and SymfonyServer side rendering with React and Symfony
Server side rendering with React and Symfony
 
Code First with Serverless Azure Functions
Code First with Serverless Azure FunctionsCode First with Serverless Azure Functions
Code First with Serverless Azure Functions
 
Going Serverless with Azure Functions in .NET
Going Serverless with Azure Functions in .NETGoing Serverless with Azure Functions in .NET
Going Serverless with Azure Functions in .NET
 

Dernier

Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
amitlee9823
 
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Christo Ananth
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
dollysharma2066
 

Dernier (20)

Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
 
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
NFPA 5000 2024 standard .
NFPA 5000 2024 standard                                  .NFPA 5000 2024 standard                                  .
NFPA 5000 2024 standard .
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineering
 
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
Vivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design SpainVivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design Spain
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
 
Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdf
 
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
 

Flux memo

  • 1. Flux architecture and design and implementation examples Iruca3 Aqutras Inc.
  • 2. What’s Flux? An architecture of single directional data flow data-flow programming or flow-based programming https://www.youtube.com/watch?list=PLb0IAmt7-GS188xDYE-u1ShQmFFGbrk0v&v=nYkdrAPrdcw1
  • 3. Flux provides improvements - Data consistency - Easier to pinpoint root of a bug - More meaningful unit tests 2
  • 4. Data consistency Store component manages all of data. No other components deal with any data. 3
  • 5. Customer List Customer Map Customer Summary Bad case Internal DB View Component External DB Customer Data Cached Customer Data Cached Customer Data No consistency among view components No consistency 4
  • 6. Internal DB View Component External DB Like Flux (not flux but having data consistency) Customer List Customer Map Customer Summary Store Customer Data Web API Action ex. CLICK, EDIT, ADD, REMOVE All changes always come from some action Data always come from store External DB or WebAPI divided from other components 5
  • 7. Flux Store Customer Data Web API Dispatcher handle actions. Action provides itself to dispatcher. Dispatcher Action View ex. Customer List, Customer Map, Customer Summary Which action does store want? Which change events does view want? Which elements or data should redraw? Where do actions come from? 6
  • 8. Where do actions come from? Actions come from View or API. Web API View Action ex. Update something ex. Add something Actions by user interaction Actions about data changing 7
  • 9. Implementation sample on Action createMessage: function(text) { ChatAppDispatcher.handleViewAction({ type: ActionTypes.CREATE_MESSAGE, text: text }); var message = MessageStore.getCreatedMessageData(text); ChatWebAPIUtils.createMessage(message); } Calling WebAPI from Action https://github.com/facebook/flux/tree/master/examples/flux-chat8
  • 10. Implementation sample on WebAPI getAllMessages: function() { // simulate retrieving data from a database var rawMessages = JSON.parse(localStorage.getItem('messages')); // simulate success callback ChatServerActionCreators.receiveAll(rawMessages); } Generating action from WebAPI https://github.com/facebook/flux/tree/master/examples/flux-chat9
  • 11. Implementation sample on View _onKeyDown: function(event) { if (event.keyCode === ENTER_KEY_CODE) { var text = this.state.text.trim(); if (text) { ChatMessageActionCreators.createMessage(text); } this.setState({text: ''}); } } Generating action from View https://github.com/facebook/flux/tree/master/examples/flux-chat10
  • 12. Which action does store wants? Each store registers itself and provides a callback. Callback is called with action. Action http://facebook.github.io/flux/docs/overview.html#content Dispatcher Store Store register itself to Dispatcher with callback below: function (action) { if ( action == ‘ADD_CUSTOMER’ ) { /* something */ } } Preparing (Register) DataFlow ex. - ADD_CUSTOMER - RESIZE - MOUSEOVER Store can deal with something when customer data is changed 11
  • 13. More complicated case Dispatcher Preparing (Register) DataFlow Dispatcher ProductStore CustomerStore SalesStore CustomerStore register ProductStore register SalesStore register ProductStore CustomerStore SalesStore SOLD Dispatcher should send SOLD to all storesWeb API View Action Something is brought and SOLD action emitted 12 Do something Do something Do nothing
  • 14. Implementation sample about Dispatcher 13 Creating a Dispatcher http://facebook.github.io/flux/docs/todo-list.html#creating-a-dispatcher See the document below:
  • 15. Implementation sample on Store MessageStore.dispatchToken = ChatAppDispatcher.register(function(payload) { var action = payload.action; switch(action.type) { case ActionTypes.CLICK_THREAD: (...) case ActionTypes.CREATE_MESSAGE: (...) case ActionTypes.RECEIVE_RAW_MESSAGES: _addMessages(action.rawMessages); ChatAppDispatcher.waitFor([ThreadStore.dispatchToken]); _markAllInThreadRead(ThreadStore.getCurrentID()); MessageStore.emitChange(); break; ... Dealing with specified action https://github.com/facebook/flux/tree/master/examples/flux-chat14 Registering to dispatcher
  • 16. Which change events does view want? View add event listener to Store. Store has to implement event emitter. 15 Store View 1. addChangeListener(callback) Store View 3. this.emit(ON_CHANGE) 2. this.on(ON_CHANGE, callback) ON_CHANGE is a simple constant string 4. Callback is called
  • 17. Implementation sample on View componentDidMount: function() { this._scrollToBottom(); MessageStore.addChangeListener(this._onChange); ThreadStore.addChangeListener(this._onChange); } Registering store change listener https://github.com/facebook/flux/tree/master/examples/flux-chat16
  • 18. Implementation sample on Store emitChange: function() { this.emit(CHANGE_EVENT); }, addChangeListener: function(callback) { this.on(CHANGE_EVENT, callback); } Registering store change listener https://github.com/facebook/flux/tree/master/examples/flux-chat17 Event should be fired on ONLY dispatching because data flow shows that input is only Dispatcher.
  • 19. Which elements or data should redraw? React does very well if you use props and state correctly. See the document below: 15 Advanced Performance http://facebook.github.io/react/docs/advanced-performance.html Reconciliation http://facebook.github.io/react/docs/reconciliation.html
  • 20. Flux flow loop 15 Store Server DBWeb API Dispatcher Action View Store registers itself to dispatcher. View adds event listener to store. View creates an action. Action notify to dispatcher. Dispatcher handle actions. Action calls some web api. Web api creates an action.