Skip to content

Commit 2b09f0e

Browse files
committed
Add maxInstances option again
This was removed in d4ec59a Default behavior is retained. So if maxInstances is not set then there is no limit.
1 parent a597c3f commit 2b09f0e

File tree

2 files changed

+62
-22
lines changed

2 files changed

+62
-22
lines changed

README.md

+17
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,23 @@ Elm.Main.init({node: document.getElementById("main")});
151151
Elm.Path.To.OtherModule.init({node: document.getElementById("other")});
152152
```
153153

154+
#### maxInstances (default `undefined`)
155+
156+
You can add `maxInstances=8` to the loader:
157+
158+
```js
159+
...
160+
use: {
161+
loader: 'elm-webpack-loader',
162+
options: {
163+
maxInstances: 8
164+
}
165+
}
166+
...
167+
```
168+
169+
Set a limit to the number of maxInstances of elm that can spawned.
170+
154171
##### Hot module reloading
155172

156173
Hot module reloading is supported by installing [elm-hot-webpack-loader](https://github.com/klazuka/elm-hot-webpack-loader)

index.js

+45-22
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ var temp = require("temp").track();
66
var loaderUtils = require("loader-utils");
77
var elmCompiler = require("node-elm-compiler");
88

9+
var runningInstances = 0;
10+
911
var 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

Comments
 (0)