slightly better waterfall warning

async-fork
Rich Harris 7 months ago
parent 0b9bfc9a31
commit 3289ac3ad1

@ -45,7 +45,7 @@ TODO
### await_waterfall
```
Detected an unnecessary async waterfall
An async value (%location%) was not read immediately after it resolved. This often indicates an unnecessary waterfall, which can slow down your app.
```
TODO

@ -38,7 +38,7 @@ TODO
## await_waterfall
> Detected an unnecessary async waterfall
> An async value (%location%) was not read immediately after it resolved. This often indicates an unnecessary waterfall, which can slow down your app.
TODO

@ -1,7 +1,7 @@
/** @import { CallExpression, Expression, Identifier, Literal, VariableDeclaration, VariableDeclarator } from 'estree' */
/** @import { Binding } from '#compiler' */
/** @import { ComponentClientTransformState, ComponentContext } from '../types' */
import { dev } from '../../../../state.js';
import { dev, is_ignored, locate_node } from '../../../../state.js';
import { extract_paths } from '../../../../utils/ast.js';
import * as b from '../../../../utils/builders.js';
import * as assert from '../../../../utils/assert.js';
@ -19,7 +19,7 @@ export function VariableDeclaration(node, context) {
if (context.state.analysis.runes) {
for (const declarator of node.declarations) {
const init = declarator.init;
const init = /** @type {Expression} */ (declarator.init);
const rune = get_rune(init, context.state.scope);
if (
@ -164,10 +164,23 @@ export function VariableDeclaration(node, context) {
if (declarator.id.type === 'Identifier') {
if (is_async) {
const location = dev && is_ignored(init, 'await_waterfall') && locate_node(init);
declarations.push(
b.declarator(
declarator.id,
b.call(b.await(b.call('$.save', b.call('$.async_derived', b.thunk(value, true)))))
b.call(
b.await(
b.call(
'$.save',
b.call(
'$.async_derived',
b.thunk(value, true),
location ? b.literal(location) : undefined
)
)
)
)
)
);
} else {

@ -39,6 +39,7 @@ export const NAMESPACE_MATHML = 'http://www.w3.org/1998/Math/MathML';
// we use a list of ignorable runtime warnings because not every runtime warning
// can be ignored and we want to keep the validation for svelte-ignore in place
export const IGNORABLE_RUNTIME_WARNINGS = /** @type {const} */ ([
'await_waterfall',
'state_snapshot_uncloneable',
'binding_property_non_reactive',
'hydration_attribute_changed',

@ -88,11 +88,11 @@ export function derived(fn) {
/**
* @template V
* @param {() => Promise<V>} fn
* @param {boolean} detect_waterfall Whether to print a warning if the value is not read immediately after update
* @param {string} [location] If provided, print a warning if the value is not read immediately after update
* @returns {Promise<Source<V>>}
*/
/*#__NO_SIDE_EFFECTS__*/
export function async_derived(fn, detect_waterfall = true) {
export function async_derived(fn, location) {
let parent = /** @type {Effect | null} */ (active_effect);
if (parent === null) {
@ -129,12 +129,12 @@ export function async_derived(fn, detect_waterfall = true) {
internal_set(signal, v);
if (DEV && detect_waterfall) {
if (DEV && location !== undefined) {
recent_async_deriveds.add(signal);
setTimeout(() => {
if (recent_async_deriveds.has(signal)) {
w.await_waterfall();
w.await_waterfall(location);
recent_async_deriveds.delete(signal);
}
});

@ -352,7 +352,7 @@ export function template_effect(fn, sync = [], async = [], d = derived) {
var restore = capture();
var unsuspend = suspend();
Promise.all(async.map((expression) => async_derived(expression, false))).then((result) => {
Promise.all(async.map((expression) => async_derived(expression))).then((result) => {
restore();
if ((effect.f & DESTROYED) !== 0) {

@ -30,11 +30,12 @@ export function await_reactivity_loss() {
}
/**
* Detected an unnecessary async waterfall
* An async value (%location%) was not read immediately after it resolved. This often indicates an unnecessary waterfall, which can slow down your app.
* @param {string} location
*/
export function await_waterfall() {
export function await_waterfall(location) {
if (DEV) {
console.warn(`%c[svelte] await_waterfall\n%cDetected an unnecessary async waterfall\nhttps://svelte.dev/e/await_waterfall`, bold, normal);
console.warn(`%c[svelte] await_waterfall\n%cAn async value (${location}) was not read immediately after it resolved. This often indicates an unnecessary waterfall, which can slow down your app.\nhttps://svelte.dev/e/await_waterfall`, bold, normal);
} else {
console.warn(`https://svelte.dev/e/await_waterfall`);
}

Loading…
Cancel
Save