Skip to content

Instantly share code, notes, and snippets.

@GarryBrown
Last active July 13, 2020 08:20
Show Gist options
  • Save GarryBrown/e9a562bb96f553e414c7cb016688d4dd to your computer and use it in GitHub Desktop.
Save GarryBrown/e9a562bb96f553e414c7cb016688d4dd to your computer and use it in GitHub Desktop.
class Customer {
checkEmail(news) {
// here we take a look on news and make a decision buy or not
}
findAnotherShop() {
// search for a new shop for us
}
checkOrder() {
// is it okay with my order?
}
}
const me = new Customer();
const obs = new Observable((customer) => {
shop.addEventListener('news', (news) => {
if (news.isSomethingWrong) {
customer.error(news);
} else {
customer.next(news);
}
})
shop.addEventListener('accident', _ => {
customer.comptele(); // shop is closing
})
return () => {
showToast(`${shop.name} has lost 1 customer. We need do something!`);
};
});
const subscription = obs.subscribe({
next: (news) => me.checkEmail(news),
error: () => me.checkOrder(),
complete: () => me.findAnotherShop()
});
subscription.unsubscribe();
// it calls "showToast" method
// and when the shop will be closed and we don't get any notifications
// because Subscriber is stopped when we had unsubscribed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment