@@ -6,6 +6,8 @@ var temp = require("temp").track();
66var loaderUtils = require ( "loader-utils" ) ;
77var elmCompiler = require ( "node-elm-compiler" ) ;
88
9+ var runningInstances = 0 ;
10+
911var getFiles = function ( options ) {
1012 var files = options && options . files ;
1113
@@ -95,6 +97,14 @@ module.exports = function() {
9597 var files = getFiles . call ( this , options ) ;
9698 var resourcePath = this . resourcePath ;
9799
100+ var maxInstances = options . maxInstances ;
101+
102+ if ( typeof maxInstances === "undefined" ) {
103+ maxInstances = null ;
104+ } else {
105+ delete options . maxInstances ;
106+ }
107+
98108 var promises = [ ] ;
99109
100110 // we only need to track deps if we are in watch mode
@@ -124,30 +134,43 @@ module.exports = function() {
124134 promises . push ( dependencies ) ;
125135 }
126136
127- var compilation = compile ( files , options )
128- . then ( function ( v ) { return { kind : "success" , result : v } ; } )
129- . catch ( function ( v ) { return { kind : "error" , error : v } ; } ) ;
130-
131- promises . push ( compilation ) ;
132-
133- Promise . all ( promises )
134- . then ( function ( results ) {
135- var output = results [ results . length - 1 ] ; // compilation output is always last
137+ var intervalId ;
138+
139+ var run = function ( ) {
140+ if ( maxInstances !== null && runningInstances >= maxInstances ) { return false } ;
141+ runningInstances += 1 ;
142+ clearInterval ( intervalId ) ;
143+
144+ var compilation = compile ( files , options )
145+ . then ( function ( v ) { runningInstances -= 1 ; return { kind : "success" , result : v } ; } )
146+ . catch ( function ( v ) { runningInstances -= 1 ; return { kind : "error" , error : v } ; } ) ;
147+
148+ promises . push ( compilation ) ;
149+
150+ Promise . all ( promises )
151+ . then ( function ( results ) {
152+ var output = results [ results . length - 1 ] ; // compilation output is always last
153+
154+ if ( output . kind === "success" ) {
155+ callback ( null , output . result ) ;
156+ } else {
157+ if ( typeof output . error === "string" ) {
158+ output . error = new Error ( output . error ) ;
159+ }
160+
161+ output . error . message = "Compiler process exited with error " + output . error . message ;
162+ output . error . stack = null ;
163+ callback ( output . error ) ;
164+ }
165+ } ) . catch ( function ( err ) {
166+ callback ( err ) ;
167+ } ) ;
136168
137- if ( output . kind === "success" ) {
138- callback ( null , output . result ) ;
139- } else {
140- if ( typeof output . error === "string" ) {
141- output . error = new Error ( output . error ) ;
142- }
169+ } ;
143170
144- output . error . message = "Compiler process exited with error " + output . error . message ;
145- output . error . stack = null ;
146- callback ( output . error ) ;
147- }
148- } ) . catch ( function ( err ) {
149- callback ( err ) ;
150- } ) ;
171+ if ( run ( ) === false ) {
172+ intervalId = setInterval ( run , 200 ) ;
173+ }
151174}
152175
153176
0 commit comments