Skip to content

Commit b1c656c

Browse files
authored
Merge pull request #36 from hydrostack/35-state-is-not-persisted-on-subsequent-action-calls
Make sure we don't skip morphing when dealing with component tag. Mak…
2 parents 0f9978b + f1dfbc1 commit b1c656c

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

src/Scripts/hydro.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@
113113
await hydroRequest(el, url, { eventData: { name: wireEventData.name, data: eventData.data, subject: eventData.subject } }, 'event', wireEventData, operationId);
114114
}
115115

116-
async function hydroBind(el) {
116+
async function hydroBind(el, debounce) {
117117
if (!isElementDirty(el)) {
118118
return;
119119
}
@@ -165,7 +165,8 @@
165165
clearTimeout(binding[component.id].timeout);
166166
}
167167

168-
return await new Promise(resolve => {
168+
169+
binding[component.id].promise = new Promise(resolve => {
169170
binding[component.id].timeout = setTimeout(async () => {
170171
const requestFormData = binding[component.id].formData;
171172
// binding[url].formData = new FormData();
@@ -176,8 +177,10 @@
176177
delete dirty[propertyName];
177178
}
178179
resolve();
179-
}, 10);
180+
}, Math.max(debounce, 10));
180181
});
182+
183+
return binding[component.id].promise;
181184
}
182185

183186
async function hydroAction(el, component, action) {
@@ -234,6 +237,12 @@
234237
operationStatus[operationId]++;
235238
}
236239

240+
if (type === 'action') {
241+
if (binding[componentId]?.promise) {
242+
await binding[componentId].promise;
243+
}
244+
}
245+
237246
await enqueueHydroPromise(componentId, async () => {
238247
try {
239248
let headers = {
@@ -327,7 +336,7 @@
327336
}
328337

329338
if (from.getAttribute && from.getAttribute("hydro-operation-id")) {
330-
if (type === 'bind' && operationId !== from.getAttribute("hydro-operation-id")) {
339+
if (type === 'bind' && operationId !== from.getAttribute("hydro-operation-id") && from.getAttribute("hydro") === null) {
331340
skip();
332341
counter++;
333342
return;
@@ -423,6 +432,7 @@
423432
operationTrigger.forEach(trigger => {
424433
trigger.disabled = false;
425434
trigger.classList.remove('hydro-request');
435+
trigger.removeAttribute('hydro-operation-id');
426436
})
427437
}
428438
}
@@ -490,10 +500,6 @@ document.addEventListener('alpine:init', () => {
490500

491501
const eventName = el.getAttribute('hydro-event') || 'click';
492502

493-
if (!component.element.parentElement) {
494-
debugger;
495-
}
496-
497503
const parentComponent = window.Hydro.findComponent(component.element.parentElement);
498504

499505
const trigger = JSON.parse(expression);
@@ -537,19 +543,13 @@ document.addEventListener('alpine:init', () => {
537543

538544
const debounce = parseInt(((modifiers[0] === 'debounce' && (modifiers[1] || '250ms')) || '0ms').replace('ms', ''));
539545

540-
let timeout = 0;
541-
542546
const eventHandler = async (event) => {
543547
if (event === 'submit' || event === 'click') {
544548
event.preventDefault();
545549
}
546550

547-
clearTimeout(timeout);
548-
549551
const target = event.currentTarget;
550-
timeout = setTimeout(async () => {
551-
await window.Hydro.hydroBind(target);
552-
}, debounce);
552+
await window.Hydro.hydroBind(target, debounce);
553553
};
554554

555555
el.addEventListener(event, eventHandler);

0 commit comments

Comments
 (0)