refactor: unwrap BusEvent namespace + self-reexport (#22930)
This commit is contained in:
parent
3f3989e694
commit
0abc0d541a
1 changed files with 25 additions and 25 deletions
|
|
@ -1,33 +1,33 @@
|
||||||
import z from "zod"
|
import z from "zod"
|
||||||
import type { ZodType } from "zod"
|
import type { ZodType } from "zod"
|
||||||
|
|
||||||
export namespace BusEvent {
|
export type Definition = ReturnType<typeof define>
|
||||||
export type Definition = ReturnType<typeof define>
|
|
||||||
|
|
||||||
const registry = new Map<string, Definition>()
|
const registry = new Map<string, Definition>()
|
||||||
|
|
||||||
export function define<Type extends string, Properties extends ZodType>(type: Type, properties: Properties) {
|
export function define<Type extends string, Properties extends ZodType>(type: Type, properties: Properties) {
|
||||||
const result = {
|
const result = {
|
||||||
type,
|
type,
|
||||||
properties,
|
properties,
|
||||||
}
|
|
||||||
registry.set(type, result)
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
export function payloads() {
|
|
||||||
return registry
|
|
||||||
.entries()
|
|
||||||
.map(([type, def]) => {
|
|
||||||
return z
|
|
||||||
.object({
|
|
||||||
type: z.literal(type),
|
|
||||||
properties: def.properties,
|
|
||||||
})
|
|
||||||
.meta({
|
|
||||||
ref: `Event.${def.type}`,
|
|
||||||
})
|
|
||||||
})
|
|
||||||
.toArray()
|
|
||||||
}
|
}
|
||||||
|
registry.set(type, result)
|
||||||
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function payloads() {
|
||||||
|
return registry
|
||||||
|
.entries()
|
||||||
|
.map(([type, def]) => {
|
||||||
|
return z
|
||||||
|
.object({
|
||||||
|
type: z.literal(type),
|
||||||
|
properties: def.properties,
|
||||||
|
})
|
||||||
|
.meta({
|
||||||
|
ref: `Event.${def.type}`,
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.toArray()
|
||||||
|
}
|
||||||
|
|
||||||
|
export * as BusEvent from "./bus-event"
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue