yield
Dominic Gannaway 6 days ago
parent 6b24dcddf9
commit c5a1f2d0fa

@ -255,7 +255,7 @@ declare namespace $effect {
*/
export function pre(fn: () => void | (() => void)): void;
export function yield(fn: () => void | (() => void)): void;
export function post(fn: () => void | (() => void)): void;
/**
* The `$effect.tracking` rune is an advanced feature that tells you whether or not the code is running inside a tracking context, such as an effect or inside your template.

@ -95,7 +95,7 @@ export function CallExpression(node, context) {
case '$effect':
case '$effect.pre':
case '$effect.yield':
case '$effect.post':
if (parent.type !== 'ExpressionStatement') {
e.effect_invalid_placement(node);
}

@ -11,12 +11,12 @@ export function ExpressionStatement(node, context) {
if (node.expression.type === 'CallExpression') {
const rune = get_rune(node.expression, context.state.scope);
if (rune === '$effect' || rune === '$effect.pre' || rune === '$effect.yield') {
if (rune === '$effect' || rune === '$effect.pre' || rune === '$effect.post') {
const callee =
rune === '$effect'
? '$.user_effect'
: rune === '$effect.yield'
? '$.user_yield_effect'
: rune === '$effect.post'
? '$.user_post_effect'
: '$.user_pre_effect';
const func = /** @type {Expression} */ (context.visit(node.expression.arguments[0]));

@ -13,7 +13,7 @@ export function ExpressionStatement(node, context) {
if (
rune === '$effect' ||
rune === '$effect.pre' ||
rune === '$effect.yield' ||
rune === '$effect.post' ||
rune === '$effect.root' ||
rune === '$inspect.trace'
) {

@ -5,7 +5,7 @@ export const BLOCK_EFFECT = 1 << 4;
export const BRANCH_EFFECT = 1 << 5;
export const ROOT_EFFECT = 1 << 6;
export const BOUNDARY_EFFECT = 1 << 7;
export const YIELD_EFFECT = 1 << 8;
export const POST_EFFECT = 1 << 8;
export const UNOWNED = 1 << 9;
export const DISCONNECTED = 1 << 10;
export const CLEAN = 1 << 11;

@ -12,11 +12,11 @@ export const schedule_yield =
// @ts-ignore
typeof scheduler === 'undefined'
? (/** @type {() => void} */ cb) => {
// For Safari, we fallback to using MessageChannel
const channel = new MessageChannel();
channel.port1.onmessage = cb;
channel.port2.postMessage(undefined);
}
// For Safari, we fallback to using MessageChannel
const channel = new MessageChannel();
channel.port1.onmessage = cb;
channel.port2.postMessage(undefined);
}
: async (/** @type {() => void} */ fn) => {
// @ts-ignore
await scheduler.yield();

@ -108,7 +108,7 @@ export {
effect,
user_effect,
user_pre_effect,
user_yield_effect
user_post_effect
} from './reactivity/effects.js';
export { mutable_state, mutate, set, state } from './reactivity/sources.js';
export {

@ -36,7 +36,7 @@ import {
HEAD_EFFECT,
MAYBE_DIRTY,
EFFECT_HAS_DERIVED,
YIELD_EFFECT
POST_EFFECT
} from '../constants.js';
import { set } from './sources.js';
import * as e from '../errors.js';
@ -46,7 +46,7 @@ import { get_next_sibling } from '../dom/operations.js';
import { destroy_derived } from './deriveds.js';
/**
* @param {'$effect' | '$effect.pre' | '$effect.yield' | '$inspect'} rune
* @param {'$effect' | '$effect.pre' | '$effect.post' | '$inspect'} rune
*/
export function validate_effect(rune) {
if (active_effect === null && active_reaction === null) {
@ -233,19 +233,19 @@ export function user_pre_effect(fn) {
}
/**
* Internal representation of `$effect.yield(...)`
* Internal representation of `$effect.post(...)`
* @param {() => void | (() => void)} fn
* @returns {Effect}
*/
export function user_yield_effect(fn) {
validate_effect('$effect.yield');
export function user_post_effect(fn) {
validate_effect('$effect.post');
if (DEV) {
define_property(fn, 'name', {
value: '$effect.yield'
value: '$effect.post'
});
}
return yield_effect(fn);
return post_effect(fn);
}
/** @param {() => void | (() => void)} fn */
@ -301,8 +301,8 @@ export function effect(fn) {
* @param {() => void | (() => void)} fn
* @returns {Effect}
*/
export function yield_effect(fn) {
return create_effect(YIELD_EFFECT, fn, false);
export function post_effect(fn) {
return create_effect(POST_EFFECT, fn, false);
}
/**

@ -26,7 +26,7 @@ import {
LEGACY_DERIVED_PROP,
DISCONNECTED,
BOUNDARY_EFFECT,
YIELD_EFFECT
POST_EFFECT
} from './constants.js';
import { flush_tasks, schedule_yield } from './dom/task.js';
import { add_owner } from './dev/ownership.js';
@ -49,9 +49,9 @@ export let is_throwing_error = false;
let scheduler_mode = FLUSH_MICROTASK;
// Used for handling scheduling
let is_micro_task_queued = false;
let is_yield_queued = false;
let is_post_task_queued = false;
/** @type {Effect[]} */
let queued_yield_effects = [];
let queued_post_effects = [];
/** @type {Effect | null} */
let last_scheduled_effect = null;
@ -594,10 +594,10 @@ function infinite_loop_guard() {
/**
* @param {Array<Effect>} root_effects
* @param {boolean} yielded_effects
* @param {boolean} post_effects
* @returns {void}
*/
function flush_queued_root_effects(root_effects, yielded_effects) {
function flush_queued_root_effects(root_effects, post_effects) {
var length = root_effects.length;
if (length === 0) {
return;
@ -618,7 +618,7 @@ function flush_queued_root_effects(root_effects, yielded_effects) {
/** @type {Effect[]} */
var collected_effects = [];
process_effects(effect, collected_effects, yielded_effects);
process_effects(effect, collected_effects, post_effects);
flush_queued_effects(collected_effects);
}
} finally {
@ -664,14 +664,14 @@ function flush_queued_effects(effects) {
}
}
function flush_deferred_effects(yielded_effects = false) {
function flush_deferred_effects(post_effects = false) {
is_micro_task_queued = false;
if (flush_count > 1001) {
return;
}
const previous_queued_root_effects = queued_root_effects;
queued_root_effects = [];
flush_queued_root_effects(previous_queued_root_effects, yielded_effects);
flush_queued_root_effects(previous_queued_root_effects, post_effects);
if (!is_micro_task_queued) {
flush_count = 0;
@ -682,18 +682,18 @@ function flush_deferred_effects(yielded_effects = false) {
}
}
function flush_yield_effects() {
is_yield_queued = false;
function flush_post_effects() {
is_post_task_queued = false;
for (var i = 0; i < queued_yield_effects.length; i++) {
var effect = queued_yield_effects[i];
for (var i = 0; i < queued_post_effects.length; i++) {
var effect = queued_post_effects[i];
if ((effect.f & (DESTROYED | INERT)) === 0) {
last_scheduled_effect = effect;
mark_parent_effects(effect);
}
}
queued_yield_effects = [];
queued_post_effects = [];
flush_deferred_effects(true);
}
@ -723,12 +723,12 @@ function mark_parent_effects(signal) {
*/
export function schedule_effect(signal) {
if (scheduler_mode !== FLUSH_SYNC) {
if ((signal.f & YIELD_EFFECT) !== 0) {
if (!is_yield_queued) {
is_yield_queued = true;
schedule_yield(flush_yield_effects);
if ((signal.f & POST_EFFECT) !== 0) {
if (!is_post_task_queued) {
is_post_task_queued = true;
schedule_yield(flush_post_effects);
}
queued_yield_effects.push(signal);
queued_post_effects.push(signal);
return;
}
if (!is_micro_task_queued) {
@ -750,10 +750,10 @@ export function schedule_effect(signal) {
*
* @param {Effect} effect
* @param {Effect[]} collected_effects
* @param {boolean} yielded_effects
* @param {boolean} post_effects
* @returns {void}
*/
function process_effects(effect, collected_effects, yielded_effects) {
function process_effects(effect, collected_effects, post_effects) {
var current_effect = effect.first;
var effects = [];
@ -783,7 +783,7 @@ function process_effects(effect, collected_effects, yielded_effects) {
current_effect = child;
continue;
}
} else if ((flags & EFFECT) !== 0 || (yielded_effects && (flags & YIELD_EFFECT) !== 0)) {
} else if ((flags & EFFECT) !== 0 || (post_effects && (flags & POST_EFFECT) !== 0)) {
effects.push(current_effect);
}
}
@ -812,7 +812,7 @@ function process_effects(effect, collected_effects, yielded_effects) {
for (var i = 0; i < effects.length; i++) {
child = effects[i];
collected_effects.push(child);
process_effects(child, collected_effects, yielded_effects);
process_effects(child, collected_effects, post_effects);
}
}

@ -427,7 +427,7 @@ const RUNES = /** @type {const} */ ([
'$derived.by',
'$effect',
'$effect.pre',
'$effect.yield',
'$effect.post',
'$effect.tracking',
'$effect.root',
'$inspect',

Loading…
Cancel
Save