Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
refactor(core): add "kind" to signal prototype nodes
Enables debug APIs to determine the kind of a signal given a node.
  • Loading branch information
AleksanderBodurri committed Dec 2, 2024
commit 0836aff20373d60421fc4b64de1fd74555580e21
1 change: 1 addition & 0 deletions goldens/public-api/core/primitives/signals/index.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export interface ReactiveNode {
consumerOnSignalRead(node: unknown): void;
debugName?: string;
dirty: boolean;
kind: string;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume there's a reason the type is string here rather than 'signal' | 'computed' | ...?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the Angular framework wants to create a node type and set kind to 'some-new-node-type' we would have to add that to the type contract in the primitive signals library. Having the type contract be less strict here lets us avoid this coupling and lets primitives/signals library consumers define their own "kind" of nodes.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point! 👍

lastCleanEpoch: Version;
liveConsumerIndexOfThis: number[] | undefined;
liveConsumerNode: ReactiveNode[] | undefined;
Expand Down
1 change: 1 addition & 0 deletions packages/core/primitives/signals/src/computed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ const COMPUTED_NODE = /* @__PURE__ */ (() => {
dirty: true,
error: null,
equal: defaultEquals,
kind: 'computed',

producerMustRecompute(node: ComputedNode<unknown>): boolean {
// Force a recomputation if there's no current value, or if the current value is in the
Expand Down
11 changes: 11 additions & 0 deletions packages/core/primitives/signals/src/graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export const REACTIVE_NODE: ReactiveNode = {
liveConsumerIndexOfThis: undefined,
consumerAllowSignalWrites: false,
consumerIsAlwaysLive: false,
kind: 'unknown',
Comment thread
dgp1130 marked this conversation as resolved.
Outdated
producerMustRecompute: () => false,
producerRecomputeValue: () => {},
consumerMarkedDirty: () => {},
Expand Down Expand Up @@ -184,6 +185,16 @@ export interface ReactiveNode {
* A debug name for the reactive node. Used in Angular DevTools to identify the node.
*/
debugName?: string;

/**
* Kind of node. Example: 'signal', 'computed', 'input', 'effect'.
*
* ReactiveNode has this as 'unknown' by default, but derived node types should override this to
* make available the kind of signal that particular instance of a ReactiveNode represents.
*
* Used in Angular DevTools to identify the kind of signal.
*/
kind: string;
}

interface ConsumerNode extends ReactiveNode {
Expand Down
1 change: 1 addition & 0 deletions packages/core/primitives/signals/src/signal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ export const SIGNAL_NODE: SignalNode<unknown> = /* @__PURE__ */ (() => {
...REACTIVE_NODE,
equal: defaultEquals,
value: undefined,
kind: 'signal',
};
})();

Expand Down