Skip to content

Commit 51843d6

Browse files
authored
Merge pull request scratchfoundation#5416 from benjiwheeler/project-state-diagram
Project state diagram
2 parents 02be215 + 4e633cb commit 51843d6

File tree

3 files changed

+59
-1
lines changed

3 files changed

+59
-1
lines changed

Diff for: README.md

+56-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ See [jest cli docs](https://facebook.github.io/jest/docs/en/cli.html#content) fo
7474

7575
*NOTE: If you're a windows user, please run these scripts in Windows `cmd.exe` instead of Git Bash/MINGW64.*
7676

77-
Before running any test, make sure you have run `npm install` from this (scratch-gui) repository's top level.
77+
Before running any tests, make sure you have run `npm install` from this (scratch-gui) repository's top level.
7878

7979
#### Main testing command
8080

@@ -194,5 +194,60 @@ Further reading: [Stack Overflow](https://stackoverflow.com/questions/46602286/n
194194
You can publish the GUI to github.io so that others on the Internet can view it.
195195
[Read the wiki for a step-by-step guide.](https://github.com/LLK/scratch-gui/wiki/Publishing-to-GitHub-Pages)
196196

197+
## Understanding the project state machine
198+
199+
Since so much code throughout scratch-gui depends on the state of the project, which goes through many different phases of loading, displaying and saving, we created a "finite state machine" to make it clear which state it is in at any moment. This is contained in the file src/reducers/project-state.js .
200+
201+
It can be hard to understand the code in src/reducers/project-state.js . There are several types of data and functions used, which relate to each other:
202+
203+
### Loading states
204+
205+
These include state constant strings like:
206+
207+
* `NOT_LOADED` (the default state),
208+
* `ERROR`,
209+
* `FETCHING_WITH_ID`,
210+
* `LOADING_VM_WITH_ID`,
211+
* `REMIXING`,
212+
* `SHOWING_WITH_ID`,
213+
* `SHOWING_WITHOUT_ID`,
214+
* etc.
215+
216+
### Transitions
217+
218+
These are names for the action which causes a state change. Some examples are:
219+
220+
* `START_FETCHING_NEW`,
221+
* `DONE_FETCHING_WITH_ID`,
222+
* `DONE_LOADING_VM_WITH_ID`,
223+
* `SET_PROJECT_ID`,
224+
* `START_AUTO_UPDATING`,
225+
226+
### How transitions relate to loading states
227+
228+
As this diagram of the project state machine shows, various transition actions can move us from one loading state to another:
229+
230+
![Project state diagram](docs/project_state_diagram.svg)
231+
232+
_Note: for clarity, the diagram above excludes states and transitions relating to error handling._
233+
234+
#### Example
235+
236+
Here's an example of how states transition.
237+
238+
Suppose a user clicks on a project, and the page starts to load with url https://scratch.mit.edu/projects/123456 .
239+
240+
Here's what will happen in the project state machine:
241+
242+
![Project state example](docs/project_state_example.png)
243+
244+
1. When the app first mounts, the project state is `NOT_LOADED`.
245+
2. The `SET_PROJECT_ID` redux action is dispatched (from src/lib/project-fetcher-hoc.jsx), with `projectId` set to `123456`. This transitions the state from `NOT_LOADED` to `FETCHING_WITH_ID`.
246+
3. The `FETCHING_WITH_ID` state. In src/lib/project-fetcher-hoc.jsx, the `projectId` value `123456` is used to request the data for that project from the server.
247+
4. When the server responds with the data, src/lib/project-fetcher-hoc.jsx dispatches the `DONE_FETCHING_WITH_ID` action, with `projectData` set. This transitions the state from `FETCHING_WITH_ID` to `LOADING_VM_WITH_ID`.
248+
5. The `LOADING_VM_WITH_ID` state. In src/lib/vm-manager-hoc.jsx, we load the `projectData` into Scratch's virtual machine ("the vm").
249+
6. When loading is done, src/lib/vm-manager-hoc.jsx dispatches the `DONE_LOADING_VM_WITH_ID` action. This transitions the state from `LOADING_VM_WITH_ID` to `SHOWING_WITH_ID`
250+
7. The `SHOWING_WITH_ID` state. Now the project appears normally and is playable and editable.
251+
197252
## Donate
198253
We provide [Scratch](https://scratch.mit.edu) free of charge, and want to keep it that way! Please consider making a [donation](https://secure.donationpay.org/scratchfoundation/) to support our continued engineering, design, community, and resource development efforts. Donations of any size are appreciated. Thank you!

Diff for: docs/project_state_diagram.svg

+3
Loading

Diff for: docs/project_state_example.png

107 KB
Loading

0 commit comments

Comments
 (0)