Closed laPlacek closed 2 years ago
Ugh. I'll have to find some time to consider alternative implementations.
Playing around with this a bit today. Interestingly, using sessionStorage seems to be problematic, but localStorage is stable. I can't even get items back in order from sessionStorage after a second write, much less a remove.
It's hard to care about trying to maintain order during each() when order is never maintained to begin with. It seems pretty clear that there is no order maintained in the storage implementation to begin with.
Do you have a particular failing test case where the each implementation introduces a problem that isn't already in the underlying storage?
I mean, i can create tests where unpredictable things happen by deleting or adding to sessionStorage during an each loop, but it's not clear that that is a bug in the implementation, so much as a shortcoming of working with sessionStorage. I can rewrite each to grab all the keys first, then iterate, which would prevent the each fn itself from causing those issues, but in a multi-page scenario, you could still cause them, and even in the each fn, such an implementation could surprise by iterating over keys deleted in earlier iterations and still maybe missing keys added.
So, to sum, order maintenance isn't a thing to begin with, set/remove calls during each will always cause some kind of surprising behavior, and i think there's really nothing to fix here, just a choice between different sorts of problems. So for now, i'm closing this. If you (or someone else) wants to make a case for a different implementation (like gather keys, then iterate), i could be persuaded, but for now, i think the best thing to do is leave it be.
In store2.js each is implemented following way:
MDN Storage.key() states that "The order of keys is user-agent defined, so you should not rely on it."
Reorder of elements can happen in the middle of for loop. Especially:
As an example please consider following code:
For me it yields:
Conclusion: Shifting on add happens more often than shift on remove, but both can occur. for loop inside each implementation should not assume any order of elements in between loop operations.