Skip to content

Instantly share code, notes, and snippets.

@LucasBadico
Created February 25, 2023 02:00
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 LucasBadico/cb1e5bb773e04ab888e744bc15deec15 to your computer and use it in GitHub Desktop.
Save LucasBadico/cb1e5bb773e04ab888e744bc15deec15 to your computer and use it in GitHub Desktop.
objeto vs tipo
class Notifier {
private subscribers: ((notification: Notification) => void)[] = [];
constructor(subscribers: ((notification: Notification) => void)[]) {
this.subscribers = subscribers;
}
public notify(notification: Notification) {
this.subscribers.forEach((subscriber) => subscriber(notification));
}
}
// Criando uma lista de subscribers
const subscribers = [
(notification: Notification) => {
console.log(`Notificação recebida: ${notification.title}`);
},
(notification: Notification) => {
// Aqui dentro da função, você pode fazer outras coisas com a notificação
alert(`Notificação recebida: ${notification.title}`);
}
];
// Criando uma nova instância da classe Notifier com a lista de subscribers
const notifier = new Notifier(subscribers);
// Criando uma nova notificação
const notification: Notification = {
title: "Nova mensagem",
message: "Você recebeu uma nova mensagem de Fulano de Tal",
date: new Date(),
isRead: false
};
// Notificando os subscribers com a nova notificação
notifier.notify(notification);
type Notification = {
title: string;
message: string;
date: Date;
isRead: boolean;
}
function sendNotification(notification: Notification) {
// Aqui dentro da função, você pode acessar as propriedades da notificação
// como `notification.title`, `notification.message`, `notification.date` e `notification.isRead`
console.log(`Nova notificação recebida: ${notification.title} - ${notification.message}`);
}
// Criando uma nova notificação
const notification: Notification = {
title: "Nova mensagem",
message: "Você recebeu uma nova mensagem de Fulano de Tal",
date: new Date(),
isRead: false
};
// Chamando a função sendNotification e passando a notificação como argumento
sendNotification(notification);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment