Skip to content

Commit

Permalink
refactor(core): add consumerOnSignalRead hook to ReactiveNode
Browse files Browse the repository at this point in the history
This hook allows a consumer to create `ReactiveNode`s with custom behavior
when signals are read.
  • Loading branch information
alxhub committed Oct 4, 2023
1 parent 5e1987f commit 59d1425
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions packages/core/primitives/signals/src/graph.ts
Expand Up @@ -60,6 +60,7 @@ export const REACTIVE_NODE: ReactiveNode = {
producerMustRecompute: () => false,
producerRecomputeValue: () => {},
consumerMarkedDirty: () => {},
consumerOnSignalRead: () => {},
};

/**
Expand Down Expand Up @@ -156,6 +157,11 @@ export interface ReactiveNode {
producerMustRecompute(node: unknown): boolean;
producerRecomputeValue(node: unknown): void;
consumerMarkedDirty(node: unknown): void;

/**
* Called when a signal is read within this consumer.
*/
consumerOnSignalRead(node: unknown): void;
}

interface ConsumerNode extends ReactiveNode {
Expand Down Expand Up @@ -185,6 +191,8 @@ export function producerAccessed(node: ReactiveNode): void {
return;
}

activeConsumer.consumerOnSignalRead(node);

// This producer is the `idx`th dependency of `activeConsumer`.
const idx = activeConsumer.nextProducerIndex++;

Expand Down

0 comments on commit 59d1425

Please sign in to comment.