fix: create `assignable_proxy` utils to prevent declaring an external const

pull/15073/head
paoloricciuti 8 months ago
parent d7876af34b
commit 82d45a203e

@ -125,24 +125,11 @@ export function ClassBody(node, context) {
let proxied = should_proxy(init, context.state.scope); let proxied = should_proxy(init, context.state.scope);
if (field.kind === 'state' && proxied && options != null) {
let generated = 'state_options';
let i = 0;
while (private_ids.includes(generated)) {
generated = `state_options_${i++}`;
}
private_ids.push(generated);
body.push(b.prop_def(b.private_id(generated), options));
options = b.member(b.this, `#${generated}`);
}
value = value =
field.kind === 'state' field.kind === 'state'
? b.call( ? should_proxy(init, context.state.scope)
'$.state', ? b.call('$.assignable_proxy', init, options)
should_proxy(init, context.state.scope) ? b.call('$.proxy', init, options) : init, : b.call('$.state', init, options)
options
)
: field.kind === 'raw_state' : field.kind === 'raw_state'
? b.call('$.state', init) ? b.call('$.state', init)
: field.kind === 'derived_by' : field.kind === 'derived_by'

@ -127,15 +127,12 @@ export function VariableDeclaration(node, context) {
context.state.scope.get(id.name) context.state.scope.get(id.name)
); );
const proxied = rune === '$state' && should_proxy(value, context.state.scope); const proxied = rune === '$state' && should_proxy(value, context.state.scope);
if (proxied) { const is_state = is_state_source(binding, context.state.analysis);
if (options != null) { if (proxied && is_state) {
const generated = context.state.scope.generate('state_options'); value = b.call('$.assignable_proxy', value, options);
declarations.push(b.declarator(generated, options)); } else if (proxied) {
options = b.id(generated);
}
value = b.call('$.proxy', value, options); value = b.call('$.proxy', value, options);
} } else if (is_state) {
if (is_state_source(binding, context.state.analysis)) {
value = b.call('$.state', value, options); value = b.call('$.state', value, options);
} }
return value; return value;

@ -152,7 +152,7 @@ export {
} from './runtime.js'; } from './runtime.js';
export { validate_binding, validate_each_keys } from './validate.js'; export { validate_binding, validate_each_keys } from './validate.js';
export { raf } from './timing.js'; export { raf } from './timing.js';
export { proxy } from './proxy.js'; export { proxy, assignable_proxy } from './proxy.js';
export { create_custom_element } from './dom/elements/custom-element.js'; export { create_custom_element } from './dom/elements/custom-element.js';
export { export {
child, child,

@ -9,7 +9,7 @@ import {
object_prototype object_prototype
} from '../shared/utils.js'; } from '../shared/utils.js';
import { check_ownership, widen_ownership } from './dev/ownership.js'; import { check_ownership, widen_ownership } from './dev/ownership.js';
import { source, set } from './reactivity/sources.js'; import { source, set, state } from './reactivity/sources.js';
import { STATE_SYMBOL, STATE_SYMBOL_METADATA } from './constants.js'; import { STATE_SYMBOL, STATE_SYMBOL_METADATA } from './constants.js';
import { UNINITIALIZED } from '../../constants.js'; import { UNINITIALIZED } from '../../constants.js';
import * as e from './errors.js'; import * as e from './errors.js';
@ -318,6 +318,17 @@ export function proxy(value, options, parent = null, prev) {
}); });
} }
/**
* @template T
* @param {T} value
* @param {ValueOptions} [options]
* @returns {Source<T>}
*/
export function assignable_proxy(value, options) {
return state(proxy(value, options), options);
}
/** /**
* @param {Source<number>} signal * @param {Source<number>} signal
* @param {1 | -1} [d] * @param {1 | -1} [d]

Loading…
Cancel
Save