Skip to content

Commit

Permalink
Add a per epoch counter to generate unique names
Browse files Browse the repository at this point in the history
Fixes #1198 this is a proposed fix to ensure unique names are generated names for identical field types at the same epoch. A per epoch counter is implemented.

Changes: New elements now have a default value of prefix-epoch-counter starting from 0

I also investigated using performance.now(), however on Safari and Firefox the resolution for performance.now() is no greater than Date().getTime().
  • Loading branch information
lucasnetau authored and kevinchappell committed Jun 24, 2021
1 parent 1eff53f commit 84a0d5c
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,21 @@ export const bindEvents = (element, events) => {
* @param {Object} field
* @return {String} name
*/
export const nameAttr = function(field) {
const epoch = new Date().getTime()
const prefix = field.type || hyphenCase(field.label)
return prefix + '-' + epoch
}
export const nameAttr = (function() {
let lepoch
let counter = 0
return function(field) {
const epoch = new Date().getTime()
if (epoch === lepoch) {
++counter
} else {
counter = 0
lepoch = epoch
}
const prefix = field.type || hyphenCase(field.label)
return prefix + '-' + epoch + '-' + counter
}
})()

/**
* Determine content type
Expand Down

0 comments on commit 84a0d5c

Please # to comment.