Fixing passes parallel generation (#31) by Ianbale

This commit is contained in:
Ian Bale
2019-06-07 11:14:21 +01:00
committed by Alexander Cerutti
parent ab6af17ee7
commit 0e46d855e4
2 changed files with 21 additions and 25 deletions

View File

@@ -1,21 +1,18 @@
const schema = require("./schema");
const debug = require("debug")("passkit:fields");
/**
* Pass fields must be unique (for key) in its scope.
* Therefore we use a Set to keep them tracked.
*/
const fieldsKeys = new Set();
/**
* Class to represent lower-level keys pass fields
* @see https://apple.co/2wkUBdh
*/
const poolSymbol = Symbol("pool");
class FieldsArray extends Array {
constructor(...items) {
super(...items);
constructor(pool,...args) {
super(...args);
this[poolSymbol] = pool;
}
/**
@@ -29,13 +26,13 @@ class FieldsArray extends Array {
return acc;
}
if (acc.some(e => e.key === current.key) || fieldsKeys.has(current.key)) {
debug(`Field with key "${current.key}" discarded: fields must be unique in pass scope.`);
} else {
fieldsKeys.add(current.key);
acc.push(current);
if (acc.some(e => e.key === current.key) || this[poolSymbol].has(current.key)) {
debug(`Field with key "${key}" discarded: fields must be unique in pass scope.`);
}
this[poolSymbol].add(current.key)
acc.push(current)
return acc;
}, []);
@@ -49,7 +46,7 @@ class FieldsArray extends Array {
pop() {
const element = Array.prototype.pop.call(this);
fieldsKeys.delete(element.key);
this[poolSymbol].delete(element.key)
return element;
}
@@ -60,7 +57,7 @@ class FieldsArray extends Array {
splice(start, deleteCount, ...items) {
const removeList = this.slice(start, deleteCount+start);
removeList.forEach(item => fieldsKeys.delete(item.key));
removeList.forEach(item => this[poolSymbol].delete(item.key));
return Array.prototype.splice.call(this, start, deleteCount, items);
}
@@ -68,10 +65,6 @@ class FieldsArray extends Array {
get length() {
return this.length;
}
static emptyUnique() {
fieldsKeys.clear();
}
}
module.exports = FieldsArray;

View File

@@ -44,7 +44,12 @@ class Pass {
this._fields = ["primaryFields", "secondaryFields", "auxiliaryFields", "backFields", "headerFields"];
this._fields.forEach(a => this[a] = new FieldsArray());
this.fieldsKeys = new Set();
this._fields.forEach(name => {
this[name] = new FieldsArray(this.fieldsKeys);
});
this[transitType] = "";
// Assigning model and _props to this
@@ -213,8 +218,6 @@ class Pass {
archive.pipe(passStream);
FieldsArray.emptyUnique();
return archive.finalize().then(() => passStream);
} catch (err) {
if (err.code && err.code === "ENOENT") {