David Khourshid Β· @davidkpiano
Γredev Developer Conference 2022
<form onSubmit={event => {
submitData(event);
}}>
{/* ... */}
</form>
const [isLoading, setIsLoading] = useState(false);
<form onSubmit={event => {
if (isLoading) { return; }
submitData(event);
setIsLoading(true);
}}>
{/* ... */}
</form>
const [isLoading, setIsLoading] = useState(false);
<form onSubmit={event => {
if (isLoading) { return; }
submitData(event)
.then(() => { setIsLoading(false) })
setIsLoading(true);
}}>
{/* ... */}
</form>
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState(null);
<form onSubmit={event => {
if (isLoading) { return; }
submitData(event)
.then(() => { setIsLoading(false) })
.catch(err => {
setIsLoading(false);
setError(err);
});
setIsLoading(true);
}}>
{/* ... */}
</form>
const [isLoading, setIsLoading] = useState(false);
const [isCanceled, setIsCanceled] = useState(false);
const [error, setError] = useState(null);
<form onSubmit={event => {
if (isLoading) { return; }
submitData(event)
.then(() => {
if (isCanceled) { return; }
setIsLoading(false)
})
.catch(err => {
if (isCanceled) { return; }
setIsLoading(false);
setError(err);
});
setIsLoading(true);
}}>
{/* ... */}
<button
type="button"
onClick={() => { setIsCanceled(true) }}
>
Cancel
</button>
{/* ... */}
</form>
+ Features
- Bugs
+/- Devs
+ Changes
+ Shiny new things
Time
Complexity
The path to
legacy code
Left to right
Top to bottom
File by file
User stories
Mockups
User flows
Code
Tests
Tutorials
User stories
Mockups
User flows
Code
Tests
Tutorials
Logged out
Logged in
LOG IN
[correct credentials]
Given a user is logged out,
When the user logs in with correct credentials
Then the user should be logged in
cart
shipping
contact
payment
confirmation
CHECKOUT
NEXT
NEXT
ORDER
PAYPAL
isLoading isSuccess hasErrors
type Status = Β | 'loading' Β | 'error' Β | 'success'
function transition(state, event) {
switch (state.value) {
case 'cart':
if (event.type === 'CHECKOUT') {
return { value: 'shipping' };
}
return state;
case 'shipping':
// ...
default:
return state;
}
}
State
Event
const machine = {
initial: 'cart',
states: {
cart: {
on: {
CHECKOUT: 'shipping'
}
},
shipping: {
on: {
NEXT: 'contact'
}
},
contact: {
// ...
},
// ...
}
}
function transition(state, event) {
const nextState = machine
.states[state]
.on?.[event.type]
?? state;
}
transition('cart', { type: 'CHECKOUT' });
// => 'shipping'
transition('cart', { type: 'UNKNOWN' });
// => 'cart'
import { createMachine } from 'xstate';
const machine = createMachine({
initial: 'cart',
states: {
cart: {
on: {
CHECKOUT: 'shipping'
}
},
shipping: {
on: {
NEXT: 'contact'
}
},
contact: {
// ...
},
// ...
}
});
import { interpret } from 'xstate';
const actor = interpret(machine).start();
actor.send({ type: 'CHECKOUT' });
// => { value: 'shipping' }
actor.send({ type: 'NEXT' });
// => { value: 'contact' }
npm install xstate
xstate.js.org/docs
Β
cart
shipping
contact
payment
confirmation
CHECKOUT
NEXT
NEXT
ORDER
PAYPAL
BACK
BACK
CANCEL
cart
shipping
contact
payment
confirmation
CHECKOUT
NEXT
NEXT
ORDER
PAYPAL
As a user, when I'm in the cartΒ and I click the checkoutΒ button, I should be on the shippingΒ page.
cart
shipping
contact
payment
confirmation
CHECKOUT
NEXT
NEXT
ORDER
PAYPAL
As a user, when I'm in the cartΒ and I checkout via PayPal, I should be taken directly to the paymentΒ screen.
cart
shipping
contact
payment
confirmation
CHECKOUT
NEXT
NEXT
ORDER
PAYPAL
confirmationΒ state
cart
shipping
contact
payment
confirmation
CHECKOUT
NEXT
NEXT
ORDER
PAYPAL
confirmationΒ state where
shipping address is provided
idle
loading
LOAD / fetchData
success
RELOAD / fetchData
RESOLVE / assign
idle
loading
Β
LOAD
success
RELOAD
RESOLVE / assign
entry / fetchData
exit / logTelemetry
loading
ready
playing
paused
loaded
next
βΈ
βΆοΈ
music
player
volume
playing
paused
muted
unmuted
π Guarded transitions β¬οΈ Raised events β± Delayed (after) transitions βΆοΈ Eventless (always) transitions β³ History states ...
Callbacks
Event handlers
Ad-hoc logic
Ad-hoc logic
Reducer
EVENT
EVENT
EVENT
EVENT
State machine
EVENT
EVENT
EVENT
Statechart
Actors
Actor π©βπ»
Actor π§
βοΈ
βοΈ
someActor.send({
type: 'greet',
value: 'Hej!',
from: self
});
// ...
switch (message.type) {
case 'greet':
message.from.send({
type: 'greet',
value: '...',
from: self
});
// ...
}
Send
Receive
π€ All computation is performed within an actor
π© Actors can communicate only through messages
π₯ In response to a message, an actor can:
π¬ Send messages to other actors
π¨βπ§βπ¦ Create a finite number of child actors
π (π₯)
Behavior
State
π Change its state/behavior
A
B
βοΈ
βοΈ
C
βοΈ
π€ All computation is performed within an actor
π© Actors can communicate only through messages
π₯ In response to a message, an actor can:
π¬ Send messages to other actors
π¨βπ§βπ¦ Create a finite number of child actors
π Change its state/behavior
A
βοΈ
π€ All computation is performed within an actor
π© Actors can communicate only through messages
π₯ In response to a message, an actor can:
π¬ Send messages to other actors
π¨βπ§βπ¦ Create a finite number of child actors
π Change its state/behavior
A β A'
A
βοΈ
π€ All computation is performed within an actor
π© Actors can communicate only through messages
π₯ In response to a message, an actor can:
π¬ Send messages to other actors
π¨βπ§βπ¦ Create a finite number of child actors
π Change its state/behavior
B
βοΈ
A
βοΈ
π€ All computation is performed within an actor
π© Actors can communicate only through messages
π₯ In response to a message, an actor can:
π¬ Send messages to other actors
π¨βπ§βπ¦ Create a finite number of child actors
π Change its state/behavior
π
π
π©βπ»
π§
I would like a coffee...
What would you like?
Β ActorΒ
I would like a coffee, please.
Β ActorΒ
π
π¬
π¬
Here you go. βοΈ
Thanks!
π©βπ»
π§
π©βπ³
βοΈβ
π
πΆβ
πΆ
πβ
βοΈ
βοΈ
Actor system architecture
Sequence diagrams
1
2
3
4