Last active
June 4, 2018 17:44
-
-
Save transducer/e104a78807b03f30dd14b8df6abbc0a9 to your computer and use it in GitHub Desktop.
Create devices aggregate from MAM event stream
This file contains 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
/** | |
* 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