symfony/mercure

Publish update when a collection is updated (item added or removed)

Closed this issue · 3 comments

Hi!

Is there a reason why a changed collection doesn't trigger a update?

For example, in my React app, I have a Group component, that have a bunch of Items
If a user creates a new Item in the group, the other user gets a notification that a Item have been created, but the Group does not get a update that it's collection have changed, and therefore the React Group component doesn't get a updated list of children...

I solved it by creating a new (copy) of PublishMercureUpdatesListener (since I can't extend it),

and checked the updated collections as well:
(since they are all 'updatedEntities' I removed the logic for different types of updates)

    /**
     * Collects created, updated and deleted entities.
     */
    public function onFlush(OnFlushEventArgs $eventArgs): void
    {
        $uow = $eventArgs->getEntityManager()->getUnitOfWork();

        /** @var PersistentCollection $collection */
        foreach ($uow->getScheduledCollectionDeletions() as $collection) {
            // Updated collection
            $entity = $collection->getOwner();
            $this->storeEntityToPublish($entity);
        }

        /** @var PersistentCollection $collection */
        foreach ($uow->getScheduledCollectionUpdates() as $collection) {
            $entity = $collection->getOwner();
            $this->storeEntityToPublish($entity);
        }
    }

But a (small) issue, since I'm duplicating the old PublishMercureUpdatesListener one, I send duplicate events now...

I guess this issue belongs in API-Platform repo :)