Answer the question
In order to leave comments, you need to log in
Have I implemented the observer pattern correctly?
Hello. Now I am writing a more or less large application for myself on node.js + typescript, which has interaction with MySQL, Redis, Google Maps API and Telegram API. I decided to use design patterns (I didn’t do this before and wrote it somehow) in order to write readable and extensible code. And I ran into the problem of implementing Observer. The code is something like this:
сonst TelegramBot = new TelegramBot(config.token, {polling: true}); //telegram-бот
const redisApi= new RedisApi(redisClient); //обёртка для удобной работы с сессиями через Redis
const Observer = new Observer();
//обычное сообщение, требующее от пользователя нужного формата входных данных
observer.on('classic', (msg: Message, text: TextMessage) => {
bot.sendMessage(msg.from.id, text.text, text.keyboard);
});
observer.on('classic', (msg: Message, text: TextMessage, level: string) => {
if (level != undefined) redisApi.setSession(msg.from.id, level);
});
//classic - тип события
//msg - сообщение от пользователя telegram
//text - ответное сообщение пользователю
//level - аналог сессий, приспособленный для телеграм-бота
Answer the question
In order to leave comments, you need to log in
Have I implemented the observer correctly?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question