Removed fieldKeysPoolSymbol based on instance to create one when FieldsArray are created. This also solves a keys reset problem when fields gets created from scratch

This commit is contained in:
Alexander Cerutti
2021-10-23 18:40:22 +02:00
parent bf2f72b14d
commit 03643834f1
2 changed files with 15 additions and 14 deletions

View File

@@ -1,5 +1,4 @@
import PKPass from "./PKPass"; import PKPass from "./PKPass";
import { fieldKeysPoolSymbol } from "./PKPass";
import * as Schemas from "./schemas"; import * as Schemas from "./schemas";
import * as Utils from "./utils"; import * as Utils from "./utils";
import formatMessage, * as Messages from "./messages"; import formatMessage, * as Messages from "./messages";
@@ -10,16 +9,20 @@ import formatMessage, * as Messages from "./messages";
*/ */
const passInstanceSymbol = Symbol("passInstance"); const passInstanceSymbol = Symbol("passInstance");
const sharedKeysPoolSymbol = Symbol("keysPool");
export default class FieldsArray extends Array<Schemas.Field> { export default class FieldsArray extends Array<Schemas.Field> {
private [passInstanceSymbol]: InstanceType<typeof PKPass>; private [passInstanceSymbol]: InstanceType<typeof PKPass>;
private [sharedKeysPoolSymbol]: Set<string>;
constructor( constructor(
passInstance: InstanceType<typeof PKPass>, passInstance: InstanceType<typeof PKPass>,
keysPool: Set<string>,
...args: Schemas.Field[] ...args: Schemas.Field[]
) { ) {
super(...args); super(...args);
this[passInstanceSymbol] = passInstance; this[passInstanceSymbol] = passInstance;
this[sharedKeysPoolSymbol] = keysPool;
} }
/** /**
@@ -43,9 +46,7 @@ export default class FieldsArray extends Array<Schemas.Field> {
return acc; return acc;
} }
const pool = this[passInstanceSymbol][fieldKeysPoolSymbol]; if (this[sharedKeysPoolSymbol].has(current.key)) {
if (pool.has(current.key)) {
console.warn( console.warn(
formatMessage( formatMessage(
Messages.FIELDS.REPEATED_KEY, Messages.FIELDS.REPEATED_KEY,
@@ -55,7 +56,7 @@ export default class FieldsArray extends Array<Schemas.Field> {
return acc; return acc;
} }
pool.add(current.key); this[sharedKeysPoolSymbol].add(current.key);
return [...acc, current]; return [...acc, current];
}, },
[], [],
@@ -73,7 +74,7 @@ export default class FieldsArray extends Array<Schemas.Field> {
Utils.assertUnfrozen(this[passInstanceSymbol]); Utils.assertUnfrozen(this[passInstanceSymbol]);
const element: Schemas.Field = super.pop(); const element: Schemas.Field = super.pop();
this[passInstanceSymbol][fieldKeysPoolSymbol].delete(element.key); this[sharedKeysPoolSymbol].delete(element.key);
return element; return element;
} }
@@ -91,7 +92,7 @@ export default class FieldsArray extends Array<Schemas.Field> {
const removeList = this.slice(start, deleteCount + start); const removeList = this.slice(start, deleteCount + start);
removeList.forEach((item) => removeList.forEach((item) =>
this[passInstanceSymbol][fieldKeysPoolSymbol].delete(item.key), this[sharedKeysPoolSymbol].delete(item.key),
); );
return super.splice(start, deleteCount, ...items); return super.splice(start, deleteCount, ...items);

View File

@@ -11,7 +11,6 @@ import formatMessage, * as Messages from "./messages";
/** Exporting for tests specs */ /** Exporting for tests specs */
export const propsSymbol = Symbol("props"); export const propsSymbol = Symbol("props");
export const localizationSymbol = Symbol("pass.l10n"); export const localizationSymbol = Symbol("pass.l10n");
export const fieldKeysPoolSymbol = Symbol("fieldKeysPoolSymbol");
export const importMetadataSymbol = Symbol("import.pass.metadata"); export const importMetadataSymbol = Symbol("import.pass.metadata");
export const createManifestSymbol = Symbol("pass.manifest"); export const createManifestSymbol = Symbol("pass.manifest");
export const closePassSymbol = Symbol("pass.close"); export const closePassSymbol = Symbol("pass.close");
@@ -20,7 +19,6 @@ export const certificatesSymbol = Symbol("pass.certificates");
export default class PKPass extends Bundle { export default class PKPass extends Bundle {
private [certificatesSymbol]: Schemas.CertificatesSchema; private [certificatesSymbol]: Schemas.CertificatesSchema;
private [fieldKeysPoolSymbol] = new Set<string>();
private [propsSymbol]: Schemas.PassProps = {}; private [propsSymbol]: Schemas.PassProps = {};
private [localizationSymbol]: { private [localizationSymbol]: {
[lang: string]: { [lang: string]: {
@@ -310,13 +308,15 @@ export default class PKPass extends Bundle {
this[propsSymbol][this.type] = undefined; this[propsSymbol][this.type] = undefined;
} }
const sharedKeysPool = new Set<string>();
this[passTypeSymbol] = type; this[passTypeSymbol] = type;
this[propsSymbol][this[passTypeSymbol]] = { this[propsSymbol][this[passTypeSymbol]] = {
headerFields /******/: new FieldsArray(this), headerFields /******/: new FieldsArray(this, sharedKeysPool),
primaryFields /*****/: new FieldsArray(this), primaryFields /*****/: new FieldsArray(this, sharedKeysPool),
secondaryFields /***/: new FieldsArray(this), secondaryFields /***/: new FieldsArray(this, sharedKeysPool),
auxiliaryFields /***/: new FieldsArray(this), auxiliaryFields /***/: new FieldsArray(this, sharedKeysPool),
backFields /********/: new FieldsArray(this), backFields /********/: new FieldsArray(this, sharedKeysPool),
transitType: undefined, transitType: undefined,
}; };
} }