Skip to content

Commit

Permalink
Fixed problem with calling crypto.randomUUID on older browsers
Browse files Browse the repository at this point in the history
  • Loading branch information
mpscholten committed Jan 4, 2023
1 parent 35bb72b commit 4035542
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion lib/IHP/DataSync/ihp-datasync.js
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ function createOptimisticRecord(table, record) {

// Ensure that the record has an ID
if (record.id === null || record.id === undefined) {
record.id = crypto.randomUUID();
record.id = randomUUID();
}

if (dataSyncController.optimisticCreatedNeedsCreatedAtField.has(table) && (record.createdAt === null || record.createdAt === undefined)) {
Expand All @@ -618,6 +618,28 @@ function createOptimisticRecord(table, record) {
dataSyncController.optimisticCreatedPendingRecordIds.push(record.id);
}

function randomUUID() {
// Some older browsers like firefox 91 ESR don't support crypto.randomUUID
// So we have a fallback to keep the app working in these browsers
try {
return crypto.randomUUID();
} catch (e) {
// https://stackoverflow.com/a/873856/14144232
// http://www.ietf.org/rfc/rfc4122.txt
var s = [];
var hexDigits = "0123456789abcdef";
for (var i = 0; i < 36; i++) {
s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
}
s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
s[8] = s[13] = s[18] = s[23] = "-";

var uuid = s.join("");
return uuid;
}
}

function undoCreateOptimisticRecord(table, record) {
const dataSyncController = DataSyncController.getInstance();
for (const dataSubscription of dataSyncController.dataSubscriptions) {
Expand Down

0 comments on commit 4035542

Please sign in to comment.