| 258 | }, |
| 259 | |
| 260 | set(target, prop, value, receiver) { |
| 261 | var s = sources.get(prop); |
| 262 | var has = prop in target; |
| 263 | |
| 264 | // variable.length = value -> clear all signals with index >= value |
| 265 | if (is_proxied_array && prop === 'length') { |
| 266 | for (var i = value; i < /** @type {Source<number>} */ (s).v; i += 1) { |
| 267 | var other_s = sources.get(i + ''); |
| 268 | if (other_s !== undefined) { |
| 269 | set(other_s, UNINITIALIZED); |
| 270 | } else if (i in target) { |
| 271 | // If the item exists in the original, we need to create an uninitialized source, |
| 272 | // else a later read of the property would result in a source being created with |
| 273 | // the value of the original item at that index. |
| 274 | other_s = with_parent(() => source(UNINITIALIZED, stack)); |
| 275 | sources.set(i + '', other_s); |
| 276 | |
| 277 | if (DEV) { |
| 278 | tag(other_s, get_label(path, i)); |
| 279 | } |
| 280 | } |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | // If we haven't yet created a source for this property, we need to ensure |
| 285 | // we do so otherwise if we read it later, then the write won't be tracked and |
| 286 | // the heuristics of effects will be different vs if we had read the proxied |
| 287 | // object property before writing to that property. |
| 288 | if (s === undefined) { |
| 289 | if (!has || get_descriptor(target, prop)?.writable) { |
| 290 | s = with_parent(() => source(undefined, stack)); |
| 291 | |
| 292 | if (DEV) { |
| 293 | tag(s, get_label(path, prop)); |
| 294 | } |
| 295 | set(s, proxy(value)); |
| 296 | |
| 297 | sources.set(prop, s); |
| 298 | } |
| 299 | } else { |
| 300 | has = s.v !== UNINITIALIZED; |
| 301 | |
| 302 | var p = with_parent(() => proxy(value)); |
| 303 | set(s, p); |
| 304 | } |
| 305 | |
| 306 | var descriptor = Reflect.getOwnPropertyDescriptor(target, prop); |
| 307 | |
| 308 | // Set the new value before updating any signals so that any listeners get the new value |
| 309 | if (descriptor?.set) { |
| 310 | descriptor.set.call(receiver, value); |
| 311 | } |
| 312 | |
| 313 | if (!has) { |
| 314 | // If we have mutated an array directly, we might need to |
| 315 | // signal that length has also changed. Do it before updating metadata |
| 316 | // to ensure that iterating over the array as a result of a metadata update |
| 317 | // will not cause the length to be out of sync. |