altcha
Version:
Privacy-first CAPTCHA widget, compliant with global regulations (GDPR/HIPAA/CCPA/LGDP/DPDPA/PIPL) and WCAG accessible. No tracking, self-verifying.
1,664 lines • 337 kB
JavaScript
(function(factory) {
typeof define === "function" && define.amd ? define(factory) : factory();
})((function() {
"use strict";
const DEV = false;
var is_array = Array.isArray;
var index_of = Array.prototype.indexOf;
var includes = Array.prototype.includes;
var array_from = Array.from;
var object_keys = Object.keys;
var define_property = Object.defineProperty;
var get_descriptor = Object.getOwnPropertyDescriptor;
var get_descriptors = Object.getOwnPropertyDescriptors;
var object_prototype = Object.prototype;
var array_prototype = Array.prototype;
var get_prototype_of = Object.getPrototypeOf;
var is_extensible = Object.isExtensible;
const noop = () => {
};
function run_all(arr) {
for (var i = 0; i < arr.length; i++) {
arr[i]();
}
}
function deferred() {
var resolve;
var reject;
var promise = new Promise((res, rej) => {
resolve = res;
reject = rej;
});
return { promise, resolve, reject };
}
const DERIVED = 1 << 1;
const EFFECT = 1 << 2;
const RENDER_EFFECT = 1 << 3;
const MANAGED_EFFECT = 1 << 24;
const BLOCK_EFFECT = 1 << 4;
const BRANCH_EFFECT = 1 << 5;
const ROOT_EFFECT = 1 << 6;
const BOUNDARY_EFFECT = 1 << 7;
const CONNECTED = 1 << 9;
const CLEAN = 1 << 10;
const DIRTY = 1 << 11;
const MAYBE_DIRTY = 1 << 12;
const INERT = 1 << 13;
const DESTROYED = 1 << 14;
const REACTION_RAN = 1 << 15;
const DESTROYING = 1 << 25;
const EFFECT_TRANSPARENT = 1 << 16;
const EAGER_EFFECT = 1 << 17;
const HEAD_EFFECT = 1 << 18;
const EFFECT_PRESERVED = 1 << 19;
const USER_EFFECT = 1 << 20;
const WAS_MARKED = 1 << 16;
const REACTION_IS_UPDATING = 1 << 21;
const ASYNC = 1 << 22;
const ERROR_VALUE = 1 << 23;
const STATE_SYMBOL = /* @__PURE__ */ Symbol("$state");
const LEGACY_PROPS = /* @__PURE__ */ Symbol("legacy props");
const LOADING_ATTR_SYMBOL = /* @__PURE__ */ Symbol("");
const ATTRIBUTES_CACHE = /* @__PURE__ */ Symbol("attributes");
const CLASS_CACHE = /* @__PURE__ */ Symbol("class");
const STYLE_CACHE = /* @__PURE__ */ Symbol("style");
const TEXT_CACHE = /* @__PURE__ */ Symbol("text");
const FORM_RESET_HANDLER = /* @__PURE__ */ Symbol("form reset");
const STALE_REACTION = new class StaleReactionError extends Error {
name = "StaleReactionError";
message = "The reaction that called `getAbortSignal()` was re-run or destroyed";
}();
const IS_XHTML = (
// We gotta write it like this because after downleveling the pure comment may end up in the wrong location
!!globalThis.document?.contentType && /* @__PURE__ */ globalThis.document.contentType.includes("xml")
);
const TEXT_NODE = 3;
const COMMENT_NODE = 8;
function equals(value) {
return value === this.v;
}
function safe_not_equal(a, b) {
return a != a ? b == b : a !== b || a !== null && typeof a === "object" || typeof a === "function";
}
function safe_equals(value) {
return !safe_not_equal(value, this.v);
}
function lifecycle_outside_component(name) {
{
throw new Error(`https://svelte.dev/e/lifecycle_outside_component`);
}
}
function async_derived_orphan() {
{
throw new Error(`https://svelte.dev/e/async_derived_orphan`);
}
}
function effect_in_teardown(rune) {
{
throw new Error(`https://svelte.dev/e/effect_in_teardown`);
}
}
function effect_in_unowned_derived() {
{
throw new Error(`https://svelte.dev/e/effect_in_unowned_derived`);
}
}
function effect_orphan(rune) {
{
throw new Error(`https://svelte.dev/e/effect_orphan`);
}
}
function effect_update_depth_exceeded() {
{
throw new Error(`https://svelte.dev/e/effect_update_depth_exceeded`);
}
}
function hydration_failed() {
{
throw new Error(`https://svelte.dev/e/hydration_failed`);
}
}
function state_descriptors_fixed() {
{
throw new Error(`https://svelte.dev/e/state_descriptors_fixed`);
}
}
function state_prototype_fixed() {
{
throw new Error(`https://svelte.dev/e/state_prototype_fixed`);
}
}
function state_unsafe_mutation() {
{
throw new Error(`https://svelte.dev/e/state_unsafe_mutation`);
}
}
function svelte_boundary_reset_onerror() {
{
throw new Error(`https://svelte.dev/e/svelte_boundary_reset_onerror`);
}
}
let tracing_mode_flag = false;
const TEMPLATE_FRAGMENT = 1;
const TEMPLATE_USE_IMPORT_NODE = 1 << 1;
const HYDRATION_START = "[";
const HYDRATION_START_ELSE = "[!";
const HYDRATION_START_FAILED = "[?";
const HYDRATION_END = "]";
const HYDRATION_ERROR = {};
const UNINITIALIZED = /* @__PURE__ */ Symbol("uninitialized");
const NAMESPACE_HTML = "http://www.w3.org/1999/xhtml";
const NAMESPACE_SVG = "http://www.w3.org/2000/svg";
const NAMESPACE_MATHML = "http://www.w3.org/1998/Math/MathML";
const ATTACHMENT_KEY = "@attach";
let component_context = null;
function set_component_context(context) {
component_context = context;
}
function push(props, runes = false, fn) {
component_context = {
p: component_context,
i: false,
c: null,
e: null,
s: props,
x: null,
r: (
/** @type {Effect} */
active_effect
),
l: null
};
}
function pop(component2) {
var context = (
/** @type {ComponentContext} */
component_context
);
var effects = context.e;
if (effects !== null) {
context.e = null;
for (var fn of effects) {
create_user_effect(fn);
}
}
if (component2 !== void 0) {
context.x = component2;
}
context.i = true;
component_context = context.p;
return component2 ?? /** @type {T} */
{};
}
function is_runes() {
return true;
}
let micro_tasks = [];
function run_micro_tasks() {
var tasks = micro_tasks;
micro_tasks = [];
run_all(tasks);
}
function queue_micro_task(fn) {
if (micro_tasks.length === 0 && !is_flushing_sync) {
var tasks = micro_tasks;
queueMicrotask(() => {
if (tasks === micro_tasks) run_micro_tasks();
});
}
micro_tasks.push(fn);
}
function flush_tasks() {
while (micro_tasks.length > 0) {
run_micro_tasks();
}
}
function derived_inert() {
{
console.warn(`https://svelte.dev/e/derived_inert`);
}
}
function hydration_mismatch(location2) {
{
console.warn(`https://svelte.dev/e/hydration_mismatch`);
}
}
function select_multiple_invalid_value() {
{
console.warn(`https://svelte.dev/e/select_multiple_invalid_value`);
}
}
function svelte_boundary_reset_noop() {
{
console.warn(`https://svelte.dev/e/svelte_boundary_reset_noop`);
}
}
let hydrating = false;
function set_hydrating(value) {
hydrating = value;
}
let hydrate_node;
function set_hydrate_node(node) {
if (node === null) {
hydration_mismatch();
throw HYDRATION_ERROR;
}
return hydrate_node = node;
}
function hydrate_next() {
return set_hydrate_node(/* @__PURE__ */ get_next_sibling(hydrate_node));
}
function reset(node) {
if (!hydrating) return;
if (/* @__PURE__ */ get_next_sibling(hydrate_node) !== null) {
hydration_mismatch();
throw HYDRATION_ERROR;
}
hydrate_node = node;
}
function next(count = 1) {
if (hydrating) {
var i = count;
var node = hydrate_node;
while (i--) {
node = /** @type {TemplateNode} */
/* @__PURE__ */ get_next_sibling(node);
}
hydrate_node = node;
}
}
function skip_nodes(remove = true) {
var depth = 0;
var node = hydrate_node;
while (true) {
if (node.nodeType === COMMENT_NODE) {
var data = (
/** @type {Comment} */
node.data
);
if (data === HYDRATION_END) {
if (depth === 0) return node;
depth -= 1;
} else if (data === HYDRATION_START || data === HYDRATION_START_ELSE || // "[1", "[2", etc. for if blocks
data[0] === "[" && !isNaN(Number(data.slice(1)))) {
depth += 1;
}
}
var next2 = (
/** @type {TemplateNode} */
/* @__PURE__ */ get_next_sibling(node)
);
if (remove) node.remove();
node = next2;
}
}
function read_hydration_instruction(node) {
if (!node || node.nodeType !== COMMENT_NODE) {
hydration_mismatch();
throw HYDRATION_ERROR;
}
return (
/** @type {Comment} */
node.data
);
}
function proxy(value) {
if (typeof value !== "object" || value === null || STATE_SYMBOL in value) {
return value;
}
const prototype = get_prototype_of(value);
if (prototype !== object_prototype && prototype !== array_prototype) {
return value;
}
var sources = /* @__PURE__ */ new Map();
var is_proxied_array = is_array(value);
var version = /* @__PURE__ */ state(0);
var parent_version = update_version;
var with_parent = (fn) => {
if (update_version === parent_version) {
return fn();
}
var reaction = active_reaction;
var version2 = update_version;
set_active_reaction(null);
set_update_version(parent_version);
var result = fn();
set_active_reaction(reaction);
set_update_version(version2);
return result;
};
if (is_proxied_array) {
sources.set("length", /* @__PURE__ */ state(
/** @type {any[]} */
value.length
));
}
return new Proxy(
/** @type {any} */
value,
{
defineProperty(_, prop2, descriptor) {
if (!("value" in descriptor) || descriptor.configurable === false || descriptor.enumerable === false || descriptor.writable === false) {
state_descriptors_fixed();
}
var s = sources.get(prop2);
if (s === void 0) {
with_parent(() => {
var s2 = /* @__PURE__ */ state(descriptor.value);
sources.set(prop2, s2);
return s2;
});
} else {
set(s, descriptor.value, true);
}
return true;
},
deleteProperty(target, prop2) {
var s = sources.get(prop2);
if (s === void 0) {
if (prop2 in target) {
const s2 = with_parent(() => /* @__PURE__ */ state(UNINITIALIZED));
sources.set(prop2, s2);
increment(version);
}
} else {
set(s, UNINITIALIZED);
increment(version);
}
return true;
},
get(target, prop2, receiver) {
if (prop2 === STATE_SYMBOL) {
return value;
}
var s = sources.get(prop2);
var exists = prop2 in target;
if (s === void 0 && (!exists || get_descriptor(target, prop2)?.writable)) {
s = with_parent(() => {
var p = proxy(exists ? target[prop2] : UNINITIALIZED);
var s2 = /* @__PURE__ */ state(p);
return s2;
});
sources.set(prop2, s);
}
if (s !== void 0) {
var v = get(s);
return v === UNINITIALIZED ? void 0 : v;
}
return Reflect.get(target, prop2, receiver);
},
getOwnPropertyDescriptor(target, prop2) {
var descriptor = Reflect.getOwnPropertyDescriptor(target, prop2);
if (descriptor && "value" in descriptor) {
var s = sources.get(prop2);
if (s) descriptor.value = get(s);
} else if (descriptor === void 0) {
var source2 = sources.get(prop2);
var value2 = source2?.v;
if (source2 !== void 0 && value2 !== UNINITIALIZED) {
return {
enumerable: true,
configurable: true,
value: value2,
writable: true
};
}
}
return descriptor;
},
has(target, prop2) {
if (prop2 === STATE_SYMBOL) {
return true;
}
var s = sources.get(prop2);
var has = s !== void 0 && s.v !== UNINITIALIZED || Reflect.has(target, prop2);
if (s !== void 0 || active_effect !== null && (!has || get_descriptor(target, prop2)?.writable)) {
if (s === void 0) {
s = with_parent(() => {
var p = has ? proxy(target[prop2]) : UNINITIALIZED;
var s2 = /* @__PURE__ */ state(p);
return s2;
});
sources.set(prop2, s);
}
var value2 = get(s);
if (value2 === UNINITIALIZED) {
return false;
}
}
return has;
},
set(target, prop2, value2, receiver) {
var s = sources.get(prop2);
var has = prop2 in target;
if (is_proxied_array && prop2 === "length") {
for (var i = value2; i < /** @type {Source<number>} */
s.v; i += 1) {
var other_s = sources.get(i + "");
if (other_s !== void 0) {
set(other_s, UNINITIALIZED);
} else if (i in target) {
other_s = with_parent(() => /* @__PURE__ */ state(UNINITIALIZED));
sources.set(i + "", other_s);
}
}
}
if (s === void 0) {
if (!has || get_descriptor(target, prop2)?.writable) {
s = with_parent(() => /* @__PURE__ */ state(void 0));
set(s, proxy(value2));
sources.set(prop2, s);
}
} else {
has = s.v !== UNINITIALIZED;
var p = with_parent(() => proxy(value2));
set(s, p);
}
var descriptor = Reflect.getOwnPropertyDescriptor(target, prop2);
if (descriptor?.set) {
descriptor.set.call(receiver, value2);
}
if (!has) {
if (is_proxied_array && typeof prop2 === "string") {
var ls = (
/** @type {Source<number>} */
sources.get("length")
);
var n = Number(prop2);
if (Number.isInteger(n) && n >= ls.v) {
set(ls, n + 1);
}
}
increment(version);
}
return true;
},
ownKeys(target) {
get(version);
var own_keys = Reflect.ownKeys(target).filter((key3) => {
var source3 = sources.get(key3);
return source3 === void 0 || source3.v !== UNINITIALIZED;
});
for (var [key2, source2] of sources) {
if (source2.v !== UNINITIALIZED && !(key2 in target)) {
own_keys.push(key2);
}
}
return own_keys;
},
setPrototypeOf() {
state_prototype_fixed();
}
}
);
}
function get_proxied_value(value) {
try {
if (value !== null && typeof value === "object" && STATE_SYMBOL in value) {
return value[STATE_SYMBOL];
}
} catch {
}
return value;
}
function is(a, b) {
return Object.is(get_proxied_value(a), get_proxied_value(b));
}
var $window;
var $document;
var is_firefox;
var first_child_getter;
var next_sibling_getter;
function init_operations() {
if ($window !== void 0) {
return;
}
$window = window;
$document = document;
is_firefox = /Firefox/.test(navigator.userAgent);
var element_prototype = Element.prototype;
var node_prototype = Node.prototype;
var text_prototype = Text.prototype;
first_child_getter = get_descriptor(node_prototype, "firstChild").get;
next_sibling_getter = get_descriptor(node_prototype, "nextSibling").get;
if (is_extensible(element_prototype)) {
element_prototype[CLASS_CACHE] = void 0;
element_prototype[ATTRIBUTES_CACHE] = null;
element_prototype[STYLE_CACHE] = void 0;
element_prototype.__e = void 0;
}
if (is_extensible(text_prototype)) {
text_prototype[TEXT_CACHE] = void 0;
}
}
function create_text(value = "") {
return document.createTextNode(value);
}
// @__NO_SIDE_EFFECTS__
function get_first_child(node) {
return (
/** @type {TemplateNode | null} */
first_child_getter.call(node)
);
}
// @__NO_SIDE_EFFECTS__
function get_next_sibling(node) {
return (
/** @type {TemplateNode | null} */
next_sibling_getter.call(node)
);
}
function child(node, is_text) {
if (!hydrating) {
return /* @__PURE__ */ get_first_child(node);
}
var child2 = /* @__PURE__ */ get_first_child(hydrate_node);
if (child2 === null) {
child2 = hydrate_node.appendChild(create_text());
} else if (is_text && child2.nodeType !== TEXT_NODE) {
var text2 = create_text();
child2?.before(text2);
set_hydrate_node(text2);
return text2;
}
if (is_text) {
merge_text_nodes(
/** @type {Text} */
child2
);
}
set_hydrate_node(child2);
return child2;
}
function first_child(node, is_text = false) {
if (!hydrating) {
var first = /* @__PURE__ */ get_first_child(node);
if (first instanceof Comment && first.data === "") return /* @__PURE__ */ get_next_sibling(first);
return first;
}
if (is_text) {
if (hydrate_node?.nodeType !== TEXT_NODE) {
var text2 = create_text();
hydrate_node?.before(text2);
set_hydrate_node(text2);
return text2;
}
merge_text_nodes(
/** @type {Text} */
hydrate_node
);
}
return hydrate_node;
}
function sibling(node, count = 1, is_text = false) {
let next_sibling = hydrating ? hydrate_node : node;
var last_sibling;
while (count--) {
last_sibling = next_sibling;
next_sibling = /** @type {TemplateNode} */
/* @__PURE__ */ get_next_sibling(next_sibling);
}
if (!hydrating) {
return next_sibling;
}
if (is_text) {
if (next_sibling?.nodeType !== TEXT_NODE) {
var text2 = create_text();
if (next_sibling === null) {
last_sibling?.after(text2);
} else {
next_sibling.before(text2);
}
set_hydrate_node(text2);
return text2;
}
merge_text_nodes(
/** @type {Text} */
next_sibling
);
}
set_hydrate_node(next_sibling);
return next_sibling;
}
function clear_text_content(node) {
node.textContent = "";
}
function should_defer_append() {
return false;
}
function create_element(tag, namespace, is2) {
let options = void 0;
return (
/** @type {T extends keyof HTMLElementTagNameMap ? HTMLElementTagNameMap[T] : Element} */
document.createElementNS(namespace ?? NAMESPACE_HTML, tag, options)
);
}
function merge_text_nodes(text2) {
if (
/** @type {string} */
text2.nodeValue.length < 65536
) {
return;
}
let next2 = text2.nextSibling;
while (next2 !== null && next2.nodeType === TEXT_NODE) {
next2.remove();
text2.nodeValue += /** @type {string} */
next2.nodeValue;
next2 = text2.nextSibling;
}
}
function handle_error(error) {
var effect2 = active_effect;
if (effect2 === null) {
active_reaction.f |= ERROR_VALUE;
return error;
}
if ((effect2.f & REACTION_RAN) === 0 && (effect2.f & EFFECT) === 0) {
throw error;
}
invoke_error_boundary(error, effect2);
}
function invoke_error_boundary(error, effect2) {
while (effect2 !== null) {
if ((effect2.f & BOUNDARY_EFFECT) !== 0) {
if ((effect2.f & REACTION_RAN) === 0) {
throw error;
}
try {
effect2.b.error(error);
return;
} catch (e) {
error = e;
}
}
effect2 = effect2.parent;
}
throw error;
}
const STATUS_MASK = -7169;
function set_signal_status(signal, status) {
signal.f = signal.f & STATUS_MASK | status;
}
function update_derived_status(derived2) {
if ((derived2.f & CONNECTED) !== 0 || derived2.deps === null) {
set_signal_status(derived2, CLEAN);
} else {
set_signal_status(derived2, MAYBE_DIRTY);
}
}
function clear_marked(deps) {
if (deps === null) return;
for (const dep of deps) {
if ((dep.f & DERIVED) === 0 || (dep.f & WAS_MARKED) === 0) {
continue;
}
dep.f ^= WAS_MARKED;
clear_marked(
/** @type {Derived} */
dep.deps
);
}
}
function defer_effect(effect2, dirty_effects, maybe_dirty_effects) {
if ((effect2.f & DIRTY) !== 0) {
dirty_effects.add(effect2);
} else if ((effect2.f & MAYBE_DIRTY) !== 0) {
maybe_dirty_effects.add(effect2);
}
clear_marked(effect2.deps);
set_signal_status(effect2, CLEAN);
}
function subscribe_to_store(store2, run, invalidate) {
if (store2 == null) {
run(void 0);
return noop;
}
const unsub = untrack(
() => store2.subscribe(
run,
// @ts-expect-error
invalidate
)
);
return unsub.unsubscribe ? () => unsub.unsubscribe() : unsub;
}
const subscriber_queue = [];
function writable(value, start = noop) {
let stop = null;
const subscribers = /* @__PURE__ */ new Set();
function set2(new_value) {
if (safe_not_equal(value, new_value)) {
value = new_value;
if (stop) {
const run_queue = !subscriber_queue.length;
for (const subscriber of subscribers) {
subscriber[1]();
subscriber_queue.push(subscriber, value);
}
if (run_queue) {
for (let i = 0; i < subscriber_queue.length; i += 2) {
subscriber_queue[i][0](subscriber_queue[i + 1]);
}
subscriber_queue.length = 0;
}
}
}
}
function update(fn) {
set2(fn(
/** @type {T} */
value
));
}
function subscribe(run, invalidate = noop) {
const subscriber = [run, invalidate];
subscribers.add(subscriber);
if (subscribers.size === 1) {
stop = start(set2, update) || noop;
}
run(
/** @type {T} */
value
);
return () => {
subscribers.delete(subscriber);
if (subscribers.size === 0 && stop) {
stop();
stop = null;
}
};
}
return { set: set2, update, subscribe };
}
function get$1(store2) {
let value;
subscribe_to_store(store2, (_) => value = _)();
return value;
}
let IS_UNMOUNTED = /* @__PURE__ */ Symbol("unmounted");
function store_get(store2, store_name, stores) {
const entry = stores[store_name] ??= {
store: null,
source: /* @__PURE__ */ mutable_source(void 0),
unsubscribe: noop
};
if (entry.store !== store2 && !(IS_UNMOUNTED in stores)) {
entry.unsubscribe();
entry.store = store2 ?? null;
if (store2 == null) {
entry.source.v = void 0;
entry.unsubscribe = noop;
} else {
var is_synchronous_callback = true;
entry.unsubscribe = subscribe_to_store(store2, (v) => {
if (is_synchronous_callback) {
entry.source.v = v;
} else {
set(entry.source, v);
}
});
is_synchronous_callback = false;
}
}
if (store2 && IS_UNMOUNTED in stores) {
return get$1(store2);
}
return get(entry.source);
}
function setup_stores() {
const stores = {};
function cleanup() {
teardown(() => {
for (var store_name in stores) {
const ref = stores[store_name];
ref.unsubscribe();
}
define_property(stores, IS_UNMOUNTED, {
enumerable: false,
value: true
});
});
}
return [stores, cleanup];
}
let first_batch = null;
let last_batch = null;
let current_batch = null;
let previous_batch = null;
let batch_values = null;
let last_scheduled_effect = null;
let is_flushing_sync = false;
let is_processing = false;
let collected_effects = null;
let legacy_updates = null;
var flush_count = 0;
var source_stacks = /* @__PURE__ */ new Set();
let uid = 1;
class Batch {
id = uid++;
/** True as soon as `#process` was called */
#started = false;
linked = true;
/** @type {Batch | null} */
#prev = null;
/** @type {Batch | null} */
#next = null;
/** @type {Map<Effect, ReturnType<typeof deferred<any>>>} */
async_deriveds = /* @__PURE__ */ new Map();
/**
* The current values of any signals that are updated in this batch.
* Tuple format: [value, is_derived] (note: is_derived is false for deriveds, too, if they were overridden via assignment)
* They keys of this map are identical to `this.#previous`
* @type {Map<Value, [any, boolean]>}
*/
current = /* @__PURE__ */ new Map();
/**
* The values of any signals (sources and deriveds) that are updated in this batch _before_ those updates took place.
* They keys of this map are identical to `this.#current`
* @type {Map<Value, any>}
*/
previous = /* @__PURE__ */ new Map();
/**
* Async effects which this batch doesn't take into account anymore when calculating blockers,
* as it has a value for it already.
* @type {Set<Effect>}
*/
unblocked = /* @__PURE__ */ new Set();
/**
* When the batch is committed (and the DOM is updated), we need to remove old branches
* and append new ones by calling the functions added inside (if/each/key/etc) blocks
* @type {Set<(batch: Batch) => void>}
*/
#commit_callbacks = /* @__PURE__ */ new Set();
/**
* If a fork is discarded, we need to destroy any effects that are no longer needed
* @type {Set<(batch: Batch) => void>}
*/
#discard_callbacks = /* @__PURE__ */ new Set();
/**
* Callbacks that should run only when a fork is committed.
* @type {Set<(batch: Batch) => void>}
*/
#fork_commit_callbacks = /* @__PURE__ */ new Set();
/**
* The number of async effects that are currently in flight
*/
#pending = 0;
/**
* Async effects that are currently in flight, _not_ inside a pending boundary
* @type {Map<Effect, number>}
*/
#blocking_pending = /* @__PURE__ */ new Map();
/**
* A deferred that resolves when the batch is committed, used with `settled()`
* TODO replace with Promise.withResolvers once supported widely enough
* @type {{ promise: Promise<void>, resolve: (value?: any) => void, reject: (reason: unknown) => void } | null}
*/
#deferred = null;
/**
* The root effects that need to be flushed
* @type {Effect[]}
*/
#roots = [];
/**
* Effects created while this batch was active.
* @type {Effect[]}
*/
#new_effects = [];
/**
* Deferred effects (which run after async work has completed) that are DIRTY
* @type {Set<Effect>}
*/
#dirty_effects = /* @__PURE__ */ new Set();
/**
* Deferred effects that are MAYBE_DIRTY
* @type {Set<Effect>}
*/
#maybe_dirty_effects = /* @__PURE__ */ new Set();
/**
* A map of branches that still exist, but will be destroyed when this batch
* is committed — we skip over these during `process`.
* The value contains child effects that were dirty/maybe_dirty before being reset,
* so they can be rescheduled if the branch survives.
* @type {Map<Effect, { d: Effect[], m: Effect[] }>}
*/
#skipped_branches = /* @__PURE__ */ new Map();
/**
* Inverse of #skipped_branches which we need to tell prior batches to unskip them when committing
* @type {Set<Effect>}
*/
#unskipped_branches = /* @__PURE__ */ new Set();
is_fork = false;
#decrement_queued = false;
#is_deferred() {
if (this.is_fork) return true;
for (const effect2 of this.#blocking_pending.keys()) {
var e = effect2;
var skipped = false;
while (e.parent !== null) {
if (this.#skipped_branches.has(e)) {
skipped = true;
break;
}
e = e.parent;
}
if (!skipped) {
return true;
}
}
return false;
}
/**
* Add an effect to the #skipped_branches map and reset its children
* @param {Effect} effect
*/
skip_effect(effect2) {
if (!this.#skipped_branches.has(effect2)) {
this.#skipped_branches.set(effect2, { d: [], m: [] });
}
this.#unskipped_branches.delete(effect2);
}
/**
* Remove an effect from the #skipped_branches map and reschedule
* any tracked dirty/maybe_dirty child effects
* @param {Effect} effect
* @param {(e: Effect) => void} callback
*/
unskip_effect(effect2, callback = (e) => this.schedule(e)) {
var tracked = this.#skipped_branches.get(effect2);
if (tracked) {
this.#skipped_branches.delete(effect2);
for (var e of tracked.d) {
set_signal_status(e, DIRTY);
callback(e);
}
for (e of tracked.m) {
set_signal_status(e, MAYBE_DIRTY);
callback(e);
}
}
this.#unskipped_branches.add(effect2);
}
#process() {
this.#started = true;
if (flush_count++ > 1e3) {
this.#unlink();
infinite_loop_guard();
}
if (!this.#is_deferred()) {
for (const e of this.#dirty_effects) {
this.#maybe_dirty_effects.delete(e);
set_signal_status(e, DIRTY);
this.schedule(e);
}
for (const e of this.#maybe_dirty_effects) {
set_signal_status(e, MAYBE_DIRTY);
this.schedule(e);
}
}
const roots = this.#roots;
this.#roots = [];
this.apply();
var effects = collected_effects = [];
var render_effects = [];
var updates = legacy_updates = [];
for (const root2 of roots) {
try {
this.#traverse(root2, effects, render_effects);
} catch (e) {
reset_all(root2);
throw e;
}
}
current_batch = null;
if (updates.length > 0) {
var batch = Batch.ensure();
for (const e of updates) {
batch.schedule(e);
}
}
collected_effects = null;
legacy_updates = null;
if (this.#is_deferred()) {
this.#defer_effects(render_effects);
this.#defer_effects(effects);
for (const [e, t] of this.#skipped_branches) {
reset_branch(e, t);
}
if (updates.length > 0) {
/** @type {unknown} */
current_batch.#process();
}
return;
}
const earlier_batch = this.#find_earlier_batch();
if (earlier_batch) {
earlier_batch.#merge(this);
return;
}
this.#dirty_effects.clear();
this.#maybe_dirty_effects.clear();
for (const fn of this.#commit_callbacks) fn(this);
this.#commit_callbacks.clear();
previous_batch = this;
flush_queued_effects(render_effects);
flush_queued_effects(effects);
previous_batch = null;
this.#deferred?.resolve();
var next_batch = (
/** @type {Batch | null} */
/** @type {unknown} */
current_batch
);
if (this.linked && this.#pending === 0) {
this.#unlink();
}
if (this.#roots.length > 0) {
if (next_batch === null) {
next_batch = this;
this.#link();
}
const batch2 = next_batch;
batch2.#roots.push(...this.#roots.filter((r2) => !batch2.#roots.includes(r2)));
}
if (next_batch !== null) {
next_batch.#process();
}
}
/**
* Traverse the effect tree, executing effects or stashing
* them for later execution as appropriate
* @param {Effect} root
* @param {Effect[]} effects
* @param {Effect[]} render_effects
*/
#traverse(root2, effects, render_effects) {
root2.f ^= CLEAN;
var effect2 = root2.first;
while (effect2 !== null) {
var flags2 = effect2.f;
var is_branch = (flags2 & (BRANCH_EFFECT | ROOT_EFFECT)) !== 0;
var is_skippable_branch = is_branch && (flags2 & CLEAN) !== 0;
var skip = is_skippable_branch || (flags2 & INERT) !== 0 || this.#skipped_branches.has(effect2);
if (!skip && effect2.fn !== null) {
if (is_branch) {
effect2.f ^= CLEAN;
} else if ((flags2 & EFFECT) !== 0) {
effects.push(effect2);
} else if (is_dirty(effect2)) {
if ((flags2 & BLOCK_EFFECT) !== 0) this.#maybe_dirty_effects.add(effect2);
update_effect(effect2);
}
var child2 = effect2.first;
if (child2 !== null) {
effect2 = child2;
continue;
}
}
while (effect2 !== null) {
var next2 = effect2.next;
if (next2 !== null) {
effect2 = next2;
break;
}
effect2 = effect2.parent;
}
}
}
#find_earlier_batch() {
var batch = this.#prev;
while (batch !== null) {
if (!batch.is_fork) {
for (const [value, [, is_derived]] of this.current) {
if (batch.current.has(value) && !is_derived) {
return batch;
}
}
}
batch = batch.#prev;
}
return null;
}
/**
* @param {Batch} batch
*/
#merge(batch) {
for (const [source2, value] of batch.current) {
if (!this.previous.has(source2) && batch.previous.has(source2)) {
this.previous.set(source2, batch.previous.get(source2));
}
this.current.set(source2, value);
}
for (const [effect2, deferred2] of batch.async_deriveds) {
const d = this.async_deriveds.get(effect2);
if (d) deferred2.promise.then(d.resolve);
}
const mark = (value) => {
var reactions = value.reactions;
if (reactions === null) return;
for (const reaction of reactions) {
var flags2 = reaction.f;
if ((flags2 & DERIVED) !== 0) {
mark(
/** @type {Derived} */
reaction
);
} else {
var effect2 = (
/** @type {Effect} */
reaction
);
if (flags2 & (ASYNC | BLOCK_EFFECT) && !this.async_deriveds.has(effect2)) {
this.#maybe_dirty_effects.delete(effect2);
set_signal_status(effect2, DIRTY);
this.schedule(effect2);
}
}
}
};
for (const source2 of this.current.keys()) {
mark(source2);
}
this.oncommit(() => batch.discard());
batch.#unlink();
current_batch = this;
this.#process();
}
/**
* @param {Effect[]} effects
*/
#defer_effects(effects) {
for (var i = 0; i < effects.length; i += 1) {
defer_effect(effects[i], this.#dirty_effects, this.#maybe_dirty_effects);
}
}
/**
* Associate a change to a given source with the current
* batch, noting its previous and current values
* @param {Value} source
* @param {any} value
* @param {boolean} [is_derived]
*/
capture(source2, value, is_derived = false) {
if (source2.v !== UNINITIALIZED && !this.previous.has(source2)) {
this.previous.set(source2, source2.v);
}
if ((source2.f & ERROR_VALUE) === 0) {
this.current.set(source2, [value, is_derived]);
batch_values?.set(source2, value);
}
if (!this.is_fork) {
source2.v = value;
}
}
activate() {
current_batch = this;
}
deactivate() {
current_batch = null;
batch_values = null;
}
flush() {
try {
if (DEV) ;
is_processing = true;
current_batch = this;
this.#process();
} finally {
flush_count = 0;
last_scheduled_effect = null;
collected_effects = null;
legacy_updates = null;
is_processing = false;
current_batch = null;
batch_values = null;
old_values.clear();
}
}
discard() {
for (const fn of this.#discard_callbacks) fn(this);
this.#discard_callbacks.clear();
this.#fork_commit_callbacks.clear();
this.#unlink();
}
/**
* @param {Effect} effect
*/
register_created_effect(effect2) {
this.#new_effects.push(effect2);
}
#commit() {
this.#unlink();
for (let batch = first_batch; batch !== null; batch = batch.#next) {
var is_earlier = batch.id < this.id;
var sources = [];
for (const [source3, [value, is_derived]] of this.current) {
if (batch.current.has(source3)) {
var batch_value = (
/** @type {[any, boolean]} */
batch.current.get(source3)[0]
);
if (is_earlier && value !== batch_value) {
batch.current.set(source3, [value, is_derived]);
} else {
continue;
}
}
sources.push(source3);
}
if (is_earlier) {
for (const [effect2, deferred2] of this.async_deriveds) {
const d = batch.async_deriveds.get(effect2);
if (d) deferred2.promise.then(d.resolve);
}
}
if (!batch.#started) continue;
var others = [...batch.current.keys()].filter((s) => !this.current.has(s));
if (others.length === 0) {
if (is_earlier) {
batch.discard();
}
} else if (sources.length > 0) {
if (is_earlier) {
for (const unskipped of this.#unskipped_branches) {
batch.unskip_effect(unskipped, (e) => {
if ((e.f & (BLOCK_EFFECT | ASYNC)) !== 0) {
batch.schedule(e);
} else {
batch.#defer_effects([e]);
}
});
}
}
batch.activate();
var marked = /* @__PURE__ */ new Set();
var checked = /* @__PURE__ */ new Map();
for (var source2 of sources) {
mark_effects(source2, others, marked, checked);
}
checked = /* @__PURE__ */ new Map();
var current_unequal = [...batch.current.keys()].filter(
(c) => this.current.has(c) ? (
/** @type {[any, boolean]} */
this.current.get(c)[0] !== c.v
) : true
);
if (current_unequal.length > 0) {
for (const effect2 of this.#new_effects) {
if ((effect2.f & (DESTROYED | INERT | EAGER_EFFECT)) === 0 && depends_on(effect2, current_unequal, checked)) {
if ((effect2.f & (ASYNC | BLOCK_EFFECT)) !== 0) {
set_signal_status(effect2, DIRTY);
batch.schedule(effect2);
} else {
batch.#dirty_effects.add(effect2);
}
}
}
}
if (batch.#roots.length > 0) {
batch.apply();
for (var root2 of batch.#roots) {
batch.#traverse(root2, [], []);
}
batch.#roots = [];
}
batch.deactivate();
}
}
}
/**
* @param {boolean} blocking
* @param {Effect} effect
*/
increment(blocking, effect2) {
this.#pending += 1;
if (blocking) {
let blocking_pending_count = this.#blocking_pending.get(effect2) ?? 0;
this.#blocking_pending.set(effect2, blocking_pending_count + 1);
}
}
/**
* @param {boolean} blocking
* @param {Effect} effect
*/
decrement(blocking, effect2) {
this.#pending -= 1;
if (blocking) {
let blocking_pending_count = this.#blocking_pending.get(effect2) ?? 0;
if (blocking_pending_count === 1) {
this.#blocking_pending.delete(effect2);
} else {
this.#blocking_pending.set(effect2, blocking_pending_count - 1);
}
}
if (this.#decrement_queued) return;
this.#decrement_queued = true;
queue_micro_task(() => {
this.#decrement_queued = false;
if (this.linked) {
this.flush();
}
});
}
/**
* @param {Set<Effect>} dirty_effects
* @param {Set<Effect>} maybe_dirty_effects
*/
transfer_effects(dirty_effects, maybe_dirty_effects) {
for (const e of dirty_effects) {
this.#dirty_effects.add(e);
}
for (const e of maybe_dirty_effects) {
this.#maybe_dirty_effects.add(e);
}
dirty_effects.clear();
maybe_dirty_effects.clear();
}
/** @param {(batch: Batch) => void} fn */
oncommit(fn) {
this.#commit_callbacks.add(fn);
}
/** @param {(batch: Batch) => void} fn */
ondiscard(fn) {
this.#discard_callbacks.add(fn);
}
/** @param {(batch: Batch) => void} fn */
on_fork_commit(fn) {
this.#fork_commit_callbacks.add(fn);
}
run_fork_commit_callbacks() {
for (const fn of this.#fork_commit_callbacks) fn(this);
this.#fork_commit_callbacks.clear();
}
settled() {
return (this.#deferred ??= deferred()).promise;
}
static ensure() {
if (current_batch === null) {
const batch = current_batch = new Batch();
batch.#link();
if (!is_processing && !is_flushing_sync) {
queue_micro_task(() => {
if (!batch.#started) {
batch.flush();
}
});
}
}
return current_batch;
}
apply() {
{
batch_values = null;
return;
}
}
/**
*
* @param {Effect} effect
*/
schedule(effect2) {
last_scheduled_effect = effect2;
if (effect2.b?.is_pending && (effect2.f & (EFFECT | RENDER_EFFECT | MANAGED_EFFECT)) !== 0 && (effect2.f & REACTION_RAN) === 0) {
effect2.b.defer_effect(effect2);
return;
}
var e = effect2;
while (e.parent !== null) {
e = e.parent;
var flags2 = e.f;
if (collected_effects !== null && e === active_effect) {
if ((active_reaction === null || (active_reaction.f & DERIVED) === 0) && true) {
return;
}
}
if ((flags2 & (ROOT_EFFECT | BRANCH_EFFECT)) !== 0) {
if ((flags2 & CLEAN) === 0) {
return;
}
e.f ^= CLEAN;
}
}
this.#roots.push(e);
}
#link() {
if (last_batch === null) {
first_batch = last_batch = this;
} else {
last_batch.#next = this;
this.#prev = last_batch;
}
last_batch = this;
}
#unlink() {
var prev = this.#prev;
var next2 = this.#next;
if (prev === null) {
first_batch = next2;
} else {
prev.#next = next2;
}
if (next2 === null) {
last_batch = prev;
} else {
next2.#prev = prev;
}
this.linked = false;
}
}
function flushSync(fn) {
var was_flushing_sync = is_flushing_sync;
is_flushing_sync = true;
try {
var result;
if (fn) ;
while (true) {
flush_tasks();
if (current_batch === null) {
return (
/** @type {T} */
result
);
}
current_batch.flush();
}
} finally {
is_flushing_sync = was_flushing_sync;
}
}
function infinite_loop_guard() {
try {
effect_update_depth_exceeded();
} catch (error) {
invoke_error_boundary(error, last_scheduled_effect);
}
}
let eager_block_effects = null;
function flush_queued_effects(effects) {
var length = effects.length;
if (length === 0) return;
var i = 0;
while (i < length) {
var effect2 = effects[i++];
if ((effect2.f & (DESTROYED | INERT)) === 0 && is_dirty(effect2)) {
eager_block_effects = /* @__PURE__ */ new Set();
update_effect(effect2);
if (effect2.deps === null && effect2.first === null && effect2.nodes === null && effect2.teardown === null && effect2.ac === null) {
unlink_effect(effect2);
}
if (eager_block_effects?.size > 0) {
old_values.clear();
for (const e of eager_block_effects) {
if ((e.f & (DESTROYED | INERT)) !== 0) continue;
const ordered_effects = [e];
let ancestor = e.parent;
while (ancestor !== null) {
if (eager_block_effects.has(ancestor)) {
eager_block_effects.delete(ancestor);
ordered_effects.push(ancestor);
}
ancestor = ancestor.parent;
}
for (let j = ordered_effects.length - 1; j >= 0; j--) {
const e2 = ordered_effects[j];
if ((e2.f & (DESTROYED | INERT)) !== 0) continue;
update_effect(e2);
}
}
eager_block_effects.clear();
}
}
}
eager_block_effects = null;
}
function mark_effects(value, sources, marked, checked) {
if (marked.has(value)) return;
marked.add(value);
if (value.reactions !== null) {
for (const reaction of value.reactions) {
const flags2 = reaction.f;
if ((flags2 & DERIVED) !== 0) {
mark_effects(
/** @type {Derived} */
reaction,
sources,
marked,
checked
);
} else if ((flags2 & (ASYNC | BLOCK_EFFECT)) !== 0 && (flags2 & DIRTY) === 0 && depends_on(reaction, sources, checked)) {
set_signal_status(reaction, DIRTY);
schedule_effect(
/** @type {Effect} */
reaction
);
}
}
}
}
function depends_on(reaction, sources, checked) {
const depends = checked.get(reaction);
if (depends !== void 0) return depends;
if (reaction.deps !== null) {
for (const dep of reaction.deps) {
if (includes.call(sources, dep)) {
return true;
}
if ((dep.f & DERIVED) !== 0 && depends_on(
/** @type {Derived} */
dep,
sources,
checked
)) {
checked.set(
/** @type {Derived} */
dep,
true
);
return true;
}
}
}
checked.set(reaction, false);
return false;
}
function schedule_effect(effect2) {
current_batch.schedule(effect2);
}
function reset_branch(effect2, tracked) {
if ((effect2.f & BRANCH_EFFECT) !== 0 && (effect2.f & CLEAN) !== 0) {
return;
}
if ((effect2.f & DIRTY) !== 0) {
tracked.d.push(effect2);
} else if ((effect2.f & MAYBE_DIRTY) !== 0) {
tracked.m.push(effect2);
}
set_signal_status(effect2, CLEAN);
var e = effect2.first;
while (e !== null) {
reset_branch(e, tracked);
e = e.next;
}
}
function reset_all(effect2) {
set_signal_status(effect2, CLEAN);
var e = effect2.first;
while (e !== null) {
reset_all(e);
e = e.next;
}
}
function createSubscriber(start) {
let subscribers = 0;
let version = source(0);
let stop;
return () => {
if (effect_tracking()) {
get(version);
render_effect(() => {
if (subscribers === 0) {
stop = untrack(() => start(() => increment(version)));
}
subscribers += 1;
return () => {
queue_micro_task(() => {
subscribers -= 1;
if (subscribers === 0) {
stop?.();
stop = void 0;
increment(version);
}
});
};
});
}
};
}
var flags = EFFECT_TRANSPARENT | EFFECT_PRESERVED;
function boundary(node, props, children, transform_error) {
new Boundary(node, props, children, transform_error);
}
class Boundary {
/** @type {Boundary | null} */
parent;
is_pending = false;
/**
* API-level transformError transform function. Transforms errors before they reach the `failed` snippet.
* Inherited from parent boundary, or defaults to identity.
* @type {(error: unknown) => unknown}
*/
transform_error;
/** @type {TemplateNode} */
#anchor;
/** @type {TemplateNode | null} */
#hydrate_open = hydrating ? hydrate_node : null;
/** @type {BoundaryProps} */
#props;
/** @type {((anchor: Node) => void)} */
#children;
/** @type {Effect} */
#effect;
/** @type {Effect | null} */
#main_effect = null;
/** @type {Effect | null} */
#pending_effect = null;
/** @type {Effect | null} */
#failed_effect = null;
/** @type {DocumentFragment | null} */
#offscreen_fragment = null;
#local_pending_count = 0;
#pending_count = 0;
#pending_count_update_queued = false;
/** @type {Set<Effect>} */
#dirty_effects = /* @__PURE__ */ new Set();
/** @type {Set<Effect>} */
#maybe_dirty_effects = /* @__PURE__ */ new Set();
/**
* A source containing the number of pending async deriveds/expressions.
* Only created if `$effect.pending()` is used inside the boundary,
* otherwise updating the source results in needless `Batch.ensure()`
* calls followed by no-op flushes
* @type {Source<number> | null}
*/
#effe