|
1 |
| -/** |
2 |
| - * @import { Attachment } from "./public.js"; |
3 |
| - * @import { ActionReturn } from "svelte/action"; |
4 |
| - */ |
| 1 | +/** @import { Action, ActionReturn } from 'svelte/action' */ |
| 2 | +/** @import { Attachment } from 'svelte/attachments' */ |
5 | 3 | import { noop, render_effect } from 'svelte/internal/client';
|
6 | 4 | import { ATTACHMENT_KEY } from '../constants.js';
|
7 | 5 | import { untrack } from 'svelte';
|
@@ -34,35 +32,41 @@ export function createAttachmentKey() {
|
34 | 32 | }
|
35 | 33 |
|
36 | 34 | /**
|
37 |
| - * @template {EventTarget} [Element=HTMLElement] |
38 |
| - * @template {*} [Par=unknown] |
39 |
| - * @typedef {<Node extends Element, Parameter extends Par>( |
40 |
| - * ...args: (undefined extends NoInfer<Parameter> |
41 |
| - * ? [ |
42 |
| - * action: (node: Node, parameter?: never) => void | ActionReturn<Parameter>, |
43 |
| - * parameter?: () => NoInfer<Parameter> |
44 |
| - * ] |
45 |
| - * : [ |
46 |
| - * action: (node: Node, parameter: Parameter) => void | ActionReturn<Parameter>, |
47 |
| - * parameter: () => NoInfer<Parameter> |
48 |
| - * ]) |
49 |
| - * ) => Attachment<Node>} FromAction |
| 35 | + * @template {EventTarget} E |
| 36 | + * @template {unknown} T |
| 37 | + * @overload |
| 38 | + * @param {Action<E, T> | function(E, T): void | ActionReturn<T>} action The action function |
| 39 | + * @param {() => T} fn A function that returns the argument for the action |
| 40 | + * @returns {Attachment<E>} |
| 41 | + */ |
| 42 | +/** |
| 43 | + * @template {EventTarget} E |
| 44 | + * @overload |
| 45 | + * @param {Action<E, void> | function(E): void | ActionReturn<void>} action The action function |
| 46 | + * @returns {Attachment<E>} |
50 | 47 | */
|
51 |
| - |
52 | 48 | /**
|
53 | 49 | * Converts an Action into an Attachment keeping the same behavior. It's useful if you want to start using
|
54 | 50 | * attachments on Components but you have library provided actions.
|
55 |
| - * @type {FromAction} |
| 51 | + * |
| 52 | + * Note that the second argument, if provided, must be a function that _returns_ the argument to the |
| 53 | + * action function, not the argument itself. |
| 54 | + * |
| 55 | + * @template {EventTarget} E |
| 56 | + * @template {unknown} T |
| 57 | + * @param {Action<E, T> | function(E, T): void | ActionReturn<T>} action The action function |
| 58 | + * @param {() => T} fn A function that returns the argument for the action |
| 59 | + * @returns {Attachment<E>} |
56 | 60 | * @since 5.32
|
57 | 61 | */
|
58 |
| -export function fromAction(action, /** @type {() => any} */ get_arg = noop) { |
| 62 | +export function fromAction(action, fn = /** @type {() => T} */ (noop)) { |
59 | 63 | return (element) => {
|
60 |
| - const { update, destroy } = untrack(() => action(element, get_arg()) ?? {}); |
| 64 | + const { update, destroy } = untrack(() => action(element, fn()) ?? {}); |
61 | 65 |
|
62 | 66 | if (update) {
|
63 | 67 | var ran = false;
|
64 | 68 | render_effect(() => {
|
65 |
| - const arg = get_arg(); |
| 69 | + const arg = fn(); |
66 | 70 | if (ran) update(arg);
|
67 | 71 | });
|
68 | 72 | ran = true;
|
|
0 commit comments