Skip to content

rpc: Collection deepChange fires once more on every update of an item #765

Open

Description

@lionelhorn

Each server-side update() of the same item in a returned Collection makes deepChange on the client fire once more than the last time: the n-th update emits n times.

import { entity } from '@deepkit/type';
import { Collection, DirectClient, rpc, RpcKernel } from '@deepkit/rpc';

@entity.name('task')
class Task {
    version: number = 0;
    constructor(public id: number, public title: string) {}
}

const tasks = new Collection(Task);

@rpc.controller('tasks')
class TaskController {
    @rpc.action()
    list(): Collection<Task> {
        tasks.set([new Task(1, 'write report')]);
        return tasks;
    }

    @rpc.action()
    rename(title: string): void {
        tasks.update([new Task(1, title)]);
    }
}

const kernel = new RpcKernel();
kernel.registerController(TaskController);
const api = new DirectClient(kernel).controller<TaskController>('tasks');

const list = await api.list();
const changes: string[] = [];
list.deepChange.subscribe(task => changes.push(task.title));
for (const title of ['draft', 'review', 'final']) {
    await api.rename(title);
    await new Promise(resolve => setTimeout(resolve, 10)); // let the change arrive
}
console.log(changes);
// actual: ['draft', 'review', 'review', 'final', 'final', 'final'], should be: ['draft', 'review', 'final']

Master 0bb9fba, Node 26.13.0.

Activity

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