Skip to content

Add client-side sorting optimization for live query subscriptions #84

@guyofeck

Description

@guyofeck

Summary

Implement client-side sorting mechanism to prevent unnecessary server refetches when entities are updated in live query subscriptions.

Background

When using watch() subscriptions without limit or skip clauses, the client can optimize updates by:

  1. Updating the modified entity in the local list
  2. Re-sorting the list client-side

This avoids the "sliding window" problem and reduces server load.

Proposed Implementation

When a snapshot update is received:

  1. If the subscription has no limit/skip options:
    • For modified: Update the entity in place and re-sort
    • For added: Insert the entity and re-sort
    • For removed: Remove the entity from the list
  2. If the subscription has limit/skip:
    • Request a full refetch from the server (sliding window case)

Technical Details

Sort Parsing

The SDK already uses -- prefix for descending sort (e.g., --price for descending by price). Implement a sort comparator that:

  • Parses the sort string
  • Handles ascending/descending
  • Supports nested field paths

Files to modify:

  • src/modules/entities.ts - Add sorting logic in watch handler
  • src/utils/common.ts - Add sort utility function

Considerations

  • Document that limit/skip subscriptions will trigger full refetches
  • Consider recommending filter-only subscriptions in docs
  • Maintain consistency with server-side sort behavior

Related

Metadata

Metadata

Assignees

No one assigned

    Labels

    Live QueryLive query subscription feature

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions