Skip to content

Commit

Permalink
⚡️ Improve performance of enabling styles/scripts in the browser
Browse files Browse the repository at this point in the history
  • Loading branch information
skerit committed Nov 15, 2023
1 parent f4a3884 commit 11b0864
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 22 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* Make sure getting template sources stay within the allowed paths
* Automatically set the `force_async` option of the `Hawkejs.series` method to false
* Use as many Swift pledges as possible
* Improve performance of enabling styles/scripts in the browser

## 2.3.14 (2023-10-15)

Expand Down
64 changes: 42 additions & 22 deletions lib/client/scene.js
Original file line number Diff line number Diff line change
Expand Up @@ -2317,7 +2317,7 @@ Scene.setMethod(function _setPageTitle(title) {
*
* @author Jelle De Loecker <jelle@develry.be>
* @since 1.0.0
* @version 2.2.10
* @version 2.3.15
*
* @param {Object} options
* @param {Boolean} force Force a reload if already loaded?
Expand Down Expand Up @@ -2346,11 +2346,17 @@ Scene.setMethod(function getScript(options, force) {
if (Array.isArray(path)) {
let tasks = [];

path.forEach(function eachScript(path) {
tasks.push(function loadScriptPath(next) {
that.getScript(path, force).done(next);
});
});
for (let entry of path) {
let task_pledge = that.getScript(entry, force);

if (task_pledge && !task_pledge.is_done) {
tasks.push(task_pledge);
}
}

if (!tasks.length) {
return Classes.Pledge.Swift.RESOLVED;
}

return Hawkejs.series(false, tasks);
}
Expand Down Expand Up @@ -2384,20 +2390,20 @@ Scene.setMethod(function getScript(options, force) {

// See if it has already been loaded
if (!force && (loaded_paths[path] || loaded_names[name])) {
return Classes.Pledge.resolve(true);
return Classes.Pledge.Swift.RESOLVED;
}

if (loading_paths[path] || loading_names[name]) {
return loading_paths[path] || loading_names[name];
}

if (!path) {
return Pledge.reject(new Error('Did not find a script path to require'));
return Classes.Pledge.Swift.reject(new Error('Did not find a script path to require'));
}

// Create the actual element
let script = document.createElement('script'),
pledge = new Classes.Pledge();
pledge = new Classes.Pledge.Swift();

// Set the source attribute
script.src = path;
Expand Down Expand Up @@ -2433,7 +2439,7 @@ Scene.setMethod(function getScript(options, force) {
*
* @author Jelle De Loecker <jelle@develry.be>
* @since 1.0.0
* @version 2.2.10
* @version 2.3.15
*
* @param {Hawkejs.Renderer} renderer
*/
Expand All @@ -2442,7 +2448,7 @@ Scene.setMethod(function handleRendererStyles(renderer) {
let styles = Blast.Bound.Array.cast(renderer.styles);

if (!styles.length) {
return Classes.Pledge.resolve();
return Classes.Pledge.Swift.RESOLVED;
}

const that = this;
Expand All @@ -2455,9 +2461,15 @@ Scene.setMethod(function handleRendererStyles(renderer) {
return;
}

tasks.push(function doStyle(next) {
that.enableStyle(parameters[0], parameters[1]).done(next);
});
let task_pledge = that.enableStyle(parameters[0], parameters[1]);

if (task_pledge && !task_pledge.is_done) {
tasks.push(task_pledge);
}
}

if (!tasks.length) {
return Classes.Pledge.Swift.RESOLVED;
}

// Unlike scripts, styles should be loaded parallel.
Expand All @@ -2472,15 +2484,14 @@ Scene.setMethod(function handleRendererStyles(renderer) {
*
* @author Jelle De Loecker <jelle@develry.be>
* @since 1.0.0
* @version 2.2.17
* @version 2.3.15
*
* @param {String} instructions The id or path to the script
* @param {Object} options
*/
Scene.setMethod(function enableStyle(instructions, options) {

var that = this,
block_name;
let block_name;

if (typeof options === 'string') {
block_name = options;
Expand Down Expand Up @@ -2509,7 +2520,7 @@ Scene.setMethod(function enableStyle(instructions, options) {

style.removeAttribute('disabled');

return Classes.Pledge.resolve();
return Classes.Pledge.Swift.RESOLVED;
}

// Handle arrays of styles in parallel
Expand All @@ -2518,17 +2529,26 @@ Scene.setMethod(function enableStyle(instructions, options) {
let amount = instructions.length;

if (amount == 0) {
return Classes.Pledge.resolve()
return Classes.Pledge.Swift.RESOLVED;
}

if (amount == 1) {
instructions = instructions[0];
} else {

let tasks = [];
let tasks = [],
task_pledge;

for (let instruction of instructions) {
tasks.push(this.enableStyle(instruction, options));
task_pledge = this.enableStyle(instruction, options);

if (task_pledge && !task_pledge.is_done) {
tasks.push(task_pledge);
}
}

if (tasks.length == 0) {
return Classes.Pledge.Swift.RESOLVED;
}

// Run the tasks in parallel, but force them to start directly,
Expand Down Expand Up @@ -2579,7 +2599,7 @@ Scene.setMethod(function enableStyle(instructions, options) {
}

let data = this.styles[plain_path],
pledge = new Classes.Pledge();
pledge = new Classes.Pledge.Swift();

// Remember this is already being loaded!
loading_styles[original_path] = loading_styles[path] = pledge;
Expand Down

0 comments on commit 11b0864

Please sign in to comment.