ricardomatias / ableton-live

A library for communicating with Live via WebSockets, works both in NodeJS and in the Browser.
https://ricardomatias.net/ableton-live/
79 stars 10 forks source link

Add a Poperties.child() getter for single children #18

Closed soimon closed 1 year ago

soimon commented 1 year ago

This is part of a set of pull requests aimed at reducing the amount of data being transmitted when using this (excellent 🙏) library extensively, often causing me timeouts in high performance situations. I've split my improvements in different features for your convenience, but I'd be happy to help combine them.

This PR adds a Properties.child(child, index) getter, mirroring the existing Properties.children(child) one:

const all = await ({} as Track).children('clip_slots'); // ClipSlot[]
const one = await ({} as Track).child('clip_slots', 1 ); // ClipSlot | undefined

It is typed to only work with children marked as an array: For determining this, it will look at the transformed property first.

await ({} as ClipSlot).children('clip'); // Valid: Clip
await ({} as ClipSlot).child('clip', 1); // Invalid: Already a single child

Example

This will drastically reduce the amount of data transmitted when you know the child index in advance:

const track = await live.song.findTrackByName(trackName);
track?.observe('playing_slot_index', async (index) => {
    const launchedClipSlot = await track.child('clip_slots', index);
});
ricardomatias commented 1 year ago

Thanks @soimon for this PR. I quite like the improvement here and the way you went about it.