@@ -23,6 +23,13 @@ export interface AssetPublishingOptions {
23
23
* @default true
24
24
*/
25
25
readonly throwOnError ?: boolean ;
26
+
27
+ /**
28
+ * Whether to publish in parallel
29
+ *
30
+ * @default false
31
+ */
32
+ readonly publishInParallel ?: boolean ;
26
33
}
27
34
28
35
/**
@@ -56,44 +63,33 @@ export class AssetPublishing implements IPublishProgress {
56
63
private readonly totalOperations : number ;
57
64
private completedOperations : number = 0 ;
58
65
private aborted = false ;
66
+ private readonly handlerHost : IHandlerHost ;
67
+ private readonly publishInParallel : boolean ;
59
68
60
69
constructor ( private readonly manifest : AssetManifest , private readonly options : AssetPublishingOptions ) {
61
70
this . assets = manifest . entries ;
62
71
this . totalOperations = this . assets . length ;
63
- }
72
+ this . publishInParallel = options . publishInParallel ?? false ;
64
73
65
- /**
66
- * Publish all assets from the manifest
67
- */
68
- public async publish ( ) : Promise < void > {
69
74
const self = this ;
70
-
71
- const handlerHost : IHandlerHost = {
75
+ this . handlerHost = {
72
76
aws : this . options . aws ,
73
77
get aborted ( ) { return self . aborted ; } ,
74
78
emitMessage ( t , m ) { self . progressEvent ( t , m ) ; } ,
75
79
} ;
80
+ }
76
81
77
- for ( const asset of this . assets ) {
78
- if ( this . aborted ) { break ; }
79
- this . currentAsset = asset ;
80
-
81
- try {
82
- if ( this . progressEvent ( EventType . START , `Publishing ${ asset . id } ` ) ) { break ; }
83
-
84
- const handler = makeAssetHandler ( this . manifest , asset , handlerHost ) ;
85
- await handler . publish ( ) ;
86
-
87
- if ( this . aborted ) {
88
- throw new Error ( 'Aborted' ) ;
82
+ /**
83
+ * Publish all assets from the manifest
84
+ */
85
+ public async publish ( ) : Promise < void > {
86
+ if ( this . publishInParallel ) {
87
+ await Promise . all ( this . assets . map ( async ( asset ) => this . publishAsset ( asset ) ) ) ;
88
+ } else {
89
+ for ( const asset of this . assets ) {
90
+ if ( ! await this . publishAsset ( asset ) ) {
91
+ break ;
89
92
}
90
-
91
- this . completedOperations ++ ;
92
- if ( this . progressEvent ( EventType . SUCCESS , `Published ${ asset . id } ` ) ) { break ; }
93
- } catch ( e ) {
94
- this . failures . push ( { asset, error : e } ) ;
95
- this . completedOperations ++ ;
96
- if ( this . progressEvent ( EventType . FAIL , e . message ) ) { break ; }
97
93
}
98
94
}
99
95
@@ -102,6 +98,33 @@ export class AssetPublishing implements IPublishProgress {
102
98
}
103
99
}
104
100
101
+ /**
102
+ * Publish an asset.
103
+ * @param asset The asset to publish
104
+ * @returns false when publishing should stop
105
+ */
106
+ private async publishAsset ( asset : IManifestEntry ) {
107
+ try {
108
+ if ( this . progressEvent ( EventType . START , `Publishing ${ asset . id } ` ) ) { return false ; }
109
+
110
+ const handler = makeAssetHandler ( this . manifest , asset , this . handlerHost ) ;
111
+ await handler . publish ( ) ;
112
+
113
+ if ( this . aborted ) {
114
+ throw new Error ( 'Aborted' ) ;
115
+ }
116
+
117
+ this . completedOperations ++ ;
118
+ if ( this . progressEvent ( EventType . SUCCESS , `Published ${ asset . id } ` ) ) { return false ; }
119
+ } catch ( e ) {
120
+ this . failures . push ( { asset, error : e } ) ;
121
+ this . completedOperations ++ ;
122
+ if ( this . progressEvent ( EventType . FAIL , e . message ) ) { return false ; }
123
+ }
124
+
125
+ return true ;
126
+ }
127
+
105
128
public get percentComplete ( ) {
106
129
if ( this . totalOperations === 0 ) { return 100 ; }
107
130
return Math . floor ( ( this . completedOperations / this . totalOperations ) * 100 ) ;
0 commit comments