abxbus 2.5.3 → 2.5.6
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.
- package/dist/cjs/BaseEvent.d.ts +68 -23
- package/dist/cjs/BaseEvent.js +131 -14
- package/dist/cjs/BaseEvent.js.map +2 -2
- package/dist/cjs/base_event.d.ts +2 -2
- package/dist/cjs/bridge_ipc.d.ts +45 -0
- package/dist/cjs/event_handler.d.ts +1 -0
- package/dist/cjs/events_suck.d.ts +1 -1
- package/dist/cjs/events_suck.js.map +2 -2
- package/dist/cjs/jsonschema.d.ts +6 -0
- package/dist/cjs/jsonschema.js +155 -0
- package/dist/cjs/jsonschema.js.map +7 -0
- package/dist/cjs/middleware_otel_tracing.d.ts +49 -0
- package/dist/cjs/retry.js.map +2 -2
- package/dist/cjs/types.d.ts +3 -4
- package/dist/cjs/types.js +8 -15
- package/dist/cjs/types.js.map +2 -2
- package/dist/esm/BaseEvent.js +131 -14
- package/dist/esm/BaseEvent.js.map +2 -2
- package/dist/esm/events_suck.js.map +2 -2
- package/dist/esm/jsonschema.js +135 -0
- package/dist/esm/jsonschema.js.map +7 -0
- package/dist/esm/retry.js.map +2 -2
- package/dist/esm/types.js +7 -14
- package/dist/esm/types.js.map +2 -2
- package/dist/types/BaseEvent.d.ts +68 -23
- package/dist/types/base_event.d.ts +2 -2
- package/dist/types/bridge_ipc.d.ts +45 -0
- package/dist/types/event_handler.d.ts +1 -0
- package/dist/types/events_suck.d.ts +1 -1
- package/dist/types/jsonschema.d.ts +6 -0
- package/dist/types/middleware_otel_tracing.d.ts +49 -0
- package/dist/types/types.d.ts +3 -4
- package/package.json +1 -1
- package/src/BaseEvent.ts +277 -54
- package/src/events_suck.ts +3 -2
- package/src/jsonschema.ts +146 -0
- package/src/retry.ts +7 -4
- package/src/types.ts +6 -14
- package/dist/cjs/CoreClient.d.ts +0 -167
- package/dist/cjs/CoreEventBus.d.ts +0 -334
- package/dist/types/CoreClient.d.ts +0 -167
- package/dist/types/CoreEventBus.d.ts +0 -334
package/dist/cjs/BaseEvent.d.ts
CHANGED
|
@@ -4,6 +4,7 @@ import { EventResult } from './EventResult.js';
|
|
|
4
4
|
import { EventHandler } from './EventHandler.js';
|
|
5
5
|
import type { EventConcurrencyMode, EventHandlerConcurrencyMode, EventHandlerCompletionMode, Deferred } from './LockManager.js';
|
|
6
6
|
import { AsyncLock } from './LockManager.js';
|
|
7
|
+
import { type JsonSchema } from './jsonschema.js';
|
|
7
8
|
import type { EventHandlerCallable, EventResultType } from './types.js';
|
|
8
9
|
export declare const BaseEventSchema: z.ZodObject<{
|
|
9
10
|
event_id: z.ZodString;
|
|
@@ -42,7 +43,7 @@ export declare const BaseEventSchema: z.ZodObject<{
|
|
|
42
43
|
first: "first";
|
|
43
44
|
}>>>;
|
|
44
45
|
}, z.core.$loose>;
|
|
45
|
-
type AnyEventSchema = z.
|
|
46
|
+
type AnyEventSchema = z.ZodObject<z.ZodRawShape>;
|
|
46
47
|
export type BaseEventData = z.infer<typeof BaseEventSchema>;
|
|
47
48
|
export type BaseEventJSON = BaseEventData & Record<string, unknown>;
|
|
48
49
|
type BaseEventFieldName = 'event_id' | 'event_created_at' | 'event_type' | 'event_version' | 'event_timeout' | 'event_slow_timeout' | 'event_handler_timeout' | 'event_handler_slow_timeout' | 'event_blocks_parent_completion' | 'event_parent_id' | 'event_path' | 'event_result_type' | 'event_emitted_by_handler_id' | 'event_pending_bus_count' | 'event_status' | 'event_started_at' | 'event_completed_at' | 'event_results' | 'event_concurrency' | 'event_handler_concurrency' | 'event_handler_completion';
|
|
@@ -52,20 +53,54 @@ type BaseEventFields = {
|
|
|
52
53
|
export type BaseEventInit<TFields extends Record<string, unknown>> = TFields & Partial<BaseEventFields>;
|
|
53
54
|
type BaseEventSchemaShape = typeof BaseEventSchema.shape;
|
|
54
55
|
export type EventSchema<TShape extends z.ZodRawShape> = z.ZodObject<BaseEventSchemaShape & TShape>;
|
|
55
|
-
type
|
|
56
|
+
type EventPayloadShape<TShape extends z.ZodRawShape> = {
|
|
57
|
+
[K in keyof TShape as K extends BaseEventFieldName ? never : K]: TShape[K];
|
|
58
|
+
};
|
|
59
|
+
type EventPayload<TShape extends z.ZodRawShape> = EventPayloadShape<TShape> extends Record<string, never> ? {} : z.infer<z.ZodObject<EventPayloadShape<TShape>>>;
|
|
60
|
+
type EventFactoryMetadataFieldName = 'class' | 'fromJSON' | 'prototype' | 'event_schema' | 'model_fields' | 'event_type' | 'event_version' | 'event_result_type';
|
|
61
|
+
type StaticDefaultSchema = z.ZodDefault<z.ZodTypeAny> | z.ZodPrefault<z.ZodTypeAny> | z.ZodCatch<z.ZodTypeAny>;
|
|
62
|
+
type EventModelFields<TShape extends z.ZodRawShape> = {
|
|
63
|
+
readonly [K in keyof TShape]: TShape[K];
|
|
64
|
+
};
|
|
65
|
+
type StaticEventDefaultValues<TShape extends z.ZodRawShape> = {
|
|
66
|
+
readonly [K in keyof TShape as K extends EventFactoryMetadataFieldName ? never : TShape[K] extends StaticDefaultSchema ? K : never]: z.output<TShape[K]>;
|
|
67
|
+
};
|
|
68
|
+
type StaticEventDefaultValuesFromSchema<TSchema extends AnyEventSchema> = TSchema extends z.ZodObject<infer TShape> ? StaticEventDefaultValues<TShape> : {};
|
|
69
|
+
type EventModelFieldsFromSchema<TSchema extends AnyEventSchema> = TSchema extends z.ZodObject<infer TShape> ? TSchema['shape'] & EventModelFields<TShape> : {};
|
|
70
|
+
type OptionalFactoryArgs<TData> = {} extends TData ? [data?: TData] : [data: TData];
|
|
56
71
|
type EventInput<TShape extends z.ZodRawShape> = z.input<EventSchema<TShape>>;
|
|
57
72
|
export type EventInit<TShape extends z.ZodRawShape> = Omit<EventInput<TShape>, keyof BaseEventFields> & Partial<BaseEventFields>;
|
|
58
|
-
type EventPayloadFromSchema<TSchema extends AnyEventSchema> = z.output<TSchema> extends Record<string, unknown> ? z.output<TSchema> : {};
|
|
73
|
+
type EventPayloadFromSchema<TSchema extends AnyEventSchema> = z.output<TSchema> extends Record<string, unknown> ? Omit<z.output<TSchema>, keyof BaseEventFields> : {};
|
|
59
74
|
type EventInputFromSchema<TSchema extends AnyEventSchema> = z.input<TSchema> extends Record<string, unknown> ? z.input<TSchema> : never;
|
|
60
75
|
export type EventInitFromSchema<TSchema extends AnyEventSchema> = Omit<EventInputFromSchema<TSchema>, keyof BaseEventFields> & Partial<BaseEventFields>;
|
|
61
76
|
type EventWithResultSchema<TResult> = BaseEvent & {
|
|
62
77
|
__event_result_type__?: TResult;
|
|
63
78
|
};
|
|
64
|
-
type
|
|
79
|
+
type NormalizedEventResultSchema<TInput> = TInput extends z.ZodTypeAny ? TInput : TInput extends z.core.$ZodType ? z.ZodType<z.output<TInput>> : TInput extends StringConstructor ? z.ZodString : TInput extends NumberConstructor ? z.ZodNumber : TInput extends BooleanConstructor ? z.ZodBoolean : TInput extends ArrayConstructor ? z.ZodArray<z.ZodUnknown> : TInput extends ObjectConstructor ? z.ZodRecord<z.ZodString, z.ZodUnknown> : TInput extends JsonSchema ? z.ZodTypeAny : z.ZodTypeAny;
|
|
80
|
+
type ResultTypeSchemaFromShape<TShape> = TShape extends {
|
|
81
|
+
event_result_type: infer S;
|
|
82
|
+
} ? NormalizedEventResultSchema<S> : z.ZodTypeAny | undefined;
|
|
83
|
+
type ResultTypeSchemaFromEventSchema<TSchema> = TSchema extends z.ZodObject<infer TShape> ? ResultTypeSchemaFromShape<TShape> : z.ZodTypeAny | undefined;
|
|
84
|
+
type ResultTypeFromEventResultTypeInput<TInput> = TInput extends z.ZodTypeAny ? z.infer<TInput> : TInput extends z.core.$ZodType ? z.output<TInput> : TInput extends StringConstructor ? string : TInput extends NumberConstructor ? number : TInput extends BooleanConstructor ? boolean : TInput extends ArrayConstructor ? unknown[] : TInput extends ObjectConstructor ? Record<string, unknown> : TInput extends JsonSchema ? unknown : unknown;
|
|
65
85
|
type ResultSchemaFromShape<TShape> = TShape extends {
|
|
66
86
|
event_result_type: infer S;
|
|
67
87
|
} ? ResultTypeFromEventResultTypeInput<S> : unknown;
|
|
68
88
|
type ResultSchemaFromEventSchema<TSchema> = TSchema extends z.ZodObject<infer TShape> ? ResultSchemaFromShape<TShape> : unknown;
|
|
89
|
+
type ZodLiteralValue = string | number | bigint | boolean | null | undefined;
|
|
90
|
+
type ShortcutDefaultModelField<K, TValue> = K extends keyof BaseEventSchemaShape ? z.ZodDefault<BaseEventSchemaShape[K]> : z.ZodDefault<TValue extends ZodLiteralValue ? z.ZodLiteral<TValue> : z.ZodType<TValue>>;
|
|
91
|
+
type ShortcutModelFields<TShape> = {
|
|
92
|
+
[K in keyof TShape as K extends 'event_result_type' ? never : K]: TShape[K] extends z.ZodTypeAny ? TShape[K] : ShortcutDefaultModelField<K, TShape[K]>;
|
|
93
|
+
} & (TShape extends {
|
|
94
|
+
event_result_type: infer TResultType;
|
|
95
|
+
} ? {
|
|
96
|
+
event_result_type: NormalizedEventResultSchema<TResultType>;
|
|
97
|
+
} : {});
|
|
98
|
+
type ShortcutZodModelFields<TShape> = {
|
|
99
|
+
[K in keyof ShortcutModelFields<TShape>]: ShortcutModelFields<TShape>[K] extends z.ZodTypeAny ? ShortcutModelFields<TShape>[K] : never;
|
|
100
|
+
};
|
|
101
|
+
type ShortcutStaticDefaultValues<TShape, TModelFields extends z.ZodRawShape> = StaticEventDefaultValues<TModelFields> & {
|
|
102
|
+
readonly [K in keyof TShape as K extends EventFactoryMetadataFieldName ? never : TShape[K] extends z.ZodTypeAny ? never : K]: TShape[K];
|
|
103
|
+
};
|
|
69
104
|
export type EventResultInclude<TEvent extends BaseEvent> = (result: EventResult<TEvent>['result'], event_result: EventResult<TEvent>) => boolean;
|
|
70
105
|
export type EventResultOptions<TEvent extends BaseEvent> = {
|
|
71
106
|
include?: EventResultInclude<TEvent>;
|
|
@@ -86,28 +121,35 @@ type EventResultUpdateOptions<TEvent extends BaseEvent> = {
|
|
|
86
121
|
result?: EventResultType<TEvent> | BaseEvent | undefined;
|
|
87
122
|
error?: unknown;
|
|
88
123
|
};
|
|
89
|
-
export type
|
|
90
|
-
(data: EventInit<TShape>): EventWithResultSchema<TResult> & EventPayload<TShape>;
|
|
91
|
-
new (data: EventInit<TShape>): EventWithResultSchema<TResult> & EventPayload<TShape>;
|
|
124
|
+
export type EventFactoryClass<TShape extends z.ZodRawShape, TResult = unknown> = (new (...args: OptionalFactoryArgs<EventInit<TShape>>) => EventWithResultSchema<TResult> & EventPayload<TShape>) & StaticEventDefaultValues<TShape> & {
|
|
92
125
|
event_schema: EventSchema<TShape>;
|
|
93
|
-
|
|
94
|
-
event_type?: string;
|
|
95
|
-
event_version?: string;
|
|
96
|
-
event_result_type?: z.ZodTypeAny;
|
|
97
|
-
fromJSON?: (data: unknown) => EventWithResultSchema<TResult> & EventPayload<TShape>;
|
|
126
|
+
model_fields: EventModelFields<TShape>;
|
|
98
127
|
};
|
|
99
|
-
export type
|
|
100
|
-
(
|
|
101
|
-
new (
|
|
128
|
+
export type EventFactory<TShape extends z.ZodRawShape, TResult = unknown, TStaticFields = StaticEventDefaultValues<TShape>> = TStaticFields & {
|
|
129
|
+
(...args: OptionalFactoryArgs<EventInit<TShape>>): EventWithResultSchema<TResult> & EventPayload<TShape>;
|
|
130
|
+
new (...args: OptionalFactoryArgs<EventInit<TShape>>): EventWithResultSchema<TResult> & EventPayload<TShape>;
|
|
131
|
+
event_schema: EventSchema<TShape>;
|
|
132
|
+
model_fields: EventModelFields<TShape>;
|
|
133
|
+
class: EventFactoryClass<TShape, TResult>;
|
|
134
|
+
event_type: string;
|
|
135
|
+
event_version: string;
|
|
136
|
+
event_result_type: ResultTypeSchemaFromShape<TShape>;
|
|
137
|
+
fromJSON: (data: unknown) => EventWithResultSchema<TResult> & EventPayload<TShape>;
|
|
138
|
+
};
|
|
139
|
+
export type SchemaEventFactoryClass<TSchema extends AnyEventSchema, TResult = unknown> = (new (...args: OptionalFactoryArgs<EventInitFromSchema<TSchema>>) => EventWithResultSchema<TResult> & EventPayloadFromSchema<TSchema>) & StaticEventDefaultValuesFromSchema<TSchema> & {
|
|
102
140
|
event_schema: TSchema;
|
|
103
|
-
|
|
104
|
-
event_type?: string;
|
|
105
|
-
event_version?: string;
|
|
106
|
-
event_result_type?: z.ZodTypeAny;
|
|
107
|
-
fromJSON?: (data: unknown) => EventWithResultSchema<TResult> & EventPayloadFromSchema<TSchema>;
|
|
141
|
+
model_fields: EventModelFieldsFromSchema<TSchema>;
|
|
108
142
|
};
|
|
109
|
-
type
|
|
110
|
-
|
|
143
|
+
export type SchemaEventFactory<TSchema extends AnyEventSchema, TResult = unknown> = StaticEventDefaultValuesFromSchema<TSchema> & {
|
|
144
|
+
(...args: OptionalFactoryArgs<EventInitFromSchema<TSchema>>): EventWithResultSchema<TResult> & EventPayloadFromSchema<TSchema>;
|
|
145
|
+
new (...args: OptionalFactoryArgs<EventInitFromSchema<TSchema>>): EventWithResultSchema<TResult> & EventPayloadFromSchema<TSchema>;
|
|
146
|
+
event_schema: TSchema;
|
|
147
|
+
model_fields: EventModelFieldsFromSchema<TSchema>;
|
|
148
|
+
class: SchemaEventFactoryClass<TSchema, TResult>;
|
|
149
|
+
event_type: string;
|
|
150
|
+
event_version: string;
|
|
151
|
+
event_result_type: ResultTypeSchemaFromEventSchema<TSchema>;
|
|
152
|
+
fromJSON: (data: unknown) => EventWithResultSchema<TResult> & EventPayloadFromSchema<TSchema>;
|
|
111
153
|
};
|
|
112
154
|
export declare class BaseEvent {
|
|
113
155
|
event_id: string;
|
|
@@ -132,10 +174,13 @@ export declare class BaseEvent {
|
|
|
132
174
|
event_handler_concurrency?: EventHandlerConcurrencyMode | null;
|
|
133
175
|
event_handler_completion?: EventHandlerCompletionMode | null;
|
|
134
176
|
event_schema?: z.ZodTypeAny;
|
|
177
|
+
_event_parse_schema?: z.ZodTypeAny;
|
|
135
178
|
static event_type?: string;
|
|
136
179
|
static event_version: string;
|
|
137
180
|
static event_result_type?: z.ZodTypeAny;
|
|
138
181
|
static event_schema: AnyEventSchema;
|
|
182
|
+
static model_fields: z.ZodRawShape;
|
|
183
|
+
static _event_parse_schema: AnyEventSchema;
|
|
139
184
|
event_bus?: EventBus;
|
|
140
185
|
_event_original?: BaseEvent;
|
|
141
186
|
_event_dispatch_context?: unknown | null;
|
|
@@ -145,8 +190,8 @@ export declare class BaseEvent {
|
|
|
145
190
|
constructor(data?: BaseEventInit<Record<string, unknown>>);
|
|
146
191
|
toString(): string;
|
|
147
192
|
static extend<TSchema extends z.ZodObject<z.ZodRawShape>>(event_type: string, event_schema: TSchema): SchemaEventFactory<TSchema, ResultSchemaFromEventSchema<TSchema>>;
|
|
193
|
+
static extend<const TShape extends Record<string, unknown>>(event_type: string, shape?: TShape): EventFactory<ShortcutZodModelFields<TShape>, ResultSchemaFromShape<ShortcutZodModelFields<TShape>>, ShortcutStaticDefaultValues<TShape, ShortcutZodModelFields<TShape>>>;
|
|
148
194
|
static extend<TShape extends z.ZodRawShape>(event_type: string, shape?: TShape): EventFactory<TShape, ResultSchemaFromShape<TShape>>;
|
|
149
|
-
static extend<TShape extends Record<string, unknown>>(event_type: string, shape?: TShape): EventFactory<ZodShapeFrom<TShape>, ResultSchemaFromShape<TShape>>;
|
|
150
195
|
static fromJSON<T extends typeof BaseEvent>(this: T, data: unknown): InstanceType<T>;
|
|
151
196
|
static toJSONArray(events: Iterable<BaseEvent>): BaseEventJSON[];
|
|
152
197
|
static fromJSONArray(data: unknown): BaseEvent[];
|
package/dist/cjs/BaseEvent.js
CHANGED
|
@@ -28,6 +28,7 @@ var import_EventResult = require("./EventResult.js");
|
|
|
28
28
|
var import_EventHandler = require("./EventHandler.js");
|
|
29
29
|
var import_LockManager = require("./LockManager.js");
|
|
30
30
|
var import_timing = require("./timing.js");
|
|
31
|
+
var import_jsonschema = require("./jsonschema.js");
|
|
31
32
|
var import_types = require("./types.js");
|
|
32
33
|
var import_helpers = require("./helpers.js");
|
|
33
34
|
const RESERVED_USER_EVENT_FIELDS = /* @__PURE__ */ new Set([
|
|
@@ -101,6 +102,16 @@ const BaseEventSchema = import_zod.z.object({
|
|
|
101
102
|
event_handler_completion: import_zod.z.enum(import_LockManager.EVENT_HANDLER_COMPLETION_MODES).nullable().optional()
|
|
102
103
|
}).loose();
|
|
103
104
|
const KNOWN_BASE_EVENT_FIELDS = new Set(Object.keys(BaseEventSchema.shape));
|
|
105
|
+
const EVENT_FACTORY_METADATA_FIELDS = /* @__PURE__ */ new Set([
|
|
106
|
+
"class",
|
|
107
|
+
"fromJSON",
|
|
108
|
+
"prototype",
|
|
109
|
+
"event_schema",
|
|
110
|
+
"model_fields",
|
|
111
|
+
"event_type",
|
|
112
|
+
"event_version",
|
|
113
|
+
"event_result_type"
|
|
114
|
+
]);
|
|
104
115
|
const ROOT_EVENTBUS_ID = "00000000-0000-0000-0000-000000000000";
|
|
105
116
|
function baseEventDefaultShape(event_type) {
|
|
106
117
|
return {
|
|
@@ -130,11 +141,67 @@ function baseEventDefaultShape(event_type) {
|
|
|
130
141
|
function missingBaseFields(event_type, user_shape) {
|
|
131
142
|
return Object.fromEntries(Object.entries(baseEventDefaultShape(event_type)).filter(([key]) => !(key in user_shape)));
|
|
132
143
|
}
|
|
144
|
+
function isZodLiteralValue(value) {
|
|
145
|
+
return value === null || value === void 0 || ["string", "number", "bigint", "boolean"].includes(typeof value);
|
|
146
|
+
}
|
|
147
|
+
function isPlainShortcutLiteralObject(value) {
|
|
148
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
149
|
+
return false;
|
|
150
|
+
}
|
|
151
|
+
const prototype = Object.getPrototypeOf(value);
|
|
152
|
+
return prototype === Object.prototype || prototype === null;
|
|
153
|
+
}
|
|
154
|
+
function alreadyComparedShortcutLiteralPair(left, right, seen) {
|
|
155
|
+
let right_values = seen.get(left);
|
|
156
|
+
if (right_values?.has(right)) {
|
|
157
|
+
return true;
|
|
158
|
+
}
|
|
159
|
+
if (!right_values) {
|
|
160
|
+
right_values = /* @__PURE__ */ new WeakSet();
|
|
161
|
+
seen.set(left, right_values);
|
|
162
|
+
}
|
|
163
|
+
right_values.add(right);
|
|
164
|
+
return false;
|
|
165
|
+
}
|
|
166
|
+
function shortcutLiteralValuesEqual(left, right, seen = /* @__PURE__ */ new WeakMap()) {
|
|
167
|
+
if (Object.is(left, right)) {
|
|
168
|
+
return true;
|
|
169
|
+
}
|
|
170
|
+
if (typeof left !== "object" || left === null || typeof right !== "object" || right === null) {
|
|
171
|
+
return false;
|
|
172
|
+
}
|
|
173
|
+
if (alreadyComparedShortcutLiteralPair(left, right, seen)) {
|
|
174
|
+
return true;
|
|
175
|
+
}
|
|
176
|
+
if (Array.isArray(left) || Array.isArray(right)) {
|
|
177
|
+
if (!Array.isArray(left) || !Array.isArray(right) || left.length !== right.length) {
|
|
178
|
+
return false;
|
|
179
|
+
}
|
|
180
|
+
return left.every((item, index) => shortcutLiteralValuesEqual(item, right[index], seen));
|
|
181
|
+
}
|
|
182
|
+
if (!isPlainShortcutLiteralObject(left) || !isPlainShortcutLiteralObject(right)) {
|
|
183
|
+
return false;
|
|
184
|
+
}
|
|
185
|
+
const left_keys = Object.keys(left);
|
|
186
|
+
const right_keys = Object.keys(right);
|
|
187
|
+
if (left_keys.length !== right_keys.length) {
|
|
188
|
+
return false;
|
|
189
|
+
}
|
|
190
|
+
return left_keys.every(
|
|
191
|
+
(key) => Object.prototype.hasOwnProperty.call(right, key) ? shortcutLiteralValuesEqual(left[key], right[key], seen) : false
|
|
192
|
+
);
|
|
193
|
+
}
|
|
194
|
+
function shortcutLiteralSchema(value) {
|
|
195
|
+
if (isZodLiteralValue(value)) {
|
|
196
|
+
return import_zod.z.literal(value);
|
|
197
|
+
}
|
|
198
|
+
return import_zod.z.custom((candidate) => shortcutLiteralValuesEqual(candidate, value), "Invalid literal value");
|
|
199
|
+
}
|
|
133
200
|
function shortcutDefaultSchema(base_field_schema, value) {
|
|
134
201
|
if (!base_field_schema) {
|
|
135
|
-
return
|
|
202
|
+
return shortcutLiteralSchema(value).default(value);
|
|
136
203
|
}
|
|
137
|
-
return base_field_schema.
|
|
204
|
+
return base_field_schema.default(base_field_schema.parse(value));
|
|
138
205
|
}
|
|
139
206
|
function schemaDefaultsForShortcut(event_type, raw_shape) {
|
|
140
207
|
const defaults = {};
|
|
@@ -157,10 +224,42 @@ function zodFieldsForShortcut(raw_shape) {
|
|
|
157
224
|
}
|
|
158
225
|
return fields;
|
|
159
226
|
}
|
|
227
|
+
function modelFieldsForShortcut(raw_shape, shortcut_shape) {
|
|
228
|
+
const event_result_type = (0, import_types.normalizeEventResultType)(raw_shape.event_result_type);
|
|
229
|
+
return event_result_type ? { ...shortcut_shape, event_result_type } : shortcut_shape;
|
|
230
|
+
}
|
|
231
|
+
function staticEventDefaultsFromModelFields(model_fields) {
|
|
232
|
+
const fields = {};
|
|
233
|
+
for (const [key, value] of Object.entries(model_fields)) {
|
|
234
|
+
if (EVENT_FACTORY_METADATA_FIELDS.has(key)) {
|
|
235
|
+
continue;
|
|
236
|
+
}
|
|
237
|
+
const parsed = value.safeParse(void 0);
|
|
238
|
+
if (parsed.success && parsed.data !== void 0) {
|
|
239
|
+
fields[key] = parsed.data;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
return fields;
|
|
243
|
+
}
|
|
244
|
+
function defineStaticEventFields(target, fields) {
|
|
245
|
+
for (const [key, value] of Object.entries(fields)) {
|
|
246
|
+
Object.defineProperty(target, key, {
|
|
247
|
+
value,
|
|
248
|
+
writable: false,
|
|
249
|
+
enumerable: true,
|
|
250
|
+
configurable: true
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
}
|
|
160
254
|
function eventResultTypeFromObjectSchema(schema) {
|
|
161
255
|
const raw_event_result_type = schema.shape.event_result_type;
|
|
162
256
|
return raw_event_result_type === void 0 ? void 0 : (0, import_types.normalizeEventResultType)(raw_event_result_type);
|
|
163
257
|
}
|
|
258
|
+
function eventParseSchemaFromEventSchema(schema) {
|
|
259
|
+
return schema.safeExtend({
|
|
260
|
+
event_result_type: import_zod.z.unknown().optional()
|
|
261
|
+
});
|
|
262
|
+
}
|
|
164
263
|
function buildFullEventSchema(event_type, spec) {
|
|
165
264
|
if (isZodObjectSchema(spec)) {
|
|
166
265
|
const user_shape = spec.shape;
|
|
@@ -168,11 +267,12 @@ function buildFullEventSchema(event_type, spec) {
|
|
|
168
267
|
assertNoUnknownEventPrefixedFields(user_shape, `BaseEvent.extend(${event_type})`);
|
|
169
268
|
assertNoModelPrefixedFields(user_shape, `BaseEvent.extend(${event_type})`);
|
|
170
269
|
const full_schema2 = spec.safeExtend({
|
|
171
|
-
event_result_type: import_zod.z.unknown().optional(),
|
|
172
270
|
...missingBaseFields(event_type, user_shape)
|
|
173
271
|
});
|
|
174
272
|
return {
|
|
175
273
|
event_schema: full_schema2,
|
|
274
|
+
event_parse_schema: eventParseSchemaFromEventSchema(full_schema2),
|
|
275
|
+
static_field_defaults: staticEventDefaultsFromModelFields(full_schema2.shape),
|
|
176
276
|
event_result_type: eventResultTypeFromObjectSchema(spec)
|
|
177
277
|
};
|
|
178
278
|
}
|
|
@@ -184,9 +284,12 @@ function buildFullEventSchema(event_type, spec) {
|
|
|
184
284
|
...schemaDefaultsForShortcut(event_type, raw_shape),
|
|
185
285
|
...zodFieldsForShortcut(raw_shape)
|
|
186
286
|
};
|
|
187
|
-
const
|
|
287
|
+
const model_fields = modelFieldsForShortcut(raw_shape, shortcut_shape);
|
|
288
|
+
const full_schema = import_zod.z.object(model_fields).safeExtend(missingBaseFields(event_type, model_fields)).loose();
|
|
188
289
|
return {
|
|
189
290
|
event_schema: full_schema,
|
|
291
|
+
event_parse_schema: eventParseSchemaFromEventSchema(full_schema),
|
|
292
|
+
static_field_defaults: staticEventDefaultsFromModelFields(full_schema.shape),
|
|
190
293
|
event_result_type: (0, import_types.normalizeEventResultType)(raw_shape.event_result_type),
|
|
191
294
|
event_version: typeof raw_shape.event_version === "string" ? raw_shape.event_version : void 0
|
|
192
295
|
};
|
|
@@ -247,12 +350,15 @@ class BaseEvent {
|
|
|
247
350
|
event_handler_completion;
|
|
248
351
|
// completion strategy: 'all' (default) waits for every handler, 'first' returns earliest non-undefined result and cancels the rest
|
|
249
352
|
event_schema;
|
|
353
|
+
_event_parse_schema;
|
|
250
354
|
static event_type;
|
|
251
355
|
// class name of the event, e.g. BaseEvent.extend("MyEvent").event_type === "MyEvent"
|
|
252
356
|
static event_version = "0.0.1";
|
|
253
357
|
static event_result_type;
|
|
254
358
|
static event_schema = BaseEventSchema;
|
|
255
359
|
// generated Zod schema for local TS event data validation; never sent over the wire
|
|
360
|
+
static model_fields = BaseEventSchema.shape;
|
|
361
|
+
static _event_parse_schema = BaseEventSchema;
|
|
256
362
|
// internal runtime state
|
|
257
363
|
event_bus;
|
|
258
364
|
// bus that dispatched this event, also used by event.emit(child)
|
|
@@ -275,6 +381,7 @@ class BaseEvent {
|
|
|
275
381
|
const raw_event_result_type = merged_data.event_result_type ?? ctor.event_result_type;
|
|
276
382
|
const event_result_type = (0, import_types.normalizeEventResultType)(raw_event_result_type);
|
|
277
383
|
const event_schema = ctor.event_schema ?? BaseEventSchema;
|
|
384
|
+
const event_parse_schema = ctor._event_parse_schema ?? event_schema;
|
|
278
385
|
const base_data = {
|
|
279
386
|
...merged_data,
|
|
280
387
|
event_id: merged_data.event_id ?? (0, import_uuid.v7)(),
|
|
@@ -283,11 +390,11 @@ class BaseEvent {
|
|
|
283
390
|
event_version,
|
|
284
391
|
event_result_type
|
|
285
392
|
};
|
|
286
|
-
if (
|
|
393
|
+
if (event_parse_schema === BaseEventSchema) {
|
|
287
394
|
base_data.event_timeout ??= null;
|
|
288
395
|
base_data.event_blocks_parent_completion ??= false;
|
|
289
396
|
}
|
|
290
|
-
const parsed = decodeEventSchema(
|
|
397
|
+
const parsed = decodeEventSchema(event_parse_schema, base_data);
|
|
291
398
|
Object.assign(this, parsed);
|
|
292
399
|
Object.defineProperty(this, "event_schema", {
|
|
293
400
|
value: event_schema,
|
|
@@ -295,6 +402,12 @@ class BaseEvent {
|
|
|
295
402
|
enumerable: false,
|
|
296
403
|
configurable: true
|
|
297
404
|
});
|
|
405
|
+
Object.defineProperty(this, "_event_parse_schema", {
|
|
406
|
+
value: event_parse_schema,
|
|
407
|
+
writable: true,
|
|
408
|
+
enumerable: false,
|
|
409
|
+
configurable: true
|
|
410
|
+
});
|
|
298
411
|
Object.defineProperty(this, "_event_fields_set", {
|
|
299
412
|
value: explicit_event_fields,
|
|
300
413
|
writable: true,
|
|
@@ -324,10 +437,14 @@ class BaseEvent {
|
|
|
324
437
|
static extend(event_type, shape) {
|
|
325
438
|
const built = buildFullEventSchema(event_type, shape ?? {});
|
|
326
439
|
const full_schema = built.event_schema;
|
|
440
|
+
const event_parse_schema = built.event_parse_schema;
|
|
441
|
+
const static_field_defaults = built.static_field_defaults;
|
|
327
442
|
const event_result_type = built.event_result_type;
|
|
328
443
|
const event_version = built.event_version;
|
|
329
444
|
class ExtendedEvent extends BaseEvent {
|
|
330
445
|
static event_schema = full_schema;
|
|
446
|
+
static model_fields = full_schema.shape;
|
|
447
|
+
static _event_parse_schema = event_parse_schema;
|
|
331
448
|
static event_type = event_type;
|
|
332
449
|
static event_version = event_version ?? BaseEvent.event_version;
|
|
333
450
|
static event_result_type = event_result_type;
|
|
@@ -339,19 +456,22 @@ class BaseEvent {
|
|
|
339
456
|
return new ExtendedEvent(data);
|
|
340
457
|
}
|
|
341
458
|
EventFactory.event_schema = full_schema;
|
|
459
|
+
EventFactory.model_fields = EventFactory.event_schema.shape;
|
|
342
460
|
EventFactory.event_type = event_type;
|
|
343
461
|
EventFactory.event_version = event_version ?? BaseEvent.event_version;
|
|
344
462
|
EventFactory.event_result_type = event_result_type;
|
|
345
463
|
EventFactory.class = ExtendedEvent;
|
|
346
464
|
EventFactory.fromJSON = (data) => ExtendedEvent.fromJSON(data);
|
|
347
465
|
EventFactory.prototype = ExtendedEvent.prototype;
|
|
466
|
+
defineStaticEventFields(ExtendedEvent, static_field_defaults);
|
|
467
|
+
defineStaticEventFields(EventFactory, static_field_defaults);
|
|
348
468
|
EVENT_TYPE_REGISTRY.set(event_type, ExtendedEvent);
|
|
349
469
|
return EventFactory;
|
|
350
470
|
}
|
|
351
471
|
static fromJSON(data) {
|
|
352
472
|
if (!data || typeof data !== "object") {
|
|
353
|
-
const
|
|
354
|
-
const parsed = decodeEventSchema(
|
|
473
|
+
const event_parse_schema = this._event_parse_schema ?? this.event_schema ?? BaseEventSchema;
|
|
474
|
+
const parsed = decodeEventSchema(event_parse_schema, data);
|
|
355
475
|
return new this(parsed);
|
|
356
476
|
}
|
|
357
477
|
const record = { ...data };
|
|
@@ -368,9 +488,6 @@ class BaseEvent {
|
|
|
368
488
|
if (this !== BaseEvent && ctor.event_result_type && record.event_result_type !== void 0) {
|
|
369
489
|
delete record.event_result_type;
|
|
370
490
|
}
|
|
371
|
-
if (record.event_result_type !== void 0 && record.event_result_type !== null) {
|
|
372
|
-
record.event_result_type = (0, import_types.normalizeEventResultType)(record.event_result_type);
|
|
373
|
-
}
|
|
374
491
|
return new this(record);
|
|
375
492
|
}
|
|
376
493
|
static toJSONArray(events) {
|
|
@@ -395,8 +512,8 @@ class BaseEvent {
|
|
|
395
512
|
const event_results = Object.fromEntries(
|
|
396
513
|
Array.from(this.event_results.entries()).map(([handler_id, result]) => [handler_id, result.toJSON()])
|
|
397
514
|
);
|
|
398
|
-
const
|
|
399
|
-
const encoded = encodeEventSchema(
|
|
515
|
+
const event_parse_schema = this.constructor._event_parse_schema ?? this._event_parse_schema ?? this.constructor.event_schema ?? this.event_schema ?? BaseEventSchema;
|
|
516
|
+
const encoded = encodeEventSchema(event_parse_schema, {
|
|
400
517
|
...record,
|
|
401
518
|
event_id: this.event_id,
|
|
402
519
|
event_type: this.event_type,
|
|
@@ -429,7 +546,7 @@ class BaseEvent {
|
|
|
429
546
|
event_id: this.event_id,
|
|
430
547
|
event_type: this.event_type,
|
|
431
548
|
event_version: this.event_version,
|
|
432
|
-
event_result_type: this.event_result_type ? (0,
|
|
549
|
+
event_result_type: this.event_result_type ? (0, import_jsonschema.toJsonSchema)(this.event_result_type) : this.event_result_type,
|
|
433
550
|
// static configuration options
|
|
434
551
|
event_timeout: this.event_timeout,
|
|
435
552
|
event_slow_timeout: this.event_slow_timeout,
|