| | 1 | | using DCL.CRDT; |
| | 2 | | using DCL.ECS7.ComponentWrapper; |
| | 3 | | using System; |
| | 4 | |
|
| | 5 | | namespace DCL.ECS7 |
| | 6 | | { |
| | 7 | | public readonly struct WriteData : IDisposable |
| | 8 | | { |
| | 9 | | public readonly IPooledWrappedComponent PooledWrappedComponent; |
| | 10 | | public readonly CrdtMessageType MessageType; |
| | 11 | |
|
| | 12 | | public WriteData(IPooledWrappedComponent pooledWrappedComponent, CrdtMessageType messageType) |
| | 13 | | { |
| 86 | 14 | | PooledWrappedComponent = pooledWrappedComponent; |
| 86 | 15 | | MessageType = messageType; |
| 86 | 16 | | } |
| | 17 | |
|
| | 18 | | public WriteData(CrdtMessageType messageType) |
| | 19 | | { |
| 2 | 20 | | MessageType = messageType; |
| 2 | 21 | | PooledWrappedComponent = null; |
| 2 | 22 | | } |
| | 23 | |
|
| | 24 | | public void Dispose() |
| | 25 | | { |
| 10 | 26 | | PooledWrappedComponent?.Dispose(); |
| 10 | 27 | | } |
| | 28 | | } |
| | 29 | |
|
| | 30 | | public record ComponentWriter |
| | 31 | | { |
| | 32 | | private readonly DualKeyValueSet<long, int, WriteData> outgoingMessages; |
| | 33 | |
|
| | 34 | | public ComponentWriter(DualKeyValueSet<long, int, WriteData> sceneOutgoingMessages) |
| | 35 | | { |
| | 36 | | outgoingMessages = sceneOutgoingMessages; |
| | 37 | | } |
| | 38 | |
|
| | 39 | | public void Put(long entityId, int componentId, IPooledWrappedComponent component) |
| | 40 | | { |
| | 41 | | if (outgoingMessages.TryGetValue(entityId, componentId, out var storedData)) |
| | 42 | | storedData.Dispose(); |
| | 43 | |
|
| | 44 | | outgoingMessages[entityId, componentId] = new WriteData(component, CrdtMessageType.PUT_COMPONENT); |
| | 45 | | } |
| | 46 | |
|
| | 47 | | public void Remove(long entityId, int componentId) |
| | 48 | | { |
| | 49 | | if (outgoingMessages.TryGetValue(entityId, componentId, out var storedData)) |
| | 50 | | storedData.Dispose(); |
| | 51 | |
|
| | 52 | | outgoingMessages[entityId, componentId] = new WriteData(CrdtMessageType.DELETE_COMPONENT); |
| | 53 | | } |
| | 54 | |
|
| | 55 | | public void Append(long entityId, int componentId, IPooledWrappedComponent component) |
| | 56 | | { |
| | 57 | | if (outgoingMessages.TryGetValue(entityId, componentId, out var storedData)) |
| | 58 | | storedData.Dispose(); |
| | 59 | |
|
| | 60 | | outgoingMessages[entityId, componentId] = new WriteData(component, CrdtMessageType.APPEND_COMPONENT); |
| | 61 | | } |
| | 62 | | } |
| | 63 | | } |