DEV Community

Discussion on: Is there a drop in JS Array-compatible React/Vue-friendly linked-list library?

Collapse
 
zspencer profile image
Zee • Edited

Hey everyone!

Here's what I wound up doing:

All my calls to push and pop into this array were encapsulated in calls to prependMessage and appendMessage; so I wrote a quick function to link the items within the array to their previous or next item on push or unshift :

export function relinkMessageAtIndex(messages, index) {
  const foundItem = messages[index];
  const nextItem = messages[index + 1];
  const previousItem = messages[index - 1];
  Vue.set(foundItem, "previousMessage", previousItem);
  Vue.set(foundItem, "nextMessage", nextItem);
  if (nextItem) {
    Vue.set(nextItem, "previousMessage", foundItem);
  }
  if (previousItem) {
    Vue.set(previousItem, "nextMessage", foundItem);
  }
}

It's maybe not as 'right' as a real linked list, which encapsulates this transparently but it's "good enough" in the sense that it works and didn't require too much open heart surgery, and saved me a hundred lines.