@azure-net/kit 0.4.0 → 0.5.1
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/core/eventBus/EventBus.d.ts +91 -0
- package/dist/core/eventBus/EventBus.js +233 -0
- package/dist/core/eventBus/index.d.ts +1 -0
- package/dist/core/eventBus/index.js +1 -0
- package/dist/svelte/ActiveForm/ActiveForm.svelte.d.ts +24 -0
- package/dist/svelte/ActiveForm/ActiveForm.svelte.js +163 -0
- package/dist/svelte/ActiveForm/index.d.ts +1 -0
- package/dist/svelte/ActiveForm/index.js +1 -0
- package/dist/svelte/AsyncSignal/AsyncSignal.svelte.d.ts +1 -1
- package/dist/svelte/AsyncSignal/AsyncSignal.svelte.js +5 -5
- package/dist/svelte/index.d.ts +1 -0
- package/dist/svelte/index.js +1 -0
- package/package.json +1 -2
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
export declare enum EventBusLayers {
|
|
2
|
+
DOMAIN = "domain",
|
|
3
|
+
APPLICATION = "application",
|
|
4
|
+
PRESENTATION = "presentation"
|
|
5
|
+
}
|
|
6
|
+
export interface IEvent<T = unknown> {
|
|
7
|
+
aggregateId: string;
|
|
8
|
+
eventType: string;
|
|
9
|
+
eventVersion: number;
|
|
10
|
+
occurredAt: Date;
|
|
11
|
+
metadata: IEventMetadata;
|
|
12
|
+
eventLayer: EventBusLayers;
|
|
13
|
+
payload: T;
|
|
14
|
+
}
|
|
15
|
+
export interface IEventMetadata {
|
|
16
|
+
userId?: string;
|
|
17
|
+
correlationId: string;
|
|
18
|
+
causationId?: string;
|
|
19
|
+
sessionId?: string;
|
|
20
|
+
[key: string]: unknown;
|
|
21
|
+
}
|
|
22
|
+
export type DomainEventHandler<T = unknown> = (event: IEvent<T>) => void | Promise<void>;
|
|
23
|
+
export interface IEventSubscription {
|
|
24
|
+
unsubscribe(): void;
|
|
25
|
+
}
|
|
26
|
+
export declare class EventBus<TEventMap extends object> {
|
|
27
|
+
private options;
|
|
28
|
+
private handlers;
|
|
29
|
+
private wildcardHandlers;
|
|
30
|
+
private middleware;
|
|
31
|
+
private eventQueue;
|
|
32
|
+
private isProcessing;
|
|
33
|
+
private eventHistory;
|
|
34
|
+
private priorities;
|
|
35
|
+
private readonly debug;
|
|
36
|
+
private readonly maxHistorySize;
|
|
37
|
+
private readonly layer;
|
|
38
|
+
constructor(options?: {
|
|
39
|
+
layer?: EventBusLayers;
|
|
40
|
+
enableHistory?: boolean;
|
|
41
|
+
maxHistorySize?: number;
|
|
42
|
+
enableAsyncProcessing?: boolean;
|
|
43
|
+
debug?: boolean;
|
|
44
|
+
});
|
|
45
|
+
publish<K extends keyof TEventMap>(eventType: K, payload: TEventMap[K], metadata?: Partial<IEventMetadata>): void;
|
|
46
|
+
publishBatch<K extends keyof TEventMap>(events: Array<{
|
|
47
|
+
type: K;
|
|
48
|
+
payload: TEventMap[K];
|
|
49
|
+
metadata?: Partial<IEventMetadata>;
|
|
50
|
+
}>): void;
|
|
51
|
+
subscribe<K extends keyof TEventMap>(eventType: K, handler: DomainEventHandler<TEventMap[K]>, options?: {
|
|
52
|
+
priority?: number;
|
|
53
|
+
}): IEventSubscription;
|
|
54
|
+
subscribeMany<K extends keyof TEventMap>(eventTypes: K[], handler: DomainEventHandler<TEventMap[K]>): IEventSubscription;
|
|
55
|
+
subscribeAll(handler: DomainEventHandler): IEventSubscription;
|
|
56
|
+
use(middleware: <T = unknown>(event: IEvent<T>, next: () => Promise<void>) => Promise<void>): void;
|
|
57
|
+
private processEvent;
|
|
58
|
+
private executeHandlers;
|
|
59
|
+
private safeExecuteHandler;
|
|
60
|
+
private getHandlersForEvent;
|
|
61
|
+
private processQueue;
|
|
62
|
+
private addToHistory;
|
|
63
|
+
getHistory(filter?: {
|
|
64
|
+
eventType?: keyof TEventMap;
|
|
65
|
+
aggregateId?: string;
|
|
66
|
+
}): IEvent[];
|
|
67
|
+
waitForCompletion(): Promise<void>;
|
|
68
|
+
clear(): void;
|
|
69
|
+
private generateAggregateId;
|
|
70
|
+
private generateCorrelationId;
|
|
71
|
+
}
|
|
72
|
+
export declare const loggingMiddleware: (event: IEvent, next: () => Promise<void>) => Promise<void>;
|
|
73
|
+
export declare const createEventBus: <TEventMap extends object>(layer: EventBusLayers, opts: {
|
|
74
|
+
history?: boolean;
|
|
75
|
+
asyncProcessing?: boolean;
|
|
76
|
+
historySize?: number;
|
|
77
|
+
middlewares?: (<T = unknown>(event: IEvent<T>, next: () => Promise<void>) => Promise<void>)[];
|
|
78
|
+
debug?: boolean;
|
|
79
|
+
}) => {
|
|
80
|
+
eventBus: EventBus<TEventMap>;
|
|
81
|
+
CreateEvent: <K extends keyof TEventMap>(eventName: K, data: TEventMap[K]) => void;
|
|
82
|
+
CreateBatch: <K extends keyof TEventMap>(events: Array<{
|
|
83
|
+
type: K;
|
|
84
|
+
payload: TEventMap[K];
|
|
85
|
+
metadata?: Partial<IEventMetadata>;
|
|
86
|
+
}>) => void;
|
|
87
|
+
CreateSubscriber: <K extends keyof TEventMap>(eventType: K, handler: DomainEventHandler<TEventMap[K]>, options?: {
|
|
88
|
+
priority?: number;
|
|
89
|
+
}) => IEventSubscription;
|
|
90
|
+
createSubscribers: <K extends keyof TEventMap>(eventTypes: K[], handler: DomainEventHandler<TEventMap[K]>) => IEventSubscription;
|
|
91
|
+
};
|
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
export var EventBusLayers;
|
|
2
|
+
(function (EventBusLayers) {
|
|
3
|
+
EventBusLayers["DOMAIN"] = "domain";
|
|
4
|
+
EventBusLayers["APPLICATION"] = "application";
|
|
5
|
+
EventBusLayers["PRESENTATION"] = "presentation";
|
|
6
|
+
})(EventBusLayers || (EventBusLayers = {}));
|
|
7
|
+
export class EventBus {
|
|
8
|
+
options;
|
|
9
|
+
handlers = new Map();
|
|
10
|
+
wildcardHandlers = new Set();
|
|
11
|
+
middleware = [];
|
|
12
|
+
eventQueue = [];
|
|
13
|
+
isProcessing = false;
|
|
14
|
+
eventHistory = [];
|
|
15
|
+
priorities = new Map();
|
|
16
|
+
debug = false;
|
|
17
|
+
maxHistorySize = 1000;
|
|
18
|
+
layer;
|
|
19
|
+
constructor(options = { layer: EventBusLayers.PRESENTATION, debug: false }) {
|
|
20
|
+
this.options = options;
|
|
21
|
+
this.layer = options.layer ?? EventBusLayers.PRESENTATION;
|
|
22
|
+
this.debug = options.debug ?? false;
|
|
23
|
+
if (options.maxHistorySize) {
|
|
24
|
+
this.maxHistorySize = options.maxHistorySize;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
publish(eventType, payload, metadata) {
|
|
28
|
+
const event = {
|
|
29
|
+
aggregateId: this.generateAggregateId(String(eventType), payload),
|
|
30
|
+
eventType: String(eventType),
|
|
31
|
+
eventVersion: 1,
|
|
32
|
+
occurredAt: new Date(),
|
|
33
|
+
metadata: {
|
|
34
|
+
correlationId: this.generateCorrelationId(),
|
|
35
|
+
...metadata
|
|
36
|
+
},
|
|
37
|
+
eventLayer: this.layer,
|
|
38
|
+
payload
|
|
39
|
+
};
|
|
40
|
+
void this.processEvent(event);
|
|
41
|
+
}
|
|
42
|
+
publishBatch(events) {
|
|
43
|
+
const domainEvents = events.map(({ type, payload, metadata }) => ({
|
|
44
|
+
aggregateId: this.generateAggregateId(type, payload),
|
|
45
|
+
eventType: type,
|
|
46
|
+
eventVersion: 1,
|
|
47
|
+
occurredAt: new Date(),
|
|
48
|
+
eventLayer: this.layer,
|
|
49
|
+
metadata: {
|
|
50
|
+
correlationId: this.generateCorrelationId(),
|
|
51
|
+
...metadata
|
|
52
|
+
},
|
|
53
|
+
payload
|
|
54
|
+
}));
|
|
55
|
+
domainEvents.forEach((event) => this.processEvent(event));
|
|
56
|
+
}
|
|
57
|
+
subscribe(eventType, handler, options) {
|
|
58
|
+
if (!this.handlers.has(eventType)) {
|
|
59
|
+
this.handlers.set(eventType, new Set());
|
|
60
|
+
}
|
|
61
|
+
const handlers = this.handlers.get(eventType);
|
|
62
|
+
handlers.add(handler);
|
|
63
|
+
if (options?.priority !== undefined) {
|
|
64
|
+
this.priorities.set(eventType, options.priority);
|
|
65
|
+
}
|
|
66
|
+
return {
|
|
67
|
+
unsubscribe: () => {
|
|
68
|
+
handlers.delete(handler);
|
|
69
|
+
if (handlers.size === 0) {
|
|
70
|
+
this.handlers.delete(eventType);
|
|
71
|
+
this.priorities.delete(eventType);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
subscribeMany(eventTypes, handler) {
|
|
77
|
+
const subscriptions = eventTypes.map((type) => this.subscribe(type, handler));
|
|
78
|
+
return {
|
|
79
|
+
unsubscribe: () => {
|
|
80
|
+
subscriptions.forEach((sub) => sub.unsubscribe());
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
subscribeAll(handler) {
|
|
85
|
+
this.wildcardHandlers.add(handler);
|
|
86
|
+
return {
|
|
87
|
+
unsubscribe: () => {
|
|
88
|
+
this.wildcardHandlers.delete(handler);
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
use(middleware) {
|
|
93
|
+
this.middleware.push(middleware);
|
|
94
|
+
}
|
|
95
|
+
async processEvent(event) {
|
|
96
|
+
if (this.options.enableHistory) {
|
|
97
|
+
this.addToHistory(event);
|
|
98
|
+
}
|
|
99
|
+
const handlers = this.getHandlersForEvent(event);
|
|
100
|
+
if (!this.debug && handlers.size === 0 && this.wildcardHandlers.size === 0) {
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
if (this.options.enableAsyncProcessing) {
|
|
104
|
+
this.eventQueue.push({ event, handlers });
|
|
105
|
+
if (!this.isProcessing) {
|
|
106
|
+
void this.processQueue();
|
|
107
|
+
}
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
await this.executeHandlers(event, handlers);
|
|
111
|
+
}
|
|
112
|
+
async executeHandlers(event, handlers) {
|
|
113
|
+
const allHandlers = [...handlers, ...this.wildcardHandlers];
|
|
114
|
+
let index = -1;
|
|
115
|
+
const dispatch = async (i) => {
|
|
116
|
+
if (i <= index) {
|
|
117
|
+
throw new Error('next() called multiple times');
|
|
118
|
+
}
|
|
119
|
+
index = i;
|
|
120
|
+
if (i === this.middleware.length) {
|
|
121
|
+
await Promise.all(allHandlers.map((handler) => this.safeExecuteHandler(handler, event)));
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
const middleware = this.middleware[i];
|
|
125
|
+
await middleware(event, () => dispatch(i + 1));
|
|
126
|
+
};
|
|
127
|
+
await dispatch(0);
|
|
128
|
+
}
|
|
129
|
+
async safeExecuteHandler(handler, event) {
|
|
130
|
+
try {
|
|
131
|
+
await handler(event);
|
|
132
|
+
}
|
|
133
|
+
catch (error) {
|
|
134
|
+
console.error(`Error handling event ${event.eventType}:`, error);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
getHandlersForEvent(event) {
|
|
138
|
+
const handlers = this.handlers.get(event.eventType);
|
|
139
|
+
return handlers || new Set();
|
|
140
|
+
}
|
|
141
|
+
async processQueue() {
|
|
142
|
+
if (this.isProcessing || this.eventQueue.length === 0) {
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
this.isProcessing = true;
|
|
146
|
+
this.eventQueue.sort((a, b) => {
|
|
147
|
+
const priorityA = this.priorities.get(a.event.eventType) || 0;
|
|
148
|
+
const priorityB = this.priorities.get(b.event.eventType) || 0;
|
|
149
|
+
return priorityB - priorityA;
|
|
150
|
+
});
|
|
151
|
+
while (this.eventQueue.length > 0) {
|
|
152
|
+
const batch = this.eventQueue.splice(0, 10);
|
|
153
|
+
await Promise.all(batch.map(({ event, handlers }) => this.executeHandlers(event, handlers)));
|
|
154
|
+
}
|
|
155
|
+
this.isProcessing = false;
|
|
156
|
+
}
|
|
157
|
+
addToHistory(event) {
|
|
158
|
+
this.eventHistory.push(event);
|
|
159
|
+
if (this.eventHistory.length > this.maxHistorySize) {
|
|
160
|
+
this.eventHistory.shift();
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
getHistory(filter) {
|
|
164
|
+
if (!filter)
|
|
165
|
+
return [...this.eventHistory];
|
|
166
|
+
return this.eventHistory.filter((event) => {
|
|
167
|
+
switch (true) {
|
|
168
|
+
case filter.eventType && event.eventType !== filter.eventType:
|
|
169
|
+
return false;
|
|
170
|
+
case filter.aggregateId && event.aggregateId !== filter.aggregateId:
|
|
171
|
+
return false;
|
|
172
|
+
default:
|
|
173
|
+
return true;
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
async waitForCompletion() {
|
|
178
|
+
while (this.isProcessing || this.eventQueue.length > 0) {
|
|
179
|
+
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
clear() {
|
|
183
|
+
this.handlers.clear();
|
|
184
|
+
this.wildcardHandlers.clear();
|
|
185
|
+
this.eventQueue = [];
|
|
186
|
+
this.eventHistory = [];
|
|
187
|
+
}
|
|
188
|
+
generateAggregateId(eventType, payload) {
|
|
189
|
+
const maybeEventPayload = payload;
|
|
190
|
+
if (typeof maybeEventPayload === 'object' && maybeEventPayload?.id)
|
|
191
|
+
return String(maybeEventPayload.id);
|
|
192
|
+
if (typeof maybeEventPayload === 'object' && maybeEventPayload?.aggregateId)
|
|
193
|
+
return String(maybeEventPayload.aggregateId);
|
|
194
|
+
return `${eventType}-${Date.now()}-${Math.random().toString(36).substring(2, 9)}`;
|
|
195
|
+
}
|
|
196
|
+
generateCorrelationId() {
|
|
197
|
+
return `${Date.now()}-${Math.random().toString(36).substring(2, 9)}`;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
export const loggingMiddleware = async (event, next) => {
|
|
201
|
+
console.log(`[${event.eventLayer.toUpperCase()}Event] ${event.eventType} at ${event.occurredAt.toISOString()}`);
|
|
202
|
+
console.log(`[${event.eventLayer.toUpperCase()}Event] data:`, event);
|
|
203
|
+
const start = performance.now();
|
|
204
|
+
await next();
|
|
205
|
+
const duration = performance.now() - start;
|
|
206
|
+
console.log(`[${event.eventLayer.toUpperCase()}Event] ${event.eventType} processed in ${duration.toFixed(2)}ms`);
|
|
207
|
+
};
|
|
208
|
+
export const createEventBus = (layer, opts) => {
|
|
209
|
+
const { history = true, asyncProcessing = true, historySize = 1000, middlewares = [], debug = false } = opts;
|
|
210
|
+
const eventBus = new EventBus({
|
|
211
|
+
layer,
|
|
212
|
+
enableHistory: history,
|
|
213
|
+
enableAsyncProcessing: asyncProcessing,
|
|
214
|
+
maxHistorySize: historySize,
|
|
215
|
+
debug
|
|
216
|
+
});
|
|
217
|
+
middlewares.forEach((value) => {
|
|
218
|
+
eventBus.use(value);
|
|
219
|
+
});
|
|
220
|
+
const CreateEvent = (eventName, data) => {
|
|
221
|
+
eventBus.publish(eventName, data);
|
|
222
|
+
};
|
|
223
|
+
const CreateBatch = (events) => {
|
|
224
|
+
eventBus.publishBatch(events);
|
|
225
|
+
};
|
|
226
|
+
const CreateSubscriber = (eventType, handler, options) => {
|
|
227
|
+
return eventBus.subscribe(eventType, handler, options);
|
|
228
|
+
};
|
|
229
|
+
const createSubscribers = (eventTypes, handler) => {
|
|
230
|
+
return eventBus.subscribeMany(eventTypes, handler);
|
|
231
|
+
};
|
|
232
|
+
return { eventBus, CreateEvent, CreateBatch, CreateSubscriber, createSubscribers };
|
|
233
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './EventBus.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './EventBus.js';
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { type RequestErrors, BaseRequest } from '../../core/index.js';
|
|
2
|
+
export interface FormConfig<T> {
|
|
3
|
+
onSubmit: (data: T) => Promise<void> | void;
|
|
4
|
+
initialData?: T;
|
|
5
|
+
validation?: {
|
|
6
|
+
schema?: new (data: Partial<T>) => BaseRequest<T, T>;
|
|
7
|
+
validateOnChange?: boolean;
|
|
8
|
+
validateOnBlur?: boolean;
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
export interface ActiveForm<T> {
|
|
12
|
+
data: Partial<T>;
|
|
13
|
+
errors: RequestErrors<T>;
|
|
14
|
+
touched: Partial<Record<keyof T, boolean>>;
|
|
15
|
+
pending: boolean;
|
|
16
|
+
dirty: boolean;
|
|
17
|
+
valid: boolean;
|
|
18
|
+
submit: () => Promise<void>;
|
|
19
|
+
validate: (field?: keyof T) => void;
|
|
20
|
+
reset: () => void;
|
|
21
|
+
setField: <K extends keyof T>(field: K, value: T[K]) => void;
|
|
22
|
+
setErrors: (errors: RequestErrors<T>) => void;
|
|
23
|
+
touchField: (field: keyof T) => void;
|
|
24
|
+
}
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import { BaseRequest } from '../../core/index.js';
|
|
2
|
+
// export function createActiveForm<T extends object>(config: FormConfig<T>): ActiveForm<T> {
|
|
3
|
+
// const initial: Partial<T> = config.initialData ?? {};
|
|
4
|
+
// let data = $state<Partial<T>>(structuredClone(initial));
|
|
5
|
+
// let errors = $state<RequestErrors<T>>({});
|
|
6
|
+
// let touched = $state<Partial<Record<keyof T, boolean>>>({});
|
|
7
|
+
// let pending = $state(false);
|
|
8
|
+
//
|
|
9
|
+
// const dirty = $derived(JSON.stringify(data) !== JSON.stringify(config.initialData));
|
|
10
|
+
//
|
|
11
|
+
// const valid = $derived(() => {
|
|
12
|
+
// if (Object.keys(errors).length > 0) return false;
|
|
13
|
+
//
|
|
14
|
+
// if (config.validation?.schema) {
|
|
15
|
+
// try {
|
|
16
|
+
// const request = new config.validation.schema(data);
|
|
17
|
+
// request.validated();
|
|
18
|
+
// return true;
|
|
19
|
+
// } catch {
|
|
20
|
+
// return false;
|
|
21
|
+
// }
|
|
22
|
+
// }
|
|
23
|
+
//
|
|
24
|
+
// return true;
|
|
25
|
+
// });
|
|
26
|
+
//
|
|
27
|
+
// function validateForm(): boolean {
|
|
28
|
+
// if (!config.validation?.schema) return true;
|
|
29
|
+
//
|
|
30
|
+
// try {
|
|
31
|
+
// const request = new config.validation.schema(data);
|
|
32
|
+
// request.validated();
|
|
33
|
+
// errors = {};
|
|
34
|
+
// return true;
|
|
35
|
+
// } catch (error) {
|
|
36
|
+
// if (error instanceof config.validation.schema) {
|
|
37
|
+
// errors = error.getErrors();
|
|
38
|
+
// return false;
|
|
39
|
+
// }
|
|
40
|
+
// throw error;
|
|
41
|
+
// }
|
|
42
|
+
// }
|
|
43
|
+
//
|
|
44
|
+
// function validateField(field: keyof T): void {
|
|
45
|
+
// if (!config.validation?.schema) return;
|
|
46
|
+
//
|
|
47
|
+
// try {
|
|
48
|
+
// const request = new config.validation.schema(data);
|
|
49
|
+
// request.validated();
|
|
50
|
+
//
|
|
51
|
+
// const newErrors = { ...errors };
|
|
52
|
+
// delete newErrors[field];
|
|
53
|
+
// errors = newErrors;
|
|
54
|
+
// } catch (error) {
|
|
55
|
+
// if (error instanceof config.validation.schema) {
|
|
56
|
+
// const allErrors = error.getErrors();
|
|
57
|
+
// if (field in allErrors) {
|
|
58
|
+
// errors = { ...errors, [field]: allErrors[field] };
|
|
59
|
+
// } else {
|
|
60
|
+
// const newErrors = { ...errors };
|
|
61
|
+
// delete newErrors[field];
|
|
62
|
+
// errors = newErrors;
|
|
63
|
+
// }
|
|
64
|
+
// }
|
|
65
|
+
// }
|
|
66
|
+
// }
|
|
67
|
+
//
|
|
68
|
+
// if (config.validation?.validateOnChange) {
|
|
69
|
+
// $effect(() => {
|
|
70
|
+
// JSON.stringify(data);
|
|
71
|
+
//
|
|
72
|
+
// const touchedFields = Object.keys(touched) as (keyof T)[];
|
|
73
|
+
// if (touchedFields.length > 0) {
|
|
74
|
+
// touchedFields.forEach((field) => {
|
|
75
|
+
// if (touched[field]) {
|
|
76
|
+
// validateField(field);
|
|
77
|
+
// }
|
|
78
|
+
// });
|
|
79
|
+
// }
|
|
80
|
+
// });
|
|
81
|
+
// }
|
|
82
|
+
//
|
|
83
|
+
// async function submit(): Promise<void> {
|
|
84
|
+
// pending = true;
|
|
85
|
+
//
|
|
86
|
+
// try {
|
|
87
|
+
// if (!validateForm()) {
|
|
88
|
+
// return;
|
|
89
|
+
// }
|
|
90
|
+
//
|
|
91
|
+
// errors = {};
|
|
92
|
+
//
|
|
93
|
+
// await config.onSubmit(data);
|
|
94
|
+
// } catch (error) {
|
|
95
|
+
// throw error;
|
|
96
|
+
// } finally {
|
|
97
|
+
// pending = false;
|
|
98
|
+
// }
|
|
99
|
+
// }
|
|
100
|
+
//
|
|
101
|
+
// function setField<K extends keyof T>(field: K, value: T[K]): void {
|
|
102
|
+
// data = { ...data, [field]: value };
|
|
103
|
+
// touched = { ...touched, [field]: true };
|
|
104
|
+
//
|
|
105
|
+
// if (config.validation?.validateOnBlur && touched[field]) {
|
|
106
|
+
// validateField(field);
|
|
107
|
+
// }
|
|
108
|
+
// }
|
|
109
|
+
//
|
|
110
|
+
// function touchField(field: keyof T): void {
|
|
111
|
+
// touched = { ...touched, [field]: true };
|
|
112
|
+
//
|
|
113
|
+
// if (config.validation?.validateOnBlur) {
|
|
114
|
+
// validateField(field);
|
|
115
|
+
// }
|
|
116
|
+
// }
|
|
117
|
+
//
|
|
118
|
+
// function reset(): void {
|
|
119
|
+
// data = structuredClone(config.initialData);
|
|
120
|
+
// errors = {};
|
|
121
|
+
// touched = {};
|
|
122
|
+
// }
|
|
123
|
+
//
|
|
124
|
+
// return {
|
|
125
|
+
// get data() {
|
|
126
|
+
// return data;
|
|
127
|
+
// },
|
|
128
|
+
// get errors() {
|
|
129
|
+
// return errors;
|
|
130
|
+
// },
|
|
131
|
+
// get touched() {
|
|
132
|
+
// return touched;
|
|
133
|
+
// },
|
|
134
|
+
// get pending() {
|
|
135
|
+
// return pending;
|
|
136
|
+
// },
|
|
137
|
+
// get dirty() {
|
|
138
|
+
// return dirty;
|
|
139
|
+
// },
|
|
140
|
+
// get valid() {
|
|
141
|
+
// return valid();
|
|
142
|
+
// },
|
|
143
|
+
//
|
|
144
|
+
// submit,
|
|
145
|
+
// validate: (field?: keyof T) => {
|
|
146
|
+
// if (field) {
|
|
147
|
+
// validateField(field);
|
|
148
|
+
// } else {
|
|
149
|
+
// validateForm();
|
|
150
|
+
// }
|
|
151
|
+
// },
|
|
152
|
+
// reset,
|
|
153
|
+
// setField,
|
|
154
|
+
// setErrors: (newErrors: RequestErrors<T>) => {
|
|
155
|
+
// errors = newErrors;
|
|
156
|
+
// },
|
|
157
|
+
// touchField
|
|
158
|
+
// };
|
|
159
|
+
// }
|
|
160
|
+
//
|
|
161
|
+
// function structuredClone<T>(obj: T): T {
|
|
162
|
+
// return JSON.parse(JSON.stringify(obj));
|
|
163
|
+
// }
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './ActiveForm.svelte.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './ActiveForm.svelte.js';
|
|
@@ -15,4 +15,4 @@ export interface AsyncSignalSvelte<TData, TError = Error> {
|
|
|
15
15
|
reset: () => void;
|
|
16
16
|
abort: () => void;
|
|
17
17
|
}
|
|
18
|
-
export declare
|
|
18
|
+
export declare const createAsyncSignal: <TData, TError = Error>(handler: (signal?: AbortSignal) => Promise<TData>, options?: AsyncSignalOptions<TData>) => AsyncSignalSvelte<TData, TError>;
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { untrack } from 'svelte';
|
|
2
2
|
import { browser } from '$app/environment';
|
|
3
|
-
export
|
|
3
|
+
export const createAsyncSignal = (handler, options = {}) => {
|
|
4
4
|
const { server = false, immediate = true, watch = [], initialData = null } = options;
|
|
5
|
-
let data = $state
|
|
6
|
-
let error = $state
|
|
7
|
-
let status = $state
|
|
5
|
+
let data = $state(initialData);
|
|
6
|
+
let error = $state(null);
|
|
7
|
+
let status = $state('idle');
|
|
8
8
|
const pending = $derived(status === 'pending');
|
|
9
9
|
let abortController = null;
|
|
10
10
|
async function execute() {
|
|
@@ -84,4 +84,4 @@ export function createAsyncSignal(handler, options = {}) {
|
|
|
84
84
|
}
|
|
85
85
|
}
|
|
86
86
|
};
|
|
87
|
-
}
|
|
87
|
+
};
|
package/dist/svelte/index.d.ts
CHANGED
package/dist/svelte/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@azure-net/kit",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.1",
|
|
4
4
|
"files": [
|
|
5
5
|
"dist",
|
|
6
6
|
"!dist/**/*.test.*",
|
|
@@ -128,7 +128,6 @@
|
|
|
128
128
|
"test:unit": "vitest",
|
|
129
129
|
"test": "npm run test:unit -- --run && npm run test:e2e",
|
|
130
130
|
"test:e2e": "playwright test",
|
|
131
|
-
"precommit": "pnpm lint && pnpm check",
|
|
132
131
|
"semantic-release": "env-cmd semantic-release",
|
|
133
132
|
"commit": "pnpm format && git add . && git-cz && git push",
|
|
134
133
|
"release": "pnpm commit && pnpm semantic-release"
|