Skip to content

Commit 6c328af

Browse files
committed
fix up
1 parent 77be484 commit 6c328af

File tree

7 files changed

+48
-25
lines changed

7 files changed

+48
-25
lines changed

packages/svelte/messages/client-warnings/warnings.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@
2222
2323
## state_proxy_equality_mismatch
2424

25-
> Detected an equality check between a $state proxy and a non-$state-proxy object for %method%. This equality check will always fail because the proxy has a different object identity. Ensure both operands are of the same kind for accurate results.
25+
> Detected an equality check between a $state proxy and a non-$state-proxy object for %method%. This equality check will always fail because the proxy has a different object identity. To ensure both operands are of the same kind for accurate results, consider using `$state.is(a, b)`.

packages/svelte/src/internal/client/constants.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,3 @@ export const EFFECT_TRANSPARENT = 1 << 14;
1717
export const LEGACY_DERIVED_PROP = 1 << 15;
1818

1919
export const STATE_SYMBOL = Symbol('$state');
20-
export const RAW_SYMBOL = Symbol();

packages/svelte/src/internal/client/dev/equality.js

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { DEV } from 'esm-env';
22
import * as w from '../warnings.js';
3-
import { raw } from '../proxy';
3+
import { get_proxied_value } from '../proxy';
44

55
export function init_array_prototype_warnings() {
66
const array_prototype = Array.prototype;
@@ -10,7 +10,13 @@ export function init_array_prototype_warnings() {
1010
array_prototype.indexOf = function (search_element, from_index) {
1111
const index = original_index_of.call(this, search_element, from_index);
1212
if (index === -1) {
13-
if (original_index_of.call(raw(this), search_element, from_index) !== -1) {
13+
if (
14+
original_index_of.call(
15+
get_proxied_value(this),
16+
get_proxied_value(search_element),
17+
from_index
18+
) !== -1
19+
) {
1420
w.state_proxy_equality_mismatch('Array.indexOf');
1521
}
1622
}
@@ -22,7 +28,13 @@ export function init_array_prototype_warnings() {
2228
array_prototype.lastIndexOf = function (search_element, from_index) {
2329
const index = original_last_index_of.call(this, search_element, from_index);
2430
if (index === -1) {
25-
if (original_last_index_of.call(raw(this), search_element, from_index) !== -1) {
31+
if (
32+
original_last_index_of.call(
33+
get_proxied_value(this),
34+
get_proxied_value(search_element),
35+
from_index
36+
) !== -1
37+
) {
2638
w.state_proxy_equality_mismatch('Array.lastIndexOf');
2739
}
2840
}
@@ -34,7 +46,13 @@ export function init_array_prototype_warnings() {
3446
array_prototype.includes = function (search_element, from_index) {
3547
const has = original_includes.call(this, search_element, from_index);
3648
if (!has) {
37-
if (original_includes.call(raw(this), search_element, from_index)) {
49+
if (
50+
original_includes.call(
51+
get_proxied_value(this),
52+
get_proxied_value(search_element),
53+
from_index
54+
)
55+
) {
3856
w.state_proxy_equality_mismatch('Array.includes');
3957
}
4058
}
@@ -49,7 +67,7 @@ export function init_array_prototype_warnings() {
4967
*/
5068
export function strict_equals(a, b) {
5169
if (DEV) {
52-
if (a !== b && raw(a) === raw(b)) {
70+
if (a !== b && get_proxied_value(a) === get_proxied_value(b)) {
5371
w.state_proxy_equality_mismatch('=== operator');
5472
}
5573
}
@@ -63,7 +81,7 @@ export function strict_equals(a, b) {
6381
*/
6482
export function equals(a, b) {
6583
if (DEV) {
66-
if (a != b && raw(a) == raw(b)) {
84+
if (a != b && get_proxied_value(a) == get_proxied_value(b)) {
6785
w.state_proxy_equality_mismatch('== operator');
6886
}
6987
}

packages/svelte/src/internal/client/dom/elements/bindings/input.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { render_effect, effect } from '../../../reactivity/effects.js';
33
import { stringify } from '../../../render.js';
44
import { listen_to_event_and_reset_event } from './shared.js';
55
import * as e from '../../../errors.js';
6+
import { get_proxied_value, is } from '../../../proxy.js';
67

78
/**
89
* @param {HTMLInputElement} input
@@ -95,10 +96,10 @@ export function bind_group(inputs, group_index, input, get_value, update) {
9596
if (is_checkbox) {
9697
value = value || [];
9798
// @ts-ignore
98-
input.checked = value.includes(input.__value);
99+
input.checked = get_proxied_value(value).includes(get_proxied_value(input.__value));
99100
} else {
100101
// @ts-ignore
101-
input.checked = input.__value === value;
102+
input.checked = is(input.__value, value);
102103
}
103104
});
104105

packages/svelte/src/internal/client/dom/elements/bindings/select.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { effect } from '../../../reactivity/effects.js';
22
import { listen_to_event_and_reset_event } from './shared.js';
33
import { untrack } from '../../../runtime.js';
4-
import { raw } from '../../../proxy.js';
4+
import { is } from '../../../proxy.js';
55

66
/**
77
* Selects the correct option(s) (depending on whether this is a multiple select)
@@ -17,7 +17,7 @@ export function select_option(select, value, mounting) {
1717

1818
for (var option of select.options) {
1919
var option_value = get_option_value(option);
20-
if (raw(option_value) === raw(value)) {
20+
if (is(option_value, value)) {
2121
option.selected = true;
2222
return;
2323
}

packages/svelte/src/internal/client/proxy.js

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
} from './utils.js';
1919
import { check_ownership, widen_ownership } from './dev/ownership.js';
2020
import { mutable_source, source, set } from './reactivity/sources.js';
21-
import { RAW_SYMBOL, STATE_SYMBOL } from './constants.js';
21+
import { STATE_SYMBOL } from './constants.js';
2222
import { UNINITIALIZED } from '../../constants.js';
2323
import * as e from './errors.js';
2424

@@ -199,9 +199,6 @@ const state_proxy_handler = {
199199
if (prop === STATE_SYMBOL) {
200200
return Reflect.get(target, STATE_SYMBOL);
201201
}
202-
if (prop === RAW_SYMBOL) {
203-
return target;
204-
}
205202

206203
/** @type {import('#client').ProxyMetadata} */
207204
const metadata = target[STATE_SYMBOL];
@@ -342,14 +339,22 @@ if (DEV) {
342339
}
343340

344341
/**
345-
* @param {any} x
342+
* @param {any} value
346343
*/
347-
export function raw(x) {
348-
if (x !== null && typeof x === 'object' && STATE_SYMBOL in x) {
349-
var raw_value = x[RAW_SYMBOL];
350-
if (raw_value !== undefined) {
351-
return raw_value;
344+
export function get_proxied_value(value) {
345+
if (value !== null && typeof value === 'object' && STATE_SYMBOL in value) {
346+
var metadata = value[STATE_SYMBOL];
347+
if (metadata) {
348+
return metadata.p;
352349
}
353350
}
354-
return x;
351+
return value;
352+
}
353+
354+
/**
355+
* @param {any} a
356+
* @param {any} b
357+
*/
358+
export function is(a, b) {
359+
return Object.is(get_proxied_value(a), get_proxied_value(b));
355360
}

packages/svelte/src/internal/client/warnings.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,12 @@ export function ownership_invalid_mutation(component, owner) {
7474
}
7575

7676
/**
77-
* Detected an equality check between a $state proxy and a non-$state-proxy object for %method%. This equality check will always fail because the proxy has a different object identity. Ensure both operands are of the same kind for accurate results.
77+
* Detected an equality check between a $state proxy and a non-$state-proxy object for %method%. This equality check will always fail because the proxy has a different object identity. To ensure both operands are of the same kind for accurate results, consider using the `$state.is(a, b)` rune.
7878
* @param {string} method
7979
*/
8080
export function state_proxy_equality_mismatch(method) {
8181
if (DEV) {
82-
console.warn(`%c[svelte] ${"state_proxy_equality_mismatch"}\n%c${`Detected an equality check between a $state proxy and a non-$state-proxy object for ${method}. This equality check will always fail because the proxy has a different object identity. Ensure both operands are of the same kind for accurate results.`}`, bold, normal);
82+
console.warn(`%c[svelte] ${"state_proxy_equality_mismatch"}\n%c${`Detected an equality check between a $state proxy and a non-$state-proxy object for ${method}. This equality check will always fail because the proxy has a different object identity. To ensure both operands are of the same kind for accurate results, consider using the \`$state.is(a, b)\` rune.`}`, bold, normal);
8383
} else {
8484
// TODO print a link to the documentation
8585
console.warn("state_proxy_equality_mismatch");

0 commit comments

Comments
 (0)