Skip to content

Commit

Permalink
review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
rkuhn committed Jan 25, 2024
1 parent 75f783b commit 914ef21
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 35 deletions.
57 changes: 26 additions & 31 deletions machine-runner/src/design/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,45 +53,40 @@ export namespace MachineEvent {
key: Key,
zodDefinition?: z.ZodType<Payload>,
) => {
const [zod, fromZodError] = (() => {
if (zodDefinition) {
const { z, fromZodError } = getZod()
const zod = z.intersection(zodDefinition, z.object({ type: z.string() }))
return [zod, fromZodError] as const
} else {
return [undefined, undefined] as const
}
})()
return (event: MachineEvent<Key, Payload>): ParseResult<Payload> => {
const defaultParser: (event: MachineEvent<Key, Payload>) => ParseResult<Payload> = (event) => {
if (typeof event !== 'object' || event === null) {
return {
success: false,
error: `Event ${event} is not an object`,
}
return { success: false, error: `Event ${event} is not an object` }
}

if (event.type !== key) {
return {
success: false,
error: `Event type ${event.type} does not match expected type ${key}`,
}
}
if (!zod) {
return {
success: true,
payload: event,
}
}
const result = zod.safeParse(event)
if (result.success) {
return {
success: true,
payload: result.data,
}
} else {
return {
success: false,
error: fromZodError(result.error).toString(),

return { success: true, event }
}

if (!zodDefinition) {
return defaultParser
} else {
const [zod, fromZodError] = (() => {
const { z, fromZodError } = getZod()
const zod = z.intersection(zodDefinition, z.object({ type: z.string() }))
return [zod, fromZodError] as const
})()

return (event: MachineEvent<Key, Payload>): ParseResult<Payload> => {
const defaultParserResult = defaultParser(event)
if (!defaultParserResult.success) return defaultParserResult

const result = zod.safeParse(event)
if (!result.success) {
return { success: false, error: fromZodError(result.error).toString() }
}

return { success: true, event: result.data }
}
}
}
Expand Down Expand Up @@ -192,7 +187,7 @@ export namespace MachineEvent {
export type ParseResult<Payload> =
| {
success: true
payload: Payload
event: Payload
}
| {
success: false
Expand Down
8 changes: 4 additions & 4 deletions machine-runner/tests/esm/design.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('MachineEvent', () => {
})
expect(event.parse({ type: 'a' })).toEqual({
success: true,
payload: { type: 'a' },
event: { type: 'a' },
})
})

Expand All @@ -40,11 +40,11 @@ describe('MachineEvent', () => {
})
expect(event.parse({ type: 'a' } as any)).toEqual({
success: true,
payload: { type: 'a' },
event: { type: 'a' },
})
expect(event.parse({ type: 'a', a: 42 })).toEqual({
success: true,
payload: { type: 'a', a: 42 },
event: { type: 'a', a: 42 },
})
})

Expand Down Expand Up @@ -72,7 +72,7 @@ describe('MachineEvent', () => {
})
expect(event.parse({ type: 'a', a: 42 })).toEqual({
success: true,
payload: { type: 'a', a: 42 },
event: { type: 'a', a: 42 },
})
})
})

0 comments on commit 914ef21

Please sign in to comment.