blockless
Rich Harris 2 years ago
parent 312322f1f4
commit 274c7c2568

@ -27,7 +27,7 @@ import { push_reference } from './utils.js';
* @param {boolean} schedule * @param {boolean} schedule
* @returns {import('#client').Effect} * @returns {import('#client').Effect}
*/ */
function internal_create_effect(type, fn, sync, schedule) { function create_effect(type, fn, sync, schedule) {
/** @type {import('#client').Effect} */ /** @type {import('#client').Effect} */
const signal = { const signal = {
c: null, c: null,
@ -66,6 +66,7 @@ function internal_create_effect(type, fn, sync, schedule) {
} }
/** /**
* Internal representation of `$effect.active()`
* @returns {boolean} * @returns {boolean}
*/ */
export function effect_active() { export function effect_active() {
@ -90,7 +91,7 @@ export function user_effect(fn) {
current_component_context !== null && current_component_context !== null &&
!current_component_context.m; !current_component_context.m;
const effect = internal_create_effect(EFFECT, fn, false, !apply_component_effect_heuristics); const effect = create_effect(EFFECT, fn, false, !apply_component_effect_heuristics);
if (apply_component_effect_heuristics) { if (apply_component_effect_heuristics) {
const context = /** @type {import('#client').ComponentContext} */ (current_component_context); const context = /** @type {import('#client').ComponentContext} */ (current_component_context);
@ -106,7 +107,7 @@ export function user_effect(fn) {
* @returns {() => void} * @returns {() => void}
*/ */
export function user_root_effect(fn) { export function user_root_effect(fn) {
const effect = internal_create_effect(RENDER_EFFECT | ROOT_EFFECT, fn, true, true); const effect = create_effect(RENDER_EFFECT | ROOT_EFFECT, fn, true, true);
return () => { return () => {
destroy_effect(effect); destroy_effect(effect);
}; };
@ -117,7 +118,7 @@ export function user_root_effect(fn) {
* @returns {import('#client').Effect} * @returns {import('#client').Effect}
*/ */
export function effect(fn) { export function effect(fn) {
return internal_create_effect(EFFECT, fn, false, true); return create_effect(EFFECT, fn, false, true);
} }
/** /**
@ -125,7 +126,7 @@ export function effect(fn) {
* @returns {import('#client').Effect} * @returns {import('#client').Effect}
*/ */
export function managed_effect(fn) { export function managed_effect(fn) {
return internal_create_effect(EFFECT | MANAGED, fn, false, true); return create_effect(EFFECT | MANAGED, fn, false, true);
} }
/** /**
@ -134,7 +135,7 @@ export function managed_effect(fn) {
* @returns {import('#client').Effect} * @returns {import('#client').Effect}
*/ */
export function managed_pre_effect(fn, sync) { export function managed_pre_effect(fn, sync) {
return internal_create_effect(PRE_EFFECT | MANAGED, fn, sync, true); return create_effect(PRE_EFFECT | MANAGED, fn, sync, true);
} }
/** /**
@ -152,7 +153,7 @@ export function pre_effect(fn) {
); );
} }
const sync = current_effect !== null && (current_effect.f & RENDER_EFFECT) !== 0; const sync = current_effect !== null && (current_effect.f & RENDER_EFFECT) !== 0;
return internal_create_effect( return create_effect(
PRE_EFFECT, PRE_EFFECT,
() => { () => {
const val = fn(); const val = fn();
@ -172,7 +173,7 @@ export function pre_effect(fn) {
* @returns {import('#client').Effect} * @returns {import('#client').Effect}
*/ */
export function invalidate_effect(fn) { export function invalidate_effect(fn) {
return internal_create_effect(PRE_EFFECT, fn, true, true); return create_effect(PRE_EFFECT, fn, true, true);
} }
/** /**
@ -186,7 +187,7 @@ export function render_effect(fn, managed = false, sync = true) {
if (managed) { if (managed) {
flags |= MANAGED; flags |= MANAGED;
} }
return internal_create_effect(flags, /** @type {any} */ (fn), sync, true); return create_effect(flags, /** @type {any} */ (fn), sync, true);
} }
/** /**

@ -66,7 +66,7 @@ export interface Effect {
/** The types that the signal represent, as a bitwise value */ /** The types that the signal represent, as a bitwise value */
f: SignalFlags; f: SignalFlags;
/** init: The function that we invoke for effects and computeds */ /** init: The function that we invoke for effects and computeds */
i: null | (() => void | (() => void)) | ((b: null, s: Signal) => void | (() => void)); i: null | (() => void | (() => void));
/** references: Anything that a signal owns */ /** references: Anything that a signal owns */
r: null | Reaction[]; r: null | Reaction[];
/** teardown */ /** teardown */

Loading…
Cancel
Save