本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
遍历实体的所有权更改事件
要获取实体在所有权区域和订阅区域之间移动的事件,请比较实体所有权和订阅事件当前与之前的变化。
您可以通过阅读以下 API 来处理这些事件:
Api::SubscriptionChangeList
Api::OwnershipEvents
然后,您可以将更改与之前存储的数据进行比较。
以下示例说明了如何处理实体所有权更改事件。该示例假设,对于在所订阅实体和所拥有实体之间转移的实体(任一方向),首先发生所有权移除/添加事件,然后在下一个刻度内发生订阅移除/添加事件。
例 示例
Result<void> ProcessOwnershipEvents(Transaction& transaction) { using EntityIdsByAction = std::unordered_map<Api::ChangeListAction, std::vector<Api::EntityId>>; using EntityIdSetByAction = std::unordered_map<Api::ChangeListAction, std::unordered_set<Api::EntityId>>; static EntityIdsByAction m_entityIdsByPreviousOwnershipAction; EntityIdSetByAction entityIdSetByAction; /** * Enumerate Api::SubscriptionChangeList items * and store Add and Remove events. */ WEAVERRUNTIME_TRY(Api::SubscriptionChangeList subscriptionEvents, Api::AllSubscriptionEvents(transaction)); for (const Api::SubscriptionEvent& event : subscriptionEvents.changes) { const Api::ChangeListAction action = event.action; switch (action) { case Api::ChangeListAction::Add: case Api::ChangeListAction::Remove: { entityIdSetByAction[action].insert( event.entity.descriptor->id); break; } case Api::ChangeListAction::None: case Api::ChangeListAction::Update: case Api::ChangeListAction::Reject: { break; } } } EntityIdsByAction entityIdsByAction; /** * Enumerate Api::OwnershipChangeList items * and store Add and Remove events. */ WEAVERRUNTIME_TRY(Api::OwnershipChangeList ownershipChangeList, Api::OwnershipChanges(transaction)); for (const Api::OwnershipChange& event : ownershipChangeList.changes) { const Api::ChangeListAction action = event.action; switch (action) { case Api::ChangeListAction::Add: case Api::ChangeListAction::Remove: { entityIdsByAction[action].push_back( event.entity.descriptor->id); break; } case Api::ChangeListAction::None: case Api::ChangeListAction::Update: case Api::ChangeListAction::Reject: { break; } } } std::vector<Api::EntityId> fromSubscribedToOwnedEntities; std::vector<Api::EntityId> fromOwnedToSubscribedEntities; /** * Enumerate the *previous* Api::OwnershipChangeList Remove items * and check if they are now in * the *current* Api::SubscriptionChangeList Add items. * * If true, then that means * OnEntityOwnershipChanged(bool isOwned = false) */ for (const Api::EntityId& id : m_entityIdsByPreviousOwnershipAction[ Api::ChangeListAction::Remove]) { if (entityIdSetBySubscriptionAction[ Api::ChangeListAction::Add].find(id) != entityIdSetBySubscriptionAction[ Api::ChangeListAction::Add].end()) { fromOwnedToSubscribedEntities.push_back(id); } } /** * Enumerate the *previous* Api::OwnershipChangeList Add items * and check if they are now in * the *current* Api::SubscriptionChangeList Remove items. * * If true, then that means * OnEntityOwnershipChanged(bool isOwned = true) */ for (const Api::EntityId& id : m_entityIdsByPreviousOwnershipAction[ Api::ChangeListAction::Add]) { if (entityIdSetBySubscriptionAction[ Api::ChangeListAction::Remove].find(id) != entityIdSetBySubscriptionAction[ Api::ChangeListAction::Remove].end()) { fromSubscribedToOwnedEntities.push_back(id); } } m_entityIdsByPreviousOwnershipAction = entityIdsByOwnershipAction; return Success(); }