@@ -14,6 +14,7 @@ import {setDirectiveInput, storePropertyBindingMetadata} from './instructions/sh
1414import { DirectiveDef } from './interfaces/definition' ;
1515import { CONTEXT } from './interfaces/view' ;
1616import { getCurrentTNode , getLView , getSelectedTNode , getTView , nextBindingIndex } from './state' ;
17+ import { stringifyForError } from './util/stringify_utils' ;
1718
1819/** Symbol used to store and retrieve metadata about a binding. */
1920export const BINDING = /* @__PURE__ */ Symbol ( 'BINDING' ) ;
@@ -53,85 +54,6 @@ export interface DirectiveWithBindings<T> {
5354const INPUT_BINDING_METADATA : Binding [ typeof BINDING ] = { kind : 'input' , requiredVars : 1 } ;
5455const OUTPUT_BINDING_METADATA : Binding [ typeof BINDING ] = { kind : 'output' , requiredVars : 0 } ;
5556
56- class InputBinding < T > implements Binding {
57- readonly target ! : DirectiveDef < T > ;
58- readonly [ BINDING ] = INPUT_BINDING_METADATA ;
59-
60- constructor (
61- private readonly publicName : string ,
62- private readonly value : ( ) => unknown ,
63- ) { }
64-
65- update ( ) : void {
66- const lView = getLView ( ) ;
67- const bindingIndex = nextBindingIndex ( ) ;
68- const value = this . value ( ) ;
69- if ( bindingUpdated ( lView , bindingIndex , value ) ) {
70- const tView = getTView ( ) ;
71- const tNode = getSelectedTNode ( ) ;
72-
73- if ( ! this . target && ngDevMode ) {
74- throw new RuntimeError (
75- RuntimeErrorCode . NO_BINDING_TARGET ,
76- `Input binding to property "${ this . publicName } " does not have a target.` ,
77- ) ;
78- }
79-
80- const hasSet = setDirectiveInput ( tNode , tView , lView , this . target , this . publicName , value ) ;
81-
82- if ( ngDevMode ) {
83- if ( ! hasSet ) {
84- throw new RuntimeError (
85- RuntimeErrorCode . NO_BINDING_TARGET ,
86- `${ this . target . type . name } does not have an input with a public name of "${ this . publicName } ".` ,
87- ) ;
88- }
89- storePropertyBindingMetadata ( tView . data , tNode , this . publicName , bindingIndex ) ;
90- }
91- }
92- }
93- }
94-
95- class OutputBinding < T > implements Binding {
96- readonly target ! : DirectiveDef < unknown > ;
97- readonly [ BINDING ] = OUTPUT_BINDING_METADATA ;
98-
99- constructor (
100- private readonly eventName : string ,
101- private readonly listener : ( event : T ) => unknown ,
102- ) { }
103-
104- create ( ) : void {
105- if ( ! this . target && ngDevMode ) {
106- throw new RuntimeError (
107- RuntimeErrorCode . NO_BINDING_TARGET ,
108- `Output binding to "${ this . eventName } " does not have a target.` ,
109- ) ;
110- }
111-
112- const lView = getLView < { } | null > ( ) ;
113- const tView = getTView ( ) ;
114- const tNode = getCurrentTNode ( ) ! ;
115- const context = lView [ CONTEXT ] ;
116- const wrappedListener = wrapListener ( tNode , lView , context , this . listener ) ;
117- const hasBound = listenToDirectiveOutput (
118- tNode ,
119- tView ,
120- lView ,
121- this . target ,
122- this . eventName ,
123- wrappedListener ,
124- ) ;
125-
126- if ( ! hasBound && ngDevMode ) {
127- throw new RuntimeError (
128- RuntimeErrorCode . INVALID_BINDING_TARGET ,
129- `${ this . target . type . name } does not have an output with a public name of "${ this . eventName } ".` ,
130- ) ;
131- }
132- }
133- }
134-
13557/**
13658 * Creates an input binding.
13759 * @param publicName Public name of the input to bind to.
@@ -150,8 +72,44 @@ class OutputBinding<T> implements Binding {
15072 * });
15173 * ```
15274 */
153- export function inputBinding < T > ( publicName : string , value : ( ) => unknown ) : Binding {
154- return new InputBinding < T > ( publicName , value ) ;
75+ export function inputBinding ( publicName : string , value : ( ) => unknown ) : Binding {
76+ // Note: ideally we would use a class here, but it seems like they
77+ // don't get tree shaken when constructed by a function like this.
78+ const binding : Binding = {
79+ [ BINDING ] : INPUT_BINDING_METADATA ,
80+ target : null ,
81+ update : ( ) => {
82+ const target = binding . target as DirectiveDef < unknown > ;
83+ const lView = getLView ( ) ;
84+ const bindingIndex = nextBindingIndex ( ) ;
85+ const resolvedValue = value ( ) ;
86+ if ( bindingUpdated ( lView , bindingIndex , resolvedValue ) ) {
87+ const tView = getTView ( ) ;
88+ const tNode = getSelectedTNode ( ) ;
89+
90+ if ( ! target && ngDevMode ) {
91+ throw new RuntimeError (
92+ RuntimeErrorCode . NO_BINDING_TARGET ,
93+ `Input binding to property "${ publicName } " does not have a target.` ,
94+ ) ;
95+ }
96+
97+ const hasSet = setDirectiveInput ( tNode , tView , lView , target , publicName , resolvedValue ) ;
98+
99+ if ( ngDevMode ) {
100+ if ( ! hasSet ) {
101+ throw new RuntimeError (
102+ RuntimeErrorCode . NO_BINDING_TARGET ,
103+ `${ stringifyForError ( target . type ) } does not have an input with a public name of "${ publicName } ".` ,
104+ ) ;
105+ }
106+ storePropertyBindingMetadata ( tView . data , tNode , publicName , bindingIndex ) ;
107+ }
108+ }
109+ } ,
110+ } ;
111+
112+ return binding ;
155113}
156114
157115/**
@@ -176,5 +134,43 @@ export function inputBinding<T>(publicName: string, value: () => unknown): Bindi
176134 * ```
177135 */
178136export function outputBinding < T > ( eventName : string , listener : ( event : T ) => unknown ) : Binding {
179- return new OutputBinding < T > ( eventName , listener ) ;
137+ // Note: ideally we would use a class here, but it seems like they
138+ // don't get tree shaken when constructed by a function like this.
139+ const binding : Binding = {
140+ [ BINDING ] : OUTPUT_BINDING_METADATA ,
141+ target : null ,
142+ create : ( ) => {
143+ const target = binding . target as DirectiveDef < unknown > ;
144+
145+ if ( ! target && ngDevMode ) {
146+ throw new RuntimeError (
147+ RuntimeErrorCode . NO_BINDING_TARGET ,
148+ `Output binding to "${ eventName } " does not have a target.` ,
149+ ) ;
150+ }
151+
152+ const lView = getLView < { } | null > ( ) ;
153+ const tView = getTView ( ) ;
154+ const tNode = getCurrentTNode ( ) ! ;
155+ const context = lView [ CONTEXT ] ;
156+ const wrappedListener = wrapListener ( tNode , lView , context , listener ) ;
157+ const hasBound = listenToDirectiveOutput (
158+ tNode ,
159+ tView ,
160+ lView ,
161+ target ,
162+ eventName ,
163+ wrappedListener ,
164+ ) ;
165+
166+ if ( ! hasBound && ngDevMode ) {
167+ throw new RuntimeError (
168+ RuntimeErrorCode . INVALID_BINDING_TARGET ,
169+ `${ stringifyForError ( target . type ) } does not have an output with a public name of "${ eventName } ".` ,
170+ ) ;
171+ }
172+ } ,
173+ } ;
174+
175+ return binding ;
180176}
0 commit comments