本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
迭代實體的所有權變更事件
若要取得實體在擁有權區域與訂閱區域之間移動的事件,請比較目前和先前實體擁有權與訂閱事件之間的變更。
您可以閱讀以下內容來處理這些事件:
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(); }