Skip to content

Commit 06edeca

Browse files
cevrclaude
andcommitted
refactor: consolidate internal utilities, flatten test structure
- Delete dead code: fiber-storage.ts (never imported) - Merge is-effect, constants, get-tag, types into utils.ts - Merge namespace.ts into machine.ts - Rename machine-schema.ts → schema.ts - Flatten test/features/ to test/ - Move transition-index.test.ts to test/internal/ - Update CODEMAP.md to reflect new structure Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 94e0f73 commit 06edeca

24 files changed

Lines changed: 169 additions & 207 deletions

‎AGENTS.md‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ const machine = Machine.make({ state, event, initial })
4545
- Actor testing needs `Effect.yieldNow()` after `send()` to let effects run
4646
- Same-state transitions skip spawn/finalizers by default
4747
- `.reenter()` runs exit/enter even on same state tag - use to restart timers/spawn
48-
- `namespace.ts` exports Machine namespace (not `Machine.ts` - macOS case-insensitivity)
4948
- Branded types: `State<T>` / `Event<T>` prevent accidental swap at compile time
5049
- Brand is phantom (type-level only) - runtime values identical to `Data.TaggedEnum`
5150
- Schemas attached to machine: `persist` and `toEntity` infer schemas automatically

‎CODEMAP.md‎

Lines changed: 17 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55
```
66
src/
77
├── index.ts # Public exports
8-
├── namespace.ts # Machine namespace (Effect-style API)
9-
├── machine-schema.ts # Schema-first State/Event (MachineStateSchema, MachineEventSchema)
10-
├── machine.ts # Machine class with fluent builder API
8+
├── machine.ts # Machine class + namespace (fluent builder API)
9+
├── schema.ts # Schema-first State/Event (MachineStateSchema, MachineEventSchema)
1110
├── slot.ts # Slot.Guards/Slot.Effects factories for parameterized slots
1211
├── actor-ref.ts # Actor reference interface
1312
├── actor-system.ts # Actor system service + layer
@@ -26,30 +25,27 @@ src/
2625
├── loop.ts # Event loop, transition resolver, lifecycle effects
2726
├── execute-transition.ts # Shared transition execution (loop, simulate, harness)
2827
├── transition-index.ts # O(1) lookup for transitions
29-
├── fiber-storage.ts # Per-actor WeakMap fiber storage utility
3028
├── brands.ts # StateBrand/EventBrand + BrandedState/BrandedEvent
31-
├── types.ts # Internal utility types
32-
├── is-effect.ts # Shared isEffect type guard
33-
└── get-tag.ts # Tag extraction from constructors
29+
└── utils.ts # Type helpers, constants, isEffect, getTag
3430
3531
test/
3632
├── machine.test.ts # Core machine tests
3733
├── actor-system.test.ts # Actor spawning/lifecycle
3834
├── actor-ref.test.ts # ActorRef ergonomics
3935
├── persistence.test.ts # Persistence tests
4036
├── testing.test.ts # Test utilities
41-
├── machine-schema.test.ts # Schema-first State/Event tests
42-
├── transition-index.test.ts # O(1) transition lookup tests
37+
├── schema.test.ts # Schema-first State/Event tests
4338
├── inspection.test.ts # Inspector tests
44-
├── features/ # Feature-specific tests
45-
│ ├── any.test.ts
46-
│ ├── delay.test.ts # Timeout via spawn patterns
47-
│ ├── dynamic-delay.test.ts # Dynamic timeout via spawn
48-
│ ├── effects.test.ts
49-
│ ├── force.test.ts # reenter transition tests
50-
│ ├── from.test.ts
51-
│ ├── guards.test.ts
52-
│ └── same-state.test.ts
39+
├── guards.test.ts # Parameterized guard tests
40+
├── choose.test.ts # Conditional transition tests
41+
├── delay.test.ts # Timeout via spawn patterns
42+
├── dynamic-delay.test.ts # Dynamic timeout via spawn
43+
├── same-state.test.ts # Same-state transition tests
44+
├── force.test.ts # reenter transition tests
45+
├── utils/
46+
│ └── effect-test.ts # Test helpers (yieldFibers, etc.)
47+
├── internal/
48+
│ └── transition-index.test.ts # O(1) transition lookup tests
5349
├── patterns/ # Real-world pattern tests
5450
│ ├── payment-flow.test.ts
5551
│ ├── session-lifecycle.test.ts
@@ -63,14 +59,13 @@ test/
6359

6460
| File | Purpose |
6561
| ----------------------------------- | ----------------------------------------------------------- |
66-
| `machine.ts` | Machine class - fluent builder, all combinators as methods |
67-
| `machine-schema.ts` | Schema-first `State`/`Event` - single source of truth |
62+
| `machine.ts` | Machine class + namespace - fluent builder, all combinators |
63+
| `schema.ts` | Schema-first `State`/`Event` - single source of truth |
6864
| `slot.ts` | `Slot.Guards`/`Slot.Effects` - parameterized slot factories |
69-
| `namespace.ts` | Machine namespace export (named for macOS compat) |
7065
| `internal/loop.ts` | Event processing, `resolveTransition`, spawn effect forking |
7166
| `internal/transition-index.ts` | O(1) lookup for transitions |
7267
| `internal/brands.ts` | Branded types: `StateBrand`, `BrandedState`, etc. |
73-
| `internal/fiber-storage.ts` | `createFiberStorage()` - per-actor WeakMap utility |
68+
| `internal/utils.ts` | Shared utilities: types, constants, `isEffect`, `getTag` |
7469
| `persistence/persistent-machine.ts` | `Machine.persist` - schemas from machine |
7570
| `cluster/entity-machine.ts` | `toEntity` - schemas from machine |
7671

@@ -172,18 +167,6 @@ const MyEffects = Slot.Effects({
172167
- Context (`ctx`) has `{ state, event, self }`
173168
- `provide()` creates new machine - original reusable with different handlers
174169

175-
## Fiber Storage Pattern
176-
177-
`spawn` uses shared utility from `internal/fiber-storage.ts`:
178-
179-
```ts
180-
import { createFiberStorage } from "../internal/fiber-storage.js";
181-
182-
const getFiberMap = createFiberStorage(); // WeakMap-backed, per-actor
183-
const instanceKey = Symbol("spawn"); // unique per combinator instance
184-
getFiberMap(self).set(instanceKey, fiber);
185-
```
186-
187170
## Transition Index
188171

189172
`internal/transition-index.ts` provides O(1) lookup via lazy-built WeakMap cache:

‎src/cluster/entity-machine.ts‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ import { Effect, Exit, type Layer, Queue, Ref, Scope } from "effect";
1010
import type { Machine, MachineRef, HandlerContext } from "../machine.js";
1111
import { resolveTransition, runSpawnEffects } from "../internal/loop.js";
1212
import type { GuardsDef, EffectsDef, MachineContext } from "../slot.js";
13-
import { isEffect } from "../internal/is-effect.js";
14-
import { INTERNAL_ENTER_EVENT } from "../internal/constants.js";
13+
import { isEffect, INTERNAL_ENTER_EVENT } from "../internal/utils.js";
1514

1615
/**
1716
* Options for EntityMachine.layer

‎src/index.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Machine namespace (Effect-style)
2-
export * as Machine from "./namespace.js";
2+
export * as Machine from "./machine.js";
33

44
// Slot module
55
export { Slot } from "./slot.js";
@@ -30,8 +30,8 @@ export {
3030
} from "./errors.js";
3131

3232
// Schema-first State/Event definitions
33-
export { State, Event } from "./machine-schema.js";
34-
export type { MachineStateSchema, MachineEventSchema } from "./machine-schema.js";
33+
export { State, Event } from "./schema.js";
34+
export type { MachineStateSchema, MachineEventSchema } from "./schema.js";
3535

3636
// Core machine types (for advanced use)
3737
export type {

‎src/internal/constants.ts‎

Lines changed: 0 additions & 8 deletions
This file was deleted.

‎src/internal/execute-transition.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { Effect } from "effect";
88
import type { Machine, MachineRef, HandlerContext } from "../machine.js";
99
import type { GuardsDef, EffectsDef, MachineContext } from "../slot.js";
1010
import { resolveTransition } from "./loop.js";
11-
import { isEffect } from "./is-effect.js";
11+
import { isEffect } from "./utils.js";
1212

1313
/**
1414
* Result of executing a transition.

‎src/internal/fiber-storage.ts‎

Lines changed: 0 additions & 22 deletions
This file was deleted.

‎src/internal/get-tag.ts‎

Lines changed: 0 additions & 23 deletions
This file was deleted.

‎src/internal/is-effect.ts‎

Lines changed: 0 additions & 5 deletions
This file was deleted.

‎src/internal/loop.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { Inspector as InspectorTag } from "../inspection.js";
77
import { findTransitions, findSpawnEffects } from "./transition-index.js";
88
import type { GuardsDef, EffectsDef, MachineContext } from "../slot.js";
99
import { executeTransition } from "./execute-transition.js";
10-
import { INTERNAL_INIT_EVENT, INTERNAL_ENTER_EVENT } from "./constants.js";
10+
import { INTERNAL_INIT_EVENT, INTERNAL_ENTER_EVENT } from "./utils.js";
1111

1212
/** Listener set for sync subscriptions */
1313
type Listeners<S> = Set<(state: S) => void>;

0 commit comments

Comments
 (0)