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 e7f262d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
1 change: 1 addition & 0 deletions goldens/public-api/core/primitives/signals/index.md
Expand Up @@ -59,6 +59,7 @@ export interface ReactiveNode {
readonly consumerIsAlwaysLive: boolean;
// (undocumented)
consumerMarkedDirty(node: unknown): void;
consumerOnSignalRead(node: unknown): void;
dirty: boolean;
liveConsumerIndexOfThis: number[] | undefined;
liveConsumerNode: ReactiveNode[] | undefined;
Expand Down
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 e7f262d

Please sign in to comment.