From 4cdc3710d02e2c968801995113e6604cfdd5db29 Mon Sep 17 00:00:00 2001 From: Dominic Gannaway Date: Thu, 13 Jun 2024 13:05:24 +0100 Subject: [PATCH] chore: tweak Map/Set for better inspect output (#12013) * chore: tweak Map/Set for better inspect output * chore: tweak Map/Set for better inspect output * chore: tweak Map/Set for better inspect output * chore: tweak Map/Set for better inspect output * chore: tweak Map/Set for better inspect output * changeset --------- Co-authored-by: Rich Harris --- .changeset/eleven-hounds-pump.md | 5 +++++ packages/svelte/src/reactivity/map.js | 19 ++++++++++--------- packages/svelte/src/reactivity/set.js | 21 +++++++++++---------- 3 files changed, 26 insertions(+), 19 deletions(-) create mode 100644 .changeset/eleven-hounds-pump.md diff --git a/.changeset/eleven-hounds-pump.md b/.changeset/eleven-hounds-pump.md new file mode 100644 index 0000000000..bcd106e61f --- /dev/null +++ b/.changeset/eleven-hounds-pump.md @@ -0,0 +1,5 @@ +--- +"svelte": patch +--- + +chore: clear `Map`/`Set` before triggering `$inspect` callbacks diff --git a/packages/svelte/src/reactivity/map.js b/packages/svelte/src/reactivity/map.js index 6c5899af32..da495eef0b 100644 --- a/packages/svelte/src/reactivity/map.js +++ b/packages/svelte/src/reactivity/map.js @@ -122,17 +122,18 @@ export class ReactiveMap extends Map { } clear() { - var sources = this.#sources; - - if (super.size !== 0) { - set(this.#size, 0); - for (var s of sources.values()) { - set(s, -1); - } - increment(this.#version); - sources.clear(); + if (super.size === 0) { + return; } + // Clear first, so we get nice console.log outputs with $inspect super.clear(); + var sources = this.#sources; + set(this.#size, 0); + for (var s of sources.values()) { + set(s, -1); + } + increment(this.#version); + sources.clear(); } #read_all() { diff --git a/packages/svelte/src/reactivity/set.js b/packages/svelte/src/reactivity/set.js index 01d4d5e903..7ccb7f9b1a 100644 --- a/packages/svelte/src/reactivity/set.js +++ b/packages/svelte/src/reactivity/set.js @@ -117,19 +117,20 @@ export class ReactiveSet extends Set { } clear() { - if (super.size !== 0) { - var sources = this.#sources; - - for (var s of sources.values()) { - set(s, false); - } + if (super.size === 0) { + return; + } + // Clear first, so we get nice console.log outputs with $inspect + super.clear(); + var sources = this.#sources; - sources.clear(); - set(this.#size, 0); - increment(this.#version); + for (var s of sources.values()) { + set(s, false); } - super.clear(); + sources.clear(); + set(this.#size, 0); + increment(this.#version); } keys() {