complete revert

pull/16131/head
Rich Harris 3 months ago
parent 1ece2eb1a2
commit 35783c3767

@ -26,6 +26,23 @@ function log_entry(signal, entry) {
return; return;
} }
if (signal.trace) {
var previous_captured_signals = captured_signals;
var captured = new Set();
set_captured_signals(captured);
try {
untrack(signal.trace);
} finally {
set_captured_signals(previous_captured_signals);
}
if (captured.size > 0) {
for (const dep of captured) log_entry(dep);
return;
}
}
const type = (signal.f & DERIVED) !== 0 ? '$derived' : '$state'; const type = (signal.f & DERIVED) !== 0 ? '$derived' : '$state';
const current_reaction = /** @type {Reaction} */ (active_reaction); const current_reaction = /** @type {Reaction} */ (active_reaction);
const dirty = signal.wv > current_reaction.wv || current_reaction.wv === 0; const dirty = signal.wv > current_reaction.wv || current_reaction.wv === 0;

@ -524,6 +524,16 @@ function create_item(
var v = reactive ? (mutable ? mutable_source(value) : source(value)) : value; var v = reactive ? (mutable ? mutable_source(value) : source(value)) : value;
var i = (flags & EACH_INDEX_REACTIVE) === 0 ? index : source(index); var i = (flags & EACH_INDEX_REACTIVE) === 0 ? index : source(index);
if (DEV && reactive) {
// For tracing purposes, we need to link the source signal we create with the
// collection + index so that tracing works as intended
/** @type {Value} */ (v).trace = () => {
var collection_index = typeof i === 'number' ? index : i.v;
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
get_collection()[collection_index];
};
}
/** @type {EachItem} */ /** @type {EachItem} */
var item = { var item = {
i, i,

@ -70,6 +70,7 @@ export function source(v, stack) {
signal.created = stack ?? get_stack('CreatedAt'); signal.created = stack ?? get_stack('CreatedAt');
signal.updated = null; signal.updated = null;
signal.set_during_effect = false; signal.set_during_effect = false;
signal.trace = null;
} }
return signal; return signal;

@ -29,6 +29,8 @@ export interface Value<V = unknown> extends Signal {
* increment the write version so that it shows up as dirty when the effect re-runs * increment the write version so that it shows up as dirty when the effect re-runs
*/ */
set_during_effect?: boolean; set_during_effect?: boolean;
/** A function that retrieves the underlying source, used for each block item signals */
trace?: null | (() => void);
} }
export interface Reaction extends Signal { export interface Reaction extends Signal {

@ -773,22 +773,27 @@ export function get(signal) {
active_reaction !== null && active_reaction !== null &&
tracing_expressions.reaction === active_reaction tracing_expressions.reaction === active_reaction
) { ) {
var trace = get_stack('TracedAt'); // Used when mapping state between special blocks like `each`
if (signal.trace) {
signal.trace();
} else {
var trace = get_stack('TracedAt');
if (trace) { if (trace) {
var entry = tracing_expressions.entries.get(signal); var entry = tracing_expressions.entries.get(signal);
if (entry === undefined) { if (entry === undefined) {
entry = { traces: [] }; entry = { traces: [] };
tracing_expressions.entries.set(signal, entry); tracing_expressions.entries.set(signal, entry);
} }
var last = entry.traces[entry.traces.length - 1]; var last = entry.traces[entry.traces.length - 1];
// traces can be duplicated, e.g. by `snapshot` invoking both // traces can be duplicated, e.g. by `snapshot` invoking both
// both `getOwnPropertyDescriptor` and `get` traps at once // both `getOwnPropertyDescriptor` and `get` traps at once
if (trace.stack !== last?.stack) { if (trace.stack !== last?.stack) {
entry.traces.push(trace); entry.traces.push(trace);
}
} }
} }
} }

Loading…
Cancel
Save