Skip to content

Commit

Permalink
Improved callback processing.
Browse files Browse the repository at this point in the history
  • Loading branch information
feuzeu committed Jun 6, 2024
1 parent d9fc15c commit b9a285c
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 134 deletions.
70 changes: 28 additions & 42 deletions src/ajax/callback.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,16 @@
*
* @returns {object} The callback object.
*/
self.create = (responseDelayTime, expirationTime) => ({
timers: {
onResponseDelay: setupTimer(responseDelayTime ?? config.defaultResponseDelayTime),
onExpiration: setupTimer(expirationTime ?? config.defaultExpirationTime),
},
onInitialize: null,
onProcessParams: null,
onPrepare: null,
onRequest: null,
onResponseDelay: null,
onExpiration: null,
beforeResponseProcessing: null,
onFailure: null,
onRedirect: null,
onSuccess: null,
onComplete: null,
});
self.create = (responseDelayTime, expirationTime) => {
const oCallback = {
timers: {
onResponseDelay: setupTimer(responseDelayTime ?? config.defaultResponseDelayTime),
onExpiration: setupTimer(expirationTime ?? config.defaultExpirationTime),
},
};
aCallbackNames.forEach(sName => oCallback[sName] = null);
return oCallback;
};

/**
* The global callback object which is active for every request.
Expand All @@ -65,48 +58,41 @@
* @return {void}
*/
self.initCallbacks = (oRequest) => {
const callback = self.create();
if (types.isObject(oRequest.callback)) {
oRequest.callback = [oRequest.callback];
}
if (types.isArray(oRequest.callback)) {
oRequest.callback.forEach(oCallback => {
// Add the timers attribute, if it is not defined.
if (oCallback.timers === undefined) {
oCallback.timers = {};
}
});
return;
}

let callbackFound = false;
// Check if any callback is defined in the request object by its own name.
const callback = self.create();
aCallbackNames.forEach(sName => {
if (oRequest[sName] !== undefined) {
callback[sName] = oRequest[sName];
callbackFound = true;
delete oRequest[sName];
}
});

if (oRequest.callback === undefined) {
oRequest.callback = callback;
return;
}
// Add the timers attribute, if it is not defined.
if (oRequest.callback.timers === undefined) {
oRequest.callback.timers = {};
}
if (callbackFound) {
oRequest.callback = [oRequest.callback, callback];
}
oRequest.callback = callbackFound ? [callback] : [];
};

/**
* Get a flatten array of callbacks
*
* @param {object} oRequest The request context object.
* @param {array=} oRequest.callback The request callback(s).
*
* @returns {array}
*/
const getCallbacks = (oRequest) => {
if(!oRequest.callback)
{
return [self.callback];
}
if(types.isArray(oRequest.callback))
{
return [self.callback, ...oRequest.callback];
}
return [self.callback, oRequest.callback];
};
const getCallbacks = ({ callback = [] }) => [self.callback, ...callback];

