Skip to content

Instantly share code, notes, and snippets.

@transducer
Last active June 4, 2018 17:44
Show Gist options
  • Save transducer/e104a78807b03f30dd14b8df6abbc0a9 to your computer and use it in GitHub Desktop.
Save transducer/e104a78807b03f30dd14b8df6abbc0a9 to your computer and use it in GitHub Desktop.
Create devices aggregate from MAM event stream
/**
* Creates the devices aggregate from the MAM event stream.
* @function toDevices
* @param {array} messages JSON messages from an MAM event stream
* @returns {array} Array of devices (device is object with address and type)
*/
function toDevices(messages) {
const devicesSet = messages.reduce((devices, { type, device }) => {
switch (type) {
case DEVICE_ADDED_TYPE:
return devices.add(device);
case DEVICE_DELETED_TYPE: {
devices.delete(device);
return devices;
}
default:
return devices;
}
}, new JsonSet());
const devices = Array.from(devicesSet.entries());
return devices;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment