abxbus 2.5.4 → 2.5.9
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 +53 -28
- package/dist/cjs/BaseEvent.js +148 -28
- package/dist/cjs/BaseEvent.js.map +2 -2
- package/dist/cjs/LockManager.js +1 -1
- package/dist/cjs/LockManager.js.map +2 -2
- package/dist/cjs/events_suck.d.ts +8 -15
- package/dist/cjs/events_suck.js +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/retry.d.ts +2 -0
- package/dist/cjs/retry.js +110 -35
- package/dist/cjs/retry.js.map +3 -3
- package/dist/cjs/types.d.ts +6 -10
- package/dist/cjs/types.js +9 -16
- package/dist/cjs/types.js.map +2 -2
- package/dist/esm/BaseEvent.js +148 -28
- package/dist/esm/BaseEvent.js.map +2 -2
- package/dist/esm/LockManager.js +1 -1
- package/dist/esm/LockManager.js.map +2 -2
- package/dist/esm/events_suck.js +1 -1
- 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 +110 -35
- package/dist/esm/retry.js.map +3 -3
- package/dist/esm/types.js +8 -15
- package/dist/esm/types.js.map +2 -2
- package/dist/types/BaseEvent.d.ts +53 -28
- package/dist/types/events_suck.d.ts +8 -15
- package/dist/types/jsonschema.d.ts +6 -0
- package/dist/types/retry.d.ts +2 -0
- package/dist/types/types.d.ts +6 -10
- package/package.json +1 -1
- package/src/BaseEvent.ts +321 -80
- package/src/LockManager.ts +1 -1
- package/src/events_suck.ts +20 -22
- package/src/jsonschema.ts +146 -0
- package/src/retry.ts +132 -38
- package/src/types.ts +10 -19
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 EventClassMetadataFieldName = '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 EventClassMetadataFieldName ? 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 EventClassMetadataFieldName ? 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,15 @@ type EventResultUpdateOptions<TEvent extends BaseEvent> = {
|
|
|
86
121
|
result?: EventResultType<TEvent> | BaseEvent | undefined;
|
|
87
122
|
error?: unknown;
|
|
88
123
|
};
|
|
89
|
-
export type
|
|
90
|
-
(
|
|
91
|
-
new (
|
|
92
|
-
event_schema: EventSchema<TShape>;
|
|
93
|
-
class?: new (data: EventInit<TShape>) => EventWithResultSchema<TResult> & EventPayload<TShape>;
|
|
94
|
-
event_type?: string;
|
|
95
|
-
event_version?: string;
|
|
96
|
-
event_result_type?: z.ZodTypeAny;
|
|
97
|
-
fromJSON?: (data: unknown) => EventWithResultSchema<TResult> & EventPayload<TShape>;
|
|
98
|
-
};
|
|
99
|
-
export type SchemaEventFactory<TSchema extends AnyEventSchema, TResult = unknown> = {
|
|
100
|
-
(data: EventInitFromSchema<TSchema>): EventWithResultSchema<TResult> & EventPayloadFromSchema<TSchema>;
|
|
101
|
-
new (data: EventInitFromSchema<TSchema>): EventWithResultSchema<TResult> & EventPayloadFromSchema<TSchema>;
|
|
124
|
+
export type EventClass<TEvent extends BaseEvent = BaseEvent, TInit = never, TSchema extends z.ZodTypeAny = AnyEventSchema, TModelFields extends z.ZodRawShape = z.ZodRawShape, TResultSchema extends z.ZodTypeAny | undefined = z.ZodTypeAny | undefined, TStaticFields = {}> = TStaticFields & {
|
|
125
|
+
(...args: OptionalFactoryArgs<TInit>): TEvent;
|
|
126
|
+
new (...args: OptionalFactoryArgs<TInit>): TEvent;
|
|
102
127
|
event_schema: TSchema;
|
|
103
|
-
|
|
104
|
-
event_type
|
|
105
|
-
event_version
|
|
106
|
-
event_result_type
|
|
107
|
-
fromJSON
|
|
108
|
-
};
|
|
109
|
-
type ZodShapeFrom<TShape extends Record<string, unknown>> = {
|
|
110
|
-
[K in keyof TShape as K extends 'event_result_type' ? never : TShape[K] extends z.ZodTypeAny ? K : never]: Extract<TShape[K], z.ZodTypeAny>;
|
|
128
|
+
model_fields: TModelFields;
|
|
129
|
+
event_type: string;
|
|
130
|
+
event_version: string;
|
|
131
|
+
event_result_type: TResultSchema;
|
|
132
|
+
fromJSON: (data: unknown) => TEvent;
|
|
111
133
|
};
|
|
112
134
|
export declare class BaseEvent {
|
|
113
135
|
event_id: string;
|
|
@@ -132,10 +154,13 @@ export declare class BaseEvent {
|
|
|
132
154
|
event_handler_concurrency?: EventHandlerConcurrencyMode | null;
|
|
133
155
|
event_handler_completion?: EventHandlerCompletionMode | null;
|
|
134
156
|
event_schema?: z.ZodTypeAny;
|
|
157
|
+
_event_parse_schema?: z.ZodTypeAny;
|
|
135
158
|
static event_type?: string;
|
|
136
159
|
static event_version: string;
|
|
137
160
|
static event_result_type?: z.ZodTypeAny;
|
|
138
161
|
static event_schema: AnyEventSchema;
|
|
162
|
+
static model_fields: z.ZodRawShape;
|
|
163
|
+
static _event_parse_schema: AnyEventSchema;
|
|
139
164
|
event_bus?: EventBus;
|
|
140
165
|
_event_original?: BaseEvent;
|
|
141
166
|
_event_dispatch_context?: unknown | null;
|
|
@@ -144,9 +169,9 @@ export declare class BaseEvent {
|
|
|
144
169
|
_lock_for_event_handler: AsyncLock | null;
|
|
145
170
|
constructor(data?: BaseEventInit<Record<string, unknown>>);
|
|
146
171
|
toString(): string;
|
|
147
|
-
static extend<TSchema extends z.ZodObject<z.ZodRawShape>>(event_type: string, event_schema: TSchema):
|
|
148
|
-
static extend<TShape extends
|
|
149
|
-
static extend<TShape extends
|
|
172
|
+
static extend<TSchema extends z.ZodObject<z.ZodRawShape>>(event_type: string, event_schema: TSchema): EventClass<EventWithResultSchema<ResultSchemaFromEventSchema<TSchema>> & EventPayloadFromSchema<TSchema>, EventInitFromSchema<TSchema>, TSchema, EventModelFieldsFromSchema<TSchema>, ResultTypeSchemaFromEventSchema<TSchema>, StaticEventDefaultValuesFromSchema<TSchema>>;
|
|
173
|
+
static extend<const TShape extends Record<string, unknown>>(event_type: string, shape?: TShape): EventClass<EventWithResultSchema<ResultSchemaFromShape<ShortcutZodModelFields<TShape>>> & EventPayload<ShortcutZodModelFields<TShape>>, EventInit<ShortcutZodModelFields<TShape>>, EventSchema<ShortcutZodModelFields<TShape>>, EventModelFields<ShortcutZodModelFields<TShape>>, ResultTypeSchemaFromShape<ShortcutZodModelFields<TShape>>, ShortcutStaticDefaultValues<TShape, ShortcutZodModelFields<TShape>>>;
|
|
174
|
+
static extend<TShape extends z.ZodRawShape>(event_type: string, shape?: TShape): EventClass<EventWithResultSchema<ResultSchemaFromShape<TShape>> & EventPayload<TShape>, EventInit<TShape>, EventSchema<TShape>, EventModelFields<TShape>, ResultTypeSchemaFromShape<TShape>, StaticEventDefaultValues<TShape>>;
|
|
150
175
|
static fromJSON<T extends typeof BaseEvent>(this: T, data: unknown): InstanceType<T>;
|
|
151
176
|
static toJSONArray(events: Iterable<BaseEvent>): BaseEventJSON[];
|
|
152
177
|
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,34 +437,44 @@ 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
|
-
class
|
|
444
|
+
const EventClass = class 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;
|
|
334
451
|
constructor(data) {
|
|
335
452
|
super(data);
|
|
336
453
|
}
|
|
337
|
-
}
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
454
|
+
};
|
|
455
|
+
Object.defineProperty(EventClass, "name", { value: event_type, configurable: true });
|
|
456
|
+
defineStaticEventFields(EventClass, static_field_defaults);
|
|
457
|
+
let CallableEventClass;
|
|
458
|
+
CallableEventClass = new Proxy(EventClass, {
|
|
459
|
+
apply(target, _this_arg, args) {
|
|
460
|
+
return Reflect.construct(target, args, target);
|
|
461
|
+
},
|
|
462
|
+
construct(target, args, new_target) {
|
|
463
|
+
return Reflect.construct(target, args, new_target === CallableEventClass ? target : new_target);
|
|
464
|
+
}
|
|
465
|
+
});
|
|
466
|
+
Object.defineProperty(EventClass.prototype, "constructor", {
|
|
467
|
+
value: CallableEventClass,
|
|
468
|
+
writable: true,
|
|
469
|
+
configurable: true
|
|
470
|
+
});
|
|
471
|
+
EVENT_TYPE_REGISTRY.set(event_type, CallableEventClass);
|
|
472
|
+
return CallableEventClass;
|
|
350
473
|
}
|
|
351
474
|
static fromJSON(data) {
|
|
352
475
|
if (!data || typeof data !== "object") {
|
|
353
|
-
const
|
|
354
|
-
const parsed = decodeEventSchema(
|
|
476
|
+
const event_parse_schema = this._event_parse_schema ?? this.event_schema ?? BaseEventSchema;
|
|
477
|
+
const parsed = decodeEventSchema(event_parse_schema, data);
|
|
355
478
|
return new this(parsed);
|
|
356
479
|
}
|
|
357
480
|
const record = { ...data };
|
|
@@ -368,9 +491,6 @@ class BaseEvent {
|
|
|
368
491
|
if (this !== BaseEvent && ctor.event_result_type && record.event_result_type !== void 0) {
|
|
369
492
|
delete record.event_result_type;
|
|
370
493
|
}
|
|
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
494
|
return new this(record);
|
|
375
495
|
}
|
|
376
496
|
static toJSONArray(events) {
|
|
@@ -395,8 +515,8 @@ class BaseEvent {
|
|
|
395
515
|
const event_results = Object.fromEntries(
|
|
396
516
|
Array.from(this.event_results.entries()).map(([handler_id, result]) => [handler_id, result.toJSON()])
|
|
397
517
|
);
|
|
398
|
-
const
|
|
399
|
-
const encoded = encodeEventSchema(
|
|
518
|
+
const event_parse_schema = this.constructor._event_parse_schema ?? this._event_parse_schema ?? this.constructor.event_schema ?? this.event_schema ?? BaseEventSchema;
|
|
519
|
+
const encoded = encodeEventSchema(event_parse_schema, {
|
|
400
520
|
...record,
|
|
401
521
|
event_id: this.event_id,
|
|
402
522
|
event_type: this.event_type,
|
|
@@ -429,7 +549,7 @@ class BaseEvent {
|
|
|
429
549
|
event_id: this.event_id,
|
|
430
550
|
event_type: this.event_type,
|
|
431
551
|
event_version: this.event_version,
|
|
432
|
-
event_result_type: this.event_result_type ? (0,
|
|
552
|
+
event_result_type: this.event_result_type ? (0, import_jsonschema.toJsonSchema)(this.event_result_type) : this.event_result_type,
|
|
433
553
|
// static configuration options
|
|
434
554
|
event_timeout: this.event_timeout,
|
|
435
555
|
event_slow_timeout: this.event_slow_timeout,
|