@@ -28,7 +28,7 @@ export function wrapDiffingPlugin(pluginClass): DiffingPluginWrapperFactory {
2828
2929
3030export interface DiffingBroccoliPlugin {
31- rebuild ( diff : ( DiffResult | DiffResult [ ] ) ) : ( Promise < any > | void ) ;
31+ rebuild ( diff : ( DiffResult | DiffResult [ ] ) ) : ( Promise < DiffResult | void > | DiffResult | void ) ;
3232 cleanup ? ( ) : void ;
3333}
3434
@@ -52,6 +52,8 @@ class DiffingPluginWrapper implements BroccoliTree {
5252 cachePath = null ;
5353 outputPath = null ;
5454
55+ private diffResult : DiffResult = null ;
56+
5557 constructor ( private pluginClass , private wrappedPluginArguments ) {
5658 if ( Array . isArray ( wrappedPluginArguments [ 0 ] ) ) {
5759 this . inputTrees = this . stabilizeTrees ( wrappedPluginArguments [ 0 ] ) ;
@@ -62,33 +64,59 @@ class DiffingPluginWrapper implements BroccoliTree {
6264 this . description = this . pluginClass . name ;
6365 }
6466
65- private calculateDiff ( firstRun : boolean ) : ( DiffResult | DiffResult [ ] ) {
66- // TODO(caitp): optionally log trees based on environment variable or
67- // command line option. It may be worth logging for trees where elapsed
68- // time exceeds some threshold, like 10ms.
69- if ( this . treeDiffer ) {
70- return this . treeDiffer . diffTree ( ) ;
71- } else if ( this . treeDiffers ) {
72- return this . treeDiffers . map ( ( treeDiffer ) => treeDiffer . diffTree ( ) ) ;
67+ private getDiffResult ( ) : ( DiffResult | DiffResult [ ] ) {
68+ let returnOrCalculateDiffResult = ( tree , index ) => {
69+ // returnOrCalculateDiffResult will do one of two things:
70+ //
71+ // If `this.diffResult` is null, calculate a DiffResult using TreeDiffer
72+ // for the input tree.
73+ //
74+ // Otherwise, `this.diffResult` was produced from the output of the
75+ // inputTree's rebuild() method, and can be used without being checked.
76+ // Set `this.diffResult` to null and return the previously stored value.
77+ if ( ! tree . diffResult ) {
78+ let differ = index === false ? this . treeDiffer : this . treeDiffers [ index ] ;
79+ return differ . diffTree ( ) ;
80+ }
81+ let diffResult = tree . diffResult ;
82+ tree . diffResult = null ;
83+ return diffResult ;
84+ } ;
85+
86+ if ( this . inputTrees ) {
87+ return this . inputTrees . map ( returnOrCalculateDiffResult ) ;
88+ } else if ( this . inputTree ) {
89+ return returnOrCalculateDiffResult ( this . inputTree , false ) ;
7390 } else {
7491 throw new Error ( "Missing TreeDiffer" ) ;
7592 }
7693 }
7794
95+ private maybeStoreDiffResult ( value : ( DiffResult | void ) ) {
96+ this . diffResult = value ? < DiffResult > ( value ) : null ;
97+ }
7898
7999 rebuild ( ) {
80100 try {
81101 let firstRun = ! this . initialized ;
82102 this . init ( ) ;
83103
84- let diffResult = this . calculateDiff ( firstRun ) ;
104+ let diffResult = this . getDiffResult ( ) ;
85105
86- var rebuildPromise = this . wrappedPlugin . rebuild ( diffResult ) ;
106+ let result = this . wrappedPlugin . rebuild ( diffResult ) ;
87107
88- if ( rebuildPromise ) {
89- return ( < Promise < any > > rebuildPromise ) . then ( this . relinkOutputAndCachePaths . bind ( this ) ) ;
108+ if ( result ) {
109+ let resultPromise = < Promise < DiffResult | void > > ( result ) ;
110+ if ( resultPromise . then ) {
111+ // rebuild() -> Promise<>
112+ return resultPromise . then ( ( result : ( DiffResult | void ) ) => {
113+ this . maybeStoreDiffResult ( result ) ;
114+ this . relinkOutputAndCachePaths ( ) ;
115+ } ) ;
116+ }
90117 }
91118
119+ this . maybeStoreDiffResult ( < ( DiffResult | void ) > ( result ) ) ;
92120 this . relinkOutputAndCachePaths ( ) ;
93121 } catch ( e ) {
94122 e . message = `[${ this . description } ]: ${ e . message } ` ;
0 commit comments