31
loading...
This website collects cookies to deliver better user experience
Observable
pattern is very common in libraries.Observable
pattern works, the most interesting of all was the trip raffle.Whoever comes up with the funniest sentence about a travelling will win a trip to Petar SP. The sentences should be emailed to [email protected], and the winner will be notified by email in three weeks.
Thanks for your participation, and we received many subscriptions, unfortunately, your entry was not the winning contribution.
The winner was Juciano Barbosa for his sentence, "Bringing a snake on a airplane os not a good idea!".
Observable
pattern works, as when the company sent the email, every participant was notified.class Observable {
constructor() {
this.observers = [];
}
subscribe(fn) {
this.observers = [...this.observers, fn];
return () => {
this.unsubscribe(fn);
};
}
unsubscribe(fn) {
this.observers = this.observers.filter((observer) => observer !== fn);
}
notify(data) {
this.observers.forEach((observer) => {
observer(data);
});
}
}
export default new Observable();
constructor
- Start by creating a class named Observable and in the constructor, assign an empty array in the observers property. The observers property will keep the observable list.subscribe
- After that, create a method named subscribe, this method receives a function as an argument, and this argument will be an observable. After that, use the spread operator to assign a new array with the function received as an argument into the observers property. The return function will be responsible for removing the observer that we just assigned into subscribers.unsubscribe
- This method is responsible for removing a certain observer. The unsubscribe method receives a function and verifies if this function is present in the observers list, and if it is, removes it.notify
- This method receives data as an argument, iterates the observers list, and passes the data as an argument into every observer.import Observable from "./Observer";
// selecting HTML elements
const input = document.getElementById("text-input");
const firstSubscriberBtn = document.getElementById("first-subscriber-btn");
const secondSubscriberBtn = document.getElementById("second-subscriber-btn");
const firstUnSubscriberBtn = document.getElementById("first-un-subscriber-btn");
const secondUnSubscriberBtn = document.getElementById(
"second-un-subscriber-btn"
);
const textFirstSubscriber = document.getElementById("first-subscriber");
const textSecondSubscriber = document.getElementById("second-subscriber");
//observers are inserting into text element the value received
const firstText = (e) => (textFirstSubscriber.innerText = `${e}`);
const secondText = (e) => (textSecondSubscriber.innerText = `${e}`);
// event source, notifying all observer
input.addEventListener("input", (e) => Observable.notify(e.target.value));
// subscribing
firstSubscriberBtn.addEventListener("click", (e) => {
e.preventDefault();
Observable.subscribe(firstText);
});
secondSubscriberBtn.addEventListener("click", (e) => {
e.preventDefault();
Observable.subscribe(secondText);
});
// unsubscribing
firstUnSubscriberBtn.addEventListener("click", (e) => {
e.preventDefault();
Observable.unsubscribe(firstText);
});
secondUnSubscriberBtn.addEventListener("click", (e) => {
e.preventDefault();
Observable.unsubscribe(secondText);
});
firstText
and secondText
are the observers that receive a certain text value and insert it into the text element using innerText
resource.Observable.notify
method. If some input event happens, the notify method will notify every observer.