Skip to content

Commit 2459461

Browse files
committed
improved reactivity for search-params
1 parent 474155b commit 2459461

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

packages/svelte/src/reactivity/url-search-params.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,33 @@ export const ReactiveURLSearchParams = make_reactive(URLSearchParams, {
55
read_properties: ['get', 'has', 'getAll'],
66
interceptors: {
77
set: (options, ...params) => {
8-
if (typeof params[0] == 'string' && options.value.get(params[0]) === params[1]) {
8+
const value = options.value.get(/**@type {string} */ (params[0]));
9+
const value_has_changed = value !== /**@type {string} */ (params[1]).toString();
10+
if (value && !value_has_changed) {
911
return false;
1012
}
11-
options.notify_read_properties(['get', 'has', 'getAll'], params[0]);
13+
14+
if (!value) {
15+
options.notify_read_properties(['has'], params[0]);
16+
}
17+
18+
if (value_has_changed) {
19+
options.notify_read_properties(['get'], params[0]);
20+
}
21+
22+
options.notify_read_properties(['getAll'], params[0]);
1223
return true;
1324
},
1425
append: (options, ...params) => {
1526
options.notify_read_properties(['getAll'], params[0]);
1627

17-
if (typeof params[0] == 'string' && !options.value.has(params[0])) {
28+
if (!options.value.has(/**@type {string} */ (params[0]))) {
1829
options.notify_read_properties(['get', 'has'], params[0]);
1930
}
2031
return true;
2132
},
2233
delete: (options, ...params) => {
23-
if (typeof params[0] == 'string' && !options.value.has(params[0])) {
34+
if (!options.value.has(/**@type {string} */ (params[0]))) {
2435
return false;
2536
}
2637
options.notify_read_properties(['get', 'has', 'getAll'], params[0]);

packages/svelte/src/reactivity/url-search-params.test.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ test('URLSearchParams.get', () => {
9898
render_effect(() => {
9999
log.push(params.get('c'));
100100
});
101+
render_effect(() => {
102+
log.push(params.get('e'));
103+
});
101104
});
102105

103106
flushSync(() => {
@@ -112,7 +115,11 @@ test('URLSearchParams.get', () => {
112115
params.delete('a');
113116
});
114117

115-
assert.deepEqual(log, ['b', 'd', 'new-b', null]);
118+
flushSync(() => {
119+
params.set('q', 'f');
120+
});
121+
122+
assert.deepEqual(log, ['b', 'd', null, 'new-b', null]);
116123

117124
cleanup();
118125
});

0 commit comments

Comments
 (0)