I am making an emoji viewer app with cycleJS, where the user can click on any emoji to add/remove it from their list of favorites. The list is also saved to localstorage
on every change. I'm constructing the list using xstream by folding the click stream (either adding or removing the emoji on each click):
const favEmojis$ = clickFavEmoji$.fold(
(favList, selectedEmoji) =>
favList.includes(selectedEmoji)
? favList.filter(emoji => emoji !== selectedEmoji)
: [...favList, selectedEmoji],
[]
);
I am able to save this stream to localStorage
and load it on the page using the @cycle/storage driver:
const storageRequest$ = favEmojis$.map(favEmojis => ({
key: "favEmojis",
value: JSON.stringify(favEmojis)
}));
...
return {
DOM: vdom$,
...
storage: storageRequest$
};
}
However, I cannot figure out how to pre-load the array from localStorage into the favorite stream. Once the array is loaded from localStorage
, I've tried to merge/concat it with the favEmojis$
stream in every way I could think of. For example:
const storedEmojis$ = localStorage
.getItem("favEmojis")
.map(favEmojis => (favEmojis ? JSON.parse(favEmojis) : []))
.take(1);
const combinedFav$ = xs.merge(storedEmojis$, favEmojis$);
But this doesn't work - the array from localstorage
gets overwritten by the folding clickFavEmoji
stream. I would greatly appreciate it if someone could point me in the right direction.
N.B. the complete code is quite long, so I only included the parts that seemed the most relevant.