Skip to content

Instantly share code, notes, and snippets.

@burakozturk16
Created October 16, 2021 09:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save burakozturk16/fb4bfea5d4fd78a4714440807db36b3d to your computer and use it in GitHub Desktop.
Save burakozturk16/fb4bfea5d4fd78a4714440807db36b3d to your computer and use it in GitHub Desktop.
/*
This is a test prototype coding for planning a react event-bus system
*/
class eventBus {
static on(event, callback) {
document.addEventListener(event, (e) => callback(e.detail));
}
static dispatch(event, data) {
document.dispatchEvent(new CustomEvent(event, { detail: data }));
}
static remove(event, callback) {
document.removeEventListener(event, callback);
}
}
class baseComponent extends eventBus {
constructor(){
super();
}
}
class myComponent1 extends baseComponent{
constructor() {
super();
eventBus.on('duydum', this.soyledim);
eventBus.on('gordum', this.gosterdim);
}
soyledim(){
alert('onu nasıl sevdgimi')
}
gosterdim(){
alert("1 resim gösteriyor.")
}
}
class myComponent2 extends baseComponent{
constructor() {
super();
eventBus.on('gordum', this.gosterdim);
}
gosterdim(){
alert("2 resim gösteriyor.")
}
}
class parentComponent extends baseComponent {
constructor(){
super();
}
islemYap(data){
eventBus.dispatch('gordum', data)
}
}
var child1 = new myComponent1();
var child2 = new myComponent2();
var app = new parentComponent()
app.islemYap('gordum');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment