Skip to content

Instantly share code, notes, and snippets.

@JoshM1994
Created October 30, 2019 04:54
Show Gist options
  • Save JoshM1994/def7c800b193365cd8fa5dd0811e76de to your computer and use it in GitHub Desktop.
Save JoshM1994/def7c800b193365cd8fa5dd0811e76de to your computer and use it in GitHub Desktop.
Pseudo Gateway App code
// If senderId does not exist in the members object mapping, add it with an empty array of messages
const addIfNotInNetwork = (members, senderId) => members[senderId] ? members : {
...members,
[senderId]: []
}
const loraMagicSend = () => {
// Send the message off over LoRa...
}
const getResponse = (members, senderId) => {
// We know that members[senderId] is an array by construction
const [response] = members[senderId];
if (response === undefined) return SPECIAL_NO_MESSAGES_CONSTANT;
// I'm just copying the members to a new object - I'm used to working in React with no-param-reassign (props = true)
const updatedMembers = {...members};
// Remove that first message
updatedMembers[senderId].shift();
return updatedMembers;
}
const receiveMessage = (members, incomingSenderId, msg) => {
// First add the sender to the list of members if they do not exist
addIfNotInNetwork(members, incomingSenderId);
loraMagicSend(getResponse(members, incomingSenderId));
// Send a response t
// For each member, append the message to the individual's queue
return Object.entries(members).reduce((accObj, [memberId, messages]) =>
({ ...accObj, [senderId]: [...messages, msg] }), {});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment