the fundamental problems of gui applications and why people choose react

76
TechTalk The fundamental problems of GUI applications & why people choose React Vu Nguyen [email protected]

Upload: nguyen-vu

Post on 07-Jan-2017

628 views

Category:

Technology


2 download

TRANSCRIPT

Page 1: The fundamental problems of GUI applications and why people choose React

TechTalk

The fundamental problems ofGUI applications

& why people choose React

Vu [email protected]

Page 2: The fundamental problems of GUI applications and why people choose React

TL;DR

• All applications for normal users are GUI applications.

• The fundamental problem is rendering GUI(include assembling GUI, handling user input and integrating with application logic).

• This is not another talk about React, ES2015, Webpack, and other fancy things!

Page 3: The fundamental problems of GUI applications and why people choose React

Desktop applications are GUI applications

Page 4: The fundamental problems of GUI applications and why people choose React

Desktop applications are GUI applications

Mobile applications are GUI applications

Page 5: The fundamental problems of GUI applications and why people choose React

Desktop applications are GUI applications

Mobile applications are GUI applications

Web applications are GUI applications

Page 6: The fundamental problems of GUI applications and why people choose React

Every application you use dailyis

GUI applications

Page 7: The fundamental problems of GUI applications and why people choose React

Every application you use dailyis

GUI applications

(We developers* are the only onesthat work with terminal**)

* Including programmers, hackers, specialists, etc.** Even the terminal itself is a GUI application!

Page 8: The fundamental problems of GUI applications and why people choose React

So if you want to build an applicationfor normal users,

you have no choice but GUI application!

Page 9: The fundamental problems of GUI applications and why people choose React

Let’s build a basic GUI application (1)(without GUI library & framework)

Tier 1: Operating System

Bare metal

Page 10: The fundamental problems of GUI applications and why people choose React

What do we have?

• A drawing buffer for output

• An input processing unit

• A programming language

• No GUI library or framework

Let’s build things from scratch*!

* Sample code using JavaScript.

Page 11: The fundamental problems of GUI applications and why people choose React

1235

Page 12: The fundamental problems of GUI applications and why people choose React

The game loop

Process input

Update state

Render

while (playing) {

processInput();

updateState();

render();

waitForNextFrame();

}

Page 13: The fundamental problems of GUI applications and why people choose React

The game state

var gameState = {score: 1235,time: 1200,player: {

x: 120,y: 30,

},enemies: [ … ],bullets: [ … ]

};

1235

Page 14: The fundamental problems of GUI applications and why people choose React

The rendering function

function render() {renderBackground();renderEnemies();renderCharacter();renderBullets();renderScore();renderTimeBar();

swapBuffer();}

1235

Page 15: The fundamental problems of GUI applications and why people choose React

Updating state

function updateScore(val) {gameState.score += val;

}

function updatePosition(dx, dy) {gameState.player.x += dx;gameState.player.y += dy;

}

Page 16: The fundamental problems of GUI applications and why people choose React

Life of a GUI application

state

1

1235

render()

Page 17: The fundamental problems of GUI applications and why people choose React

Life of a GUI application

state

1

1235

1235

state

2

render()

render()

updateState()

Page 18: The fundamental problems of GUI applications and why people choose React

Life of a GUI application

state

1

1235

1235

state

2

render()

render()

processInput()

updateState()

processInput()

Page 19: The fundamental problems of GUI applications and why people choose React

Life of a GUI application

state

1

1235

1235

state

2

render()

render()

processInput()

updateState()

processInput()

Page 20: The fundamental problems of GUI applications and why people choose React

Luckily you don’t have towrite “the loop” yourself.

The operating system handles it for you.

Page 21: The fundamental problems of GUI applications and why people choose React

The fundamental problemsof GUI applications

Page 22: The fundamental problems of GUI applications and why people choose React

The problems

• Creating and assembling GUI

• Handling user input

• Integrating GUI & business logic

Page 23: The fundamental problems of GUI applications and why people choose React

Basic GUI application architecture

Viewstaterender()

input inputlogic

Page 24: The fundamental problems of GUI applications and why people choose React

Assembling GUI

Viewstate

render()

input inputlogic

Viewstate

render()

input inputlogic

Viewstate

render()

input inputlogic

Page 25: The fundamental problems of GUI applications and why people choose React

Assembling GUI

Viewstate

render()

input inputlogic

Viewstate

render()

input inputlogic

Viewstate

render()

input inputlogic

input

render()

Page 26: The fundamental problems of GUI applications and why people choose React

Tier 1

• Creating and assembling GUI → render() function

• Handling user input → (…)

• Integrating GUI & business logic → do it yourself

Page 27: The fundamental problems of GUI applications and why people choose React

What does a modernoperating system offers?

Tier 1: Operating System

Bare metal

Page 28: The fundamental problems of GUI applications and why people choose React

Process input

Update state

Render

onClick()onKeydown()

WM_LBUTTONDOWNWM_KEYDOWN

onPaint()

WM_PAINT

Applicationbusiness logic

Application input logic

Application rendering logic

Operating System ApplicationEvent system

Page 29: The fundamental problems of GUI applications and why people choose React

What does a modern operating system offers?

• Handle “the loop”

• Process raw input and provide event system

Page 30: The fundamental problems of GUI applications and why people choose React

What does a modern operating system offers?

• Creating and assembling GUI → (defer to app platform)

• Handling user input → Event system

• Integrating GUI & business logic → do it yourself

Page 31: The fundamental problems of GUI applications and why people choose React

Let’s build a basic GUI application (2)(without GUI library & framework)

Tier 1: Operating System

Tier 2: App Platform

Close to bare metal

Page 32: The fundamental problems of GUI applications and why people choose React

What do we have? (at tier 2)

• Component system

• Event system

Sample code using Windows API*

* Win32 & COM API. Read more: https://msdn.microsoft.com/en-us/library/windows/desktop/ff381399(v=vs.85).aspx

Page 33: The fundamental problems of GUI applications and why people choose React

The application state

struct {int score,int time,PLAYER player,ENEMIES enemies,BULLETS bullets

} gameState;

1235

Page 34: The fundamental problems of GUI applications and why people choose React

The rendering function

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)PAINT_STRUCT ps;HDC hdc;switch (message) {

case WM_PAINT:hdc = BeginPaint(hWnd, &ps);// ...

EndPaint(hWnd, &ps);break;

}};

1235

Page 35: The fundamental problems of GUI applications and why people choose React

Composing components

• Create child windows

• Attach them to the app window

• In response to WM_PAINT:• Pass WM_PAINT to child windows

Page 36: The fundamental problems of GUI applications and why people choose React

Handling input

• Handling user inputResponse to input events WM_LBUTTONDOWN, WM_KEYDOWN

• Handling application life cycle WM_CREATE, WM_DESTROY

Page 37: The fundamental problems of GUI applications and why people choose React

Process input

Update state

Render

onClick()onKeydown()

WM_LBUTTONDOWNWM_KEYDOWN

onPaint()

WM_PAINT

Applicationbusiness logic

Application input logic

Application rendering logic

Operating System ApplicationEvent system

Page 38: The fundamental problems of GUI applications and why people choose React

What does a modern application platform offers?

Tier 1: Operating System

Tier 2: App Platform

Close to bare metal

Page 39: The fundamental problems of GUI applications and why people choose React

Android Platform

• Composing elements: XML Layout, GUI components

• Handling user input: Event system

• Integrating business logic: Callback

<Buttonxmlns:android="http://schemas..."android:id="@+id/button_send"android:text="@string/button_send"android:onClick="sendMessage" />

public void sendMessage(View view) {// Do something

}

http://developer.android.com/guide/topics/ui/controls/button.html

Page 40: The fundamental problems of GUI applications and why people choose React

Windows Presentation Foundation (WPF)

• Composing elements: XAML, GUI components

• Handling user input: Event system

• Integrating business logic: Handler

<ButtonGrid.Column="1" Grid.Row="3"Margin="0,10,0,0" Width="125"Height="25" HorizontalAlignment="Right"Click="Button_Click">View</Button>

private void Button_Click(object sender, RoutedEventArgs e) {// Do something

}

https://msdn.microsoft.com/en-us/library/mt270964.aspx

Page 41: The fundamental problems of GUI applications and why people choose React

Web Platform (HTML & JS)

• Composing elements: HTML, GUI components

• Handling user input: Event system

• Integrating business logic: Callback

<buttonstyle="width:100px;height:40px"onclick="sayHello()">Say Hello

</button>

function sayHello(e) {// Do something

}

Page 42: The fundamental problems of GUI applications and why people choose React

What does an application platform offers?

• Creating and assembling GUI → Pre-built components, Domain specific language (DSL) for GUI

• Handling user input → Event system

• Integrating GUI & business logic → Callback, set state

Page 43: The fundamental problems of GUI applications and why people choose React

Let’s build a basic GUI application (3)(without GUI library & framework)

Tier 1: Operating System

Tier 2: App Platform

Tier 3: App

Page 44: The fundamental problems of GUI applications and why people choose React

What do we have? (at tier 3, HTML & JS)

• DSL & pre-built GUI components

• Event system

• Callback & set component state

• No GUI library or framework.

We still want to create our custom components!

Let’s build a TODO application.

Page 45: The fundamental problems of GUI applications and why people choose React

The application state

var appState = {todos: [{ title: “hello”,complete: false

}, {title: “world”,complete: false

}]};

Page 46: The fundamental problems of GUI applications and why people choose React

Updating state

function addTodo(label) {appState.todos.push({

title: label, completed: false

});}

function toggle(index) {var item = appState.todos[index];item.completed = !item.completed;

}

Page 47: The fundamental problems of GUI applications and why people choose React

The rendering function

function render() {// !?

}

Page 48: The fundamental problems of GUI applications and why people choose React

The rendering function – First try

function render() {var $listTodos = document.getElementById(“todos”)for (var i=0; i < appState.todos; i++) {

// update, insert or delete DOM elements}

var $numActive = document.getElementById(“num-active”)$numActive.innerHTML = getNumActive();

// ...}

Page 49: The fundamental problems of GUI applications and why people choose React

So far, we have defined application stateand logic just fine.

The only hard part that kept us back is rendering step.

(Include generating HTML, keeping updated with app state and registering event callbacks)

Page 50: The fundamental problems of GUI applications and why people choose React

The rendering function – Second try

var lastState; // store last appState for comparingfunction render() {var $listTodos = document.getElementById(“todos”)for (var i=0; i < appState.todos; i++) {

// update, insert or delete DOM elements}

var $numActive = document.getElementById(“num-active”)$numActive.innerHTML = getNumActive();

// ...lastState = deepClone(appState);

}

Page 51: The fundamental problems of GUI applications and why people choose React

We have tried storing last application statefor rendering only changed parts.

This is what frameworks like Angular.js or Backbone (Underscore) offers.

Page 52: The fundamental problems of GUI applications and why people choose React

EnterMVC & MVP

Page 53: The fundamental problems of GUI applications and why people choose React

MVC

• Applying Separation ofConcern to GUI applications.

• Input event → Controller → Model → View

Page 54: The fundamental problems of GUI applications and why people choose React

MVP

• Applying Separation ofConcern to GUI applications.

• Model → Presenter → View

• View → Presenter → Model

Page 55: The fundamental problems of GUI applications and why people choose React

Backbone.js

var Todo = Backbone.Model.extend({default: { title: "", complete: false },toggle: function() { this.save(…); // trigger "change" (model)

}});

var TodoView = Backbone.View.extend({template: …,events: …, // callback to manipulate model (handled by controller)initialize: {this.listenTo(this.model, "change", this.render); // listen to "change"

},render: function() { … }, // rendering function (view)// …

});

Page 56: The fundamental problems of GUI applications and why people choose React

Angular.js (version 1)

function TodoCtrl($scope) {var todos = $scope.todos = [];

$scope.addTodo = function() { // user eventtodos.push({ title: $scope.newTodo, completed: false }); // update state

};

$scope.$watchCollection("todos", function() { // state-change event// …

});}

<form id="todo-form" ng-submit="addTodo()"> … </form><ul id="todo-list">

<li ng-repeat="todo in todos"> … </li></ul>

Page 57: The fundamental problems of GUI applications and why people choose React

What does aapplication MV* framework offers?

Tier 1: Operating System

Tier 2: App Platform

Tier 3: App

Page 58: The fundamental problems of GUI applications and why people choose React

What does a MV* framework offers?

• Creating and assembling GUI → view, template

• Handling user input → user event

• Integrating GUI & business logic → state-change event (Backbone.js, Angular.js)

Page 59: The fundamental problems of GUI applications and why people choose React

Wow, so many concepts!model, view, template, controller, presenter,

user event, state-change event

Let’s return to our starting architecture

Page 60: The fundamental problems of GUI applications and why people choose React

Life of a GUI application

state

1

1235

1235

state

2

render()

render()

processInput()

updateState()

processInput()

Page 61: The fundamental problems of GUI applications and why people choose React

Enter React solution

Page 62: The fundamental problems of GUI applications and why people choose React

Let’s put aside the fancy ways to define application state.

The only hard part is rendering step.

Page 63: The fundamental problems of GUI applications and why people choose React

The application state

var appState = {todos: [{ title: “hello”,complete: false

}, {title: “world”,complete: false

}]};

Page 64: The fundamental problems of GUI applications and why people choose React

Updating state

function addTodo(label) {appState.todos.push({

title: label, completed: false

});}

function toggle(index) {var item = appState.todos[index];item.completed = !item.completed;

}

Page 65: The fundamental problems of GUI applications and why people choose React

The rendering function

React.createClass({render: function() {

return (<ul>{

appState.todos.map(function(item) {return <li> { item.title } </li>;

})}</ul>

);}

});

Page 66: The fundamental problems of GUI applications and why people choose React

Handle state change

React.createClass({addTodoHandler: function() {var label = this.refs.inputTodo.value;addTodo(label); // update application statethis.forceUpdate(); // trigger rendering function

},render: function() {return (

<div><input ref="inputTodo"/><button onClick={this.addTodoHandler}/><ul> … </ul>

</div>);}

});

Page 67: The fundamental problems of GUI applications and why people choose React

Life of a GUI application

state

1

1235

1235

state

2

render()

render()

addTodoHandler()

addTodo()

toggleHandler()

toggle()

Page 68: The fundamental problems of GUI applications and why people choose React

React lets us work with our classic architectureand helps solving the hard part: rendering!

No need to rewrite our application in an opinion way.

Page 69: The fundamental problems of GUI applications and why people choose React

We only need to understand 2 functions to start working with React:- forceUpdate()- render()

Page 70: The fundamental problems of GUI applications and why people choose React

What does React offers?

• Creating and assembling GUI → React components

• Handling user input → user events

• Integrating GUI & business logic → keep GUI updated when application state changed

Page 71: The fundamental problems of GUI applications and why people choose React

Why do people choose React?

Page 72: The fundamental problems of GUI applications and why people choose React

What do people choose React?

• As we see, the only hard part is rendering step.

• React is a view library. It solves the right problem and solves it well.

• It leaves application state for us. This is good because:• We work with classic architecture of GUI applications.• We can choose which architecture works best for us.• We can migrate legacy applications to React without

changing so much code.

Page 73: The fundamental problems of GUI applications and why people choose React

How to choose a library or framework?

1. Write the prototype by your ownwithout using library or framework.

2. Understand what them offer.

3. Choose only which ones you need.

4. Keep in mind the design that you can switch to another library later.

Page 74: The fundamental problems of GUI applications and why people choose React

What’s next?

• Redux, an application state solution for React.• Because we understand how to handle application state,

we can decide to use Redux or not. It’s up to you.

Page 75: The fundamental problems of GUI applications and why people choose React

THANK YOU

Vu [email protected]

TechTalk

Page 76: The fundamental problems of GUI applications and why people choose React

TechTalk

The fundamental problems ofGUI applications

& why people choose React

Vu [email protected]