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 f92ac49 commit 67b2be3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/plugin/scripts/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { EventBus } from '../../shared/event-bus';

const progressContainer = document.getElementById( 'progress-container' );
const progressBar = document.getElementById( 'progress-bar' );
const progressText = document.getElementById( 'progress-text' );

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

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

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

addListener( type, callback ) {
// TODO.
console.log( type, callback );
}

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

0 comments on commit 67b2be3

Please sign in to comment.