Skip to content

Instantly share code, notes, and snippets.

@westonruter
Last active May 23, 2024 00:22
Show Gist options
  • Save westonruter/59f7c58230bd5d1e82733e5b87d245dc to your computer and use it in GitHub Desktop.
Save westonruter/59f7c58230bd5d1e82733e5b87d245dc to your computer and use it in GitHub Desktop.
<?php
/**
* Plugin Name: Try Slow Click Event Directives
* Plugin URI: https://gist.github.com/westonruter/59f7c58230bd5d1e82733e5b87d245dc
* Description: Testing
* Requires at least: 6.5
* Requires PHP: 7.2
* Version: 0.1.0
* Author: Weston Ruter
* Update URI: https://gist.github.com/westonruter/59f7c58230bd5d1e82733e5b87d245dc
*/
if ( ! isset( $_GET['try-slow-events'] ) ) {
return;
}
add_action(
'wp_enqueue_scripts',
static function () {
wp_register_script_module( 'try-slow-events', plugin_dir_url( __FILE__ ) . 'view.js' );
}
);
add_filter(
'render_block_core/image',
static function ( $block_content ) {
wp_enqueue_script_module( 'try-slow-events' );
$directive = $_GET['try-slow-events'] === 'async' ? 'data-wp-on-async' : 'data-wp-on';
$p = new WP_HTML_Tag_Processor( $block_content );
while ( $p->next_tag( array( 'tag_name' => 'IMG' ) ) ) {
for ( $i = 1; $i <= 10; $i++ ) {
$p->set_attribute( "$directive--click--slow-event-$i", 'trySlowEvents::actions.doFortyFiveMillisecondsOfWork' );
}
}
return $p->get_updated_html();
}
);
import { store } from '@wordpress/interactivity';
console.info( 'Testing Slow Event Directives' );
function doWork( ms ) {
const startTime = performance.now();
while ( performance.now() - startTime < ms ) {}
}
function doFortyFiveMillisecondsOfWork() {
doWork( 45 );
}
store( 'trySlowEvents', {
actions: {
doFortyFiveMillisecondsOfWork
}
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment