slapflow 1.0.2 → 1.2.0

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/index.d.cts CHANGED
@@ -4,6 +4,7 @@ type Config = {
4
4
  version?: 1;
5
5
  strategies: Record<string, Strategy>;
6
6
  entrypoints?: Record<string, string>;
7
+ guards?: Record<string, ConditionExpression>;
7
8
  };
8
9
  type Strategy = {
9
10
  fn: string;
@@ -23,7 +24,7 @@ type Next = string | {
23
24
  props?: Props;
24
25
  when?: ConditionExpression;
25
26
  };
26
- type ConditionExpression = boolean | [operator: string, ...args: unknown[]];
27
+ type ConditionExpression = boolean | [operator: string, ...args: unknown[]] | ['guard', name: string];
27
28
  type Action<TContext, TPatch = unknown> = (args: ActionArgs<TContext>) => ActionResult<TContext, TPatch> | Promise<ActionResult<TContext, TPatch>>;
28
29
  type ActionArgs<TContext> = {
29
30
  context: TContext;
@@ -77,6 +78,7 @@ type SlapEvent = {
77
78
  };
78
79
  type EventMap = Record<string, unknown>;
79
80
  type EventName<TEvents extends object> = Extract<keyof TEvents, string>;
81
+ type EventPattern = `${string}*${string}`;
80
82
  type BusEvent<TPayload = unknown> = {
81
83
  id: string;
82
84
  topic: string;
@@ -104,8 +106,14 @@ type BusOptions<TEvents extends object> = {
104
106
  onError?: (event: BusErrorEvent<TEvents>) => void;
105
107
  };
106
108
  type Bus<TEvents extends object = EventMap> = {
107
- on<TEvent extends EventName<TEvents>>(event: TEvent, handler: EventHandler<TEvents, TEvent>): () => void;
108
- off<TEvent extends EventName<TEvents>>(event: TEvent, handler?: EventHandler<TEvents, TEvent>): void;
109
+ on: {
110
+ <TEvent extends EventName<TEvents>>(event: TEvent, handler: EventHandler<TEvents, TEvent>): () => void;
111
+ (event: EventPattern, handler: (event: BusEvent<unknown>) => void): () => void;
112
+ };
113
+ off: {
114
+ <TEvent extends EventName<TEvents>>(event: TEvent, handler?: EventHandler<TEvents, TEvent>): void;
115
+ (event: EventPattern, handler?: (event: BusEvent<unknown>) => void): void;
116
+ };
109
117
  emit<TEvent extends EventName<TEvents>>(topic: TEvent, payload: TEvents[TEvent], options?: BusEmitOptions): BusEvent<TEvents[TEvent]>;
110
118
  dispatch(event: unknown): BusEvent | undefined;
111
119
  };
@@ -127,8 +135,8 @@ type WSRetryOptions = RetryOptions;
127
135
  type WSOptions<TEvents extends object = EventMap> = {
128
136
  bus: Bus<TEvents>;
129
137
  createSocket: () => WSSocket;
130
- inboundTopics?: EventName<TEvents>[];
131
- outboundTopics?: EventName<TEvents>[];
138
+ inboundTopics?: (EventName<TEvents> | EventPattern)[];
139
+ outboundTopics?: (EventName<TEvents> | EventPattern)[];
132
140
  origin?: string;
133
141
  retry?: WSRetryOptions;
134
142
  };
@@ -140,6 +148,21 @@ type WS = {
140
148
  reconnect(): void;
141
149
  status(): WSStatus;
142
150
  };
151
+ type SocketEventTopic = 'open' | 'message' | 'close' | 'error';
152
+ type WebSocketOptions<TEvents extends object = EventMap> = {
153
+ url: string;
154
+ bus: Bus<TEvents>;
155
+ origin?: string;
156
+ protocols?: string | string[];
157
+ reconnect?: RetryOptions;
158
+ };
159
+ type WebSocketStatus = 'idle' | 'connecting' | 'connected' | 'reconnecting' | 'stopped';
160
+ type WSClient = {
161
+ start(): void;
162
+ stop(): void;
163
+ reconnect(): void;
164
+ status(): WebSocketStatus;
165
+ };
143
166
  type BindingEventMap = Record<string, Input>;
144
167
  type BusBindingKey<TEvents extends object> = {
145
168
  [TEvent in EventName<TEvents>]: TEvents[TEvent] extends Input ? `[bus] ${TEvent}` : never;
@@ -366,14 +389,21 @@ declare const PubSub: Bus<EventMap>;
366
389
 
367
390
  declare const createFlow: <TContext, TPatch = unknown, TEvents extends object = BindingEventMap>(definition: FlowDefinition<TContext, TPatch, TEvents>, options: FlowOptions<TContext, TPatch, TEvents>) => Flow<TContext, TPatch>;
368
391
 
392
+ /**
393
+ * @deprecated Use `createWebSocket` instead. This adapter will be removed in a future release.
394
+ */
369
395
  declare const createWS: <TEvents extends object = EventMap>(options: WSOptions<TEvents>) => WS;
370
396
 
397
+ declare const createWebSocket: <TEvents extends object = EventMap>(options: WebSocketOptions<TEvents>) => WSClient;
398
+
371
399
  declare const catchError: <T>(callback: () => T | PromiseLike<T>) => Promise<T>;
372
400
 
373
401
  type ActionsRegistry<TContext, TPatch> = Map<string, Action<TContext, TPatch>>;
374
- declare const createActionsRegistry: <TContext, TPatch>() => ActionsRegistry<TContext, TPatch>;
402
+ declare const BUILTIN_ACTIONS: readonly [name: string, action: Action<unknown, unknown>][];
403
+ declare const BUILTIN_ACTION_NAMES: Set<string>;
375
404
 
376
405
  type ConditionsRegistry<TContext> = Map<string, ConditionFn<TContext>>;
377
- declare const createConditionsRegistry: <TContext>() => ConditionsRegistry<TContext>;
406
+ declare const BUILTIN_CONDITIONS: readonly [name: string, condition: ConditionFn<unknown>][];
407
+ declare const BUILTIN_CONDITION_NAMES: Set<string>;
378
408
 
379
- export { type Action, type ActionArgs, type ActionFail, type ActionResult, type ActionSkip, type ActionStop, type ActionSuccess, type ActionsRegistry, type BindingEventMap, type Bus, type BusBinding, type BusBindingKey, type BusBindings, type BusEmitOptions, type BusErrorEvent, type BusEvent, type BusOptions, type ConcurrencyMode, type ConcurrencyOptions, type ConditionExpression, type ConditionFn, type ConditionsRegistry, type Config, type DomBinding, type DomBindingKey, type DomBindings, type DomForm, type DomInput, type ErrorReporter, type ErrorReporterHandlers, type ErrorStage, type EventHandler, type EventMap, type EventName, type ExpressionOperator, type FetchResponseType, type Flow, type FlowDefinition, type FlowOptions, type InactiveBinding, type Input, type Mode, type Next, type Props, PubSub, type QueueOverflow, type RetryOptions, type RunOptions, type RunResult, type Runner, type RunnerErrorEvent, type RunnerOptions, type Runtime, type SlapError, type SlapErrorEvent, type SlapEvent, type StartResult, type Strategy, type TraceEntry, type TraceSink, type ValidationIssue, type ValidationResult, type VariableValue, type Variables, type WS, type WSOptions, type WSRetryOptions, type WSSocket, type WSStatus, catchError, createActionsRegistry, createConditionsRegistry, createFlow, createMemoryTraceSink, createPubSub, createWS, defineConfig, defineErrorReporter };
409
+ export { type Action, type ActionArgs, type ActionFail, type ActionResult, type ActionSkip, type ActionStop, type ActionSuccess, type ActionsRegistry, BUILTIN_ACTIONS, BUILTIN_ACTION_NAMES, BUILTIN_CONDITIONS, BUILTIN_CONDITION_NAMES, type BindingEventMap, type Bus, type BusBinding, type BusBindingKey, type BusBindings, type BusEmitOptions, type BusErrorEvent, type BusEvent, type BusOptions, type ConcurrencyMode, type ConcurrencyOptions, type ConditionExpression, type ConditionFn, type ConditionsRegistry, type Config, type DomBinding, type DomBindingKey, type DomBindings, type DomForm, type DomInput, type ErrorReporter, type ErrorReporterHandlers, type ErrorStage, type EventHandler, type EventMap, type EventName, type EventPattern, type ExpressionOperator, type FetchResponseType, type Flow, type FlowDefinition, type FlowOptions, type InactiveBinding, type Input, type Mode, type Next, type Props, PubSub, type QueueOverflow, type RetryOptions, type RunOptions, type RunResult, type Runner, type RunnerErrorEvent, type RunnerOptions, type Runtime, type SlapError, type SlapErrorEvent, type SlapEvent, type SocketEventTopic, type StartResult, type Strategy, type TraceEntry, type TraceSink, type ValidationIssue, type ValidationResult, type VariableValue, type Variables, type WS, type WSClient, type WSOptions, type WSRetryOptions, type WSSocket, type WSStatus, type WebSocketOptions, type WebSocketStatus, catchError, createFlow, createMemoryTraceSink, createPubSub, createWS, createWebSocket, defineConfig, defineErrorReporter };
package/dist/index.d.ts CHANGED
@@ -4,6 +4,7 @@ type Config = {
4
4
  version?: 1;
5
5
  strategies: Record<string, Strategy>;
6
6
  entrypoints?: Record<string, string>;
7
+ guards?: Record<string, ConditionExpression>;
7
8
  };
8
9
  type Strategy = {
9
10
  fn: string;
@@ -23,7 +24,7 @@ type Next = string | {
23
24
  props?: Props;
24
25
  when?: ConditionExpression;
25
26
  };
26
- type ConditionExpression = boolean | [operator: string, ...args: unknown[]];
27
+ type ConditionExpression = boolean | [operator: string, ...args: unknown[]] | ['guard', name: string];
27
28
  type Action<TContext, TPatch = unknown> = (args: ActionArgs<TContext>) => ActionResult<TContext, TPatch> | Promise<ActionResult<TContext, TPatch>>;
28
29
  type ActionArgs<TContext> = {
29
30
  context: TContext;
@@ -77,6 +78,7 @@ type SlapEvent = {
77
78
  };
78
79
  type EventMap = Record<string, unknown>;
79
80
  type EventName<TEvents extends object> = Extract<keyof TEvents, string>;
81
+ type EventPattern = `${string}*${string}`;
80
82
  type BusEvent<TPayload = unknown> = {
81
83
  id: string;
82
84
  topic: string;
@@ -104,8 +106,14 @@ type BusOptions<TEvents extends object> = {
104
106
  onError?: (event: BusErrorEvent<TEvents>) => void;
105
107
  };
106
108
  type Bus<TEvents extends object = EventMap> = {
107
- on<TEvent extends EventName<TEvents>>(event: TEvent, handler: EventHandler<TEvents, TEvent>): () => void;
108
- off<TEvent extends EventName<TEvents>>(event: TEvent, handler?: EventHandler<TEvents, TEvent>): void;
109
+ on: {
110
+ <TEvent extends EventName<TEvents>>(event: TEvent, handler: EventHandler<TEvents, TEvent>): () => void;
111
+ (event: EventPattern, handler: (event: BusEvent<unknown>) => void): () => void;
112
+ };
113
+ off: {
114
+ <TEvent extends EventName<TEvents>>(event: TEvent, handler?: EventHandler<TEvents, TEvent>): void;
115
+ (event: EventPattern, handler?: (event: BusEvent<unknown>) => void): void;
116
+ };
109
117
  emit<TEvent extends EventName<TEvents>>(topic: TEvent, payload: TEvents[TEvent], options?: BusEmitOptions): BusEvent<TEvents[TEvent]>;
110
118
  dispatch(event: unknown): BusEvent | undefined;
111
119
  };
@@ -127,8 +135,8 @@ type WSRetryOptions = RetryOptions;
127
135
  type WSOptions<TEvents extends object = EventMap> = {
128
136
  bus: Bus<TEvents>;
129
137
  createSocket: () => WSSocket;
130
- inboundTopics?: EventName<TEvents>[];
131
- outboundTopics?: EventName<TEvents>[];
138
+ inboundTopics?: (EventName<TEvents> | EventPattern)[];
139
+ outboundTopics?: (EventName<TEvents> | EventPattern)[];
132
140
  origin?: string;
133
141
  retry?: WSRetryOptions;
134
142
  };
@@ -140,6 +148,21 @@ type WS = {
140
148
  reconnect(): void;
141
149
  status(): WSStatus;
142
150
  };
151
+ type SocketEventTopic = 'open' | 'message' | 'close' | 'error';
152
+ type WebSocketOptions<TEvents extends object = EventMap> = {
153
+ url: string;
154
+ bus: Bus<TEvents>;
155
+ origin?: string;
156
+ protocols?: string | string[];
157
+ reconnect?: RetryOptions;
158
+ };
159
+ type WebSocketStatus = 'idle' | 'connecting' | 'connected' | 'reconnecting' | 'stopped';
160
+ type WSClient = {
161
+ start(): void;
162
+ stop(): void;
163
+ reconnect(): void;
164
+ status(): WebSocketStatus;
165
+ };
143
166
  type BindingEventMap = Record<string, Input>;
144
167
  type BusBindingKey<TEvents extends object> = {
145
168
  [TEvent in EventName<TEvents>]: TEvents[TEvent] extends Input ? `[bus] ${TEvent}` : never;
@@ -366,14 +389,21 @@ declare const PubSub: Bus<EventMap>;
366
389
 
367
390
  declare const createFlow: <TContext, TPatch = unknown, TEvents extends object = BindingEventMap>(definition: FlowDefinition<TContext, TPatch, TEvents>, options: FlowOptions<TContext, TPatch, TEvents>) => Flow<TContext, TPatch>;
368
391
 
392
+ /**
393
+ * @deprecated Use `createWebSocket` instead. This adapter will be removed in a future release.
394
+ */
369
395
  declare const createWS: <TEvents extends object = EventMap>(options: WSOptions<TEvents>) => WS;
370
396
 
397
+ declare const createWebSocket: <TEvents extends object = EventMap>(options: WebSocketOptions<TEvents>) => WSClient;
398
+
371
399
  declare const catchError: <T>(callback: () => T | PromiseLike<T>) => Promise<T>;
372
400
 
373
401
  type ActionsRegistry<TContext, TPatch> = Map<string, Action<TContext, TPatch>>;
374
- declare const createActionsRegistry: <TContext, TPatch>() => ActionsRegistry<TContext, TPatch>;
402
+ declare const BUILTIN_ACTIONS: readonly [name: string, action: Action<unknown, unknown>][];
403
+ declare const BUILTIN_ACTION_NAMES: Set<string>;
375
404
 
376
405
  type ConditionsRegistry<TContext> = Map<string, ConditionFn<TContext>>;
377
- declare const createConditionsRegistry: <TContext>() => ConditionsRegistry<TContext>;
406
+ declare const BUILTIN_CONDITIONS: readonly [name: string, condition: ConditionFn<unknown>][];
407
+ declare const BUILTIN_CONDITION_NAMES: Set<string>;
378
408
 
379
- export { type Action, type ActionArgs, type ActionFail, type ActionResult, type ActionSkip, type ActionStop, type ActionSuccess, type ActionsRegistry, type BindingEventMap, type Bus, type BusBinding, type BusBindingKey, type BusBindings, type BusEmitOptions, type BusErrorEvent, type BusEvent, type BusOptions, type ConcurrencyMode, type ConcurrencyOptions, type ConditionExpression, type ConditionFn, type ConditionsRegistry, type Config, type DomBinding, type DomBindingKey, type DomBindings, type DomForm, type DomInput, type ErrorReporter, type ErrorReporterHandlers, type ErrorStage, type EventHandler, type EventMap, type EventName, type ExpressionOperator, type FetchResponseType, type Flow, type FlowDefinition, type FlowOptions, type InactiveBinding, type Input, type Mode, type Next, type Props, PubSub, type QueueOverflow, type RetryOptions, type RunOptions, type RunResult, type Runner, type RunnerErrorEvent, type RunnerOptions, type Runtime, type SlapError, type SlapErrorEvent, type SlapEvent, type StartResult, type Strategy, type TraceEntry, type TraceSink, type ValidationIssue, type ValidationResult, type VariableValue, type Variables, type WS, type WSOptions, type WSRetryOptions, type WSSocket, type WSStatus, catchError, createActionsRegistry, createConditionsRegistry, createFlow, createMemoryTraceSink, createPubSub, createWS, defineConfig, defineErrorReporter };
409
+ export { type Action, type ActionArgs, type ActionFail, type ActionResult, type ActionSkip, type ActionStop, type ActionSuccess, type ActionsRegistry, BUILTIN_ACTIONS, BUILTIN_ACTION_NAMES, BUILTIN_CONDITIONS, BUILTIN_CONDITION_NAMES, type BindingEventMap, type Bus, type BusBinding, type BusBindingKey, type BusBindings, type BusEmitOptions, type BusErrorEvent, type BusEvent, type BusOptions, type ConcurrencyMode, type ConcurrencyOptions, type ConditionExpression, type ConditionFn, type ConditionsRegistry, type Config, type DomBinding, type DomBindingKey, type DomBindings, type DomForm, type DomInput, type ErrorReporter, type ErrorReporterHandlers, type ErrorStage, type EventHandler, type EventMap, type EventName, type EventPattern, type ExpressionOperator, type FetchResponseType, type Flow, type FlowDefinition, type FlowOptions, type InactiveBinding, type Input, type Mode, type Next, type Props, PubSub, type QueueOverflow, type RetryOptions, type RunOptions, type RunResult, type Runner, type RunnerErrorEvent, type RunnerOptions, type Runtime, type SlapError, type SlapErrorEvent, type SlapEvent, type SocketEventTopic, type StartResult, type Strategy, type TraceEntry, type TraceSink, type ValidationIssue, type ValidationResult, type VariableValue, type Variables, type WS, type WSClient, type WSOptions, type WSRetryOptions, type WSSocket, type WSStatus, type WebSocketOptions, type WebSocketStatus, catchError, createFlow, createMemoryTraceSink, createPubSub, createWS, createWebSocket, defineConfig, defineErrorReporter };