Last active
July 13, 2020 08:20
-
-
Save GarryBrown/e9a562bb96f553e414c7cb016688d4dd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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