-
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
feat: add $log rune #9670
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add $log rune #9670
Conversation
🦋 Changeset detectedLatest commit: d89d54d The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
/** | ||
* @template V | ||
* @param {import('./types.js').Signal<V>} signal | ||
* @param {V} value | ||
* @returns {V} | ||
*/ | ||
export function set(signal, value) { | ||
attach_trace(signal); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should attach_trace
be called inside set_signal_value
, rather than immediately before it? It might save us from forgetting to attach the trace in some future function that calls set_signal_value
.
I see there are two occurrences of set_signal_value
that don't have a preceding attach_trace
, inside update_each_item_block
— I don't fully understand the code, but is there any real downside to always attaching the trace when a signal is set?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reason for it being like this is to ensure that the same depth in the stack for the place the user does the mutation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wouldn't we just need to just that number by 1 if attach_trace
was inside set_signal_value
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
attach_trace
is used in various places where set_signal_value
can be the 2nd or 4th in the call-stack from memory so I tried to ensure it was always the same place. Maybe there's a more sane way of doing this.
it appears that |
So we'd have an arrow function per each signal as a callback? Not sure what you mean here.
Seems like something is going wrong with the hoisting transform for event handlers. I'll take a look today. |
Digging more into the sourcemap issues:
|
During function deep_read(value, fn) {
// ...
signals = expose(() => get.call(value));
for (const signal of signals) {
signal.d = fn;
}
// ...
} (Note that this requires a = $state(1);
b = $state(1);
return {
get total() {
return a + b;
}
}; Then export function log_break(get_values, break_fn) {
let initial = true;
pre_effect(() => {
const values = get_values();
deep_read(values, break_fn);
});
} ...and |
@Rich-Harris That approach you outlined was my kind of the original approach to dealing with this. However it broke down in a couple of cases:
However, if you want to take a bash in making it work, then go for it :) |
Someone of Discord noticed an issue with the docs: https://discord.com/channels/457912077277855764/1153350350158450758/1178846700639629373 It's a bit weird to have the old runes, "How to opt in" in the middle, and then the new |
$log
The
$log
rune is roughly equivalent toconsole.log
, with the exception that when anything passed to the rune changes, the latest values will be logged out.$log
tracks reactive state deeply, meaning that mutating something from with an object or array using fine-grain reactivity will be tracked.$log.break
This works just like
$log
, except runtime execution will be paused via adebugger
statement.$log.table
This works just like
$log
, but triggersconsole.table
instead ofconsole.log
.$log.trace
This works just like
$log
, but in addition to logging the values, it will also trace any mutations to fine-grain reactive state and log the sourcecode location to help find the cause for something changing.