🚧 Fixes to the part dependency resolver
This commit is contained in:
parent
87b097e9d8
commit
6aa14f0613
2 changed files with 65 additions and 27 deletions
|
@ -37,31 +37,6 @@ export default function Pattern(config = { options: {} }) {
|
||||||
if (typeof this.config.dependencies === "undefined")
|
if (typeof this.config.dependencies === "undefined")
|
||||||
this.config.dependencies = {};
|
this.config.dependencies = {};
|
||||||
if (typeof this.config.inject === "undefined") this.config.inject = {};
|
if (typeof this.config.inject === "undefined") this.config.inject = {};
|
||||||
else {
|
|
||||||
for (let i in this.config.inject) {
|
|
||||||
if (typeof this.config.dependencies[i] === "undefined")
|
|
||||||
this.config.dependencies[i] = this.config.inject[i];
|
|
||||||
else if (this.config.dependencies[i] !== this.config.inject[i]) {
|
|
||||||
if (typeof this.config.dependencies[i] === "string")
|
|
||||||
this.config.dependencies[i] = [
|
|
||||||
this.config.dependencies[i],
|
|
||||||
this.config.inject[i]
|
|
||||||
];
|
|
||||||
else if (Array.isArray(this.config.dependencies[i])) {
|
|
||||||
if (this.config.dependencies[i].indexOf(i) === -1)
|
|
||||||
this.config.dependencies[i].push(i);
|
|
||||||
} else
|
|
||||||
throw new Error(
|
|
||||||
"Part dependencies should be a string or an array of strings"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
// Parts both in the parts and dependencies array trip up the dependency resolver
|
|
||||||
if (Array.isArray(this.config.parts)) {
|
|
||||||
let pos = this.config.parts.indexOf(this.config.inject[i]);
|
|
||||||
if (pos !== -1) this.config.parts.splice(pos, 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (typeof this.config.hide === "undefined") this.config.hide = [];
|
if (typeof this.config.hide === "undefined") this.config.hide = [];
|
||||||
this.config.resolvedDependencies = this.resolveDependencies(
|
this.config.resolvedDependencies = this.resolveDependencies(
|
||||||
this.config.dependencies
|
this.config.dependencies
|
||||||
|
@ -507,9 +482,10 @@ Pattern.prototype.resolveDependency = function(
|
||||||
graph = this.config.dependencies,
|
graph = this.config.dependencies,
|
||||||
deps = []
|
deps = []
|
||||||
) {
|
) {
|
||||||
|
console.log("resolving", part, JSON.stringify(graph));
|
||||||
if (typeof seen[part] === "undefined") seen[part] = true;
|
if (typeof seen[part] === "undefined") seen[part] = true;
|
||||||
if (typeof graph[part] === "string") {
|
if (typeof graph[part] === "string") {
|
||||||
deps.push(graph[part]);
|
if (deps.indexOf(graph[part]) === -1) deps.push(graph[part]);
|
||||||
return this.resolveDependency(seen, graph[part], graph, deps);
|
return this.resolveDependency(seen, graph[part], graph, deps);
|
||||||
} else if (Array.isArray(graph[part])) {
|
} else if (Array.isArray(graph[part])) {
|
||||||
if (graph[part].length === 0) return [];
|
if (graph[part].length === 0) return [];
|
||||||
|
@ -527,6 +503,28 @@ Pattern.prototype.resolveDependency = function(
|
||||||
Pattern.prototype.resolveDependencies = function(
|
Pattern.prototype.resolveDependencies = function(
|
||||||
graph = this.config.dependencies
|
graph = this.config.dependencies
|
||||||
) {
|
) {
|
||||||
|
for (let i in this.config.inject) {
|
||||||
|
let dependency = this.config.inject[i];
|
||||||
|
if (typeof this.config.dependencies[i] === "undefined")
|
||||||
|
this.config.dependencies[i] = dependency;
|
||||||
|
else if (this.config.dependencies[i] !== dependency) {
|
||||||
|
if (typeof this.config.dependencies[i] === "string")
|
||||||
|
this.config.dependencies[i] = [this.config.dependencies[i], dependency];
|
||||||
|
else if (Array.isArray(this.config.dependencies[i])) {
|
||||||
|
if (this.config.dependencies[i].indexOf(dependency) === -1)
|
||||||
|
this.config.dependencies[i].push(dependency);
|
||||||
|
} else
|
||||||
|
throw new Error(
|
||||||
|
"Part dependencies should be a string or an array of strings"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
// Parts both in the parts and dependencies array trip up the dependency resolver
|
||||||
|
if (Array.isArray(this.config.parts)) {
|
||||||
|
let pos = this.config.parts.indexOf(this.config.inject[i]);
|
||||||
|
if (pos !== -1) this.config.parts.splice(pos, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Include parts outside the dependency graph
|
// Include parts outside the dependency graph
|
||||||
if (Array.isArray(this.config.parts)) {
|
if (Array.isArray(this.config.parts)) {
|
||||||
for (let part of this.config.parts) {
|
for (let part of this.config.parts) {
|
||||||
|
|
|
@ -115,7 +115,6 @@ it("Design constructor discover all parts", () => {
|
||||||
parts: ["step1", "step2"]
|
parts: ["step1", "step2"]
|
||||||
});
|
});
|
||||||
let pattern = new design();
|
let pattern = new design();
|
||||||
console.log("order is", pattern.config.draftOrder);
|
|
||||||
expect(pattern.config.draftOrder[0]).to.equal("step3");
|
expect(pattern.config.draftOrder[0]).to.equal("step3");
|
||||||
expect(pattern.config.draftOrder[1]).to.equal("step4");
|
expect(pattern.config.draftOrder[1]).to.equal("step4");
|
||||||
expect(pattern.config.draftOrder[2]).to.equal("step5");
|
expect(pattern.config.draftOrder[2]).to.equal("step5");
|
||||||
|
@ -128,3 +127,44 @@ it("Design constructor discover all parts", () => {
|
||||||
expect(pattern.config.draftOrder[9]).to.equal("step1");
|
expect(pattern.config.draftOrder[9]).to.equal("step1");
|
||||||
expect(pattern.config.draftOrder[10]).to.equal("step2");
|
expect(pattern.config.draftOrder[10]).to.equal("step2");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("Design constructor should handle Simon", () => {
|
||||||
|
let design = new freesewing.Design({
|
||||||
|
dependencies: {
|
||||||
|
sleeve: ["front", "back"]
|
||||||
|
},
|
||||||
|
inject: {
|
||||||
|
frontBase: "base",
|
||||||
|
backBase: "base",
|
||||||
|
back: "backBase",
|
||||||
|
front: "frontBase",
|
||||||
|
frontRight: "front",
|
||||||
|
frontLeft: "front",
|
||||||
|
buttonPlacket: "front",
|
||||||
|
buttonholePlacket: "front",
|
||||||
|
yoke: "backBase",
|
||||||
|
sleeve: "sleeveBase"
|
||||||
|
},
|
||||||
|
parts: [
|
||||||
|
"collarStand",
|
||||||
|
"collar",
|
||||||
|
"sleevePlacketUnderlap",
|
||||||
|
"sleevePlacketOverlap",
|
||||||
|
"cuff"
|
||||||
|
],
|
||||||
|
hide: ["base", "frontBase", "front", "backBase", "sleeveBase"]
|
||||||
|
});
|
||||||
|
let pattern = new design();
|
||||||
|
console.log(pattern.config);
|
||||||
|
//expect(pattern.config.draftOrder[0]).to.equal("step3");
|
||||||
|
//expect(pattern.config.draftOrder[1]).to.equal("step4");
|
||||||
|
//expect(pattern.config.draftOrder[2]).to.equal("step5");
|
||||||
|
//expect(pattern.config.draftOrder[3]).to.equal("step6");
|
||||||
|
//expect(pattern.config.draftOrder[4]).to.equal("step7");
|
||||||
|
//expect(pattern.config.draftOrder[5]).to.equal("step8");
|
||||||
|
//expect(pattern.config.draftOrder[6]).to.equal("step9");
|
||||||
|
//expect(pattern.config.draftOrder[7]).to.equal("step10");
|
||||||
|
//expect(pattern.config.draftOrder[8]).to.equal("step11");
|
||||||
|
//expect(pattern.config.draftOrder[9]).to.equal("step1");
|
||||||
|
//expect(pattern.config.draftOrder[10]).to.equal("step2");
|
||||||
|
});
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue