Skip to content

Commit 98e6ca8

Browse files
committed
refactor: improve internal types
refactor: continue improving internal types refactor: again chore: wip chore: more wip
1 parent 70ade4d commit 98e6ca8

17 files changed

Lines changed: 532 additions & 205 deletions

File tree

apps/demo/src/data-access/garage-door-operations.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
createMachine,
44
effect,
55
interpret,
6+
invoke,
67
sendParent,
78
type MachineSnapshot,
89
type MachineActor,
@@ -205,7 +206,7 @@ export class GarageDoorMachineService extends Effect.Service<GarageDoorMachineSe
205206
assign(() => ({ position: 100, lastUpdated: new Date(), weather: { status: "loading" } })),
206207
effect(() => Effect.log("Entering: open - fetching weather")),
207208
],
208-
invoke: {
209+
invoke: invoke({
209210
id: "fetchWeather",
210211
src: () => weatherService.getWeather(DEFAULT_LAT, DEFAULT_LON),
211212
assignResult: {
@@ -229,7 +230,7 @@ export class GarageDoorMachineService extends Effect.Service<GarageDoorMachineSe
229230
weather: { status: "error", error: `Unexpected: ${String(defect)}` },
230231
}),
231232
},
232-
},
233+
}),
233234
on: {
234235
CLICK: { target: "closing", guard: ({ context }) => context.isPowered },
235236
POWER_ON: { actions: [assign(() => ({ isPowered: true }))] },

apps/docs/src/content/docs/api/actions.mdx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ function assignOnDefect<TContext>(
350350
**Example:**
351351

352352
```typescript
353-
invoke: {
353+
invoke: invoke({
354354
src: () => fetchUser(),
355355
onSuccess: {
356356
target: "ready",
@@ -369,7 +369,7 @@ invoke: {
369369
})),
370370
],
371371
},
372-
}
372+
})
373373
```
374374

375375
---
@@ -379,7 +379,7 @@ invoke: {
379379
A shorthand for handling all invoke outcomes in one place. Use this when you want to update context without transitioning to different states.
380380

381381
```typescript
382-
invoke: {
382+
invoke: invoke({
383383
src: () => Effect<TOutput, TError, R>,
384384
assignResult: {
385385
success: (params: { context: TContext; output: TOutput }) => Partial<TContext>;
@@ -389,13 +389,13 @@ invoke: {
389389
};
390390
defect?: (params: { context: TContext; defect: unknown }) => Partial<TContext>;
391391
}
392-
}
392+
})
393393
```
394394

395395
**Example:**
396396

397397
```typescript
398-
invoke: {
398+
invoke: invoke({
399399
src: () => fetchWeather(),
400400
assignResult: {
401401
success: ({ output }) => ({
@@ -408,13 +408,13 @@ invoke: {
408408
weather: { status: "crashed", message: String(defect) },
409409
}),
410410
},
411-
}
411+
})
412412
```
413413

414414
**With typed error handling:**
415415

416416
```typescript
417-
invoke: {
417+
invoke: invoke({
418418
src: () => fetchWeather(), // Effect<Weather, NetworkError | ParseError, never>
419419
assignResult: {
420420
success: ({ output }) => ({ weather: output }),
@@ -429,7 +429,7 @@ invoke: {
429429
failure: ({ error }) => ({ error: error.message }), // Fallback
430430
defect: ({ defect }) => ({ error: String(defect) }),
431431
},
432-
}
432+
})
433433
```
434434

435435
## See Also

apps/docs/src/content/docs/api/create-machine.mdx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,11 @@ const machine = createMachine({
276276

277277
### With Invoke
278278

279+
Use the `invoke()` helper for proper type inference:
280+
279281
```typescript
282+
import { createMachine, assign, invoke } from "effstate";
283+
280284
const machine = createMachine({
281285
id: "userLoader",
282286
initial: "idle",
@@ -287,7 +291,7 @@ const machine = createMachine({
287291
on: { LOAD: { target: "loading" } },
288292
},
289293
loading: {
290-
invoke: {
294+
invoke: invoke({
291295
id: "loadUser",
292296
src: ({ context }) => fetchUser(context.userId),
293297
onSuccess: {
@@ -298,7 +302,7 @@ const machine = createMachine({
298302
target: "error",
299303
actions: [assign(({ event }) => ({ error: event.error }))],
300304
},
301-
},
305+
}),
302306
},
303307
ready: {},
304308
error: {},

apps/docs/src/content/docs/api/interpret.mdx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ Effect.runPromise(
6666
### With Service Dependencies
6767

6868
```typescript
69+
import { createMachine, invoke, interpret } from "effstate";
70+
6971
class ApiClient extends Effect.Service<ApiClient>()("ApiClient", {
7072
succeed: {
7173
fetchUser: (id: string) => Effect.tryPromise(() => fetch(`/api/users/${id}`)),
@@ -76,14 +78,14 @@ class ApiClient extends Effect.Service<ApiClient>()("ApiClient", {
7678
const userMachine = createMachine({
7779
states: {
7880
loading: {
79-
invoke: {
81+
invoke: invoke({
8082
src: ({ context }) =>
8183
Effect.gen(function* () {
8284
const api = yield* ApiClient;
8385
return yield* api.fetchUser(context.userId);
8486
}),
8587
onSuccess: { target: "ready" },
86-
},
88+
}),
8789
},
8890
},
8991
});
@@ -189,22 +191,22 @@ function withRequirements<R>(): <...>(
189191
### Example
190192

191193
```typescript
192-
import { withRequirements, createMachine } from "effstate";
194+
import { withRequirements, createMachine, invoke } from "effstate";
193195
194196
// Machine that uses WeatherService in its invoke
195197
const garageDoorMachine = withRequirements<WeatherService>()(
196198
createMachine({
197199
id: "garageDoor",
198200
states: {
199201
checking: {
200-
invoke: {
202+
invoke: invoke({
201203
src: () =>
202204
Effect.gen(function* () {
203205
const weather = yield* WeatherService;
204206
return yield* weather.getCurrentWeather();
205207
}),
206208
onSuccess: { target: "ready" },
207-
},
209+
}),
208210
},
209211
},
210212
})

apps/docs/src/content/docs/api/types.mdx

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -161,17 +161,6 @@ actor.on<{ type: "notification"; message: string }>("notification", (event) => {
161161

162162
---
163163

164-
## Effect.Service Pattern Types
165-
166-
When using the Effect.Service pattern, these utility types help extract service requirements:
167-
168-
```typescript
169-
import type { MachineServiceR } from "effstate";
170-
171-
// Get R channel from a machine service
172-
type Requirements = MachineServiceR<typeof MyMachineService>;
173-
```
174-
175164
## See Also
176165

177166
- [createMachine](/api/create-machine/) - Creating machine definitions

apps/docs/src/content/docs/getting-started/introduction.mdx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ The recommended pattern for using effstate is to wrap your machines in `Effect.S
1616
- **Type Safety** - Full inference for dependencies and effects
1717

1818
```typescript
19-
import { createMachine, interpret, assign, effect } from "effstate";
19+
import { createMachine, interpret, assign, effect, invoke } from "effstate";
2020
import { Data, Effect, Schema } from "effect";
2121

2222
// Define events
@@ -57,7 +57,7 @@ export class ConnectionMachine extends Effect.Service<ConnectionMachine>()(
5757
},
5858
},
5959
connecting: {
60-
invoke: {
60+
invoke: invoke({
6161
src: () => api.connect(), // Use injected service
6262
onSuccess: { target: "connected" },
6363
onFailure: {
@@ -69,7 +69,7 @@ export class ConnectionMachine extends Effect.Service<ConnectionMachine>()(
6969
})),
7070
],
7171
},
72-
},
72+
}),
7373
},
7474
connected: {
7575
entry: [assign({ status: "connected", retryCount: 0 })],

apps/docs/src/content/docs/guides/actions.mdx

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -397,9 +397,9 @@ Special assign helpers for invoke handlers with proper typing:
397397
### assignOnSuccess
398398

399399
```typescript
400-
import { assignOnSuccess } from "effstate";
400+
import { assignOnSuccess, invoke } from "effstate";
401401

402-
invoke: {
402+
invoke: invoke({
403403
src: () => fetchUser(),
404404
onSuccess: {
405405
target: "ready",
@@ -410,15 +410,15 @@ invoke: {
410410
})),
411411
],
412412
},
413-
}
413+
})
414414
```
415415

416416
### assignOnFailure
417417

418418
```typescript
419-
import { assignOnFailure } from "effstate";
419+
import { assignOnFailure, invoke } from "effstate";
420420

421-
invoke: {
421+
invoke: invoke({
422422
src: () => fetchUser(),
423423
onFailure: {
424424
target: "error",
@@ -428,15 +428,15 @@ invoke: {
428428
})),
429429
],
430430
},
431-
}
431+
})
432432
```
433433

434434
### assignOnDefect
435435

436436
```typescript
437-
import { assignOnDefect } from "effstate";
437+
import { assignOnDefect, invoke } from "effstate";
438438

439-
invoke: {
439+
invoke: invoke({
440440
src: () => fetchUser(),
441441
onDefect: {
442442
target: "crashed",
@@ -446,7 +446,7 @@ invoke: {
446446
})),
447447
],
448448
},
449-
}
449+
})
450450
```
451451

452452
## Action Order

apps/docs/src/content/docs/guides/activities.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,9 +254,9 @@ activities: [
254254
]
255255

256256
// Invoke: one-shot operation with result handling
257-
invoke: {
257+
invoke: invoke({
258258
src: () => fetchUser(),
259259
onSuccess: { target: "ready" },
260260
onFailure: { target: "error" },
261-
}
261+
})
262262
```

apps/docs/src/content/docs/guides/guards.mdx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ The first transition whose guard returns `true` (or has no guard) will be taken.
167167
Guards can also control invoke result handling:
168168

169169
```typescript
170-
invoke: {
170+
invoke: invoke({
171171
src: () => fetchUser(),
172172
onSuccess: [
173173
{
@@ -179,7 +179,7 @@ invoke: {
179179
// No guard - default case
180180
},
181181
],
182-
}
182+
})
183183
```
184184

185185
## Type-Safe Guards
@@ -212,7 +212,7 @@ states: {
212212
},
213213
},
214214
validating: {
215-
invoke: {
215+
invoke: invoke({
216216
src: ({ context }) => validateData(context.data),
217217
onSuccess: {
218218
target: "valid",
@@ -223,7 +223,7 @@ states: {
223223
// Fallback when validation fails
224224
},
225225
onFailure: { target: "error" },
226-
},
226+
}),
227227
},
228228
valid: { /* proceed */ },
229229
invalid: { /* show errors */ },

0 commit comments

Comments
 (0)