Skip to content

[LiveComponent] Avoid polling over renders #278

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions src/LiveComponent/assets/dist/live_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -1107,7 +1107,7 @@ class default_1 extends Controller {
});
}
$render() {
this._makeRequest(null);
this._makeRequest(null, {});
}
_getValueFromElement(element) {
return element.dataset.value || element.value;
Expand All @@ -1121,17 +1121,18 @@ class default_1 extends Controller {
const clonedElement = cloneHTMLElement(element);
throw new Error(`The update() method could not be called for "${clonedElement.outerHTML}": the element must either have a "data-model" or "name" attribute set to the model name.`);
}
let finalValue = value;
if (/\[]$/.test(model)) {
const { currentLevelData, finalKey } = parseDeepData(this.dataValue, normalizeModelName(model));
const currentValue = currentLevelData[finalKey];
value = updateArrayDataFromChangedElement(element, value, currentValue);
finalValue = updateArrayDataFromChangedElement(element, value, currentValue);
}
else if (element instanceof HTMLInputElement
&& element.type === 'checkbox'
&& !element.checked) {
value = null;
finalValue = null;
}
this.$updateModel(model, value, shouldRender, element.hasAttribute('name') ? element.getAttribute('name') : null, {});
this.$updateModel(model, finalValue, shouldRender, element.hasAttribute('name') ? element.getAttribute('name') : null, {});
}
$updateModel(model, value, shouldRender = true, extraModelName = null, options = {}) {
const directives = parseDirectives(model);
Expand Down Expand Up @@ -1427,10 +1428,13 @@ class default_1 extends Controller {
}
else {
callback = () => {
this._makeRequest(actionName);
this._makeRequest(actionName, {});
};
}
const timer = setInterval(() => {
if (this.renderPromiseStack.countActivePromises() > 0) {
return;
}
callback();
}, duration);
this.pollingIntervals.push(timer);
Expand Down Expand Up @@ -1539,6 +1543,9 @@ class PromiseStack {
findPromiseIndex(promise) {
return this.stack.findIndex((item) => item === promise);
}
countActivePromises() {
return this.stack.length;
}
}
const parseLoadingAction = function (action, isLoading) {
switch (action) {
Expand Down
22 changes: 16 additions & 6 deletions src/LiveComponent/assets/src/live_controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ export default class extends Controller {
}

$render() {
this._makeRequest(null);
this._makeRequest(null, {});
}

_getValueFromElement(element: HTMLElement|SVGElement) {
Expand All @@ -206,22 +206,23 @@ export default class extends Controller {
// HTML form elements with name ending with [] require array as data
// we need to handle addition and removal of values from it to send
// back only required data
let finalValue : string|null|string[] = value
if (/\[]$/.test(model)) {
// Get current value from data
const { currentLevelData, finalKey } = parseDeepData(this.dataValue, normalizeModelName(model))
const currentValue = currentLevelData[finalKey];

value = updateArrayDataFromChangedElement(element, value, currentValue);
finalValue = updateArrayDataFromChangedElement(element, value, currentValue);
} else if (
element instanceof HTMLInputElement
&& element.type === 'checkbox'
&& !element.checked
) {
// Unchecked checkboxes in a single value scenarios should map to `null`
value = null;
finalValue = null;
}

this.$updateModel(model, value, shouldRender, element.hasAttribute('name') ? element.getAttribute('name') : null, {});
this.$updateModel(model, finalValue, shouldRender, element.hasAttribute('name') ? element.getAttribute('name') : null, {});
}

/**
Expand Down Expand Up @@ -309,7 +310,7 @@ export default class extends Controller {
}
}

_makeRequest(action: string|null, args: Record<string,unknown>) {
_makeRequest(action: string|null, args: Record<string, string>) {
const splitUrl = this.urlValue.split('?');
let [url] = splitUrl
const [, queryString] = splitUrl;
Expand Down Expand Up @@ -659,11 +660,16 @@ export default class extends Controller {
}
} else {
callback = () => {
this._makeRequest(actionName);
this._makeRequest(actionName, {});
}
}

const timer = setInterval(() => {
// if there is already an active render promise, skip the poll
if (this.renderPromiseStack.countActivePromises() > 0) {
return;
}

callback();
}, duration);
this.pollingIntervals.push(timer);
Expand Down Expand Up @@ -820,6 +826,10 @@ class PromiseStack {
findPromiseIndex(promise: Promise<any>) {
return this.stack.findIndex((item) => item === promise);
}

countActivePromises(): number {
return this.stack.length;
}
}

const parseLoadingAction = function(action: string, isLoading: boolean) {
Expand Down