@@ -1884,28 +1884,37 @@ function $ParseProvider() {
18841884 function oneTimeWatchDelegate ( scope , listener , objectEquality , parsedExpression , prettyPrintExpression ) {
18851885 var isDone = parsedExpression . literal ? isAllDefined : isDefined ;
18861886 var unwatch , lastValue ;
1887- if ( parsedExpression . inputs ) {
1888- unwatch = inputsWatchDelegate ( scope , oneTimeListener , objectEquality , parsedExpression , prettyPrintExpression ) ;
1889- } else {
1890- unwatch = scope . $watch ( oneTimeWatch , oneTimeListener , objectEquality ) ;
1891- }
1887+
1888+ var exp = parsedExpression . $$intercepted || parsedExpression ;
1889+ var post = parsedExpression . $$interceptor || identity ;
1890+
1891+ var useInputs = parsedExpression . inputs && ! exp . inputs ;
1892+
1893+ // Propogate the literal/inputs/constant attributes
1894+ // ... but not oneTime since we are handling it
1895+ oneTimeWatch . literal = parsedExpression . literal ;
1896+ oneTimeWatch . constant = parsedExpression . constant ;
1897+ oneTimeWatch . inputs = parsedExpression . inputs ;
1898+
1899+ // Allow other delegates to run on this wrapped expression
1900+ addWatchDelegate ( oneTimeWatch ) ;
1901+
1902+ unwatch = scope . $watch ( oneTimeWatch , listener , objectEquality , prettyPrintExpression ) ;
1903+
18921904 return unwatch ;
18931905
1894- function oneTimeWatch ( scope ) {
1895- return parsedExpression ( scope ) ;
1896- }
1897- function oneTimeListener ( value , old , scope ) {
1898- lastValue = value ;
1899- if ( isFunction ( listener ) ) {
1900- listener ( value , old , scope ) ;
1906+ function unwatchIfDone ( ) {
1907+ if ( isDone ( lastValue ) ) {
1908+ unwatch ( ) ;
19011909 }
1902- if ( isDone ( value ) ) {
1903- scope . $$postDigest ( function ( ) {
1904- if ( isDone ( lastValue ) ) {
1905- unwatch ( ) ;
1906- }
1907- } ) ;
1910+ }
1911+
1912+ function oneTimeWatch ( scope , locals , assign , inputs ) {
1913+ lastValue = useInputs && inputs ? inputs [ 0 ] : exp ( scope , locals , assign , inputs ) ;
1914+ if ( isDone ( lastValue ) ) {
1915+ scope . $$postDigest ( unwatchIfDone ) ;
19081916 }
1917+ return post ( lastValue , scope , locals ) ;
19091918 }
19101919 }
19111920
@@ -1937,27 +1946,35 @@ function $ParseProvider() {
19371946 return parsedExpression ;
19381947 }
19391948
1949+ function chainInterceptors ( first , second ) {
1950+ function chainedInterceptor ( value ) {
1951+ return second ( first ( value ) ) ;
1952+ }
1953+ chainedInterceptor . $stateful = first . $stateful || second . $stateful ;
1954+
1955+ return chainedInterceptor ;
1956+ }
1957+
19401958 function addInterceptor ( parsedExpression , interceptorFn ) {
19411959 if ( ! interceptorFn ) return parsedExpression ;
19421960
1943- var useInputs = false ;
1961+ // Extract any existing interceptors out of the parsedExpression
1962+ // to ensure the original parsedExpression is always the $$intercepted
1963+ if ( parsedExpression . $$interceptor ) {
1964+ interceptorFn = chainInterceptors ( parsedExpression . $$interceptor , interceptorFn ) ;
1965+ parsedExpression = parsedExpression . $$intercepted ;
1966+ }
19441967
1945- var isDone = parsedExpression . literal ? isAllDefined : isDefined ;
1968+ var useInputs = false ;
19461969
1947- function regularInterceptedExpression ( scope , locals , assign , inputs ) {
1970+ var fn = function interceptedExpression ( scope , locals , assign , inputs ) {
19481971 var value = useInputs && inputs ? inputs [ 0 ] : parsedExpression ( scope , locals , assign , inputs ) ;
19491972 return interceptorFn ( value ) ;
1950- }
1951-
1952- function oneTimeInterceptedExpression ( scope , locals , assign , inputs ) {
1953- var value = useInputs && inputs ? inputs [ 0 ] : parsedExpression ( scope , locals , assign , inputs ) ;
1954- var result = interceptorFn ( value ) ;
1955- // we only return the interceptor's result if the
1956- // initial value is defined (for bind-once)
1957- return isDone ( value ) ? result : value ;
1958- }
1973+ } ;
19591974
1960- var fn = parsedExpression . oneTime ? oneTimeInterceptedExpression : regularInterceptedExpression ;
1975+ // Maintain references to the interceptor/intercepted
1976+ fn . $$intercepted = parsedExpression ;
1977+ fn . $$interceptor = interceptorFn ;
19611978
19621979 // Propogate the literal/oneTime/constant attributes
19631980 fn . literal = parsedExpression . literal ;
0 commit comments