Skip to content

How do we add an item after/before another item or at a specified index of the collection? #3

@niieani

Description

@niieani

Imagine a chat app which implements offline support:

  • you can "send" messages and they're added to the queue and shown immediately (optimistic updates)
  • when the client reconnects, it pulls messages that were sent by others: the Date of their messages can thus be before the messages the user has sent out
  • we need to place those messages at their appropriate places in the list (e.g. in the middle), not append them.

This is just one use case, but there are many others (like reordering lists, where you'd need to remove an item and re-add it in a different place).
I'd imagine a lot of the times you don't know the index and you'd actually need to find after/before which specific item you want your new item to appear with some sort of a search function.

Not sure how a pure/functional API would look like for this case, but if it were a classic mutable state, it'd be something like:

collection.addAfter(newItem, item => item.date < newItem.date)

This would iterate on all the elements until the return value of the search is false, then insert the new item at the index of the first item that returned false.

How can we deal with this case using cycle-collection?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions