UNPKG

lifxlan

Version:

TypeScript library for controlling LIFX products over LAN

75 lines 2.34 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Groups = Groups; function Groups(options = {}) { const onAdded = options.onAdded; const onChanged = options.onChanged; const onRemoved = options.onRemoved; const knownGroups = new Map(); function removeGroup(uuid) { const group = knownGroups.get(uuid); if (group) { knownGroups.delete(uuid); if (onRemoved) { onRemoved(group); } } } function indexOfDevice(group, device) { for (let i = 0; i < group.devices.length; i += 1) { if (group.devices[i] === device) { return i; } } return -1; } return { register(device, group) { const existingGroup = knownGroups.get(group.group); if (existingGroup) { if (indexOfDevice(existingGroup, device) < 0) { existingGroup.devices.push(device); if (onChanged) { onChanged(existingGroup); } } } else { const newGroup = { label: group.label, uuid: group.group, devices: [device], }; knownGroups.set(group.group, newGroup); if (onAdded) { onAdded(newGroup); } } }, remove(uuid) { removeGroup(uuid); }, removeDevice(device) { knownGroups.forEach((group) => { const index = indexOfDevice(group, device); if (index >= 0) { group.devices[index] = group.devices[group.devices.length - 1]; group.devices.pop(); if (group.devices.length === 0) { removeGroup(group.uuid); } else if (onChanged) { onChanged(group); } } }); }, get registered() { return knownGroups; }, [Symbol.iterator]() { return knownGroups.values(); }, }; } //# sourceMappingURL=groups.js.map