Skip to content

Commit ea73ad3

Browse files
committed
simplify, make ReactiveURLSearchParams signature match URLSearchParams
1 parent a0645dd commit ea73ad3

File tree

2 files changed

+53
-47
lines changed

2 files changed

+53
-47
lines changed

packages/svelte/src/reactivity/url.js

Lines changed: 29 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { source, set } from '../internal/client/reactivity/sources.js';
22
import { get } from '../internal/client/runtime.js';
33

4-
const UPDATE = Symbol('UPDATE');
5-
const VERSION = Symbol('version');
4+
const REPLACE = Symbol();
65

76
export class ReactiveURL extends URL {
87
#url = {
@@ -16,7 +15,7 @@ export class ReactiveURL extends URL {
1615
hash: source(super.hash)
1716
};
1817

19-
#searchParams = new ReactiveURLSearchParams(super.searchParams, this.#url.search);
18+
#searchParams = new ReactiveURLSearchParams(super.searchParams);
2019

2120
get hash() {
2221
return get(this.#url.hash);
@@ -70,7 +69,7 @@ export class ReactiveURL extends URL {
7069
set(this.#url.pathname, super.pathname);
7170
set(this.#url.search, super.search);
7271
set(this.#url.hash, super.hash);
73-
this.#searchParams[UPDATE](super.searchParams);
72+
this.#searchParams[REPLACE](super.searchParams);
7473
}
7574

7675
get password() {
@@ -110,15 +109,13 @@ export class ReactiveURL extends URL {
110109
}
111110

112111
get search() {
113-
get(this.#url.search);
114-
get(this.#searchParams[VERSION]);
115-
return super.search;
112+
const search = this.#searchParams.toString();
113+
return search ? `?${search}` : '';
116114
}
117115

118116
set search(value) {
119117
super.search = value;
120-
set(this.#url.search, super.search);
121-
this.#searchParams[UPDATE](super.searchParams);
118+
this.#searchParams[REPLACE](super.searchParams);
122119
}
123120

124121
get username() {
@@ -151,36 +148,25 @@ export class ReactiveURL extends URL {
151148
}
152149

153150
export class ReactiveURLSearchParams extends URLSearchParams {
154-
#url_search_params;
155-
#search;
156151
#version = source(0);
157-
[VERSION] = this.#version;
158152

159153
#increment_version() {
160154
set(this.#version, this.#version.v + 1);
161155
}
162156

163-
#update_search() {
164-
set(this.#search, '?' + this.#url_search_params.toString());
165-
}
166-
167157
/**
168-
*
169-
* @param {URLSearchParams} value
158+
* @param {URLSearchParams} params
170159
*/
171-
[UPDATE](value) {
172-
this.#url_search_params = value;
173-
this.#increment_version();
174-
}
160+
[REPLACE](params) {
161+
for (const key of [...super.keys()]) {
162+
super.delete(key);
163+
}
175164

176-
/**
177-
* @param {URLSearchParams} url_search_params
178-
* @param {import('../internal/client/reactivity/types.js').Source<string>} search
179-
*/
180-
constructor(url_search_params, search) {
181-
super();
182-
this.#url_search_params = url_search_params;
183-
this.#search = search;
165+
for (const [key, value] of params) {
166+
super.append(key, value);
167+
}
168+
169+
this.#increment_version();
184170
}
185171

186172
/**
@@ -190,8 +176,7 @@ export class ReactiveURLSearchParams extends URLSearchParams {
190176
*/
191177
append(name, value) {
192178
this.#increment_version();
193-
this.#update_search();
194-
return this.#url_search_params.append(name, value);
179+
return super.append(name, value);
195180
}
196181

197182
/**
@@ -201,8 +186,7 @@ export class ReactiveURLSearchParams extends URLSearchParams {
201186
*/
202187
delete(name, value) {
203188
this.#increment_version();
204-
this.#update_search();
205-
return this.#url_search_params.delete(name, value);
189+
return super.delete(name, value);
206190
}
207191

208192
/**
@@ -211,7 +195,7 @@ export class ReactiveURLSearchParams extends URLSearchParams {
211195
*/
212196
get(name) {
213197
get(this.#version);
214-
return this.#url_search_params.get(name);
198+
return super.get(name);
215199
}
216200

217201
/**
@@ -220,7 +204,7 @@ export class ReactiveURLSearchParams extends URLSearchParams {
220204
*/
221205
getAll(name) {
222206
get(this.#version);
223-
return this.#url_search_params.getAll(name);
207+
return super.getAll(name);
224208
}
225209

226210
/**
@@ -230,12 +214,12 @@ export class ReactiveURLSearchParams extends URLSearchParams {
230214
*/
231215
has(name, value) {
232216
get(this.#version);
233-
return this.#url_search_params.has(name, value);
217+
return super.has(name, value);
234218
}
235219

236220
keys() {
237221
get(this.#version);
238-
return this.#url_search_params.keys();
222+
return super.keys();
239223
}
240224

241225
/**
@@ -245,36 +229,35 @@ export class ReactiveURLSearchParams extends URLSearchParams {
245229
*/
246230
set(name, value) {
247231
this.#increment_version();
248-
this.#update_search();
249-
return this.#url_search_params.set(name, value);
232+
return super.set(name, value);
250233
}
251234

252235
sort() {
253236
this.#increment_version();
254-
this.#update_search();
255-
return this.#url_search_params.sort();
237+
return super.sort();
256238
}
257239

258240
toString() {
259241
get(this.#version);
260-
return this.#url_search_params.toString();
242+
return super.toString();
261243
}
262244

263245
values() {
264246
get(this.#version);
265-
return this.#url_search_params.values();
247+
return super.values();
266248
}
267249

268250
entries() {
269251
get(this.#version);
270-
return this.#url_search_params.entries();
252+
return super.entries();
271253
}
272254

273255
[Symbol.iterator]() {
274256
return this.entries();
275257
}
276258

277259
get size() {
278-
return this.#url_search_params.size;
260+
get(this.#version);
261+
return super.size;
279262
}
280263
}

packages/svelte/src/reactivity/url.test.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { render_effect, effect_root } from '../internal/client/reactivity/effects.js';
22
import { flushSync } from '../index-client.js';
3-
import { ReactiveURL } from './url.js';
3+
import { ReactiveURL, ReactiveURLSearchParams } from './url.js';
44
import { assert, test } from 'vitest';
55

66
test('url.hash', () => {
@@ -76,3 +76,26 @@ test('url.searchParams', () => {
7676

7777
cleanup();
7878
});
79+
80+
test('URLSearchParams', () => {
81+
const params = new ReactiveURLSearchParams();
82+
const log: any = [];
83+
84+
const cleanup = effect_root(() => {
85+
render_effect(() => {
86+
log.push(params.toString());
87+
});
88+
});
89+
90+
flushSync(() => {
91+
params.set('a', 'b');
92+
});
93+
94+
flushSync(() => {
95+
params.append('a', 'c');
96+
});
97+
98+
assert.deepEqual(log, ['', 'a=b', 'a=b&a=c']);
99+
100+
cleanup();
101+
});

0 commit comments

Comments
 (0)