Skip to content

Commit

Permalink
WIP: Add EventBus abstraction
Browse files Browse the repository at this point in the history
  • Loading branch information
psrpinto committed Aug 16, 2024
1 parent aa35972 commit a2dea8d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/plugin/scripts/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { EventBus } from '../../shared/event-bus';

const startImportButton = document.getElementById( 'try-wordpress-import' );
if ( startImportButton ) {
const eventBus = new EventBus( { targetWindow: window.parent } );

Check failure on line 5 in src/plugin/scripts/index.js

View workflow job for this annotation

GitHub Actions / lint

'eventBus' is assigned a value but never used
window.addEventListener( 'message', handleStartImportResponse );
window.addEventListener( 'message', handleMessage );
startImportButton.addEventListener( 'click', startImport );
Expand Down
24 changes: 24 additions & 0 deletions src/shared/event-bus.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
export class EventBus {
#window;
#targetWindow;
#listeners;

constructor( options = {} ) {
if ( ! options.targetWindow ) {
throw Error( 'targetWindow option must be set' );
}
this.#targetWindow = options.window;
this.#window = options.window || window;
this.#listeners = [];
this.#window.addEventListener( 'message', this.handleEvent );
}

addListener( type, callback ) {

Check failure on line 16 in src/shared/event-bus.js

View workflow job for this annotation

GitHub Actions / lint

'type' is defined but never used

Check failure on line 16 in src/shared/event-bus.js

View workflow job for this annotation

GitHub Actions / lint

'callback' is defined but never used
// TODO.
}

handleEvent( event ) {
// TODO.
console.log( event.target );
}
}

0 comments on commit a2dea8d

Please sign in to comment.