effect-machine 0.2.3 → 0.2.4

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/inspection.ts +24 -15
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "effect-machine",
3
- "version": "0.2.3",
3
+ "version": "0.2.4",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/cevr/effect-machine.git"
package/src/inspection.ts CHANGED
@@ -1,4 +1,13 @@
1
- import { Context } from "effect";
1
+ import { Context, type Schema } from "effect";
2
+
3
+ // ============================================================================
4
+ // Type-level helpers
5
+ // ============================================================================
6
+
7
+ /**
8
+ * Resolve a type param: if it's a Schema, extract `.Type`; otherwise use as-is.
9
+ */
10
+ type ResolveType<T> = T extends Schema.Schema<infer A, infer _I, infer _R> ? A : T;
2
11
 
3
12
  // ============================================================================
4
13
  // Inspection Events
@@ -113,15 +122,15 @@ export const Inspector = Context.GenericTag<Inspector<any, any>>("@effect/machin
113
122
 
114
123
  /**
115
124
  * Create an inspector from a callback function.
116
- * Defaults to `AnyInspectionEvent` when type params are not provided,
117
- * giving access to `_tag` on state/event fields without casts.
125
+ *
126
+ * Type params accept either raw tagged types or Schema constructors:
127
+ * - `makeInspector(cb)` — defaults to `AnyInspectionEvent`
128
+ * - `makeInspector<MyState, MyEvent>(cb)` — explicit tagged types
129
+ * - `makeInspector<typeof MyState, typeof MyEvent>(cb)` — schema constructors (auto-extracts `.Type`)
118
130
  */
119
- export const makeInspector = <
120
- S extends { readonly _tag: string } = { readonly _tag: string },
121
- E extends { readonly _tag: string } = { readonly _tag: string },
122
- >(
123
- onInspect: (event: InspectionEvent<S, E>) => void,
124
- ): Inspector<S, E> => ({ onInspect });
131
+ export const makeInspector = <S = { readonly _tag: string }, E = { readonly _tag: string }>(
132
+ onInspect: (event: InspectionEvent<ResolveType<S>, ResolveType<E>>) => void,
133
+ ): Inspector<ResolveType<S>, ResolveType<E>> => ({ onInspect });
125
134
 
126
135
  // ============================================================================
127
136
  // Built-in Inspectors
@@ -130,11 +139,11 @@ export const makeInspector = <
130
139
  /**
131
140
  * Console inspector that logs events in a readable format
132
141
  */
133
- export const consoleInspector = <
134
- S extends { readonly _tag: string } = { readonly _tag: string },
135
- E extends { readonly _tag: string } = { readonly _tag: string },
136
- >(): Inspector<S, E> =>
137
- makeInspector<S, E>((event) => {
142
+ export const consoleInspector = (): Inspector<
143
+ { readonly _tag: string },
144
+ { readonly _tag: string }
145
+ > =>
146
+ makeInspector((event) => {
138
147
  const prefix = `[${event.actorId}]`;
139
148
  switch (event.type) {
140
149
  case "@machine.spawn":
@@ -166,4 +175,4 @@ export const collectingInspector = <
166
175
  E extends { readonly _tag: string },
167
176
  >(
168
177
  events: InspectionEvent<S, E>[],
169
- ): Inspector<S, E> => makeInspector<S, E>((event) => events.push(event));
178
+ ): Inspector<S, E> => ({ onInspect: (event) => events.push(event) });