Skip to content

Ordered entities #316

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jamakase opened this issue Apr 15, 2020 · 5 comments
Closed

Ordered entities #316

jamakase opened this issue Apr 15, 2020 · 5 comments
Labels
enhancement New feature or request
Milestone

Comments

@jamakase
Copy link

jamakase commented Apr 15, 2020

In some cases, it could be useful to store and display data in timed order. Like messages in the chat.

I tried to implement the receiving of new messages via custom redux action, but we know only ids of entities in updaters, so it is not possible to identify where to store entity.

export const conversationUpdated = (conversationUpdate: ConversationByOperator) => ({
  type: RECEIVE_MUTATE_TYPE,
  payload: conversationUpdate,
  meta: {
    schema: ConversationByOperatorResource.asSchema(),
    updaters: {
      [ConversationByOperatorResource.listShape().getFetchKey({})]: (
        newConvId: string,
        prevResults: { conversations: string[] | undefined },
      ) => ({
        conversations: [...(prevResults?.conversations ?? []), newConvId],
      }),
    },
    date: new Date(),
    expiresAt: 189231293812939889,
  },
});

Do you know how this can be done except manually sorting everything in react components?

@jamakase jamakase added the enhancement New feature or request label Apr 15, 2020
@ntucker
Copy link
Collaborator

ntucker commented Apr 15, 2020

Thanks for the report!

Let me see if I understand your scenario correctly: Is there a member of the conversation that has it's date, that you want to use to determine ordering rather than the order in which it was processed? If this is the case, then you want to do a sort of 'insertion' event where you look for the spot to place it in a sorted list, correct?

@jamakase
Copy link
Author

@ntucker Exactly! For example, we need to display a list of messages within conversation and receive new message via websocket. But it is possible for some case to get an older message first and then get a new one. If you just append ( or prepend ) ordering could be wrong.

I will be glad to help you with this feature if you do not have time if you give me some guidance.

@ntucker
Copy link
Collaborator

ntucker commented Apr 17, 2020

Ah interesting, ya we did start seeing use cases like this. We'll probably need to make some sort of backwards incompatible change to support this - though we're in 5.0 beta development now so that's not a huge deal.

I guess the most obvious thing to do would be to actually pass in what the denormalized response would be (something resembling what is returned from useResource or useCache) and then you return a new denormalized response as well. Then the return value would be normalized again to just grab the results to update. This would technically be a bit more computation, which is why I didn't initially do it, but I think it is less confusing and allows cases like this, so probably makes sense. Computation on a write like this is probably fine considering there will likely be more responses updating as a result.

One open question would be what if people alter the entity themeslves in the updater function? Should this be allowed? It probably has to. I guess that is fine?

@ntucker
Copy link
Collaborator

ntucker commented Apr 17, 2020

Now that I think a little more, this works well with my plans to create a universal memoization of denormalized forms as those will end up simply being lookups into the table.

@ntucker ntucker added this to the 5.0 milestone Apr 17, 2020
@ntucker
Copy link
Collaborator

ntucker commented Apr 17, 2021

RFC to fix this and others in #760

@ntucker ntucker closed this as completed Apr 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants