blockless
Rich Harris 2 years ago
parent 8d383aed7e
commit 20b7b54b22

@ -16,11 +16,10 @@ export function derived(fn) {
/** @type {import('#client').Derived<V>} */ /** @type {import('#client').Derived<V>} */
const signal = { const signal = {
consumers: null, consumers: null,
d: null, deps: null,
eq: default_equals, eq: default_equals,
f: flags, f: flags,
fn: fn, fn: fn,
r: null,
// @ts-expect-error // @ts-expect-error
v: UNINITIALIZED, v: UNINITIALIZED,
w: 0, w: 0,
@ -33,10 +32,11 @@ export function derived(fn) {
} }
if (current_consumer !== null) { if (current_consumer !== null) {
if (current_consumer.r === null) { const effect = /** @type {import('#client').Effect} */ (current_consumer);
current_consumer.r = [signal]; if (effect.r === null) {
effect.r = [signal];
} else { } else {
current_consumer.r.push(signal); effect.r.push(signal);
} }
} }

@ -28,7 +28,7 @@ import {
function 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 = {
d: null, deps: null,
f: type | DIRTY, f: type | DIRTY,
l: 0, l: 0,
fn: fn, fn: fn,

@ -29,11 +29,9 @@ export interface SourceDebug<V = unknown> extends Source<V> {
export interface Derived<V = unknown> extends Source<V> { export interface Derived<V = unknown> extends Source<V> {
/** dependencies: Signals that this signal reads from */ /** dependencies: Signals that this signal reads from */
d: null | ValueSignal[]; deps: null | ValueSignal[];
/** init: The function that we invoke for effects and computeds */ /** init: The function that we invoke for effects and computeds */
fn: () => V; fn: () => V;
/** references: Anything that a signal owns */
r: null | Reaction[];
} }
export interface DerivedDebug<V = unknown> extends Derived<V> { export interface DerivedDebug<V = unknown> extends Derived<V> {
@ -44,7 +42,7 @@ export interface Effect {
/** context: The associated component if this signal is an effect/computed */ /** context: The associated component if this signal is an effect/computed */
ctx: null | ComponentContext; ctx: null | ComponentContext;
/** dependencies: Signals that this signal reads from */ /** dependencies: Signals that this signal reads from */
d: null | ValueSignal[]; deps: null | ValueSignal[];
/** destroy: Thing(s) that need destroying */ /** destroy: Thing(s) that need destroying */
y: null | (() => void); y: null | (() => void);
/** The types that the signal represent, as a bitwise value */ /** The types that the signal represent, as a bitwise value */

@ -163,7 +163,7 @@ function is_signal_dirty(signal) {
} }
if ((flags & MAYBE_DIRTY) !== 0) { if ((flags & MAYBE_DIRTY) !== 0) {
const dependencies = /** @type {import('#client').Reaction} **/ (signal).d; const dependencies = /** @type {import('#client').Reaction} **/ (signal).deps;
if (dependencies !== null) { if (dependencies !== null) {
const length = dependencies.length; const length = dependencies.length;
@ -230,7 +230,7 @@ function execute_reaction(signal) {
try { try {
const res = /** @type {() => V} */ (init)(); const res = /** @type {() => V} */ (init)();
let dependencies = /** @type {import('#client').ValueSignal<unknown>[]} **/ (signal.d); let dependencies = /** @type {import('#client').ValueSignal<unknown>[]} **/ (signal.deps);
if (current_dependencies !== null) { if (current_dependencies !== null) {
let i; let i;
if (dependencies !== null) { if (dependencies !== null) {
@ -265,7 +265,7 @@ function execute_reaction(signal) {
dependencies[current_dependencies_index + i] = current_dependencies[i]; dependencies[current_dependencies_index + i] = current_dependencies[i];
} }
} else { } else {
signal.d = /** @type {import('#client').ValueSignal<V>[]} **/ ( signal.deps = /** @type {import('#client').ValueSignal<V>[]} **/ (
dependencies = current_dependencies dependencies = current_dependencies
); );
} }
@ -336,7 +336,7 @@ function remove_consumer(signal, dependency) {
* @returns {void} * @returns {void}
*/ */
function remove_consumers(signal, start_index) { function remove_consumers(signal, start_index) {
const dependencies = signal.d; const dependencies = signal.deps;
if (dependencies !== null) { if (dependencies !== null) {
const active_dependencies = start_index === 0 ? null : dependencies.slice(0, start_index); const active_dependencies = start_index === 0 ? null : dependencies.slice(0, start_index);
let i; let i;
@ -664,7 +664,7 @@ function update_derived(signal, force_schedule) {
updating_derived = previous_updating_derived; updating_derived = previous_updating_derived;
const status = const status =
(current_skip_consumer || (signal.f & UNOWNED) !== 0) && signal.d !== null (current_skip_consumer || (signal.f & UNOWNED) !== 0) && signal.deps !== null
? MAYBE_DIRTY ? MAYBE_DIRTY
: CLEAN; : CLEAN;
@ -706,7 +706,7 @@ export function get(signal) {
// Register the dependency on the current consumer signal. // Register the dependency on the current consumer signal.
if (current_consumer !== null && (current_consumer.f & MANAGED) === 0 && !current_untracking) { if (current_consumer !== null && (current_consumer.f & MANAGED) === 0 && !current_untracking) {
const unowned = (current_consumer.f & UNOWNED) !== 0; const unowned = (current_consumer.f & UNOWNED) !== 0;
const dependencies = current_consumer.d; const dependencies = current_consumer.deps;
if ( if (
current_dependencies === null && current_dependencies === null &&
dependencies !== null && dependencies !== null &&

@ -229,7 +229,7 @@ describe('signals', () => {
// Ensure we're not leaking dependencies // Ensure we're not leaking dependencies
assert.deepEqual( assert.deepEqual(
nested.slice(0, -2).map((s) => s.d), nested.slice(0, -2).map((s) => s.deps),
[null, null] [null, null]
); );
}; };

Loading…
Cancel
Save