Skip to content

Commit 82b7dfe

Browse files
author
Matthew Phillips
committed
Merge branch 'main' into new-docs
2 parents 1d56328 + 0cf6366 commit 82b7dfe

32 files changed

+3302
-13098
lines changed

.changeset/config.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,8 @@
77
"access": "public",
88
"baseBranch": "main",
99
"updateInternalDependencies": "patch",
10+
"___experimentalUnsafeOptions_WILL_CHANGE_IN_PATCH": {
11+
"onlyUpdatePeerDependentsWhenOutOfRange": true
12+
},
1013
"ignore": []
1114
}

.changeset/odd-fans-cough.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
"robot3": minor
3+
---
4+
5+
'/logging' is now exported, so you can import it in your dev environment to log state changes.
6+
7+
```ts
8+
import 'robot3/logging';
9+
10+
import {...} from 'robot3';
11+
```

.github/workflows/nodejs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ on: [push]
55
jobs:
66
build:
77

8-
runs-on: ubuntu-latest
8+
runs-on: ubuntu-22.04
99

1010
strategy:
1111
matrix:

.github/workflows/site.yml

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ on:
66
- main
77
paths:
88
- 'docs/**'
9+
- '.github/workflows/site.yml'
910

1011
jobs:
1112
deploy:
@@ -18,22 +19,18 @@ jobs:
1819
with:
1920
node-version: 22
2021

22+
- name: Setup Rclone
23+
uses: AnimMouse/setup-rclone@v1
24+
with:
25+
rclone_config: ${{ secrets.RCLONE_CONFIG }}
26+
2127
- name: Install dependencies
2228
run: npm ci
2329

2430
- name: Build Site
2531
run: npm run --workspace=docs build
2632

27-
- name: Sync Bucket
28-
uses: jakejarvis/s3-sync-action@master
29-
with:
30-
args: --delete
31-
env:
32-
AWS_S3_BUCKET: ${{ secrets.AWS_S3_BUCKET }}
33-
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
34-
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
35-
DEST_DIR: '/'
36-
SOURCE_DIR: 'docs/_site' # optional: defaults to entire repository
33+
- run: rclone sync docs/_site remote:thisrobot.life
3734

3835
- name: Invalidate CDN
3936
uses: chetan/invalidate-cloudfront-action@master

docs/api/invoke.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,3 +196,34 @@ const machine = createMachine({
196196
error: state()
197197
})
198198
```
199+
200+
## Cancellation
201+
202+
JavaScript does not support cancellation of promises: the action which will
203+
resolve the promise will run to completion or until it encounters an error.
204+
205+
There are situations where the result of the promise no longer matters. The
206+
machine should proceed to a different action, or simply stop changing state
207+
entirely. To achieve this with the `invoke` state, an extra event can be
208+
added to the state like the `cancel` event in the example below.
209+
210+
```js
211+
import { createMachine, invoke, reduce, state, transition } from 'robot3';
212+
213+
const loadTodos = () => Promise.reject("Sorry but you can't do that");
214+
215+
const machine = createMachine({
216+
start: invoke(loadTodos,
217+
transition('cancel', 'cancelled'),
218+
transition('done', 'loaded',
219+
reduce((ctx, ev) => ({ ...ctx, todo: ev.data }))
220+
)
221+
),
222+
cancelled: state(),
223+
loaded: state()
224+
})
225+
```
226+
227+
By moving out of `start` state before the promise returned by `loadTodos`
228+
resolves, the function result will be discarded: the machine finds that it
229+
is no longer in the state from which is was invoked and discards the event.

docs/guides.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ These guides are meant to help you along your journey learning Finite State Mach
99

1010
* __[Composition](./guides/composition.html)__: How to use functional composition when building state machines, to maximize code reuse where possible.
1111
* __[Nested States](./guides/nested-states.html)__: Create machines with nested states by [invoking](./api/invoke.html) other machines.
12-
* __[Comparison with XState](./guides/comparison-with-xstate.html)__: Differences and tradeoffs between Robot and [XState](https://xstate.js.org).
12+
* __[Awaiting asynchronous completion](./guides/awaiting-asynchronous-execution.html)__: How to await a machine to enter a `final` state.
13+
* __[Comparison with XState](./guides/comparison-with-xstate.html)__: Differences and tradeoffs between Robot and [XState](https://xstate.js.org).
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
layout: page.njk
3+
title: Awaiting asynchronous execution
4+
tags: guide
5+
permalink: guides/awaiting-asynchronous-execution.html
6+
---
7+
8+
In a scenario where it's necessary to `await` the machine to enter a `final`
9+
state (a state which has no transitions), the `onChange` callback (the second argument to `interpret`) can be used
10+
to resolve a promise. The promise can be externally awaited.
11+
12+
```js
13+
let resolve;
14+
let promise = new Promise(_resolve => {
15+
resolve = _resolve;
16+
})
17+
18+
service = interpret(machine, () => {
19+
if(machine.state.value.final) {
20+
resolve();
21+
}
22+
});
23+
24+
await promise;
25+
// All done!
26+
```
27+
28+
This is particularly useful for state machines which consist entirely
29+
of `immediate` and `invoke` states: these model execution flows which do
30+
not depend on external events for their execution and state transition.

docs/guides/nested-states.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ Here we create a `nested` function that defines a submachine.
7272

7373
```js
7474
// A helper function for more easily building nested state machines.
75-
const nested = (to, states) => createMachine(states,
75+
const nested = (to, states) => invoke(
76+
createMachine(states),
7677
transition('done', to)
7778
);
7879

@@ -90,4 +91,4 @@ const stoplight = createMachine({
9091
dontWalk: final()
9192
})
9293
});
93-
```
94+
```

0 commit comments

Comments
 (0)