Last active
May 7, 2024 08:04
-
-
Save dra1n/ae9df841d3ad00a47602e484d5bb2e0c 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
// user module | |
const receive = (_user, sender, message) => { | |
console.log(`${new Date().toLocaleString()} [${sender}]: ${message}`); | |
}; | |
// chat room module | |
const createChatRoom = (users = []) => ({ | |
users | |
}); | |
const addUser = (chatroom, user) => ({ | |
...chatroom, | |
users: [...chatroom.users, user] | |
}); | |
const send = (chatroom, user, message) => { | |
chatroom.users | |
.filter(u => u !== user) | |
.forEach(u => receive(u, user.name, message)); | |
}; | |
// usage | |
const user1 = { name: "John Doe" }; | |
const user2 = { name: "Jane Doe" }; | |
const chatroom = [user1, user2].reduce( | |
(chatroom, user) => addUser(chatroom, user), | |
createChatRoom() | |
); | |
send(chatroom, user1, "Hi there!"); | |
send(chatroom, user2, "Hey!"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment