| 33 | * ``` |
| 34 | */ |
| 35 | export class SvelteURLSearchParams extends URLSearchParams { |
| 36 | #version = DEV ? tag(state(0), 'SvelteURLSearchParams version') : state(0); |
| 37 | #url = get_current_url(); |
| 38 | |
| 39 | #updating = false; |
| 40 | |
| 41 | #update_url() { |
| 42 | if (!this.#url || this.#updating) return; |
| 43 | this.#updating = true; |
| 44 | |
| 45 | const search = this.toString(); |
| 46 | this.#url.search = search && `?${search}`; |
| 47 | |
| 48 | this.#updating = false; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * @param {URLSearchParams} params |
| 53 | * @internal |
| 54 | */ |
| 55 | [REPLACE](params) { |
| 56 | if (this.#updating) return; |
| 57 | |
| 58 | // the URL may have changed in a way that leaves the search string untouched — |
| 59 | // don't rebuild the params or notify readers if nothing changed |
| 60 | if (params.toString() === super.toString()) return; |
| 61 | |
| 62 | this.#updating = true; |
| 63 | |
| 64 | for (const key of [...super.keys()]) { |
| 65 | super.delete(key); |
| 66 | } |
| 67 | |
| 68 | for (const [key, value] of params) { |
| 69 | super.append(key, value); |
| 70 | } |
| 71 | |
| 72 | increment(this.#version); |
| 73 | this.#updating = false; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * @param {string} name |
| 78 | * @param {string} value |
| 79 | * @returns {void} |
| 80 | */ |
| 81 | append(name, value) { |
| 82 | super.append(name, value); |
| 83 | this.#update_url(); |
| 84 | increment(this.#version); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * @param {string} name |
| 89 | * @param {string=} value |
| 90 | * @returns {void} |
| 91 | */ |
| 92 | delete(name, value) { |
nothing calls this directly
no test coverage detected