Skip to content

Commit 172a12c

Browse files
Refactor meta attribute parsing in insertComponentFromDom for improved clarity and reusability
1 parent ca57a8a commit 172a12c

File tree

1 file changed

+38
-36
lines changed

1 file changed

+38
-36
lines changed

src/django_unicorn/static/unicorn/js/unicorn.js

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,28 @@ export function componentInit(args) {
7474
component.setModelValues();
7575
}
7676

77+
/**
78+
* Parses the unicorn:meta attribute string into its component parts.
79+
*
80+
* The meta attribute has the format "checksum:hash:epoch" where hash and epoch
81+
* are optional.
82+
*
83+
* @param {string|null} metaAttr The value of the unicorn:meta attribute.
84+
* @returns {{ checksum: string, hash: string, epoch: string }}
85+
*/
86+
function parseMetaAttribute(metaAttr) {
87+
if (!metaAttr) {
88+
return { checksum: "", hash: "", epoch: "" };
89+
}
90+
91+
const parts = metaAttr.split(":");
92+
return {
93+
checksum: parts[0] || "",
94+
hash: parts.length > 1 ? parts[1] : "",
95+
epoch: parts.length > 2 ? parts[2] : "",
96+
};
97+
}
98+
7799
/**
78100
* Initialize the component from the DOM element if it hasn't been initialized yet.
79101
* If the component already exists, update its data and checksum if the server has
@@ -88,29 +110,15 @@ export function insertComponentFromDom(node) {
88110
const nodeId = node.getAttribute("unicorn:id");
89111

90112
if (!components[nodeId]) {
91-
const fullMeta = node.getAttribute("unicorn:meta");
92-
let meta = fullMeta;
93-
let hash = "";
94-
let epoch = "";
95-
96-
if (fullMeta && fullMeta.indexOf(":") > -1) {
97-
const parts = fullMeta.split(":");
98-
meta = parts[0];
99-
100-
if (parts.length > 1) {
101-
hash = parts[1];
102-
}
103-
104-
if (parts.length > 2) {
105-
epoch = parts[2];
106-
}
107-
}
113+
const { checksum, hash, epoch } = parseMetaAttribute(
114+
node.getAttribute("unicorn:meta")
115+
);
108116

109117
const args = {
110118
id: nodeId,
111119
name: node.getAttribute("unicorn:name"),
112120
key: node.getAttribute("unicorn:key"),
113-
checksum: meta,
121+
checksum,
114122
hash,
115123
epoch,
116124
data: JSON.parse(node.getAttribute("unicorn:data")),
@@ -123,25 +131,19 @@ export function insertComponentFromDom(node) {
123131
// component's state (e.g. a parent method modified it), the DOM will have a
124132
// different checksum in unicorn:meta. In that case, sync the JS component's
125133
// data so the next request doesn't send stale data that overwrites the update.
126-
const fullMeta = node.getAttribute("unicorn:meta");
127-
if (fullMeta) {
128-
const parts = fullMeta.split(":");
129-
const newChecksum = parts[0];
130-
131-
if (newChecksum && newChecksum !== components[nodeId].checksum) {
132-
const newDataAttr = node.getAttribute("unicorn:data");
133-
if (newDataAttr) {
134-
components[nodeId].data = JSON.parse(newDataAttr);
135-
}
136-
components[nodeId].checksum = newChecksum;
137-
if (parts.length > 1) {
138-
components[nodeId].hash = parts[1];
139-
}
140-
if (parts.length > 2) {
141-
components[nodeId].epoch = parts[2];
142-
}
143-
components[nodeId].setModelValues();
134+
const { checksum: newChecksum, hash, epoch } = parseMetaAttribute(
135+
node.getAttribute("unicorn:meta")
136+
);
137+
138+
if (newChecksum && newChecksum !== components[nodeId].checksum) {
139+
const newDataAttr = node.getAttribute("unicorn:data");
140+
if (newDataAttr) {
141+
components[nodeId].data = JSON.parse(newDataAttr);
144142
}
143+
components[nodeId].checksum = newChecksum;
144+
components[nodeId].hash = hash;
145+
components[nodeId].epoch = epoch;
146+
components[nodeId].setModelValues();
145147
}
146148
}
147149
}

0 commit comments

Comments
 (0)