Skip to content

Add maxInstances option again #228

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,23 @@ Elm.Main.init({node: document.getElementById("main")});
Elm.Path.To.OtherModule.init({node: document.getElementById("other")});
```

#### maxInstances (default `undefined`)

You can add `maxInstances=8` to the loader:

```js
...
use: {
loader: 'elm-webpack-loader',
options: {
maxInstances: 8
}
}
...
```

Set a limit to the number of maxInstances of elm that can spawned.

##### Hot module reloading

Hot module reloading is supported by installing [elm-hot-webpack-loader](https://github.com/klazuka/elm-hot-webpack-loader)
Expand Down
67 changes: 45 additions & 22 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ var temp = require("temp").track();
var loaderUtils = require("loader-utils");
var elmCompiler = require("node-elm-compiler");

var runningInstances = 0;

var getFiles = function(options) {
var files = options && options.files;

Expand Down Expand Up @@ -95,6 +97,14 @@ module.exports = function() {
var files = getFiles.call(this, options);
var resourcePath = this.resourcePath;

var maxInstances = options.maxInstances;

if (typeof maxInstances === "undefined"){
maxInstances = null;
} else {
delete options.maxInstances;
}

var promises = [];

// we only need to track deps if we are in watch mode
Expand Down Expand Up @@ -124,30 +134,43 @@ module.exports = function() {
promises.push(dependencies);
}

var compilation = compile(files, options)
.then(function(v) { return { kind: "success", result: v }; })
.catch(function(v) { return { kind: "error", error: v }; });

promises.push(compilation);

Promise.all(promises)
.then(function(results) {
var output = results[results.length - 1]; // compilation output is always last
var intervalId;

var run = function() {
if (maxInstances !== null && runningInstances >= maxInstances) { return false };
runningInstances += 1;
clearInterval(intervalId);

var compilation = compile(files, options)
.then(function(v) { runningInstances -= 1; return { kind: "success", result: v }; })
.catch(function(v) { runningInstances -= 1; return { kind: "error", error: v }; });

promises.push(compilation);

Promise.all(promises)
.then(function(results) {
var output = results[results.length - 1]; // compilation output is always last

if (output.kind === "success") {
callback(null, output.result);
} else {
if (typeof output.error === "string") {
output.error = new Error(output.error);
}

output.error.message = "Compiler process exited with error " + output.error.message;
output.error.stack = null;
callback(output.error);
}
}).catch(function(err){
callback(err);
});

if (output.kind === "success") {
callback(null, output.result);
} else {
if (typeof output.error === "string") {
output.error = new Error(output.error);
}
};

output.error.message = "Compiler process exited with error " + output.error.message;
output.error.stack = null;
callback(output.error);
}
}).catch(function(err){
callback(err);
});
if (run() === false) {
intervalId = setInterval(run, 200);
}
}


Expand Down