@mantaq/core 0.5.0 → 0.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -4
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -12,7 +12,7 @@ npm install @mantaq/core
|
|
|
12
12
|
|
|
13
13
|
| Pattern | Correct | Anti-pattern |
|
|
14
14
|
| --------------- | -------------------------------------------------- | ------------------------------------- |
|
|
15
|
-
| Type context | `
|
|
15
|
+
| Type context | `const c: MyContext = {...}`, `context: c` | `as` cast on the context value |
|
|
16
16
|
| Write context | `const s = context.get(); s.x = y; context.set(s)` | mutate without `set()` |
|
|
17
17
|
| Type events | `event("ID")<Payload>()` | `event("ID")()` without generic |
|
|
18
18
|
| Send events | `actor.send(signInEvent.create(data))` | raw `{ type: "ID", payload }` objects |
|
|
@@ -81,7 +81,7 @@ See [anderscan.github.io/mantaq](https://anderscan.github.io/mantaq) for full do
|
|
|
81
81
|
|
|
82
82
|
### Typed Actor Context
|
|
83
83
|
|
|
84
|
-
|
|
84
|
+
Declare the context value with its type once, then pass it to the constructor. Handlers receive the typed context automatically. `as` on the context is an anti-pattern: it hides missing fields.
|
|
85
85
|
|
|
86
86
|
```ts
|
|
87
87
|
interface AuthContext {
|
|
@@ -89,11 +89,13 @@ interface AuthContext {
|
|
|
89
89
|
phoneNumber?: string;
|
|
90
90
|
}
|
|
91
91
|
|
|
92
|
+
const initialContext: AuthContext = {};
|
|
93
|
+
|
|
92
94
|
const actor = new Actor({
|
|
93
95
|
inputs: [signInEvent],
|
|
94
96
|
states: [idleState, signingInState],
|
|
95
97
|
initial: idleState,
|
|
96
|
-
context:
|
|
98
|
+
context: initialContext,
|
|
97
99
|
setup: (m) => {
|
|
98
100
|
m.on(idleState, signInEvent, (event, opts) => {
|
|
99
101
|
// ✅ typed — context.get()/set() flow the constructor type
|
|
@@ -299,12 +301,13 @@ const doneEvent = event("WORK_DONE")();
|
|
|
299
301
|
const failedEvent = event("WORK_FAILED")<{ error: string }>();
|
|
300
302
|
|
|
301
303
|
function createActor() {
|
|
304
|
+
const initialContext: MyContext = { retryCount: 0, maxRetries: 3 };
|
|
302
305
|
return new Actor({
|
|
303
306
|
inputs: [startEvent],
|
|
304
307
|
internal: [doneEvent, failedEvent],
|
|
305
308
|
states: [idleState, workingState, doneState, errorState],
|
|
306
309
|
initial: idleState,
|
|
307
|
-
context:
|
|
310
|
+
context: initialContext,
|
|
308
311
|
setup: (m) => {
|
|
309
312
|
m.effect(workingState, {
|
|
310
313
|
name: "attemptWork",
|