@alwatr/action 9.16.0 → 9.17.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/src/registry.ts CHANGED
@@ -1,38 +1,51 @@
1
+ import type {Action} from './action.js';
2
+
1
3
  // ─── Type Definitions ────────────────────────────────────────────────────────
2
4
 
3
5
  /**
4
- * A modifier handler used in `on-action` attribute syntax.
6
+ * A modifier handler used in `on-<eventType>` attribute syntax.
5
7
  *
6
- * Receives the triggering DOM `event` and the `element` that owns the
7
- * `on-action` attribute. Return `true` (or any truthy value) to allow the
8
- * action to proceed, or `false` to cancel the dispatch.
8
+ * Receives the triggering DOM `event`, the `element` that owns the
9
+ * `on-<eventType>` attribute, and the **mutable** `action` object being built.
10
+ * The handler may mutate `action.meta` to attach cross-cutting data (e.g. a
11
+ * trace ID, a timestamp, or an A/B flag) before the action reaches subscribers.
12
+ *
13
+ * Return `true` (or any truthy value) to allow the action to proceed, or
14
+ * `false` to cancel the dispatch entirely.
9
15
  *
10
16
  * Using explicit parameters instead of `this` binding makes handlers
11
17
  * compatible with arrow functions and easier to test in isolation.
12
18
  *
13
- * @example
19
+ * @example — a modifier that stamps a timestamp into meta
20
+ * ```ts
21
+ * const timestampHandler: ModifierHandler = (_event, _element, action) => {
22
+ * action.meta ??= {};
23
+ * action.meta['timestamp'] = Date.now();
24
+ * return true;
25
+ * };
26
+ * ```
27
+ *
28
+ * @example — a modifier that cancels dispatch when the element is disabled
14
29
  * ```ts
15
- * // A modifier that only allows the action when the element is not disabled
16
30
  * const notDisabledHandler: ModifierHandler = (_event, element) => {
17
31
  * return !(element as HTMLButtonElement).disabled;
18
32
  * };
19
33
  * ```
20
34
  */
21
- export type ModifierHandler = (event: Event, element: HTMLElement) => boolean;
35
+ export type ModifierHandler = (event: Event, element: HTMLElement, action: Action) => boolean;
22
36
 
23
37
  /**
24
- * A payload resolver used in `on-action` attribute syntax.
38
+ * A payload resolver used in `on-<eventType>` attribute syntax.
25
39
  *
26
40
  * Receives the triggering DOM `event` and the `element` that owns the
27
- * `on-action` attribute. The return value becomes the `actionPayload` passed
28
- * to `onAction` subscribers. Use this to compute dynamic payloads from DOM state.
41
+ * `on-<eventType>` attribute. The return value becomes the `payload` field of
42
+ * the `Action` object passed to `onAction` subscribers.
29
43
  *
30
44
  * Using explicit parameters instead of `this` binding makes resolvers
31
45
  * compatible with arrow functions and easier to test in isolation.
32
46
  *
33
- * @example
47
+ * @example — a resolver that returns the element's dataset id
34
48
  * ```ts
35
- * // A resolver that returns the element's dataset id
36
49
  * const dataIdResolver: PayloadResolver = (_event, element) => {
37
50
  * return (element as HTMLElement).dataset.id ?? null;
38
51
  * };
@@ -89,7 +102,7 @@ modifierRegistry.set('prevent', (event) => {
89
102
  * allowing native constraint-validation UI to surface errors. If no form is
90
103
  * found the dispatch is also cancelled.
91
104
  *
92
- * Pair with `.prevent` on `submit` events to avoid page reloads:
105
+ * Pair with `prevent` on `submit` events to avoid page reloads:
93
106
  *
94
107
  * @example `<form on-submit="submit_form:$formdata; prevent,validate" novalidate>`
95
108
  */
@@ -122,8 +135,8 @@ payloadRegistry.set('$value', (_event, element) => {
122
135
  *
123
136
  * @example `<form on-submit="submit_form:$formdata; prevent,validate">`
124
137
  * ```ts
125
- * onAction('submit_form', (data) => {
126
- * console.log(data); // {username: 'ali', password: '…'}
138
+ * onAction('submit_form', (action) => {
139
+ * console.log(action.payload); // {username: 'ali', password: '…'}
127
140
  * });
128
141
  * ```
129
142
  */
@@ -140,8 +153,9 @@ payloadRegistry.set('$formdata', (_event, element) => {
140
153
  *
141
154
  * @example `<input type="checkbox" on-change="toggle_feature:$checked" />`
142
155
  * ```ts
143
- * onAction('toggle_feature', (isChecked) => {
144
- * featureSignal.set(isChecked as boolean);
156
+ * onAction('toggle_feature', (action) => {
157
+ * console.log(action.payload); // true or false
158
+ * featureSignal.set(action.payload);
145
159
  * });
146
160
  * ```
147
161
  */