/**
* Execute a callback event.
Expand All @@ -119,10 +105,10 @@
*/
const execute = (oCallback, sFunction, oRequest) => {
const func = oCallback[sFunction];
const timer = !oCallback.timers ? null : oCallback.timers[sFunction];
if (!func || !types.isFunction(func)) {
return;
}
const timer = oCallback.timers[sFunction];
if (!timer) {
func(oRequest); // Call the function directly.
return;
Expand Down
7 changes: 3 additions & 4 deletions src/ajax/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@
* @returns {boolean}
*/
const initialize = (oRequest) => {
cbk.execute(oRequest, 'onInitialize');

cfg.setRequestOptions(oRequest);
cbk.initCallbacks(oRequest);
cbk.execute(oRequest, 'onInitialize');

oRequest.status = (oRequest.statusMessages) ? cfg.status.update : cfg.status.dontUpdate;
oRequest.cursor = (oRequest.waitCursor) ? cfg.cursor.update : cfg.cursor.dontUpdate;
Expand All @@ -43,6 +42,7 @@
* @return {void}
*/
const prepare = (oRequest) => {
--oRequest.requestRetry;
cbk.execute(oRequest, 'onPrepare');

oRequest.httpRequestOptions = {
Expand Down Expand Up @@ -90,7 +90,6 @@
* @returns {mixed}
*/
const submit = (oRequest) => {
--oRequest.requestRetry;
oRequest.status.onRequest();

// The onResponseDelay and onExpiration aren't called immediately, but a timer
Expand Down Expand Up @@ -208,7 +207,7 @@
}
catch (e) {
cbk.execute(oRequest, 'onFailure');
if (oRequest.requestRetry === 0) {
if (oRequest.requestRetry <= 0) {
throw e;
}
}
Expand Down
74 changes: 18 additions & 56 deletions src/cmd/body.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,13 @@
* Assign an element's attribute to the specified value.
*
* @param {object} args The command arguments.
* @param {string} args.id The target element id
* @param {object} args.target The HTML element to effect.
* @param {Element} args.target The HTML element to effect.
* @param {string} args.attr The name of the attribute to set.
* @param {string} args.value The new value to be applied.
*
* @returns {true} The operation completed successfully.
*/
self.assign = ({ target, attr, value }) => {
if (attr === 'innerHTML') {
target.innerHTML = value;
return true;
}
if (attr === 'outerHTML') {
target.outerHTML = value;
return true;
}

const xElt = dom.getInnerObject(attr, target);
if (xElt !== null) {
xElt.node[xElt.attr] = value;
Expand All @@ -35,23 +25,13 @@
* Append the specified value to an element's attribute.
*
* @param {object} args The command arguments.
* @param {string} args.id The target element id
* @param {object} args.target The HTML element to effect.
* @param {Element} args.target The HTML element to effect.
* @param {string} args.attr The name of the attribute to append to.
* @param {string} args.value The new value to be appended.
*
* @returns {true} The operation completed successfully.
*/
self.append = ({ target, attr, value }) => {
if (attr === 'innerHTML') {
target.innerHTML = target.innerHTML + value;
return true;
}
if (attr === 'outerHTML') {
target.outerHTML = target.outerHTML + value;
return true;
}

const xElt = dom.getInnerObject(attr, target);
if (xElt !== null) {
xElt.node[xElt.attr] = xElt.node[xElt.attr] + value;
Expand All @@ -63,23 +43,13 @@
* Prepend the specified value to an element's attribute.
*
* @param {object} args The command arguments.
* @param {string} args.id The target element id
* @param {object} args.target The HTML element to effect.
* @param {Element} args.target The HTML element to effect.
* @param {string} args.attr The name of the attribute.
* @param {string} args.value The new value to be prepended.
*
* @returns {true} The operation completed successfully.
*/
self.prepend = ({ target, attr, value }) => {
if (attr === 'innerHTML') {
target.innerHTML = value + target.innerHTML;
return true;
}
if (attr === 'outerHTML') {
target.outerHTML = value + target.outerHTML;
return true;
}

const xElt = dom.getInnerObject(attr, target);
if (xElt !== null) {
xElt.node[xElt.attr] = value + xElt.node[xElt.attr];
Expand All @@ -90,39 +60,36 @@
/**
* Replace a text in the value of a given attribute in an element
*
* @param {object} xElement The element to search in
* @param {string} sAttribute The attribute to search in
* @param {object} xElt The value returned by the dom.getInnerObject() function
* @param {string} sSearch The text to search
* @param {string} sReplace The text to use as replacement
*
* @returns {void}
*/
const replaceText = (xElement, sAttribute, sSearch, sReplace) => {
const bFunction = types.isFunction(xElement[sAttribute]);
const sCurText = bFunction ? xElement[sAttribute].join('') : xElement[sAttribute];
const replaceText = (xElt, sSearch, sReplace) => {
const bFunction = types.isFunction(xElt.node[xElt.attr]);
const sCurText = bFunction ? xElt.node[xElt.attr].join('') : xElt.node[xElt.attr];
const sNewText = sCurText.replaceAll(sSearch, sReplace);
if (bFunction || dom.willChange(xElement, sAttribute, sNewText)) {
xElement[sAttribute] = sNewText;
if (bFunction || dom.willChange(xElt.node, xElt.attr, sNewText)) {
xElt.node[xElt.attr] = sNewText;
}
};

/**
* Search and replace the specified text.
*
* @param {object} args The command arguments.
* @param {string} args.id The target element id
* @param {object} args.target The element which is to be modified.
* @param {Element} args.target The element which is to be modified.
* @param {string} args.attr The name of the attribute to be set.
* @param {array} args.search The search text and replacement text.
* @param {array} args.replace The search text and replacement text.
* @param {string} args.search The search text and replacement text.
* @param {string} args.replace The search text and replacement text.
*
* @returns {true} The operation completed successfully.
*/
self.replace = ({ target, attr, search, replace }) => {
const sSearch = attr === 'innerHTML' ? dom.getBrowserHTML(search) : search;
const xElt = dom.getInnerObject(attr, target);
if (xElt !== null) {
replaceText(xElt.node, xElt.attr, sSearch, replace);
replaceText(xElt, attr === 'innerHTML' ? dom.getBrowserHTML(search) : search, replace);
}
return true;
};
Expand All @@ -131,8 +98,7 @@
* Clear an element.
*
* @param {object} args The command arguments.
* @param {string} args.id The target element id
* @param {object} args.target The element which is to be modified.
* @param {Element} args.target The element which is to be modified.
* @param {string} args.attr The name of the attribute to clear.
*
* @returns {true} The operation completed successfully.
Expand All @@ -146,8 +112,7 @@
* Delete an element.
*
* @param {object} args The command arguments.
* @param {string} args.id The target element id
* @param {object} args.target The element which will be deleted.
* @param {Element} args.target The element which will be deleted.
*
* @returns {true} The operation completed successfully.
*/
Expand All @@ -172,8 +137,7 @@
* Create a new element and append it to the specified parent element.
*
* @param {object} args The command arguments.
* @param {string} args.id The target element id
* @param {object} args.target The element which will contain the new element.
* @param {Element} args.target The element which will contain the new element.
* @param {string} args.tag.name The tag name for the new element.
* @param {string} args.tag.id The id attribute of the new element.
*
Expand All @@ -188,8 +152,7 @@
* Insert a new element before the specified element.
*
* @param {object} args The command arguments.
* @param {string} args.id The target element id
* @param {object} args.target The element that will be used as the reference point for insertion.
* @param {Element} args.target The element that will be used as the reference point for insertion.
* @param {string} args.tag.name The tag name for the new element.
* @param {string} args.tag.id The id attribute of the new element.
*
Expand All @@ -205,8 +168,7 @@
* Insert a new element after the specified element.
*
* @param {object} args The command arguments.
* @param {string} args.id The target element id
* @param {object} args.target The element that will be used as the reference point for insertion.
* @param {Element} args.target The element that will be used as the reference point for insertion.
* @param {string} args.tag.name The tag name for the new element.
* @param {string} args.tag.id The id attribute of the new element.
*
Expand Down
Loading

0 comments on commit b9a285c

Please sign in to comment.