more efficient initialisation

pull/10803/head
Rich Harris 1 year ago
parent 503384def8
commit 29d4a8078b

@ -1,4 +1,3 @@
import { DEV } from 'esm-env';
import { source, set } from '../internal/client/reactivity/sources.js'; import { source, set } from '../internal/client/reactivity/sources.js';
import { get } from '../internal/client/runtime.js'; import { get } from '../internal/client/runtime.js';
import { UNINITIALIZED } from '../internal/client/constants.js'; import { UNINITIALIZED } from '../internal/client/constants.js';
@ -18,15 +17,14 @@ export class ReactiveMap extends Map {
* @param {Iterable<readonly [K, V]> | null | undefined} [value] * @param {Iterable<readonly [K, V]> | null | undefined} [value]
*/ */
constructor(value) { constructor(value) {
super(); super(value);
// If the value is invalid then the native exception will fire here
if (DEV) new Map(value);
if (value) { if (value) {
for (var [key, v] of value) { for (var [key, v] of value) {
this.set(key, v); this.#sources.set(key, source(v));
} }
this.#size.v = this.#sources.size;
} }
} }
@ -78,7 +76,7 @@ export class ReactiveMap extends Map {
/** /**
* @param {K} key * @param {K} key
* @param {V} value * @param {V} value
* */ */
set(key, value) { set(key, value) {
var sources = this.#sources; var sources = this.#sources;
var source_value = sources.get(key); var source_value = sources.get(key);

@ -1,4 +1,3 @@
import { DEV } from 'esm-env';
import { source, set } from '../internal/client/reactivity/sources.js'; import { source, set } from '../internal/client/reactivity/sources.js';
import { get } from '../internal/client/runtime.js'; import { get } from '../internal/client/runtime.js';
import { make_iterable } from './utils.js'; import { make_iterable } from './utils.js';
@ -21,10 +20,7 @@ export class ReactiveSet extends Set {
* @param {T[] | null} [value] * @param {T[] | null} [value]
*/ */
constructor(value) { constructor(value) {
super(); super(value);
// If the value is invalid then the native exception will fire here
if (DEV) new Set(value);
if (value) { if (value) {
// Support set-like objects that have a keys() method // Support set-like objects that have a keys() method
@ -34,8 +30,10 @@ export class ReactiveSet extends Set {
} }
for (var element of value) { for (var element of value) {
this.add(/** @type {T} */ (element)); this.#sources.set(element, source(true));
} }
this.#size.v = this.#sources.size;
} }
if (!inited) this.#init(); if (!inited) this.#init();

Loading…
Cancel
Save