10000 Merge remote-tracking branch 'origin/from-action-remove-interface' in… · sveltejs/svelte@1d4aa8a · GitHub
[go: up one dir, main page]

Skip to content

Commit 1d4aa8a

Browse files
committed
Merge remote-tracking branch 'origin/from-action-remove-interface' into from-action
2 parents d190509 + 7c2a49e commit 1d4aa8a

File tree

2 files changed

+31
-25
lines changed

2 files changed

+31
-25
lines changed

packages/svelte/src/attachments/index.js

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
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' */
53
import { noop, render_effect } from 'svelte/internal/client';
64
import { ATTACHMENT_KEY } from '../constants.js';
75
import { untrack } from 'svelte';
@@ -34,35 +32,41 @@ export function createAttachmentKey() {
3432
}
3533

3634
/**
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>}
5047
*/
51-
5248
/**
5349
* Converts an Action into an Attachment keeping the same behavior. It's useful if you want to start using
5450
* 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>}
5660
* @since 5.32
5761
*/
58-
export function fromAction(action, /** @type {() => any} */ get_arg = noop) {
62+
export function fromAction(action, fn = /** @type {() => T} */ (noop)) {
5963
return (element) => {
60-
const { update, destroy } = untrack(() => action(element, get_arg()) ?? {});
64+
const { update, destroy } = untrack(() => action(element, fn()) ?? {});
6165

6266
if (update) {
6367
var ran = false;
6468
render_effect(() => {
65-
const arg = get_arg();
69+
const arg = fn();
6670
if (ran) update(arg);
6771
});
6872
ran = true;

packages/svelte/types/index.d.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,7 @@ declare module 'svelte/animate' {
625625
}
626626

627627
declare module 'svelte/attachments' {
628-
import type { ActionReturn } from 'svelte/action';
628+
import type { ActionReturn, Action } from 'svelte/action';
629629
/**
630630
* An [attachment](https://svelte.dev/docs/svelte/@attach) is a function that runs when an element is mounted
631631
* to the DOM, and optionally returns a function that is called when the element is later removed.
@@ -659,8 +659,10 @@ declare module 'svelte/attachments' {
659659
* @since 5.29
660660
*/
661661
export function createAttachmentKey(): symbol;
662-
export function fromAction<Node extends HTMLElement, Parameter extends unknown>(...args: undefined extends NoInfer<Parameter> ? [action: (node: Node, parameter?: never) => void | ActionReturn<Parameter, any>, parameter?: (() => NoInfer<Parameter>) | undefined] : [action: (node: Node, parameter: Parameter) => void | ActionReturn<Parameter, any>, parameter: () => NoInfer<Parameter>]): Attachment<Node>;
663-
export type FromAction<Element extends EventTarget = HTMLElement, Par extends unknown = unknown> = <Node extends Element, Parameter extends Par>(...args: (undefined extends NoInfer<Parameter> ? [action: (node: Node, parameter?: never) => void | ActionReturn<Parameter>, parameter?: () => NoInfer<Parameter>] : [action: (node: Node, parameter: Parameter) => void | ActionReturn<Parameter>, parameter: () => NoInfer<Parameter>])) => Attachment<Node>;
662+
663+
export function fromAction<E extends EventTarget, T extends unknown>(action: Action<E, T> | ((arg0: E, arg1: T) => void | ActionReturn<T>), fn: () => T): Attachment<E>;
664+
665+
export function fromAction<E extends EventTarget>(action: Action<E, void> | ((arg0: E) => void | ActionReturn<void>)): Attachment<E>;
664666

665667
export {};
666668
}

0 commit comments

Comments
 (0)
0