Skip to content

Commit 98b46cb

Browse files
committed
make use:action reactive
1 parent da1fefc commit 98b46cb

File tree

2 files changed

+16
-19
lines changed

2 files changed

+16
-19
lines changed

packages/svelte/src/compiler/phases/3-transform/client/visitors/UseDirective.js

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,19 @@ import { parse_directive_name } from './shared/utils.js';
99
* @param {ComponentContext} context
1010
*/
1111
export function UseDirective(node, context) {
12-
const params = [b.id('$$node')];
13-
14-
if (node.expression) {
15-
params.push(b.id('$$action_arg'));
12+
let action = /** @type {Expression} */ (context.visit(parse_directive_name(node.name)));
13+
if (action.type === 'MemberExpression') {
14+
action = b.maybe_call(b.member(action, 'bind', false, true), /** @type {Expression} */ (action.object));
1615
}
1716

18-
/** @type {Expression[]} */
19-
const args = [
20-
context.state.node,
21-
b.arrow(
22-
params,
23-
b.call(/** @type {Expression} */ (context.visit(parse_directive_name(node.name))), ...params)
24-
)
25-
];
26-
27-
if (node.expression) {
28-
args.push(b.thunk(/** @type {Expression} */ (context.visit(node.expression))));
29-
}
17+
const get_action = b.arrow([], action);
18+
const get_value = node.expression
19+
? b.thunk(/** @type {Expression} */ (context.visit(node.expression)))
20+
: undefined;
3021

3122
// actions need to run after attribute updates in order with bindings/events
32-
context.state.after_update.push(b.stmt(b.call('$.action', ...args)));
23+
context.state.after_update.push(
24+
b.stmt(b.call('$.action', context.state.node, get_action, get_value))
25+
);
3326
context.next();
3427
}

packages/svelte/src/internal/client/dom/elements/actions.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,16 @@ import { deep_read_state, untrack } from '../../runtime.js';
66
/**
77
* @template P
88
* @param {Element} dom
9-
* @param {(dom: Element, value?: P) => ActionPayload<P>} action
9+
* @param {() => (((dom: Element, value?: P) => ActionPayload<P>) | null | undefined)} get_action
1010
* @param {() => P} [get_value]
1111
* @returns {void}
1212
*/
13-
export function action(dom, action, get_value) {
13+
export function action(dom, get_action, get_value) {
1414
effect(() => {
15+
const action = get_action();
16+
if (action == null) {
17+
return;
18+
}
1519
var payload = untrack(() => action(dom, get_value?.()) || {});
1620

1721
if (get_value && payload?.update) {

0 commit comments

Comments
 (0)