Skip to content

Filter works incorrectly if fields are splitted by several log actions #115

Description

@LexxFedoroff

Hello, I've debugged this issue logux/examples#21 and found that there is a bug in the filters.
I can reproduce it with a simple test:

it('filter works correctly if sent not all fields', async () => {
  let client = new TestClient('10')
  await client.connect()
  client.log.keepActions()

  let posts = createFilter(client, Post, { authorId: '1'})
  let unbind = posts.listen(() => {})
  await allTasks()

  await client.server.sendAll({ channel: 'posts/1', type: 'logux/subscribed' })
  await client.server.sendAll({
    fields: { title: 'A' },
    id: '1',
    type: 'posts/created'
  })
  await client.server.sendAll({
    fields: { authorId: '1' },
    id: '1',
    type: 'posts/changed'
  })
  await allTasks()

  expect(ensureLoaded(posts.get()).list).toEqual([
    { id: '1', isLoading: false, title: 'A', authorId: '1' },
  ])
  unbind()
})

I want to fix it but I need some information on how that should work.
As I understand we need to accumulate log actions for an object until we receive the fields required for a filter.
What is the best way to achieve this?
Version: 0.21.1

Activity

  1. ai commented on Aug 16, 2024

    @ai
    Member

    Hm. I thought about this case.

    This block should do the stuff
    https://github.com/logux/client/blob/main/create-filter/index.js#L280-L291

    Can you investigate why it doesn’t work?

  2. LexxFedoroff commented on Aug 19, 2024

    @LexxFedoroff
    Author

    I've investigated and I think the problem comes from the commit e2dac17
    Currently, the sync map is created from the last received action but this action doesn't contain all necessary fields.
    @euaaaio what do you think?

  3. euaaaio commented on Aug 19, 2024

    @euaaaio
    Member

    I won't be able to review it closely today.

    I only remember that we had a problem with the implementation, which was somehow very different from how it was designed. Could it be that the bug is in the example repository?

  4. LexxFedoroff commented on Aug 20, 2024

    @LexxFedoroff
    Author

    Could it be that the bug is in the example repository?

    not sure, I think the bug is in the createFilter method
    I'm a newbie in the logux framework and I don't know exactly how it should work but I see two options:

    • a filter must accumulate all changes of the map and when any of the filter's fields comes then creates a sync map with all changes. In this case, there are no extra sync operations but we spend more memory to keep unnecessary data.
    • a filter listens to changes of the map and when any of the filter's fields comes then creates an empty sync map that will sync again. In this case, there are extra sync operations but we don't spend more memory to keep unnecessary data.

    the second option was implemented before the commit e2dac17 and I tend that was correct

  5. added 2 commits that reference this issue on Sep 17, 2024
    0a9904d
    e02e610
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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