Lists

Lists are an ordered collection of items, stored in a CRDT-like data structure. Lists can contain other lists and maps, resolve conflicts automatically, and do their best to preserve user intent while maintaining order.

If you have a room setup, you can create a list:

1let list = room.list("todos")

Like in other parts of Room Service, you should manually listen for updates:

1room.subscribe(list, l => {
2 // l is a new, efficient copy of the list with the update applied
3})

Indexes are handled automatically

What happens if Alice updates something at i=10 and Bob deletes i=0. Is Alice's operation incorrectly applied to i=9 now? No! Behind the scenes, Room Service assigns an ID to every item in the list and automatically figures out which indexes are where on any client.

So no matter who's operation is executed first, the result is the same:

1// Alice
2list.set(10, "cats")
3
4// Bob
5list.delete(0)
6
7// ... After network communication
8// the result is always the same.

List operations

Much like maps, updates to lists return an efficient copy of the list. To add a new item to the end of the list, use push:

1list = list.push("wash dishes")

To get that item back, use get:

1list.get(0) // "wash dishes"

To update the value at an index, use set:

1list.set(0, "clean counters")

To add something to the middle of a list, use insertAfter:

1list = list.insertAfter(8, "feed dogs")

To delete an item, use delete:

1list = list.delete(2)