- commit
- 797ba54
- parent
- e582179
- author
- Eric Bower
- date
- 2024-02-08 15:10:18 +0000 UTC
docs: readme
1 files changed,
+18,
-8
+18,
-8
1@@ -290,6 +290,8 @@ In review:
2
3 # what is a supervisor task?
4
5+[Supplemental reading from erlang](https://www.erlang.org/doc/design_principles/des_princ)
6+
7 A supervisor task is a way to monitor children tasks and probably most
8 importantly, manage their health. By structuring your side-effects and business
9 logic around supervisor tasks, we gain very interesting coding paradigms that
10@@ -320,10 +322,9 @@ for events and if they fail, restart them.
11 import { parallel, run, takeEvery } from "starfx";
12
13 function* watchFetch() {
14- const task = yield* takeEvery("FETCH_USERS", function* (action) {
15+ yield* takeEvery("FETCH_USERS", function* (action) {
16 console.log(action);
17 });
18- yield* task;
19 }
20
21 function* send() {
22@@ -340,17 +341,16 @@ await run(
23 Here we create a supervisor function using a helper `takeEvery` to call a
24 function for every `FETCH_USERS` event emitted.
25
26-However, this means that we are going to make the same request 3 times, we need
27-a throttle or debounce to prevent this issue.
28+However, this means that we are going to make the same request 3 times, we
29+probably want a throttle or debounce to prevent this behavior.
30
31 ```ts
32 import { takeLeading } from "starfx";
33
34 function* watchFetch() {
35- const task = yield* takeLeading("FETCH_USERS", function* (action) {
36+ yield* takeLeading("FETCH_USERS", function* (action) {
37 console.log(action);
38 });
39- yield* task;
40 }
41 ```
42
43@@ -364,8 +364,18 @@ Both thunks and endpoints support overriding the default `takeEvery` supervisor
44 for either our officially supported supervisors `takeLatest` and `takeLeading`,
45 or a user-defined supervisor.
46
47-That's it. We are just leveraging the same tiny API that we are already using in
48-`starfx`.
49+Because every thunk and endpoint have their own supervisor tasks monitoring the
50+health of their children, we allow the end-developer to change the default
51+supervisor -- which is `takeEvery`:
52+
53+```ts
54+const someAction = thunks.create("some-action", { supervisor: takeLatest });
55+dispatch(someAction()); // this task gets cancelled
56+dispatch(someAction()); // this task gets cancelled
57+dispatch(someAction()); // this tasks lives
58+```
59+
60+This is the power of supervisors and is fundamental to how `starfx` works.
61
62 # example: test that doesn't need an http interceptor
63