forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(core): introduce runtime
InputSignal
implementation (angul…
…ar#53571) This commit introduces the runtime `InputSignal` implementation. Input initializers using `input` or `input.required` will result in an instance of `InputSignal` to be created. An input signal extends the signal primtive, with a couple of small differences: - it's a readonly signal. There is no public `set` or `update`. - equality is non-configurable. As per CD semantics, the value is guaranteed to be different when the `property` instruction attempts to update an input signal. - we support a `transform` function, that allows transforming input values. The transform is called whenever the input is set. An alternative could have been to follow computed-semantics and call the transform upon accessing, if dirty. In the future, we might change this to extend the computed reactive node, so that we can support computed inputs that do not rely on continious bound value assignments. See signal based components RFC. PR Close angular#53571
- Loading branch information
1 parent
75646da
commit f21657f
Showing
5 changed files
with
105 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import {SIGNAL_NODE, SignalNode, signalSetFn} from '@angular/core/primitives/signals'; | ||
|
||
export const REQUIRED_UNSET_VALUE = /* @__PURE__ */ Symbol('InputSignalNode#UNSET'); | ||
|
||
/** | ||
* Reactive node type for an input signal. An input signal extends a signal. | ||
* There are special properties to enable transforms and required inputs. | ||
*/ | ||
export interface InputSignalNode<ReadT, WriteT> extends | ||
SignalNode<ReadT|typeof REQUIRED_UNSET_VALUE> { | ||
/** | ||
* User-configured transform that will run whenever a new value is applied | ||
* to the input signal node. | ||
*/ | ||
transformFn: ((value: WriteT) => ReadT)|undefined; | ||
|
||
/** | ||
* Applies a new value to the input signal. Expects transforms to be run | ||
* manually before. | ||
* | ||
* This function is called by the framework runtime code whenever a binding | ||
* changes. The value can in practice be anything at runtime, but for typing | ||
* purposes we assume it's a valid `ReadT` value. Type-checking will enforce that. | ||
*/ | ||
applyValueToInputSignal<ReadT, WriteT>(node: InputSignalNode<ReadT, WriteT>, value: ReadT): void; | ||
} | ||
|
||
// Note: Using an IIFE here to ensure that the spread assignment is not considered | ||
// a side-effect, ending up preserving `COMPUTED_NODE` and `REACTIVE_NODE`. | ||
// TODO: remove when https://github.com/evanw/esbuild/issues/3392 is resolved. | ||
export const INPUT_SIGNAL_NODE: InputSignalNode<unknown, unknown> = /* @__PURE__ */ (() => { | ||
return { | ||
...SIGNAL_NODE, | ||
transformFn: undefined, | ||
|
||
applyValueToInputSignal<ReadT, WriteT>(node: InputSignalNode<ReadT, WriteT>, value: ReadT) { | ||
signalSetFn(node, value); | ||
} | ||
}; | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters