From 09fb4974db75b33e20ba8b1498b639e22b93696b Mon Sep 17 00:00:00 2001 From: Ignatius Bagus Date: Tue, 11 Jan 2022 18:05:10 +0700 Subject: [PATCH 01/11] action interfaces --- src/runtime/action/index.ts | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 src/runtime/action/index.ts diff --git a/src/runtime/action/index.ts b/src/runtime/action/index.ts new file mode 100644 index 00000000000..d5c303a888d --- /dev/null +++ b/src/runtime/action/index.ts @@ -0,0 +1,8 @@ +export interface ActionReturn { + update?: (parameters: Parameters) => void; + destroy?: () => void; +} + +export interface Action { + (node: HTMLElement, parameters: Parameters): ActionReturn; +} From 87469bd1e87f4295901eb891da614ad50d3bd55a Mon Sep 17 00:00:00 2001 From: Ignatius Bagus Date: Tue, 11 Jan 2022 18:07:56 +0700 Subject: [PATCH 02/11] ignore build output --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 228136cbc8c..aeded324ae5 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,7 @@ node_modules /compiler.*js /index.*js /ssr.*js +/action /internal /store /easing From aaf0bf44dea4c44a7c6cfc40877bc87c0c513286 Mon Sep 17 00:00:00 2001 From: Ignatius Bagus Date: Tue, 11 Jan 2022 18:31:29 +0700 Subject: [PATCH 03/11] allow void return --- src/runtime/action/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/runtime/action/index.ts b/src/runtime/action/index.ts index d5c303a888d..fe182b16d5f 100644 --- a/src/runtime/action/index.ts +++ b/src/runtime/action/index.ts @@ -4,5 +4,5 @@ export interface ActionReturn { } export interface Action { - (node: HTMLElement, parameters: Parameters): ActionReturn; + (node: HTMLElement, parameters: Parameters): void | ActionReturn; } From f66e2c3e365de9f7256251dec76b369a0303f806 Mon Sep 17 00:00:00 2001 From: Ignatius Bagus Date: Tue, 11 Jan 2022 18:40:57 +0700 Subject: [PATCH 04/11] package exports map --- package.json | 3 +++ 1 file changed, 3 insertions(+) diff --git a/package.json b/package.json index 7d36c109714..b1e6cc56eb7 100644 --- a/package.json +++ b/package.json @@ -39,6 +39,9 @@ "import": "./compiler.mjs", "require": "./compiler.js" }, + "./action": { + "types": "./types/runtime/action/index.d.ts" + }, "./animate": { "types": "./types/runtime/animate/index.d.ts", "import": "./animate/index.mjs", From e31a7cdf5281fae34dd8e1b49397c5f6b87ec0ab Mon Sep 17 00:00:00 2001 From: Ignatius Bagus Date: Wed, 12 Jan 2022 11:35:14 +0700 Subject: [PATCH 05/11] make parameters optional, allow empty `use:action` --- src/runtime/action/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/runtime/action/index.ts b/src/runtime/action/index.ts index fe182b16d5f..f712265c87e 100644 --- a/src/runtime/action/index.ts +++ b/src/runtime/action/index.ts @@ -4,5 +4,5 @@ export interface ActionReturn { } export interface Action { - (node: HTMLElement, parameters: Parameters): void | ActionReturn; + (node: HTMLElement, parameters?: Parameters): void | ActionReturn; } From 336872dfd7b3f4758b8ce485b79f89c05a319cf5 Mon Sep 17 00:00:00 2001 From: Ignatius Bagus Date: Wed, 12 Jan 2022 11:38:25 +0700 Subject: [PATCH 06/11] allow Element to be inferred and constrained --- src/runtime/action/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/runtime/action/index.ts b/src/runtime/action/index.ts index f712265c87e..32925085055 100644 --- a/src/runtime/action/index.ts +++ b/src/runtime/action/index.ts @@ -3,6 +3,6 @@ export interface ActionReturn { destroy?: () => void; } -export interface Action { - (node: HTMLElement, parameters?: Parameters): void | ActionReturn; +export interface Action { + (node: Node, parameters?: Parameters): void | ActionReturn; } From c7adb027434ddeca2593562d7779932304747be5 Mon Sep 17 00:00:00 2001 From: Ignatius Bagus Date: Wed, 12 Jan 2022 15:46:54 +0700 Subject: [PATCH 07/11] make parameters in update optional too --- src/runtime/action/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/runtime/action/index.ts b/src/runtime/action/index.ts index 32925085055..97e7ca3da0c 100644 --- a/src/runtime/action/index.ts +++ b/src/runtime/action/index.ts @@ -1,5 +1,5 @@ export interface ActionReturn { - update?: (parameters: Parameters) => void; + update?: (parameters?: Parameters) => void; destroy?: () => void; } From 1614bdc31679b0e5d506e16f5ab6d3f4f2e62732 Mon Sep 17 00:00:00 2001 From: Ignatius Bagus Date: Wed, 12 Jan 2022 15:56:27 +0700 Subject: [PATCH 08/11] revert optional parameters in update the parameters of callback provided to update can simply be empty to represent an optional value, restricting with the optional mark prevents authors from immediately destructuring because it can be undefined --- src/runtime/action/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/runtime/action/index.ts b/src/runtime/action/index.ts index 97e7ca3da0c..32925085055 100644 --- a/src/runtime/action/index.ts +++ b/src/runtime/action/index.ts @@ -1,5 +1,5 @@ export interface ActionReturn { - update?: (parameters?: Parameters) => void; + update?: (parameters: Parameters) => void; destroy?: () => void; } From ae01d6acaaffd4b5aa889fd569a7afa26d684357 Mon Sep 17 00:00:00 2001 From: Ignatius Bagus Date: Fri, 11 Feb 2022 18:43:54 +0700 Subject: [PATCH 09/11] reverse the order of generics --- src/runtime/action/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/runtime/action/index.ts b/src/runtime/action/index.ts index 32925085055..d170e0589ef 100644 --- a/src/runtime/action/index.ts +++ b/src/runtime/action/index.ts @@ -3,6 +3,6 @@ export interface ActionReturn { destroy?: () => void; } -export interface Action { +export interface Action { (node: Node, parameters?: Parameters): void | ActionReturn; } From 2372b6e74e799e96a265fd819d5565325e595e26 Mon Sep 17 00:00:00 2001 From: Ignatius Bagus Date: Wed, 16 Feb 2022 17:43:52 +0700 Subject: [PATCH 10/11] actually only 1 and avoid conflict with TS utility --- src/runtime/action/index.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/runtime/action/index.ts b/src/runtime/action/index.ts index d170e0589ef..0ac5bd0a350 100644 --- a/src/runtime/action/index.ts +++ b/src/runtime/action/index.ts @@ -1,8 +1,8 @@ -export interface ActionReturn { - update?: (parameters: Parameters) => void; +export interface ActionReturn { + update?: (parameter: Parameter) => void; destroy?: () => void; } -export interface Action { - (node: Node, parameters?: Parameters): void | ActionReturn; +export interface Action { + (node: Node, parameter?: Parameter): void | ActionReturn; } From 5ac1868da677a526cf8a30b4b30892e3cdd3704b Mon Sep 17 00:00:00 2001 From: Simon H <5968653+dummdidumm@users.noreply.github.com> Date: Sat, 26 Feb 2022 19:32:51 +0100 Subject: [PATCH 11/11] docs --- src/runtime/action/index.ts | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/runtime/action/index.ts b/src/runtime/action/index.ts index 0ac5bd0a350..6d1d3941393 100644 --- a/src/runtime/action/index.ts +++ b/src/runtime/action/index.ts @@ -1,8 +1,42 @@ +/** + * Actions can return an object containing the two properties defined in this interface. Both are optional. + * - update: An action can have a parameter. This method will be called whenever that parameter changes, + * immediately after Svelte has applied updates to the markup. + * - destroy: Method that is called after the element is unmounted + * + * Example usage: + * ```ts + * export function myAction(node: HTMLElement, paramater: Parameter): ActionReturn { + * // ... + * return { + * update: (updatedParameter) => {...}, + * destroy: () => {...} + * }; + * } + * ``` + * + * Docs: https://svelte.dev/docs#template-syntax-element-directives-use-action + */ export interface ActionReturn { update?: (parameter: Parameter) => void; destroy?: () => void; } +/** + * Actions are functions that are called when an element is created. + * You can use this interface to type such actions. + * The following example defines an action that only works on `
` elements + * and optionally accepts a parameter which it has a default value for: + * ```ts + * export const myAction: Action = (node, param = { someProperty: true }) => { + * // ... + * } + * ``` + * You can return an object with methods `update` and `destroy` from the function. + * See interface `ActionReturn` for more details. + * + * Docs: https://svelte.dev/docs#template-syntax-element-directives-use-action + */ export interface Action { (node: Node, parameter?: Parameter): void | ActionReturn; }